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 |
+ |
FCaseSensitiveParams: boolean; |
77 |
|
procedure CheckHandle; virtual; abstract; |
78 |
|
procedure CheckTransaction(aTransaction: ITransaction); |
79 |
|
procedure GetDsqlInfo(info_request: byte; buffer: ISQLInfoResults); overload; virtual; abstract; |
80 |
|
procedure InternalPrepare; virtual; abstract; |
81 |
|
function InternalExecute(aTransaction: ITransaction): IResults; virtual; abstract; |
82 |
|
function InternalOpenCursor(aTransaction: ITransaction): IResultSet; virtual; abstract; |
83 |
+ |
procedure ProcessSQL(sql: AnsiString; GenerateParamNames: boolean; var processedSQL: AnsiString); virtual; abstract; |
84 |
|
procedure FreeHandle; virtual; abstract; |
85 |
|
procedure InternalClose(Force: boolean); virtual; abstract; |
86 |
+ |
function TimeStampToMSecs(const TimeStamp: TTimeStamp): Int64; |
87 |
|
public |
88 |
|
constructor Create(Attachment: IAttachment; Transaction: ITransaction; |
89 |
< |
sql: string; SQLDialect: integer); |
89 |
> |
sql: AnsiString; SQLDialect: integer); |
90 |
|
constructor CreateWithParameterNames(Attachment: IAttachment; Transaction: ITransaction; |
91 |
< |
sql: string; SQLDialect: integer; GenerateParamNames: boolean =false); |
91 |
> |
sql: AnsiString; SQLDialect: integer; GenerateParamNames: boolean =false; |
92 |
> |
CaseSensitiveParams: boolean = false); |
93 |
|
destructor Destroy; override; |
94 |
|
procedure Close; |
95 |
|
procedure TransactionEnding(aTransaction: ITransaction; Force: boolean); |
96 |
|
property SQLDialect: integer read FSQLDialect; |
97 |
+ |
property FirebirdClientAPI: TFBClientAPI read FFirebirdClientAPI; |
98 |
|
|
99 |
|
public |
100 |
|
function GetSQLParams: ISQLParams; virtual; abstract; |
102 |
|
function GetRowsAffected(var SelectCount, InsertCount, UpdateCount, |
103 |
|
DeleteCount: integer): boolean; |
104 |
|
function GetSQLStatementType: TIBSQLStatementTypes; |
105 |
< |
function GetSQLText: string; |
105 |
> |
function GetSQLText: AnsiString; |
106 |
> |
function GetProcessedSQLText: AnsiString; |
107 |
|
function GetSQLDialect: integer; |
108 |
|
|
109 |
|
{GetDSQLInfo only supports isc_info_sql_stmt_type, isc_info_sql_get_plan, isc_info_sql_records} |
110 |
|
procedure Prepare(aTransaction: ITransaction=nil); virtual; |
111 |
|
function Execute(aTransaction: ITransaction=nil): IResults; |
112 |
|
function OpenCursor(aTransaction: ITransaction=nil): IResultSet; |
113 |
< |
function CreateBlob(paramName: string): IBlob; overload; |
113 |
> |
function CreateBlob(paramName: AnsiString): IBlob; overload; |
114 |
|
function CreateBlob(index: integer): IBlob; overload; |
115 |
|
function CreateBlob(column: TColumnMetaData): IBlob; overload; virtual; abstract; |
116 |
< |
function CreateArray(paramName: string): IArray; overload; |
116 |
> |
function CreateArray(paramName: AnsiString): IArray; overload; |
117 |
|
function CreateArray(index: integer): IArray; overload; |
118 |
|
function CreateArray(column: TColumnMetaData): IArray; overload; virtual; abstract; |
119 |
|
function GetAttachment: IAttachment; |
120 |
|
function GetTransaction: ITransaction; |
121 |
|
function GetDSQLInfo(Request: byte): ISQLInfoResults; overload; |
122 |
|
procedure SetRetainInterfaces(aValue: boolean); virtual; |
123 |
+ |
procedure EnableStatistics(aValue: boolean); |
124 |
+ |
function GetPerfStatistics(var stats: TPerfCounters): boolean; |
125 |
|
property ChangeSeqNo: integer read FChangeSeqNo; |
126 |
|
property SQLParams: ISQLParams read GetSQLParams; |
127 |
|
property SQLStatementType: TIBSQLStatementTypes read GetSQLStatementType; |
142 |
|
IBError(ibxeNotInTransaction,[]); |
143 |
|
end; |
144 |
|
|
145 |
+ |
function TFBStatement.TimeStampToMSecs(const TimeStamp: TTimeStamp): Int64; |
146 |
+ |
begin |
147 |
+ |
Result := TimeStamp.Time + Int64(timestamp.date)*msecsperday; |
148 |
+ |
end; |
149 |
+ |
|
150 |
|
constructor TFBStatement.Create(Attachment: IAttachment; |
151 |
< |
Transaction: ITransaction; sql: string; SQLDialect: integer); |
151 |
> |
Transaction: ITransaction; sql: AnsiString; SQLDialect: integer); |
152 |
|
begin |
153 |
|
inherited Create(Transaction as TFBTransaction,2); |
154 |
|
FAttachmentIntf := Attachment; |
155 |
|
FTransactionIntf := Transaction; |
156 |
+ |
FFirebirdClientAPI := Attachment.getFirebirdAPI as TFBClientAPI; |
157 |
|
FSQLDialect := SQLDialect; |
158 |
|
FSQL := sql; |
159 |
|
end; |
160 |
|
|
161 |
|
constructor TFBStatement.CreateWithParameterNames(Attachment: IAttachment; |
162 |
< |
Transaction: ITransaction; sql: string; SQLDialect: integer; |
163 |
< |
GenerateParamNames: boolean); |
162 |
> |
Transaction: ITransaction; sql: AnsiString; SQLDialect: integer; |
163 |
> |
GenerateParamNames: boolean; CaseSensitiveParams: boolean); |
164 |
|
begin |
165 |
|
FHasParamNames := true; |
166 |
|
FGenerateParamNames := GenerateParamNames; |
167 |
+ |
FCaseSensitiveParams := CaseSensitiveParams; |
168 |
|
Create(Attachment,Transaction,sql,SQLDialect); |
169 |
|
end; |
170 |
|
|
231 |
|
Result := FSQLStatementType; |
232 |
|
end; |
233 |
|
|
234 |
< |
function TFBStatement.GetSQLText: string; |
234 |
> |
function TFBStatement.GetSQLText: AnsiString; |
235 |
|
begin |
236 |
|
Result := FSQL; |
237 |
|
end; |
238 |
|
|
239 |
+ |
function TFBStatement.GetProcessedSQLText: AnsiString; |
240 |
+ |
begin |
241 |
+ |
if FProcessedSQL = '' then |
242 |
+ |
ProcessSQL(FSQL,FGenerateParamNames,FProcessedSQL); |
243 |
+ |
Result := FProcessedSQL |
244 |
+ |
end; |
245 |
+ |
|
246 |
|
function TFBStatement.GetSQLDialect: integer; |
247 |
|
begin |
248 |
|
Result := FSQLDialect; |
277 |
|
Result := InternalOpenCursor(aTransaction); |
278 |
|
end; |
279 |
|
|
280 |
< |
function TFBStatement.CreateBlob(paramName: string): IBlob; |
280 |
> |
function TFBStatement.CreateBlob(paramName: AnsiString): IBlob; |
281 |
|
var column: TColumnMetaData; |
282 |
|
begin |
283 |
|
InternalPrepare; |
293 |
|
Result := CreateBlob(SQLParams[index] as TSQLParam); |
294 |
|
end; |
295 |
|
|
296 |
< |
function TFBStatement.CreateArray(paramName: string): IArray; |
296 |
> |
function TFBStatement.CreateArray(paramName: AnsiString): IArray; |
297 |
|
var column: TColumnMetaData; |
298 |
|
begin |
299 |
|
InternalPrepare; |
321 |
|
|
322 |
|
function TFBStatement.GetDSQLInfo(Request: byte): ISQLInfoResults; |
323 |
|
begin |
324 |
< |
Result := TSQLInfoResultsBuffer.Create; |
324 |
> |
Result := TSQLInfoResultsBuffer.Create(FFirebirdClientAPI); |
325 |
|
GetDsqlInfo(Request,Result); |
326 |
|
end; |
327 |
|
|
330 |
|
RetainInterfaces := aValue; |
331 |
|
end; |
332 |
|
|
333 |
+ |
procedure TFBStatement.EnableStatistics(aValue: boolean); |
334 |
+ |
begin |
335 |
+ |
if FCollectStatistics <> aValue then |
336 |
+ |
begin |
337 |
+ |
FCollectStatistics := aValue; |
338 |
+ |
FStatisticsAvailable := false; |
339 |
+ |
end; |
340 |
+ |
end; |
341 |
+ |
|
342 |
+ |
function TFBStatement.GetPerfStatistics(var stats: TPerfCounters): boolean; |
343 |
+ |
begin |
344 |
+ |
Result := FStatisticsAvailable; |
345 |
+ |
if Result then |
346 |
+ |
begin |
347 |
+ |
stats[psCurrentMemory] := FAfterStats[psCurrentMemory]; |
348 |
+ |
stats[psDeltaMemory] := FAfterStats[psCurrentMemory] - FBeforeStats[psCurrentMemory]; |
349 |
+ |
stats[psMaxMemory] := FAfterStats[psMaxMemory]; |
350 |
+ |
stats[psRealTime] := FAfterStats[psRealTime] - FBeforeStats[psRealTime]; |
351 |
+ |
stats[psUserTime] := FAfterStats[psUserTime] - FBeforeStats[psUserTime]; |
352 |
+ |
stats[psReads] := FAfterStats[psReads] - FBeforeStats[psReads]; |
353 |
+ |
stats[psWrites] := FAfterStats[psWrites] - FBeforeStats[psWrites]; |
354 |
+ |
stats[psFetches] := FAfterStats[psFetches] - FBeforeStats[psFetches]; |
355 |
+ |
stats[psBuffers] := FAfterStats[psBuffers]; |
356 |
+ |
end; |
357 |
+ |
end; |
358 |
+ |
|
359 |
|
end. |
360 |
|
|