-
Notifications
You must be signed in to change notification settings - Fork 58
/
pFIBCacheQueries.pas
357 lines (316 loc) · 8.11 KB
/
pFIBCacheQueries.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:gdeatz@hlmdd.com }
{ }
{ Copyright (c) 1998-2007 Devrace Ltd. }
{ Written by Serge Buzadzhy (buzz@devrace.com) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit pFIBCacheQueries;
interface
{$I FIBPlus.inc}
uses
SysUtils,Classes,FIBQuery,FIBDatabase,SyncObjs;
function GetQueryForUse(aTransaction:TFIBTransaction; const SQLText:string):TFIBQuery;
procedure FreeQueryForUse(aFIBQuery:TFIBQuery);
procedure FreeHandleCachedQuery(DB:TFIBDataBase;const SQLText:string);
procedure ClearQueryCacheList(DB:TFIBDataBase);
implementation
uses
{$IFDEF D_XE3}
System.Types, // for inline funcs
{$ENDIF}
SqlTxtRtns,pFIBLists,StrUtil;
type
TCacheQueries=class(TComponent)
private
FFIBDataBase:TFIBDataBase;
FListUnused :TObjStringList;
FListUsed :TList;
vInClear :boolean;
procedure Clear;
function UseQuery(aTransaction:TFIBTransaction;
const SQLText:string
):TFIBQuery;
procedure UnUseQuery(aFIBQuery:TFIBQuery);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation);override;
public
constructor Create(aFIBDataBase:TFIBDataBase); reintroduce;
destructor Destroy; override;
end;
TCacheList =class(TComponent)
private
FList:TList;
FLock: TCriticalSection;
function GetCacheForDB(aDataBase:TFIBDatabase):TCacheQueries;
procedure RemoveDataBase(aDataBase:TFIBDatabase);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation);override;
public
constructor Create(AOwner:TComponent);override;
destructor Destroy; override;
function UseQuery(aTransaction:TFIBTransaction; const SQLText:string):TFIBQuery;
procedure UnUseQuery(aFIBQuery:TFIBQuery);
procedure FreeUnusedQueries;
end;
var
CacheList:TCacheList;
{ TCacheQueries }
procedure TCacheQueries.Clear;
var i:integer;
begin
try
vInClear := true;
with FListUsed do
for i := 0 to Pred(Count) do
TObject(FListUsed[i]).Free;
finally
vInClear := false;
end;
end;
constructor TCacheQueries.Create(aFIBDataBase:TFIBDataBase);
begin
inherited Create(nil);
FFIBDataBase:=aFIBDataBase;
FListUnused :=TObjStringList.Create(nil,true);
FListUsed :=TList.Create;
end;
destructor TCacheQueries.Destroy;
begin
Clear;
try
vInClear := true;
FListUnused.Free;
finally
vInClear := false;
end;
FListUsed.Free;
inherited;
end;
procedure TCacheQueries.Notification(AComponent: TComponent;
Operation: TOperation);
begin
if (Operation=opRemove) and not vInClear then
if Acomponent is TFIBQuery then
begin
FListUsed.Remove(Acomponent);
FListUnused.Remove(FastTrim(TFIBQuery(Acomponent).SQL.Text));
end;
inherited;
end;
procedure TCacheQueries.UnUseQuery(aFIBQuery:TFIBQuery);
var
i:integer;
begin
i:=FListUsed.IndexOf(aFIBQuery);
if i>=0 then
begin
FListUsed.Delete(i);
FListUnused.AddObject(
FastTrim(aFIBQuery.SQL.Text),aFIBQuery
);
end;
end;
function TCacheQueries.UseQuery(
aTransaction: TFIBTransaction; const SQLText: string
): TFIBQuery;
var i:integer;
begin
if FListUnused.Find(FastTrim(SQLText),i) then
begin
Result:=TFIBQuery(FListUnused.Objects[i]);
if Result.Transaction<>aTransaction then
Result.Transaction:=aTransaction;
FListUsed.Add(Result);
FListUnused.Delete(i)
end
else
begin
Result:=TFIBQuery.Create(nil);
Result.FreeNotification(Self);
with Result do
begin
DataBase:=FFIBDataBase;
Transaction:=aTransaction;
SQl.Text :=SQlText;
FListUsed.Add(Result);
end;
end;
end;
{ TCacheList }
constructor TCacheList.Create(AOwner: TComponent);
begin
inherited;
FList:=TList.Create;
FLock:=TCriticalSection.Create
end;
destructor TCacheList.Destroy;
var
i:integer;
begin
FLock.Acquire;
try
with FList do
for i := 0 to Pred(Count) do TObject(FList[i]).Free;
FList.Free;
inherited;
finally
FLock.Release;
FLock.Free
end
end;
function TCacheList.GetCacheForDB(aDataBase: TFIBDatabase): TCacheQueries;
var
j:integer;
begin
FLock.Acquire;
try
with FList do
for j := 0 to Pred(Count) do // Iterate
if TCacheQueries(FList[j]).FFIBDataBase=aDataBase then
begin
Result:=TCacheQueries(FList[j]);
Exit;
end;
Result:=TCacheQueries.Create(aDataBase);
FList.Add(Result);
finally
FLock.Release
end;
end;
procedure TCacheList.Notification(AComponent: TComponent;
Operation: TOperation);
begin
if (Operation=opRemove)and (AComponent is TFIBDatabase) then
RemoveDataBase(TFIBDatabase(AComponent));
inherited;
end;
procedure TCacheList.RemoveDataBase(aDataBase: TFIBDatabase);
var i:integer;
begin
FLock.Acquire;
try
with FList do
for i := Pred(Count) downto 0 do
if TCacheQueries(FList[i]).FFIBDataBase=aDataBase then
begin
TCacheQueries(FList[i]).Free;
Delete(i)
end;
finally
FLock.Release
end
end;
procedure TCacheList.UnUseQuery(aFIBQuery: TFIBQuery);
begin
if(aFIBQuery=nil) or(aFIBQuery.Database=nil) then Exit;
FLock.Acquire;
try
GetCacheForDB(aFIBQuery.Database).UnUseQuery(aFIBQuery);
finally
FLock.Release
end;
end;
function TCacheList.UseQuery(aTransaction: TFIBTransaction;
const SQLText: string): TFIBQuery;
var
DB:TFIBDatabase;
begin
Result:=nil;
// if(aTransaction=nil) or(aTransaction.DefaultDatabase=nil) then Exit;
if(aTransaction=nil) then Exit;
DB:=aTransaction.DefaultDatabase;
if (DB=nil) then
if aTransaction.DatabaseCount<>1 then
Exit
else
DB:=aTransaction.Databases[0];
FLock.Acquire;
try
DB.FreeNotification(Self);
Result:=
GetCacheForDB(DB).UseQuery(aTransaction,SQLText);
finally
FLock.Release;
end;
end;
procedure TCacheList.FreeUnusedQueries;
var
i:integer;
begin
FLock.Acquire;
try
for i:=0 to Pred(FList.Count) do
begin
TCacheQueries(FList[i]).FListUnused.FullClear
end;
finally
FLock.Release;
end;
end;
// interface
function
GetQueryForUse(aTransaction:TFIBTransaction; const SQLText:string):TFIBQuery;
begin
Result:=CacheList.UseQuery(aTransaction,SQLText);
if (Result<>nil) and Result.Open then Result.Close;
end;
procedure FreeQueryForUse(aFIBQuery:TFIBQuery);
begin
if aFIBQuery.Open then
aFIBQuery.Close;
CacheList.UnUseQuery(aFIBQuery)
end;
procedure FreeHandleCachedQuery(DB:TFIBDataBase;const SQLText:string);
var
cq:TCacheQueries;
i:integer;
begin
cq:=CacheList.GetCacheForDB(DB);
if cq<>nil then
with cq,CacheList do
begin
FLock.Acquire;
try
if FListUnused.Find(FastTrim(SQLText),i) then
TFIBQuery(FListUnused.Objects[i]).FreeHandle;
finally
FLock.Release
end;
end;
end;
procedure ClearQueryCacheList(DB:TFIBDataBase);
var
cq:TCacheQueries;
i:integer;
begin
cq:=CacheList.GetCacheForDB(DB);
if cq<>nil then
with cq do
begin
CacheList.FLock.Acquire;
try
for i:=FListUnused.Count-1 downto 0 do
TFIBQuery(FListUnused.Objects[i]).Free;
finally
CacheList.FLock.Release
end;
end;
end;
initialization
CacheList:=TCacheList.Create(nil);
finalization
CacheList.Free;
end.