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 |
procedure StartMultiple; |
51 |
procedure FreeHandle; |
52 |
protected |
53 |
function GetActivityIntf(att: IAttachment): IActivityMonitor; override; |
54 |
public |
55 |
destructor Destroy; override; |
56 |
property TransactionIntf: Firebird.ITransaction read FTransactionIntf; |
57 |
{ITransaction} |
58 |
function GetInTransaction: boolean; override; |
59 |
procedure PrepareForCommit; override; |
60 |
procedure Commit(Force: boolean=false); override; |
61 |
procedure CommitRetaining; override; |
62 |
procedure Start(DefaultCompletion: TTransactionCompletion=taCommit); overload; override; |
63 |
procedure Rollback(Force: boolean=false); override; |
64 |
procedure RollbackRetaining; override; |
65 |
end; |
66 |
|
67 |
|
68 |
implementation |
69 |
|
70 |
uses FBMessages; |
71 |
|
72 |
{ TFB30Transaction } |
73 |
|
74 |
procedure TFB30Transaction.StartMultiple; |
75 |
var Dtc: IDtc; |
76 |
DtcStart: IDtcStart; |
77 |
i: integer; |
78 |
begin |
79 |
with Firebird30ClientAPI do |
80 |
begin |
81 |
Dtc := MasterIntf.getDtc; |
82 |
DtcStart := Dtc.startBuilder(StatusIntf); |
83 |
Check4DataBaseError; |
84 |
|
85 |
for i := 0 to Length(FAttachments) - 1 do |
86 |
if (FAttachments[i] <> nil) then |
87 |
begin |
88 |
DTCStart.addWithTpb(StatusIntf, |
89 |
(FAttachments[i] as TFB30Attachment).AttachmentIntf, |
90 |
(FTPB as TTPB).getDataLength, |
91 |
BytePtr((FTPB as TTPB).getBuffer)); |
92 |
Check4DataBaseError; |
93 |
end; |
94 |
FTransactionIntf := DtcStart.start(StatusIntf); |
95 |
Check4DataBaseError; |
96 |
end; |
97 |
end; |
98 |
|
99 |
procedure TFB30Transaction.FreeHandle; |
100 |
begin |
101 |
if assigned(FTransactionIntf) then |
102 |
FTransactionIntf.release; |
103 |
FTransactionIntf := nil; |
104 |
end; |
105 |
|
106 |
function TFB30Transaction.GetActivityIntf(att: IAttachment): IActivityMonitor; |
107 |
begin |
108 |
Result := att as TFB30Attachment; |
109 |
end; |
110 |
|
111 |
destructor TFB30Transaction.Destroy; |
112 |
begin |
113 |
inherited Destroy; |
114 |
FreeHandle; |
115 |
end; |
116 |
|
117 |
function TFB30Transaction.GetInTransaction: boolean; |
118 |
begin |
119 |
Result := FTransactionIntf <> nil; |
120 |
end; |
121 |
|
122 |
procedure TFB30Transaction.PrepareForCommit; |
123 |
begin |
124 |
if Length(FAttachments) < 2 then |
125 |
IBError(ibxeNotAMultiDatabaseTransaction,[nil]); |
126 |
if FTransactionIntf = nil then |
127 |
Exit; |
128 |
with Firebird30ClientAPI do |
129 |
begin |
130 |
FTransactionIntf.prepare(StatusIntf,0,nil); |
131 |
Check4DataBaseError; |
132 |
end; |
133 |
SignalActivity; |
134 |
end; |
135 |
|
136 |
procedure TFB30Transaction.Commit(Force: boolean); |
137 |
begin |
138 |
if FTransactionIntf = nil then |
139 |
Exit; |
140 |
with Firebird30ClientAPI do |
141 |
begin |
142 |
FTransactionIntf.commit(StatusIntf); |
143 |
if not Force and InErrorState then |
144 |
IBDataBaseError; |
145 |
end; |
146 |
SignalActivity; |
147 |
FreeHandle; |
148 |
end; |
149 |
|
150 |
procedure TFB30Transaction.CommitRetaining; |
151 |
begin |
152 |
if FTransactionIntf = nil then |
153 |
Exit; |
154 |
with Firebird30ClientAPI do |
155 |
begin |
156 |
FTransactionIntf.commitRetaining(StatusIntf); |
157 |
Check4DataBaseError; |
158 |
end; |
159 |
SignalActivity; |
160 |
end; |
161 |
|
162 |
procedure TFB30Transaction.Start(DefaultCompletion: TTransactionCompletion); |
163 |
begin |
164 |
if FTransactionIntf <> nil then |
165 |
Exit; |
166 |
|
167 |
FDefaultCompletion := DefaultCompletion; |
168 |
|
169 |
if Length(FAttachments) > 0 then |
170 |
StartMultiple |
171 |
else |
172 |
with Firebird30ClientAPI do |
173 |
begin |
174 |
FTransactionIntf := (FAttachments[0] as TFB30Attachment).AttachmentIntf.startTransaction(StatusIntf, |
175 |
(FTPB as TTPB).getDataLength,BytePtr((FTPB as TTPB).getBuffer)); |
176 |
Check4DataBaseError; |
177 |
end; |
178 |
SignalActivity; |
179 |
Inc(FSeqNo); |
180 |
end; |
181 |
|
182 |
procedure TFB30Transaction.Rollback(Force: boolean); |
183 |
begin |
184 |
if FTransactionIntf = nil then |
185 |
Exit; |
186 |
with Firebird30ClientAPI do |
187 |
begin |
188 |
FTransactionIntf.rollback(StatusIntf); |
189 |
if not Force and InErrorState then |
190 |
IBDataBaseError; |
191 |
end; |
192 |
SignalActivity; |
193 |
FreeHandle; |
194 |
end; |
195 |
|
196 |
procedure TFB30Transaction.RollbackRetaining; |
197 |
begin |
198 |
if FTransactionIntf = nil then |
199 |
Exit; |
200 |
with Firebird30ClientAPI do |
201 |
begin |
202 |
FTransactionIntf.rollbackRetaining(StatusIntf); |
203 |
Check4DataBaseError; |
204 |
end; |
205 |
SignalActivity; |
206 |
end; |
207 |
|
208 |
|
209 |
end. |
210 |
|