1 |
(* |
2 |
* Firebird Interface (fbintf). The fbintf components provide a set of |
3 |
* Pascal language bindings for the Firebird API. Although predominantly |
4 |
* a new development they include source code taken from IBX and may be |
5 |
* considered a derived product. This software thus also includes the copyright |
6 |
* notice and license conditions from IBX. |
7 |
* |
8 |
* Except for those parts dervied from IBX, contents of this file are subject |
9 |
* to the Initial Developer's Public License Version 1.0 (the "License"); you |
10 |
* may not use this file except in compliance with the License. You may obtain a |
11 |
* copy of the License here: |
12 |
* |
13 |
* http://www.firebirdsql.org/index.php?op=doc&id=idpl |
14 |
* |
15 |
* Software distributed under the License is distributed on an "AS |
16 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
17 |
* implied. See the License for the specific language governing rights |
18 |
* and limitations under the License. |
19 |
* |
20 |
* The Initial Developer of the Original Code is Tony Whyman. |
21 |
* |
22 |
* The Original Code is (C) 2016 Tony Whyman, MWA Software |
23 |
* (http://www.mwasoftware.co.uk). |
24 |
* |
25 |
* All Rights Reserved. |
26 |
* |
27 |
* Contributor(s): ______________________________________. |
28 |
* |
29 |
*) |
30 |
unit FBStatement; |
31 |
{$IFDEF MSWINDOWS} |
32 |
{$DEFINE WINDOWS} |
33 |
{$ENDIF} |
34 |
|
35 |
{$IFDEF FPC} |
36 |
{$mode delphi} |
37 |
{$codepage UTF8} |
38 |
{$interfaces COM} |
39 |
{$ENDIF} |
40 |
|
41 |
interface |
42 |
|
43 |
uses |
44 |
Classes, SysUtils, IB, FBClientAPI, FBSQLData, FBOutputBlock, FBActivityMonitor, |
45 |
FBTransaction; |
46 |
|
47 |
const |
48 |
DefaultBatchRowLimit = 1000; |
49 |
|
50 |
type |
51 |
TPerfStatistics = array[psCurrentMemory..psFetches] of Int64; |
52 |
|
53 |
{ TFBStatement } |
54 |
|
55 |
TFBStatement = class(TActivityReporter,ITransactionUser) |
56 |
private |
57 |
FAttachmentIntf: IAttachment; |
58 |
FConnectionCodePage: TSystemCodePage; |
59 |
FFirebirdClientAPI: TFBClientAPI; |
60 |
protected |
61 |
FTransactionIntf: ITransaction; |
62 |
FExecTransactionIntf: ITransaction; |
63 |
FStaleReferenceChecks: boolean; |
64 |
FSQLStatementType: TIBSQLStatementTypes; { Select, update, delete, insert, create, alter, etc...} |
65 |
FSQLDialect: integer; |
66 |
FOpen: boolean; |
67 |
FPrepared: boolean; |
68 |
FPrepareSeqNo: integer; {used to check for out of date references from interfaces} |
69 |
FSQL: AnsiString; |
70 |
FProcessedSQL: AnsiString; |
71 |
FHasParamNames: boolean; |
72 |
FBOF: boolean; |
73 |
FEOF: boolean; |
74 |
FSingleResults: boolean; |
75 |
FGenerateParamNames: boolean; |
76 |
FChangeSeqNo: integer; |
77 |
FCollectStatistics: boolean; |
78 |
FStatisticsAvailable: boolean; |
79 |
FBeforeStats: TPerfStatistics; |
80 |
FAfterStats: TPerfStatistics; |
81 |
FCaseSensitiveParams: boolean; |
82 |
FBatchRowLimit: integer; |
83 |
procedure CheckChangeBatchRowLimit; virtual; |
84 |
procedure CheckHandle; virtual; abstract; |
85 |
procedure CheckTransaction(aTransaction: ITransaction); |
86 |
procedure DoJournaling(force: boolean); |
87 |
function GetJournalIntf: IJournallingHook; |
88 |
function GetStatementIntf: IStatement; virtual; abstract; |
89 |
procedure GetDsqlInfo(info_request: byte; buffer: ISQLInfoResults); overload; virtual; abstract; |
90 |
procedure InternalPrepare(CursorName: AnsiString=''); virtual; abstract; |
91 |
function InternalExecute(Transaction: ITransaction): IResults; virtual; abstract; |
92 |
function InternalOpenCursor(aTransaction: ITransaction; Scrollable: boolean): IResultSet; virtual; abstract; |
93 |
procedure ProcessSQL(sql: AnsiString; GenerateParamNames: boolean; var processedSQL: AnsiString); virtual; abstract; |
94 |
procedure FreeHandle; virtual; abstract; |
95 |
procedure InternalClose(Force: boolean); virtual; abstract; |
96 |
function TimeStampToMSecs(const TimeStamp: TTimeStamp): Int64; |
97 |
public |
98 |
constructor Create(Attachment: IAttachment; Transaction: ITransaction; |
99 |
sql: AnsiString; SQLDialect: integer); |
100 |
constructor CreateWithParameterNames(Attachment: IAttachment; Transaction: ITransaction; |
101 |
sql: AnsiString; SQLDialect: integer; GenerateParamNames: boolean =false; |
102 |
CaseSensitiveParams: boolean = false); |
103 |
destructor Destroy; override; |
104 |
procedure Close; |
105 |
procedure TransactionEnding(aTransaction: ITransaction; Force: boolean); |
106 |
property SQLDialect: integer read FSQLDialect; |
107 |
property FirebirdClientAPI: TFBClientAPI read FFirebirdClientAPI; |
108 |
property ConnectionCodePage: TSystemCodePage read FConnectionCodePage; |
109 |
|
110 |
public |
111 |
function GetSQLParams: ISQLParams; virtual; abstract; |
112 |
function GetMetaData: IMetaData; virtual; abstract; |
113 |
function GetRowsAffected(var SelectCount, InsertCount, UpdateCount, |
114 |
DeleteCount: integer): boolean; |
115 |
function GetSQLStatementType: TIBSQLStatementTypes; |
116 |
function GetSQLStatementTypeName: AnsiString; |
117 |
function GetSQLText: AnsiString; |
118 |
function GetProcessedSQLText: AnsiString; |
119 |
function GetSQLDialect: integer; |
120 |
function GetFlags: TStatementFlags; virtual; |
121 |
|
122 |
{GetDSQLInfo only supports isc_info_sql_stmt_type, isc_info_sql_get_plan, isc_info_sql_records} |
123 |
procedure Prepare(aTransaction: ITransaction=nil); overload; |
124 |
procedure Prepare(CursorName: AnsiString; aTransaction: ITransaction=nil); overload; virtual; |
125 |
function Execute(aTransaction: ITransaction=nil): IResults; |
126 |
function OpenCursor(aTransaction: ITransaction=nil): IResultSet; overload; |
127 |
function OpenCursor(Scrollable: boolean; aTransaction: ITransaction=nil): IResultSet; overload; |
128 |
function CreateBlob(paramName: AnsiString): IBlob; overload; |
129 |
function CreateBlob(index: integer): IBlob; overload; |
130 |
function CreateBlob(column: TColumnMetaData): IBlob; overload; virtual; abstract; |
131 |
function CreateArray(paramName: AnsiString): IArray; overload; |
132 |
function CreateArray(index: integer): IArray; overload; |
133 |
function CreateArray(column: TColumnMetaData): IArray; overload; virtual; abstract; |
134 |
function GetAttachment: IAttachment; |
135 |
function GetTransaction: ITransaction; |
136 |
function GetDSQLInfo(Request: byte): ISQLInfoResults; overload; |
137 |
procedure SetRetainInterfaces(aValue: boolean); virtual; |
138 |
procedure EnableStatistics(aValue: boolean); |
139 |
function GetPerfStatistics(var stats: TPerfCounters): boolean; |
140 |
function IsInBatchMode: boolean; virtual; |
141 |
function HasBatchMode: boolean; virtual; |
142 |
public |
143 |
{IBatch support} |
144 |
procedure AddToBatch; virtual; |
145 |
function ExecuteBatch(aTransaction: ITransaction): IBatchCompletion; virtual; |
146 |
procedure CancelBatch; virtual; |
147 |
function GetBatchCompletion: IBatchCompletion; virtual; |
148 |
function GetBatchRowLimit: integer; |
149 |
procedure SetBatchRowLimit(aLimit: integer); |
150 |
{Stale Reference Check} |
151 |
procedure SetStaleReferenceChecks(Enable:boolean); {default true} |
152 |
function GetStaleReferenceChecks: boolean; |
153 |
public |
154 |
property ChangeSeqNo: integer read FChangeSeqNo; |
155 |
property SQLParams: ISQLParams read GetSQLParams; |
156 |
property SQLStatementType: TIBSQLStatementTypes read GetSQLStatementType; |
157 |
end; |
158 |
|
159 |
implementation |
160 |
|
161 |
uses FBMessages; |
162 |
|
163 |
{ TFBStatement } |
164 |
|
165 |
procedure TFBStatement.CheckChangeBatchRowLimit; |
166 |
begin |
167 |
//Do Nothing |
168 |
end; |
169 |
|
170 |
procedure TFBStatement.CheckTransaction(aTransaction: ITransaction); |
171 |
begin |
172 |
if (aTransaction = nil) then |
173 |
IBError(ibxeTransactionNotAssigned,[]); |
174 |
|
175 |
if not aTransaction.InTransaction then |
176 |
IBError(ibxeNotInTransaction,[]); |
177 |
end; |
178 |
|
179 |
procedure TFBStatement.DoJournaling(force: boolean); |
180 |
function doGetRowsAffected: integer; |
181 |
var a,i,u,d: integer; |
182 |
begin |
183 |
GetRowsAffected(a,i,u,d); |
184 |
Result := i + u + d; |
185 |
end; |
186 |
|
187 |
var RowsAffected: integer; |
188 |
begin |
189 |
RowsAffected := doGetRowsAffected; |
190 |
with GetAttachment do |
191 |
if JournalingActive and (force or |
192 |
(((joReadOnlyQueries in GetJournalOptions) and (RowsAffected = 0)) or |
193 |
((joModifyQueries in GetJournalOptions) and (RowsAffected > 0)))) then |
194 |
GetJournalIntf.ExecQuery(GetStatementIntf); |
195 |
end; |
196 |
|
197 |
function TFBStatement.GetJournalIntf: IJournallingHook; |
198 |
begin |
199 |
GetAttachment.QueryInterface(IJournallingHook,Result) |
200 |
end; |
201 |
|
202 |
function TFBStatement.TimeStampToMSecs(const TimeStamp: TTimeStamp): Int64; |
203 |
begin |
204 |
Result := TimeStamp.Time + Int64(timestamp.date)*msecsperday; |
205 |
end; |
206 |
|
207 |
constructor TFBStatement.Create(Attachment: IAttachment; |
208 |
Transaction: ITransaction; sql: AnsiString; SQLDialect: integer); |
209 |
begin |
210 |
inherited Create(Transaction as TFBTransaction,2); |
211 |
FAttachmentIntf := Attachment; |
212 |
FTransactionIntf := Transaction; |
213 |
FFirebirdClientAPI := Attachment.getFirebirdAPI as TFBClientAPI; |
214 |
FSQLDialect := SQLDialect; |
215 |
FSQL := sql; |
216 |
FBatchRowLimit := DefaultBatchRowLimit; |
217 |
FStaleReferenceChecks := true; |
218 |
FConnectionCodePage := Attachment.GetCodePage; |
219 |
end; |
220 |
|
221 |
constructor TFBStatement.CreateWithParameterNames(Attachment: IAttachment; |
222 |
Transaction: ITransaction; sql: AnsiString; SQLDialect: integer; |
223 |
GenerateParamNames: boolean; CaseSensitiveParams: boolean); |
224 |
begin |
225 |
FHasParamNames := true; |
226 |
FGenerateParamNames := GenerateParamNames; |
227 |
FCaseSensitiveParams := CaseSensitiveParams; |
228 |
Create(Attachment,Transaction,sql,SQLDialect); |
229 |
end; |
230 |
|
231 |
destructor TFBStatement.Destroy; |
232 |
begin |
233 |
Close; |
234 |
FreeHandle; |
235 |
inherited Destroy; |
236 |
end; |
237 |
|
238 |
procedure TFBStatement.Close; |
239 |
begin |
240 |
InternalClose(false); |
241 |
end; |
242 |
|
243 |
procedure TFBStatement.TransactionEnding(aTransaction: ITransaction; |
244 |
Force: boolean); |
245 |
begin |
246 |
if FOpen and ((FExecTransactionIntf as TObject) = (aTransaction as TObject)) then |
247 |
InternalClose(Force); |
248 |
|
249 |
if FTransactionIntf = aTransaction then |
250 |
begin |
251 |
FreeHandle; |
252 |
FPrepared := false; |
253 |
end; |
254 |
end; |
255 |
|
256 |
function TFBStatement.GetRowsAffected(var SelectCount, InsertCount, |
257 |
UpdateCount, DeleteCount: integer): boolean; |
258 |
var |
259 |
RB: ISQLInfoResults; |
260 |
i, j: integer; |
261 |
begin |
262 |
InsertCount := 0; |
263 |
UpdateCount := 0; |
264 |
DeleteCount := 0; |
265 |
Result := FPrepared; |
266 |
if not Result then Exit; |
267 |
|
268 |
RB := GetDsqlInfo(isc_info_sql_records); |
269 |
|
270 |
for i := 0 to RB.Count - 1 do |
271 |
with RB[i] do |
272 |
case getItemType of |
273 |
isc_info_sql_records: |
274 |
for j := 0 to Count -1 do |
275 |
with Items[j] do |
276 |
case getItemType of |
277 |
isc_info_req_select_count: |
278 |
SelectCount := GetAsInteger; |
279 |
isc_info_req_insert_count: |
280 |
InsertCount := GetAsInteger; |
281 |
isc_info_req_update_count: |
282 |
UpdateCount := GetAsInteger; |
283 |
isc_info_req_delete_count: |
284 |
DeleteCount := GetAsInteger; |
285 |
end; |
286 |
end; |
287 |
end; |
288 |
|
289 |
function TFBStatement.GetSQLStatementType: TIBSQLStatementTypes; |
290 |
begin |
291 |
Result := FSQLStatementType; |
292 |
end; |
293 |
|
294 |
function TFBStatement.GetSQLStatementTypeName: AnsiString; |
295 |
begin |
296 |
case FSQLStatementType of |
297 |
SQLUnknown: Result := 'SQL_Unknown'; |
298 |
SQLSelect: Result := 'SQL_Select'; |
299 |
SQLInsert: Result := 'SQL_Insert'; |
300 |
SQLUpdate: Result := 'SQL_Update'; |
301 |
SQLDelete: Result := 'SQL_Delete'; |
302 |
SQLDDL: Result := 'SQL_DDL'; |
303 |
SQLGetSegment: Result := 'SQL_GetSegment'; |
304 |
SQLPutSegment: Result := 'SQL_PutSegment'; |
305 |
SQLExecProcedure: Result := 'SQL_ExecProcedure'; |
306 |
SQLStartTransaction: Result := 'SQL_StartTransaction'; |
307 |
SQLCommit: Result := 'SQL_Commit'; |
308 |
SQLRollback: Result := 'SQL_Rollback'; |
309 |
SQLSelectForUpdate: Result := 'SQL_SelectForUpdate'; |
310 |
SQLSetGenerator: Result := 'SQL_SetGenerator'; |
311 |
SQLSavePoint: Result := 'SQL_SavePoint'; |
312 |
end; |
313 |
end; |
314 |
|
315 |
function TFBStatement.GetSQLText: AnsiString; |
316 |
begin |
317 |
Result := FSQL; |
318 |
end; |
319 |
|
320 |
function TFBStatement.GetProcessedSQLText: AnsiString; |
321 |
begin |
322 |
if not FHasParamNames then |
323 |
Result := FSQL |
324 |
else |
325 |
begin |
326 |
if FProcessedSQL = '' then |
327 |
ProcessSQL(FSQL,FGenerateParamNames,FProcessedSQL); |
328 |
Result := FProcessedSQL; |
329 |
end; |
330 |
end; |
331 |
|
332 |
function TFBStatement.GetSQLDialect: integer; |
333 |
begin |
334 |
Result := FSQLDialect; |
335 |
end; |
336 |
|
337 |
function TFBStatement.GetFlags: TStatementFlags; |
338 |
begin |
339 |
Result := []; |
340 |
end; |
341 |
|
342 |
procedure TFBStatement.Prepare(aTransaction: ITransaction); |
343 |
begin |
344 |
Prepare('',aTransaction); |
345 |
end; |
346 |
|
347 |
procedure TFBStatement.Prepare(CursorName: AnsiString; |
348 |
aTransaction: ITransaction); |
349 |
begin |
350 |
if FPrepared then FreeHandle; |
351 |
if aTransaction <> nil then |
352 |
begin |
353 |
RemoveMonitor(FTransactionIntf as TFBTransaction); |
354 |
FTransactionIntf := aTransaction; |
355 |
AddMonitor(FTransactionIntf as TFBTransaction); |
356 |
end; |
357 |
InternalPrepare(CursorName); |
358 |
end; |
359 |
|
360 |
function TFBStatement.Execute(aTransaction: ITransaction): IResults; |
361 |
begin |
362 |
try |
363 |
if aTransaction = nil then |
364 |
Result := InternalExecute(FTransactionIntf) |
365 |
else |
366 |
Result := InternalExecute(aTransaction); |
367 |
finally |
368 |
DoJournaling(ExceptObject <> nil); |
369 |
end; |
370 |
end; |
371 |
|
372 |
procedure TFBStatement.AddToBatch; |
373 |
begin |
374 |
IBError(ibxeBatchModeNotSupported,[]); |
375 |
end; |
376 |
|
377 |
function TFBStatement.ExecuteBatch(aTransaction: ITransaction |
378 |
): IBatchCompletion; |
379 |
begin |
380 |
IBError(ibxeBatchModeNotSupported,[]); |
381 |
end; |
382 |
|
383 |
procedure TFBStatement.CancelBatch; |
384 |
begin |
385 |
IBError(ibxeBatchModeNotSupported,[]); |
386 |
end; |
387 |
|
388 |
function TFBStatement.GetBatchCompletion: IBatchCompletion; |
389 |
begin |
390 |
IBError(ibxeBatchModeNotSupported,[]); |
391 |
end; |
392 |
|
393 |
function TFBStatement.GetBatchRowLimit: integer; |
394 |
begin |
395 |
Result := FBatchRowLimit; |
396 |
end; |
397 |
|
398 |
procedure TFBStatement.SetBatchRowLimit(aLimit: integer); |
399 |
begin |
400 |
CheckChangeBatchRowLimit; |
401 |
FBatchRowLimit := aLimit; |
402 |
end; |
403 |
|
404 |
procedure TFBStatement.SetStaleReferenceChecks(Enable: boolean); |
405 |
begin |
406 |
FStaleReferenceChecks := Enable; |
407 |
end; |
408 |
|
409 |
function TFBStatement.GetStaleReferenceChecks: boolean; |
410 |
begin |
411 |
Result := FStaleReferenceChecks; |
412 |
end; |
413 |
|
414 |
function TFBStatement.OpenCursor(aTransaction: ITransaction): IResultSet; |
415 |
begin |
416 |
Result := OpenCursor(false,aTransaction); |
417 |
end; |
418 |
|
419 |
function TFBStatement.OpenCursor(Scrollable: boolean; aTransaction: ITransaction |
420 |
): IResultSet; |
421 |
begin |
422 |
Close; |
423 |
try |
424 |
if aTransaction = nil then |
425 |
Result := InternalOpenCursor(FTransactionIntf,Scrollable) |
426 |
else |
427 |
Result := InternalOpenCursor(aTransaction,Scrollable); |
428 |
finally |
429 |
DoJournaling(ExceptObject <> nil); |
430 |
end; |
431 |
end; |
432 |
|
433 |
function TFBStatement.CreateBlob(paramName: AnsiString): IBlob; |
434 |
var column: TColumnMetaData; |
435 |
begin |
436 |
InternalPrepare; |
437 |
column := SQLParams.ByName(paramName) as TSQLParam; |
438 |
if column = nil then |
439 |
IBError(ibxeFieldNotFound,[paramName]); |
440 |
Result := CreateBlob(column); |
441 |
end; |
442 |
|
443 |
function TFBStatement.CreateBlob(index: integer): IBlob; |
444 |
begin |
445 |
InternalPrepare; |
446 |
Result := CreateBlob(SQLParams[index] as TSQLParam); |
447 |
end; |
448 |
|
449 |
function TFBStatement.CreateArray(paramName: AnsiString): IArray; |
450 |
var column: TColumnMetaData; |
451 |
begin |
452 |
InternalPrepare; |
453 |
column := SQLParams.ByName(paramName) as TSQLParam; |
454 |
if column = nil then |
455 |
IBError(ibxeFieldNotFound,[paramName]); |
456 |
Result := CreateArray(column); |
457 |
end; |
458 |
|
459 |
function TFBStatement.CreateArray(index: integer): IArray; |
460 |
begin |
461 |
InternalPrepare; |
462 |
Result := CreateArray(SQLParams[index] as TSQLParam); |
463 |
end; |
464 |
|
465 |
function TFBStatement.GetAttachment: IAttachment; |
466 |
begin |
467 |
Result := FAttachmentIntf; |
468 |
end; |
469 |
|
470 |
function TFBStatement.GetTransaction: ITransaction; |
471 |
begin |
472 |
Result := FTransactionIntf |
473 |
end; |
474 |
|
475 |
function TFBStatement.GetDSQLInfo(Request: byte): ISQLInfoResults; |
476 |
begin |
477 |
Result := TSQLInfoResultsBuffer.Create(FFirebirdClientAPI); |
478 |
GetDsqlInfo(Request,Result); |
479 |
end; |
480 |
|
481 |
procedure TFBStatement.SetRetainInterfaces(aValue: boolean); |
482 |
begin |
483 |
RetainInterfaces := aValue; |
484 |
end; |
485 |
|
486 |
procedure TFBStatement.EnableStatistics(aValue: boolean); |
487 |
begin |
488 |
if FCollectStatistics <> aValue then |
489 |
begin |
490 |
FCollectStatistics := aValue; |
491 |
FStatisticsAvailable := false; |
492 |
end; |
493 |
end; |
494 |
|
495 |
function TFBStatement.GetPerfStatistics(var stats: TPerfCounters): boolean; |
496 |
begin |
497 |
Result := FStatisticsAvailable; |
498 |
if Result then |
499 |
begin |
500 |
stats[psCurrentMemory] := FAfterStats[psCurrentMemory]; |
501 |
stats[psDeltaMemory] := FAfterStats[psCurrentMemory] - FBeforeStats[psCurrentMemory]; |
502 |
stats[psMaxMemory] := FAfterStats[psMaxMemory]; |
503 |
stats[psRealTime] := FAfterStats[psRealTime] - FBeforeStats[psRealTime]; |
504 |
stats[psUserTime] := FAfterStats[psUserTime] - FBeforeStats[psUserTime]; |
505 |
stats[psReads] := FAfterStats[psReads] - FBeforeStats[psReads]; |
506 |
stats[psWrites] := FAfterStats[psWrites] - FBeforeStats[psWrites]; |
507 |
stats[psFetches] := FAfterStats[psFetches] - FBeforeStats[psFetches]; |
508 |
stats[psBuffers] := FAfterStats[psBuffers]; |
509 |
end; |
510 |
end; |
511 |
|
512 |
function TFBStatement.IsInBatchMode: boolean; |
513 |
begin |
514 |
Result := false; |
515 |
end; |
516 |
|
517 |
function TFBStatement.HasBatchMode: boolean; |
518 |
begin |
519 |
Result := false; |
520 |
end; |
521 |
|
522 |
end. |
523 |
|