1 |
(* |
2 |
* Firebird Interface (fbintf). The fbintf components provide a set of |
3 |
* Pascal language bindings for the Firebird API. |
4 |
* |
5 |
* The contents of this file are subject to the Initial Developer's |
6 |
* Public License Version 1.0 (the "License"); you may not use this |
7 |
* file except in compliance with the License. You may obtain a copy |
8 |
* of the License here: |
9 |
* |
10 |
* http://www.firebirdsql.org/index.php?op=doc&id=idpl |
11 |
* |
12 |
* Software distributed under the License is distributed on an "AS |
13 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
14 |
* implied. See the License for the specific language governing rights |
15 |
* and limitations under the License. |
16 |
* |
17 |
* The Initial Developer of the Original Code is Tony Whyman. |
18 |
* |
19 |
* The Original Code is (C) 2016 Tony Whyman, MWA Software |
20 |
* (http://www.mwasoftware.co.uk). |
21 |
* |
22 |
* All Rights Reserved. |
23 |
* |
24 |
* Contributor(s): ______________________________________. |
25 |
* |
26 |
*) |
27 |
unit FB30Transaction; |
28 |
{$IFDEF MSWINDOWS} |
29 |
{$DEFINE WINDOWS} |
30 |
{$ENDIF} |
31 |
|
32 |
{$IFDEF FPC} |
33 |
{$mode delphi} |
34 |
{$interfaces COM} |
35 |
{$ENDIF} |
36 |
|
37 |
interface |
38 |
|
39 |
uses |
40 |
Classes, SysUtils, Firebird, IB, FBClientAPI, FB30ClientAPI, |
41 |
FB30Attachment, FBParamBlock, FBActivityMonitor, FBTransaction; |
42 |
|
43 |
type |
44 |
|
45 |
{ TFB30Transaction } |
46 |
|
47 |
TFB30Transaction = class(TFBTransaction,ITransaction, IActivityMonitor) |
48 |
private |
49 |
FTransactionIntf: Firebird.ITransaction; |
50 |
FFirebird30ClientAPI: TFB30ClientAPI; |
51 |
procedure FreeHandle(Force: boolean); |
52 |
protected |
53 |
function GetActivityIntf(att: IAttachment): IActivityMonitor; override; |
54 |
procedure SetInterface(api: TFBClientAPI); override; |
55 |
function GetTrInfo(ReqBuffer: PByte; ReqBufLen: integer): ITrInformation; override; |
56 |
procedure InternalStartSingle(attachment: IAttachment); override; |
57 |
procedure InternalStartMultiple; override; |
58 |
function InternalCommit(Force: boolean): TTrCompletionState; override; |
59 |
procedure InternalCommitRetaining; override; |
60 |
function InternalRollback(Force: boolean): TTrCompletionState; override; |
61 |
procedure InternalRollbackRetaining; override; |
62 |
public |
63 |
constructor Create(api: TFBClientAPI; Attachment: IAttachment; aTransactionIntf: Firebird.ITransaction); overload; |
64 |
destructor Destroy; override; |
65 |
property TransactionIntf: Firebird.ITransaction read FTransactionIntf; |
66 |
{ITransaction} |
67 |
function GetInTransaction: boolean; override; |
68 |
procedure PrepareForCommit; override; |
69 |
end; |
70 |
|
71 |
|
72 |
implementation |
73 |
|
74 |
uses FBMessages; |
75 |
|
76 |
{ TFB30Transaction } |
77 |
|
78 |
procedure TFB30Transaction.FreeHandle(Force: boolean); |
79 |
begin |
80 |
if assigned(FTransactionIntf) then |
81 |
try |
82 |
FTransactionIntf.release; |
83 |
except |
84 |
if not Force then raise; |
85 |
{else ignore if Force = true} |
86 |
end; |
87 |
FTransactionIntf := nil; |
88 |
end; |
89 |
|
90 |
function TFB30Transaction.GetActivityIntf(att: IAttachment): IActivityMonitor; |
91 |
begin |
92 |
att.QueryInterface(IActivityMonitor,Result); |
93 |
end; |
94 |
|
95 |
procedure TFB30Transaction.SetInterface(api: TFBClientAPI); |
96 |
begin |
97 |
inherited SetInterface(api); |
98 |
FFirebird30ClientAPI := api as TFB30ClientAPI; |
99 |
end; |
100 |
|
101 |
function TFB30Transaction.GetTrInfo(ReqBuffer: PByte; ReqBufLen: integer |
102 |
): ITrInformation; |
103 |
begin |
104 |
Result := TTrInformation.Create(FFirebird30ClientAPI); |
105 |
with FFirebird30ClientAPI, Result as TTrInformation do |
106 |
begin |
107 |
FTransactionIntf.getInfo(StatusIntf, ReqBufLen, BytePtr(ReqBuffer), |
108 |
getBufSize, BytePtr(Buffer)); |
109 |
Check4DataBaseError; |
110 |
end |
111 |
end; |
112 |
|
113 |
procedure TFB30Transaction.InternalStartSingle(attachment: IAttachment); |
114 |
begin |
115 |
if FTransactionIntf = nil then |
116 |
with FFirebird30ClientAPI do |
117 |
begin |
118 |
FTransactionIntf := (attachment as TFB30Attachment).AttachmentIntf.startTransaction(StatusIntf, |
119 |
(FTPB as TTPB).getDataLength,BytePtr((FTPB as TTPB).getBuffer)); |
120 |
Check4DataBaseError; |
121 |
FTransactionIntf.addRef(); |
122 |
end; |
123 |
SignalActivity; |
124 |
end; |
125 |
|
126 |
procedure TFB30Transaction.InternalStartMultiple; |
127 |
var Dtc: IDtc; |
128 |
DtcStart: IDtcStart; |
129 |
i: integer; |
130 |
begin |
131 |
if FTransactionIntf = nil then |
132 |
with FFirebird30ClientAPI do |
133 |
begin |
134 |
Dtc := MasterIntf.getDtc; |
135 |
DtcStart := Dtc.startBuilder(StatusIntf); |
136 |
Check4DataBaseError; |
137 |
|
138 |
for i := 0 to Length(FAttachments) - 1 do |
139 |
if (FAttachments[i] <> nil) then |
140 |
begin |
141 |
DTCStart.addWithTpb(StatusIntf, |
142 |
(FAttachments[i] as TFB30Attachment).AttachmentIntf, |
143 |
(FTPB as TTPB).getDataLength, |
144 |
BytePtr((FTPB as TTPB).getBuffer)); |
145 |
Check4DataBaseError; |
146 |
end; |
147 |
|
148 |
FTransactionIntf := DtcStart.start(StatusIntf); |
149 |
Check4DataBaseError; |
150 |
FTransactionIntf.addRef(); |
151 |
SignalActivity; |
152 |
end; |
153 |
end; |
154 |
|
155 |
function TFB30Transaction.InternalCommit(Force: boolean): TTrCompletionState; |
156 |
begin |
157 |
with FFirebird30ClientAPI do |
158 |
begin |
159 |
Result := trCommitted; |
160 |
FTransactionIntf.commit(StatusIntf); |
161 |
if InErrorState then |
162 |
begin |
163 |
if Force then |
164 |
Result := trCommitFailed |
165 |
else |
166 |
IBDataBaseError; |
167 |
end; |
168 |
end; |
169 |
SignalActivity; |
170 |
FreeHandle(Force); |
171 |
end; |
172 |
|
173 |
procedure TFB30Transaction.InternalCommitRetaining; |
174 |
begin |
175 |
with FFirebird30ClientAPI do |
176 |
begin |
177 |
FTransactionIntf.commitRetaining(StatusIntf); |
178 |
Check4DataBaseError; |
179 |
end; |
180 |
SignalActivity; |
181 |
end; |
182 |
|
183 |
function TFB30Transaction.InternalRollback(Force: boolean): TTrCompletionState; |
184 |
begin |
185 |
with FFirebird30ClientAPI do |
186 |
begin |
187 |
FTransactionIntf.rollback(StatusIntf); |
188 |
Result := trRolledback; |
189 |
if InErrorState then |
190 |
begin |
191 |
if Force then |
192 |
Result := trRollbackFailed |
193 |
else |
194 |
IBDataBaseError; |
195 |
end; |
196 |
end; |
197 |
SignalActivity; |
198 |
FreeHandle(Force); |
199 |
end; |
200 |
|
201 |
procedure TFB30Transaction.InternalRollbackRetaining; |
202 |
begin |
203 |
with FFirebird30ClientAPI do |
204 |
begin |
205 |
FTransactionIntf.rollbackRetaining(StatusIntf); |
206 |
Check4DataBaseError; |
207 |
end; |
208 |
SignalActivity; |
209 |
end; |
210 |
|
211 |
constructor TFB30Transaction.Create(api: TFBClientAPI; Attachment: IAttachment; |
212 |
aTransactionIntf: Firebird.ITransaction); |
213 |
begin |
214 |
FTransactionIntf := aTransactionIntf; |
215 |
FTransactionIntf.addRef(); |
216 |
FForeignHandle := true; |
217 |
inherited Create(api,Attachment,nil,taCommit,''); |
218 |
end; |
219 |
|
220 |
destructor TFB30Transaction.Destroy; |
221 |
begin |
222 |
inherited Destroy; |
223 |
FreeHandle(true); |
224 |
end; |
225 |
|
226 |
function TFB30Transaction.GetInTransaction: boolean; |
227 |
begin |
228 |
Result := FTransactionIntf <> nil; |
229 |
end; |
230 |
|
231 |
procedure TFB30Transaction.PrepareForCommit; |
232 |
begin |
233 |
if Length(FAttachments) < 2 then |
234 |
IBError(ibxeNotAMultiDatabaseTransaction,[nil]); |
235 |
if FTransactionIntf = nil then |
236 |
Exit; |
237 |
with FFirebird30ClientAPI do |
238 |
begin |
239 |
FTransactionIntf.prepare(StatusIntf,0,nil); |
240 |
Check4DataBaseError; |
241 |
end; |
242 |
SignalActivity; |
243 |
end; |
244 |
|
245 |
|
246 |
end. |
247 |
|