forked from adaloveless/commonx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractRDTPDataModule.pas
741 lines (591 loc) · 16.1 KB
/
AbstractRDTPDataModule.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
unit AbstractRDTPDataModule;
interface
uses
typex, orderlyinit, tickcount, managedthread, SysUtils, Classes, SqlExpr, better_Sockets, systemx,inifiles, commandprocessor,
sockfix, classfactory, sharedobject, betterobject, windows, stringx, genericRDTPClient, storageenginetypes, helpers.list;
const
DB_CONNECTION_TIMEOUT: cardinal = 300000;
MAX_DM_AGE = 300000;
SQL_CHANNEL_READ = 0;
SQL_CHANNEL_WRITE = 1;
SQL_CHANNEL_SYSTEM = 2;
type
TSQLChannel = (sqlRead, sqlWrite, sqlSystem);
TAbstractRDTPDataModule = class;
TRDTPDataModuleClass= class of TAbstractRDTPDataModule;
TRDTPExecutionTHread = class;
TDataClass = class of TObject;
TAbstractRDTPDataModuleClass = class of TAbstractRDTPDataModule;//forward
TSQLChannelStats = record
WriteQueries: int64;
ReadQueries: int64;
InTransaction: boolean;
procedure Init;
end;
PSQLChannelStats = ^TSQLChannelStats;
TDataPool = class(TSharedObject)
private
function GetClassToCreate: TRDTPDataMOduleClass;
procedure SetClassToCreate(const Value: TRDTPDataMOduleClass);
function GetCount: integer;
function GetData(idx: integer): TAbstractRDTPDAtaModule;
function GetActiveCount: integer;
protected
FRDTPDataModuleClass: TRDTPDataModuleClass;
FData: TList;
Fet: TRDTPExecutionThread;
FActiveCount: integer;
FCreationTime: cardinal;
property Data[idx: integer]: TAbstractRDTPDAtaModule read GetData;
public
factory: TClassClassFactory;
constructor create;override;
destructor destroy;override;
function NeedData(cClass: TDataClass; sContext: string): TAbstractRDTPDataModule;
procedure NoNeedData(dm: TAbstractRDTPDataModule);
property DefaultProduct: TRDTPDataMOduleClass read GetClassToCreate write SetClassToCreate;
property Count: integer read GetCount;
{$IFDEF ALLOW_CONTROLS}
procedure SyncToListView(lv: TListView);
{$ENDIF}
property ActiveCount: integer read GetActiveCount;
end;
Tcmd_DBWriteBehind = class(TCommand)
public
dm: TAbstractRDTPDataModule;
query: string;
procedure InitExpense;override;
procedure DoExecute;override;
end;
TAbstractRDTPDataModule = class(TBetterObject)
private
FLastUsed: cardinal;
FCreationTime: cardinal;
procedure SetConfigFromLocalFile(const Value: boolean);
protected
FChannelStats: array[0..2] of TSQLChannelStats;
cmd: TCommand;
Fet: TRDTpExecutionThread;
FProg: TRDTPProgressEvent;
FContext: string;
FConfigFromLocalFile: boolean;
function GetChannelStats(ch: TSQLChannel): PSQLChannelStats;
procedure WaitForCommands;
function GetExecutionThread: TRDTPExecutionThread;
procedure DataModuleCreate(Sender: TObject);
procedure Execute(sQuery:string; connection: TSQLConnection; ds: TCustomSQLDataset);
procedure ExecuteDirect(sQuery: string; connection: TSQLConnection);
procedure SetProgress(const Value: TRDTPProgressEvent);virtual;
procedure SetContext(value: string);
function GetContext: string;
public
constructor create;overload;override;
constructor create2(sContext: string);overload;virtual;
destructor destroy;override;
procedure BeginTransaction;
procedure Commit;
procedure Rollback;
procedure ResetForPool;virtual;abstract;
procedure BeginTransactionOn(ch: TSQLChannel);virtual;abstract;
procedure CommitOn(ch: TSQLChannel);virtual;abstract;
procedure RollbackOn(ch: TSQLChannel);virtual;abstract;
procedure WriteOn(ch: TSQLChannel; sQuery: string);virtual;abstract;
function ReadOn(ch: TSQLChannel; sQuery: string): TSERowSEt;virtual;abstract;
function GetNextID(sKey: string): int64;virtual;abstract;
function GetNextIDEx(sKey: string; sTable, sField: string; count: int64): int64;virtual;abstract;
function SetNextID(sKey: string; iValue: int64): int64;virtual;abstract;
//RETURNS IHolder<TSERowSet>
function ReadQuery(sQuery: string): IHolder<TSERowset>;
function SystemReadQuery(sQuery: string): IHolder<TSERowset>;
//RETURNS TSEROWSET
function ExecuteSystem(sQuery: string; out dataset: TSERowSet): integer;overload;virtual;
function ExecuteWrite(sQuery: string; out dataset: TSERowSet): integer;overload;virtual;
procedure ExecuteRead(sQuery: string; out dataset: TSERowSet);virtual;
//RETURNS NOTHING
function ExecuteSystem(sQuery: string): integer;overload;virtual;
function ExecuteWrite(sQuery: string): integer;overload;virtual;
function ExecuteWriteRaw(sQuery: string): integer;overload;virtual;
procedure ExecuteWriteBehind(sQuery: string);
procedure ConnectRead;virtual;
procedure ConnectWrite;virtual;
procedure ConnectSystem;virtual;
procedure ConfigureFromContext;virtual;
property OnConnectionProgress: TRDTPProgressEvent read FProg write SetProgress;
function CreateExecutionThread: TRDTPExecutionThread; virtual;
property ExecutionThread: TRDTPExecutionThread read GetExecutionThread;
property Context: string read GetContext write SetContext;
procedure CheckContextSet;
function GetConfigFile: string;virtual;
property ConfigFromLocalFile: boolean read FConfigFromLocalFile write SetConfigFromLocalFile;
property LastUsed: cardinal read FLastUsed write FLastUsed;
function IsExpired: boolean;
end;
TRDTPExecutionThread = class(TProcessorThread)
private
FDSResult: TSERowSet;
function GetMod: TAbstractRDTPDAtaModule;
procedure SetMod(const Value: TAbstractRDTPDAtaModule);
function GEtDSResult: TSERowSet;
protected
FDS: TSERowset;
FQuery: string;
FMod: TAbstractRDTPDAtaModule;
procedure DoExecute; override;
public
destructor Destroy;override;
property DSResult: TSERowSet read GEtDSResult;
property Module:TAbstractRDTPDAtaModule read GetMod write SetMod;
procedure ExecuteSystem(sQuery: string; bWAitWithKeepAlive: boolean = true);
end;
var
datapool: TDataPool = nil;
implementation
uses DatabaseDictionary, ExceptionsX, beeper;
{ TAbstractRDTPDataModule }
procedure TAbstractRDTPDataModule.BeginTransaction;
begin
BeginTransactionOn(TSQLChannel.sqlWrite);
end;
procedure TAbstractRDTPDataModule.CheckContextSet;
begin
if FContext = '' then
raise EClassException.create('Server context not set in '+self.ClassName);
end;
procedure TAbstractRDTPDataModule.Commit;
begin
CommitOn(TSQLChannel.sqlWrite);
end;
procedure TAbstractRDTPDataModule.ConfigureFromContext;
begin
//no implementation needed
end;
procedure TAbstractRDTPDataModule.ConnectRead;
begin
// FChannelStats[SQL_CHANNEL_READ].Init;
//TODO -cunimplemented: unimplemented block
end;
procedure TAbstractRDTPDataModule.ConnectSystem;
begin
// FChannelStats[SQL_CHANNEL_SYSTEM].Init;
//TODO -cunimplemented: unimplemented block
end;
procedure TAbstractRDTPDataModule.ConnectWrite;
begin
// FChannelStats[SQL_CHANNEL_WRITE].Init;
//TODO -cunimplemented: unimplemented block
end;
constructor TAbstractRDTPDataModule.create;
begin
inherited;
FCreationtime := GetTicker;
FContext := '';
end;
constructor TAbstractRDTPDataModule.create2(sContext: string);
begin
CReate;
// beeper.beeparray([200,300],[50]);
FContext := sContext;
end;
function TAbstractRDTPDataModule.CreateExecutionThread: TRDTPExecutionThread;
begin
result := TPM.Needthread<TRDTPExecutionThread>(nil);
end;
procedure TAbstractRDTPDataModule.DataModuleCreate(Sender: TObject);
begin
//TODO -cunimplemented: unimplemented block
end;
destructor TAbstractRDTPDataModule.destroy;
begin
WaitForCommands;
// beeper.beeparray([300,200],[50]);
Fet.free;
inherited;
end;
procedure TAbstractRDTPDataModule.Execute(sQuery: string;
connection: TSQLConnection; ds: TCustomSQLDataset);
begin
CheckContextSet;
//TODO -cunimplemented: unimplemented block
end;
procedure TAbstractRDTPDataModule.ExecuteDirect(sQuery: string;
connection: TSQLConnection);
begin
CheckContextSet;
//TODO -cunimplemented: unimplemented block
end;
procedure TAbstractRDTPDataModule.ExecuteRead(sQuery: string;
out dataset: TSERowSet);
begin
raise ECritical.create('not implemented. implement ExecuteRead in '+self.classname);
end;
function TAbstractRDTPDataModule.ExecuteSystem(sQuery: string;
out dataset: TSERowSet): integer;
begin
raise ECritical.create('not implemented');
end;
function TAbstractRDTPDataModule.ExecuteSystem(sQuery: string): integer;
begin
raise ECritical.create('not implemented');
end;
function TAbstractRDTPDataModule.ExecuteWrite(sQuery: string): integer;
begin
WaitForCommands;
result := ExecuteWriteRaw(sQuery);
end;
function TAbstractRDTPDataModule.ExecuteWrite(sQuery: string;
out dataset: TSERowSet): integer;
begin
result := 0;
WaitForCommands;
//
end;
procedure TAbstractRDTPDataModule.ExecuteWriteBehind(sQuery: string);
var
c: Tcmd_DBWriteBehind;
begin
ExecuteWrite(sQuery);
// WaitForCommands;
//
// c := Tcmd_DBWriteBehind.create;
// c.dm := self;
// c.Query := sQuery;
// c.Start;
// cmd := c;
end;
function TAbstractRDTPDataModule.ExecuteWriteRaw(sQuery: string): integer;
begin
raise Exception.create('unimplemented');
//TODO -cunimplemented: unimplemented block
end;
function TAbstractRDTPDataModule.GetContext: string;
begin
result := FContext;
end;
function TAbstractRDTPDataModule.GetExecutionThread: TRDTPExecutionThread;
begin
if Fet = nil then begin
Fet := CreateExecutionThread;
Fet.Module := self;
end;
result := Fet;
end;
function TAbstractRDTPDataModule.IsExpired: boolean;
begin
result := (GetTimeSince(GetTicker, FLastUsed) > DB_CONNECTION_TIMEOUT) or (GEtTimeSince(FCreationTime) > MAX_DM_AGE);
end;
function TAbstractRDTPDataModule.ReadQuery(sQuery: string): IHolder<TSERowset>;
var
se: TSERowSet;
begin
result := nil;
ExecuteRead(sQuery, se);
if se <> nil then begin
result := THolder<TSERowSet>.create();
result.o := se;
end;
end;
procedure TAbstractRDTPDataModule.Rollback;
begin
RollbackOn(TSQLChannel.sqlWrite);
end;
procedure TAbstractRDTPDataModule.SetConfigFromLocalFile(const Value: boolean);
begin
FConfigFromLocalFile := Value;
end;
procedure TAbstractRDTPDataModule.SetContext(value: string);
begin
FContext := value;
ConfigureFromContext;
end;
procedure TAbstractRDTPDataModule.SetProgress(const Value: TRDTPProgressEvent);
begin
FProg := Value;
end;
function TAbstractRDTPDataModule.SystemReadQuery(
sQuery: string): IHolder<TSERowset>;
var
se: TSERowSet;
begin
result := nil;
ExecuteSystem(sQuery, se);
if se <> nil then begin
result := THolder<TSERowSet>.create;
result.o := se;
end;
end;
procedure TAbstractRDTPDataModule.WaitForCommands;
begin
if assigned(cmd) then begin
cmd.waitfor;
cmd.free;
end;
cmd := nil;
end;
{ TDataPool }
constructor TDataPool.create;
begin
inherited;
FData := TList.create;
FRDTPDataModuleClass := TAbstractRDTPDataModule;
factory :=classfactory.TClassClassFactory.create();
end;
destructor TDataPool.destroy;
begin
FACTORY.Free;
factory := nil;
FreeListContents(FData);
Fdata.free;
inherited;
end;
function TDataPool.GetActiveCount: integer;
begin
Lock;
try
result := FActiveCount;
finally
Unlock;
end;
end;
function TAbstractRDTPDataModule.GetChannelStats(ch: TSQLChannel): PSQLChannelStats;
begin
result := @FChannelStats[ord(ch)];
end;
function TDataPool.GetClassToCreate: TRDTPDataMOduleClass;
begin
Lock;
try
result := FRDTPDAtaModuleClass;
finally
Unlock;
end;
end;
function TDataPool.GetCount: integer;
begin
Lock;
try
result := FData.count;
finally
Unlock;
end;
end;
function TDataPool.GetData(idx: integer): TAbstractRDTPDAtaModule;
begin
Lock;
try
result := TAbstractRDTPDAtaModule(FData[idx]);
finally
Unlock;
end;
end;
{$IFDEF ALLOW_CONTROLS}
procedure TDataPool.SyncToListView(lv: TListView);
var
t: integer;
iCount: integer;
begin
Lock;
try
iCount := count;
with lv do begin
while items.count > iCount do begin
items.Delete(items.count-1);
end;
while items.count < iCount do begin
items.Add;
end;
for t:= 0 to iCount-1 do begin
items[t].caption := data[t].Context;
end;
end;
finally
Unlock;
end;
end;
{$ENDIF}
function TDataPool.NeedData(cClass: TDataClass; sContext: string): TAbstractRDTPDataModule;
var
t: integer;
d: TAbstractRDTPDataModule;
begin
// if sContext = '' then
// raise EClassException.create('Blank context in NeedData');
result := nil;
Lock;
try
for t:= FData.count-1 downto 0 do begin
d := FData[t];
if d = nil then
raise EClassException.create('nil data in Data Array found in TDataPool.NeedData');
if d.IsExpired then begin
d.free;
FData.delete(t);
d := nil;
continue;
end;
if not assigned(result) then begin
if (d.Context = sContext) and (d.classtype = cClass) then begin
result := d;
FData.delete(t);
break;
end;
end;
end;
if result = nil then begin
d := TAbstractRDTPDataModule(factory.CreateClass(TClass(cClass)));
d.Context := sContext;
result := d;
end;
result.LastUsed := GetTicker;
inc(FActiveCount);
finally
Unlock;
end;
end;
procedure TDataPool.NoNeedData(dm: TAbstractRDTPDataModule);
begin
if dm= nil then
exit;
dm.ResetForPool;
{$IFDEF POOL_DMS}
Lock;
try
dm.LastUsed := GetTicker;
FData.add(dm);
dec(FActiveCount);
finally
Unlock;
end;
{$ELSE}
dm.free;
{$ENDIF}
end;
procedure TDataPool.SetClassToCreate(const Value: TRDTPDataMOduleClass);
begin
Lock;
try
FRDTPDAtaModuleClass := value;
finally
Unlock;
end;
end;
destructor TRDTPExecutionThread.Destroy;
begin
FDS.free;
FDS := nil;
inherited;
end;
procedure TRDTPExecutionThread.DoExecute;
begin
inherited;
try
//make sure if dataset was not taken in last run
//that it is freed
if assigned(Fds) then begin
FDs.free;
FDs := nil;
end;
//execute
FMod.ExecuteSystem(FQuery);
except
on E: Exception do begin
Error := e.message;
end;
end;
end;
procedure TRDTPExecutionThread.ExecuteSystem(sQuery: string; bWaitWithKeepAlive: boolean = true);
var
t: integer;
begin
//TODO 5: TRDTPExecutionThread might want to support queries other than system queries, including queries that return datasets
FQuery := sQuery;
while not ready do sleep(1);
Start;
if bWaitWithKeepAlive then begin
t := 0;
while not Ready do begin
//send back keep-alive every 4 seconds
inc(t);
t := t mod 400;
if (t=0) then begin
if assigned(Module.OnConnectionProgress) then begin
try
self.Module.OnConnectionProgress('','',-99,-99);
except
end;
end;
end;
sleep(10);
end;
end;
end;
function TRDTPExecutionThread.GEtDSResult: TSERowSet;
begin
Lock;
try
result := FDSREsult;
FDSResult := nil; //<<-- called takes ownership
finally
Unlock;
end;
end;
function TRDTPExecutionThread.GetMod: TabstractRDTPDataModule;
begin
Lock;
try
result := FMod;
finally
Unlock;
end;
end;
procedure TRDTPExecutionThread.SetMod(const Value: TabstractRDTPDataModule);
begin
Lock;
try
FMod := value;
finally
Unlock;
end;
end;
function TAbstractRDTPDataModule.GetConfigFile: string;
begin
if copy(context, 1,1) = '[' then
result := Context
else
result := LoadStringFromFile(slash(extractfilepath(DLLName))+'MWContext_'+Context+'.ini')
end;
{ TDataPoolViewer }
procedure oinit;
begin
// raise Exception.create('unimplemented');
//TODO -cunimplemented: unimplemented block
end;
procedure ofinal;
begin
// raise Exception.create('unimplemented');
//TODO -cunimplemented: unimplemented block
end;
{ Tcmd_DBWriteBehind }
procedure Tcmd_DBWriteBehind.DoExecute;
begin
inherited;
dm.ExecuteWriteRaw(query);
end;
procedure Tcmd_DBWriteBehind.InitExpense;
begin
inherited;
CPuExpense := 0.0;
end;
{ TSQLChannelStats }
procedure TSQLChannelStats.Init;
begin
Writequeries := 0;
ReadQueries := 0;
InTransaction := false;
end;
initialization
init.RegisterProcs('AbstractRDTPDataModule', oinit, ofinal);
datapool := TDataPool.create;
finalization
if assigned(datapool) then
datapool.free;
end.