forked from sirodcar/commonx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractDB.pas
More file actions
476 lines (395 loc) · 10.6 KB
/
AbstractDB.pas
File metadata and controls
476 lines (395 loc) · 10.6 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
unit AbstractDB;
{x$DEFINE NO_DB}
interface
uses
tickcount, consolelock, betterobject, classes, stringx, typex, systemx, sysutils, storageenginetypes, commandprocessor, commandicons, namevaluepair;
type
TAbstractDBCursor = class;//forward
TAbstractdb = class(TBetterObject)
private
function GetAge: ticker;
protected
FMWhost, FMWendPoint: string;
function GetMWEndPoint: string;
function GetMWHost: string;
procedure SetMWEndpoint(Value: string);
procedure SetMWHost(Value: string);
public
{$IFNDEF CONTEXT_ONLY}
database, DBHost, DBuser, DBpassword, DBPort: string;
{$ENDIF}
Created: ticker;
LastUsed: ticker;
accumulator: TStringBuilder;
procedure Init;override;
destructor Destroy;override;
procedure Connect;overload;virtual;
{$IFNDEF CONTEXT_ONLY}
procedure Connect(sHost: string; sDatabase: string; sUser: string; sPAssword: string; sPort: string = '')overload;
{$ENDIF}
function ReadQueryDBC(sQuery: string): TAbstractDBCursor;overload;virtual;
function ReadQueryNVPL(sQuery: string): TNameValuePairList;
function ReadQuery(sQuery: string): TSERowSet;overload;virtual;abstract;
function ReadQueryH(sQuery: string): IHolder<TSERowSet>;overload;
procedure WriteQuery(sQuery: string);virtual;
procedure Writebehind(sQuery: string; bDontLog: boolean = false);virtual;
// property conn: TSQLConnection read Fconn;
function FunctionQuery(sQuery: string; sDefault: int64): int64;overload;virtual;abstract;
function FunctionQuery(sQuery: string; rDefault: double): double;overload;virtual;abstract;
function FunctionQuery(sQuery: string; sDefault: string): string;overload;virtual;abstract;
function FunctionQuery_Cached(sQuery: string; iDefault: int64): int64;overload;
function FunctionQuery_Cached(sQuery: string; rDefault: double): double;overload;
function FunctionQuery_Cached(sQuery: string; sDefault: string): string;overload;
procedure ClearQueryCache;
function Connected: boolean;virtual;abstract;
property MWHost: string read GetMWHost write SetMWHost;
property MWEndPoint: string read GetMWEndPoint write SetMWEndpoint;
property Age: ticker read GetAge;
function GetNextID(sType: string): int64;virtual;abstract;
function SetNextID(sType: string; id: int64): boolean;virtual;abstract;
end;
TAbstractFieldDef = TSERowsetFieldDef;
TAbstractDBCursor = class(TBetterObject)
private
protected
function GetFieldByindex(idx: ni): variant;virtual;abstract;
function GetField(sName: string): variant;virtual;abstract;
function GetFieldCount: ni;virtual;abstract;
public
procedure BeforeDestruction;override;
procedure Open(db: TAbstractDb; sQuery: string);virtual;
procedure Close;virtual;
procedure First;virtual;abstract;
function EOF: boolean;virtual;abstract;
procedure Next;virtual;abstract;
function CountRecords: ni;virtual;ABSTRACT;
property Fields[sName: string]: variant read GetField;default;
property FieldsByIndex[idx: ni]: variant read GetFieldByindex;
property FieldCount: ni read GetFieldCount;
property RecordCount: ni read CountRecords;
end;
TSERowSetCursor = class (TAbstractDBCursor)
protected
function GetField(sName: string): variant;override;
function GetFieldCount: ni;override;
function GetFieldByIndex(idx: ni): variant;override;
public
RS: TSERowSet;
RecordIndex: ni;
destructor Destroy;override;
procedure First;override;
function EOF: boolean;override;
procedure Next;override;
function CountRecords: ni;override;
end;
Tcmd_RunQueries = class(TCommand)
public
sdb: TAbstractDB;
queries: string;
procedure Initexpense;override;
procedure DoExecute;override;
end;
var
queryCache: TNameValuePairList;
implementation
{ TAbstractdb }
procedure TAbstractdb.ClearQueryCache;
begin
lockconsole;
try
queryCache.Clear;
finally
UnlockConsole;
end;
end;
{$IFNDEF CONTEXT_ONLY}
procedure TAbstractdb.Connect(sHost, sDatabase, sUser, sPAssword,
sPort: string);
begin
DBhost := shost;
database := sDatabase;
DBuser := sUser;
DBpassword := sPassword;
DbPort := sPort;
if sPort = '' then
DBPort := '3306';
Connect;
end;
{$ENDIF}
procedure TAbstractdb.Connect;
begin
//
end;
destructor TAbstractdb.Destroy;
begin
accumulator.Free;
inherited;
end;
function TAbstractdb.FunctionQuery_Cached(sQuery: string;
iDefault: int64): int64;
var
sKey, sValue: string;
ix: ni;
begin
lockconsole;
try
sKey := StringReplace(sQuery, #13,'', [rfReplaceAll]);
sKey := StringReplace(sKey, #10,'', [rfReplaceAll]);
sKey := StringReplace(sKey, '=',#2, [rfReplaceAll]);
ix := queryCache.IndexOf(sKey);
if ix > 0 then begin
Result := strtoint64(queryCache.ItemsByIndex[ix].Value);
end else begin
result := FunctionQuery(sQuery, iDefault);
queryCache.Add(sKey, inttostr(result));
if random(100) = 50 then
queryCache.SaveToFile;
end;
finally
UnlockConsole;
end;
end;
function TAbstractdb.FunctionQuery_Cached(sQuery: string;
rDefault: double): double;
var
sKey, sValue: string;
ix: ni;
begin
lockconsole;
try
sKey := StringReplace(sQuery, #13,'', [rfReplaceAll]);
sKey := StringReplace(sKey, #10,'', [rfReplaceAll]);
sKey := StringReplace(sKey, '=',#2, [rfReplaceAll]);
ix := queryCache.IndexOf(sKey);
if ix > 0 then begin
Result := strtofloat(queryCache.ItemsByIndex[ix].Value);
end else begin
result := FunctionQuery(sQuery, rDefault);
queryCache.Add(sKey, floattostr(result));
if random(100) = 50 then
queryCache.SaveToFile;
end;
finally
UnlockConsole;
end;
end;
function TAbstractdb.FunctionQuery_Cached(sQuery, sDefault: string): string;
var
sKey, sValue: string;
ix: ni;
begin
lockconsole;
try
sKey := StringReplace(sQuery, #13,'', [rfReplaceAll]);
sKey := StringReplace(sKey, #10,'', [rfReplaceAll]);
sKey := StringReplace(sKey, '=',#2, [rfReplaceAll]);
ix := queryCache.IndexOf(sKey);
if ix > 0 then begin
Result := queryCache.ItemsByIndex[ix].Value;
end else begin
result := FunctionQuery(sQuery, sDefault);
queryCache.Add(sKey, result);
if random(100) = 50 then
queryCache.SaveToFile;
end;
finally
UnlockConsole;
end;
end;
function TAbstractdb.GetAge: ticker;
begin
result := getTimeSince(Created);
end;
function TAbstractdb.GetMWEndPoint: string;
begin
result := FMWEndPoint;
end;
function TAbstractdb.GetMWHost: string;
begin
result := FMWhost;
end;
procedure TAbstractdb.Init;
begin
inherited;
accumulator := TStringBuilder.create;
DBPort := '3306';
end;
function TAbstractdb.ReadQueryDBC(sQuery: string): TAbstractDBCursor;
begin
//
result := nil;
end;
function TAbstractdb.ReadQueryH(sQuery: string): IHolder<TSERowSet>;
begin
result := THolder<TSERowSet>.create;
result.o := ReadQuery(sQuery);
end;
function TAbstractdb.ReadQueryNVPL(sQuery: string): TNameValuePairList;
var
dbc: TAbstractDBCursor;
begin
result := nil;
dbc := nil;
try
dbc := ReadQueryDBC(sQuery);
result := TNameValuePairList.create;
dbc.first;
while not dbc.eof do begin
result.Add(dbc.fieldsbyindex[0], dbc.fieldsbyindex[1]);
dbc.next;
end;
finally
dbc.free;
end;
end;
procedure TAbstractdb.SetMWEndpoint(Value: string);
begin
FMWendpoint := value;
end;
procedure TAbstractdb.SetMWHost(Value: string);
begin
FMWhost := value;
end;
procedure TAbstractdb.Writebehind(sQuery: string; bDontLog: boolean = false);
begin
// raise Exception.create('unimplemented');
//TODO -cunimplemented: unimplemented block
end;
procedure TAbstractdb.WriteQuery(sQuery: string);
begin
//
end;
{ Tcmd_RunQueries }
procedure Tcmd_RunQueries.DoExecute;
var
sLeft, sRight: string;
slParsed: TStringlist;
t: ni;
u: ni;
s: string;
iStartAt: ni;
sAccum: string;
begin
slParsed := nil;
try
sRight := queries;
StepCount := length(Queries);
{$IFDEF SL_PARSE}
queries := '';//<-- do this to conserve memory
{$IFDEF SLOW_PARSE}
while SplitString(sRight, '--execute--', sLeft, sRight) do begin
Step := StepCount-length(sRight);
{$IFNDEF NO_DB}
sdb.WriteBehind(sLeft);
{$ENDIF}
end;
{$IFNDEF NO_DB}
sdb.WriteBehind(sLeft);
{$ENDIF}
{$ELSE}
slPArsed := parseString_quick(sright, '--execute--');
stepcount := slParsed.count;
for t:= 0 to ((slPArsed.count div 16)*16)-1 do begin
IF (t mod 16) <> 0 then continue;
step := t;
s := '';
for u := 0 to 15 do begin
s := s + slparsed[t+u]+'--execute--';
end;
sdb.WriteBehind(s);
end;
for t:= ((slPArsed.count div 16)*16) to slPArsed.count-1 do begin
step := t;
s := '';
s := s + slparsed[t]+'--execute--';
end;
sdb.WriteBehind(s);
{$ENDIF}
{$ELSE}
iStartAt := 0;
StepCount := length(queries);
sAccum := '';
while zExtractDelimitedString(queries, '--execute--', iStartAt, s) do begin
step := iStartAt;
sAccum := sAccum + s+ '--execute--';
if length(sAccum) > 10000 then begin
{$IFNDEF NO_DB}
sdb.Writebehind(sAccum);
{$ENDIF}
sAccum := '';
end;
end;
sAccum := sAccum + s;
if sAccum <> '' then begin
{$IFNDEF NO_DB}
sdb.Writebehind(sAccum);
{$ENDIF}
end;
{$ENDIF}
finally
slPArsed.free;
end;
end;
procedure Tcmd_RunQueries.Initexpense;
begin
inherited;
CPUExpense := 0.0;
Icon := @CMD_ICON_DATABASE;
end;
{ TAbstractDBCursor }
procedure TAbstractDBCursor.BeforeDestruction;
begin
inherited;
//
end;
procedure TAbstractDBCursor.Close;
begin
//
end;
procedure TAbstractDBCursor.Open(db: TAbstractDb; sQuery: string);
begin
//
end;
{ TSERowSetCursor }
function TSERowSetCursor.CountRecords: ni;
begin
result := RS.RowCount;
end;
destructor TSERowSetCursor.Destroy;
begin
RS.Free;
rs := nil;
inherited;
end;
function TSERowSetCursor.EOF: boolean;
begin
result := RecordIndex >= RS.RowCount;
end;
procedure TSERowSetCursor.First;
begin
RecordIndex := 0;
end;
function TSERowSetCursor.GetField(sName: string): variant;
begin
result := rs.FieldValues[recordindex, sName];
end;
function TSERowSetCursor.GetFieldByIndex(idx: ni): variant;
begin
result := RS.Values[idx, RecordIndex];
end;
function TSERowSetCursor.GetFieldCount: ni;
begin
result := RS.FieldCount;
end;
procedure TSERowSetCursor.Next;
begin
inc(RecordIndex);
end;
initialization
queryCache := TNameValuePairList.create;
queryCache.LoadFromFile(GetTempPath+extractfilename(dllname)+'.querycache');
finalization
querycache.savetofile(GetTempPath+extractfilename(dllname)+'.querycache');
querycache.free;
querycache := nil;
end.