28 |
|
* |
29 |
|
*) |
30 |
|
unit FBStatement; |
31 |
+ |
{$IFDEF MSWINDOWS} |
32 |
+ |
{$DEFINE WINDOWS} |
33 |
+ |
{$ENDIF} |
34 |
|
|
35 |
|
{$IFDEF FPC} |
36 |
< |
{$mode objfpc}{$H+} |
36 |
> |
{$mode delphi} |
37 |
|
{$codepage UTF8} |
38 |
|
{$interfaces COM} |
39 |
|
{$ENDIF} |
45 |
|
FBTransaction; |
46 |
|
|
47 |
|
type |
48 |
+ |
TPerfStatistics = array[psCurrentMemory..psFetches] of Int64; |
49 |
|
|
50 |
|
{ TFBStatement } |
51 |
|
|
52 |
|
TFBStatement = class(TActivityReporter) |
53 |
|
private |
54 |
|
FAttachmentIntf: IAttachment; |
55 |
+ |
FFirebirdClientAPI: TFBClientAPI; |
56 |
|
protected |
57 |
|
FTransactionIntf: ITransaction; |
58 |
|
FExecTransactionIntf: ITransaction; |
61 |
|
FOpen: boolean; |
62 |
|
FPrepared: boolean; |
63 |
|
FPrepareSeqNo: integer; {used to check for out of date references from interfaces} |
64 |
< |
FSQL: string; |
65 |
< |
FProcessedSQL: string; |
64 |
> |
FSQL: AnsiString; |
65 |
> |
FProcessedSQL: AnsiString; |
66 |
|
FHasParamNames: boolean; |
67 |
|
FBOF: boolean; |
68 |
|
FEOF: boolean; |
69 |
|
FSingleResults: boolean; |
70 |
|
FGenerateParamNames: boolean; |
71 |
|
FChangeSeqNo: integer; |
72 |
+ |
FCollectStatistics: boolean; |
73 |
+ |
FStatisticsAvailable: boolean; |
74 |
+ |
FBeforeStats: TPerfStatistics; |
75 |
+ |
FAfterStats: TPerfStatistics; |
76 |
|
procedure CheckHandle; virtual; abstract; |
77 |
|
procedure CheckTransaction(aTransaction: ITransaction); |
78 |
|
procedure GetDsqlInfo(info_request: byte; buffer: ISQLInfoResults); overload; virtual; abstract; |
79 |
|
procedure InternalPrepare; virtual; abstract; |
80 |
|
function InternalExecute(aTransaction: ITransaction): IResults; virtual; abstract; |
81 |
|
function InternalOpenCursor(aTransaction: ITransaction): IResultSet; virtual; abstract; |
82 |
+ |
procedure ProcessSQL(sql: AnsiString; GenerateParamNames: boolean; var processedSQL: AnsiString); virtual; abstract; |
83 |
|
procedure FreeHandle; virtual; abstract; |
84 |
|
procedure InternalClose(Force: boolean); virtual; abstract; |
85 |
+ |
function TimeStampToMSecs(const TimeStamp: TTimeStamp): Int64; |
86 |
|
public |
87 |
|
constructor Create(Attachment: IAttachment; Transaction: ITransaction; |
88 |
< |
sql: string; SQLDialect: integer); |
88 |
> |
sql: AnsiString; SQLDialect: integer); |
89 |
|
constructor CreateWithParameterNames(Attachment: IAttachment; Transaction: ITransaction; |
90 |
< |
sql: string; SQLDialect: integer; GenerateParamNames: boolean =false); |
90 |
> |
sql: AnsiString; SQLDialect: integer; GenerateParamNames: boolean =false); |
91 |
|
destructor Destroy; override; |
92 |
|
procedure Close; |
93 |
|
procedure TransactionEnding(aTransaction: ITransaction; Force: boolean); |
94 |
|
property SQLDialect: integer read FSQLDialect; |
95 |
+ |
property FirebirdClientAPI: TFBClientAPI read FFirebirdClientAPI; |
96 |
|
|
97 |
|
public |
98 |
|
function GetSQLParams: ISQLParams; virtual; abstract; |
100 |
|
function GetRowsAffected(var SelectCount, InsertCount, UpdateCount, |
101 |
|
DeleteCount: integer): boolean; |
102 |
|
function GetSQLStatementType: TIBSQLStatementTypes; |
103 |
< |
function GetSQLText: string; |
103 |
> |
function GetSQLText: AnsiString; |
104 |
> |
function GetProcessedSQLText: AnsiString; |
105 |
|
function GetSQLDialect: integer; |
106 |
|
|
107 |
|
{GetDSQLInfo only supports isc_info_sql_stmt_type, isc_info_sql_get_plan, isc_info_sql_records} |
108 |
|
procedure Prepare(aTransaction: ITransaction=nil); virtual; |
109 |
|
function Execute(aTransaction: ITransaction=nil): IResults; |
110 |
|
function OpenCursor(aTransaction: ITransaction=nil): IResultSet; |
111 |
< |
function CreateBlob(paramName: string): IBlob; overload; |
111 |
> |
function CreateBlob(paramName: AnsiString): IBlob; overload; |
112 |
|
function CreateBlob(index: integer): IBlob; overload; |
113 |
|
function CreateBlob(column: TColumnMetaData): IBlob; overload; virtual; abstract; |
114 |
< |
function CreateArray(paramName: string): IArray; overload; |
114 |
> |
function CreateArray(paramName: AnsiString): IArray; overload; |
115 |
|
function CreateArray(index: integer): IArray; overload; |
116 |
|
function CreateArray(column: TColumnMetaData): IArray; overload; virtual; abstract; |
117 |
|
function GetAttachment: IAttachment; |
118 |
|
function GetTransaction: ITransaction; |
119 |
|
function GetDSQLInfo(Request: byte): ISQLInfoResults; overload; |
120 |
|
procedure SetRetainInterfaces(aValue: boolean); virtual; |
121 |
+ |
procedure EnableStatistics(aValue: boolean); |
122 |
+ |
function GetPerfStatistics(var stats: TPerfCounters): boolean; |
123 |
|
property ChangeSeqNo: integer read FChangeSeqNo; |
124 |
|
property SQLParams: ISQLParams read GetSQLParams; |
125 |
|
property SQLStatementType: TIBSQLStatementTypes read GetSQLStatementType; |
140 |
|
IBError(ibxeNotInTransaction,[]); |
141 |
|
end; |
142 |
|
|
143 |
+ |
function TFBStatement.TimeStampToMSecs(const TimeStamp: TTimeStamp): Int64; |
144 |
+ |
begin |
145 |
+ |
Result := TimeStamp.Time + Int64(timestamp.date)*msecsperday; |
146 |
+ |
end; |
147 |
+ |
|
148 |
|
constructor TFBStatement.Create(Attachment: IAttachment; |
149 |
< |
Transaction: ITransaction; sql: string; SQLDialect: integer); |
149 |
> |
Transaction: ITransaction; sql: AnsiString; SQLDialect: integer); |
150 |
|
begin |
151 |
|
inherited Create(Transaction as TFBTransaction,2); |
152 |
|
FAttachmentIntf := Attachment; |
153 |
|
FTransactionIntf := Transaction; |
154 |
+ |
FFirebirdClientAPI := Attachment.getFirebirdAPI as TFBClientAPI; |
155 |
|
FSQLDialect := SQLDialect; |
156 |
|
FSQL := sql; |
157 |
|
end; |
158 |
|
|
159 |
|
constructor TFBStatement.CreateWithParameterNames(Attachment: IAttachment; |
160 |
< |
Transaction: ITransaction; sql: string; SQLDialect: integer; |
160 |
> |
Transaction: ITransaction; sql: AnsiString; SQLDialect: integer; |
161 |
|
GenerateParamNames: boolean); |
162 |
|
begin |
163 |
|
FHasParamNames := true; |
228 |
|
Result := FSQLStatementType; |
229 |
|
end; |
230 |
|
|
231 |
< |
function TFBStatement.GetSQLText: string; |
231 |
> |
function TFBStatement.GetSQLText: AnsiString; |
232 |
|
begin |
233 |
|
Result := FSQL; |
234 |
|
end; |
235 |
|
|
236 |
+ |
function TFBStatement.GetProcessedSQLText: AnsiString; |
237 |
+ |
begin |
238 |
+ |
if FProcessedSQL = '' then |
239 |
+ |
ProcessSQL(FSQL,FGenerateParamNames,FProcessedSQL); |
240 |
+ |
Result := FProcessedSQL |
241 |
+ |
end; |
242 |
+ |
|
243 |
|
function TFBStatement.GetSQLDialect: integer; |
244 |
|
begin |
245 |
|
Result := FSQLDialect; |
274 |
|
Result := InternalOpenCursor(aTransaction); |
275 |
|
end; |
276 |
|
|
277 |
< |
function TFBStatement.CreateBlob(paramName: string): IBlob; |
277 |
> |
function TFBStatement.CreateBlob(paramName: AnsiString): IBlob; |
278 |
|
var column: TColumnMetaData; |
279 |
|
begin |
280 |
|
InternalPrepare; |
290 |
|
Result := CreateBlob(SQLParams[index] as TSQLParam); |
291 |
|
end; |
292 |
|
|
293 |
< |
function TFBStatement.CreateArray(paramName: string): IArray; |
293 |
> |
function TFBStatement.CreateArray(paramName: AnsiString): IArray; |
294 |
|
var column: TColumnMetaData; |
295 |
|
begin |
296 |
|
InternalPrepare; |
318 |
|
|
319 |
|
function TFBStatement.GetDSQLInfo(Request: byte): ISQLInfoResults; |
320 |
|
begin |
321 |
< |
Result := TSQLInfoResultsBuffer.Create; |
321 |
> |
Result := TSQLInfoResultsBuffer.Create(FFirebirdClientAPI); |
322 |
|
GetDsqlInfo(Request,Result); |
323 |
|
end; |
324 |
|
|
327 |
|
RetainInterfaces := aValue; |
328 |
|
end; |
329 |
|
|
330 |
+ |
procedure TFBStatement.EnableStatistics(aValue: boolean); |
331 |
+ |
begin |
332 |
+ |
if FCollectStatistics <> aValue then |
333 |
+ |
begin |
334 |
+ |
FCollectStatistics := aValue; |
335 |
+ |
FStatisticsAvailable := false; |
336 |
+ |
end; |
337 |
+ |
end; |
338 |
+ |
|
339 |
+ |
function TFBStatement.GetPerfStatistics(var stats: TPerfCounters): boolean; |
340 |
+ |
begin |
341 |
+ |
Result := FStatisticsAvailable; |
342 |
+ |
if Result then |
343 |
+ |
begin |
344 |
+ |
stats[psCurrentMemory] := FAfterStats[psCurrentMemory]; |
345 |
+ |
stats[psDeltaMemory] := FAfterStats[psCurrentMemory] - FBeforeStats[psCurrentMemory]; |
346 |
+ |
stats[psMaxMemory] := FAfterStats[psMaxMemory]; |
347 |
+ |
stats[psRealTime] := FAfterStats[psRealTime] - FBeforeStats[psRealTime]; |
348 |
+ |
stats[psUserTime] := FAfterStats[psUserTime] - FBeforeStats[psUserTime]; |
349 |
+ |
stats[psReads] := FAfterStats[psReads] - FBeforeStats[psReads]; |
350 |
+ |
stats[psWrites] := FAfterStats[psWrites] - FBeforeStats[psWrites]; |
351 |
+ |
stats[psFetches] := FAfterStats[psFetches] - FBeforeStats[psFetches]; |
352 |
+ |
stats[psBuffers] := FAfterStats[psBuffers]; |
353 |
+ |
end; |
354 |
+ |
end; |
355 |
+ |
|
356 |
|
end. |
357 |
|
|