-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDbcEngine.pas
1582 lines (1389 loc) · 33.7 KB
/
DbcEngine.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ async dbc
Copyright (C) 2018-2020 Red_prig
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
}
unit DbcEngine;
{$mode objfpc}{$H+}
interface
uses
SysUtils,
Classes,
gset,
RWLock,
ZSysUtils,
ZDbcIntfs,
ZAbstractConnection,
ZDbcConnection,
ZDbcLogging,
ZCompatibility,
UAsyncResultSet,
TaskManager,
UAsyncQueue,
ULog,
rutils;
type
dbc_event=class(app_logic)
end;
TZResultSet =UAsyncResultSet.TZAsyncResultSet;
TZResultSetMetadata=UAsyncResultSet.TZAsyncResultSetMetadata;
TZSQLType =ZDbcIntfs.TZSQLType;
TZResultSetType =ZDbcIntfs.TZResultSetType;
TZURL =ZDbcIntfs.TZURL;
TOnDbcProc=UAsyncQueue.TOnParent;
TOnParent=UAsyncQueue.TOnParent;
PQNode=^TQNode;
TQNode=object(UAsyncQueue.TQFrom2Node)
public
FOnDbcProc:TOnDbcProc;
FErrorCode:Integer;
FMessage:RawByteString;
end;
TDbcConnection=class;
PZPreparedCache=^TZPreparedCache;
TZPreparedCache=record
FKey:RawByteString;
FTag:SizeInt;
stmt:IZPreparedStatement;
end;
TZPreparedCacheCompare=class
class function c(var a,b:TZPreparedCache):boolean; static;
end;
_TZPreparedCacheSet=specialize TSet<TZPreparedCache,TZPreparedCacheCompare>;
TZPreparedCacheSet=class(_TZPreparedCacheSet)
Function PrepareStatement(FConnection:IZConnection;Const FSQL:RawByteString):IZPreparedStatement;
Function GetStatement(Const FSQL:RawByteString):IZPreparedStatement;
Procedure DelStatement(Const FSQL:RawByteString);
end;
TDbcThread=class(TCustomAsyncThread)
private
FDbcCon:TDbcConnection;
FConnection:IZConnection;
FPreparedCache:TZPreparedCacheSet;
FPingSupport:Boolean;
protected
FThreadID:TThreadID;
Procedure DBOpen;
Function DBPing:Boolean;
Procedure DBClose;
Procedure InherBegin; override;
Procedure InherUpdate; override;
Procedure InherEnd; override;
Function GetPreparedCache:TZPreparedCacheSet;
Function PrepareStatement(Const FSQL:RawByteString):IZPreparedStatement;
public
TransactIsolation:TZTransactIsolationLevel;
Constructor Create(F:TDbcConnection);
end;
TDbcConnection=class(TBaseTask)
private
FThread:TDbcThread;
FNode:TQNode;
FZURL:RawByteString;
protected
Procedure SetZURL(Const _ZURL:RawByteString);
Procedure FinishOpen;
Procedure FinishClose;
Procedure FinishError;
function SendCmd(Node:PQNode):Boolean;
Procedure SetTransactIsolation(V:TZTransactIsolationLevel);
function GetTransactIsolation:TZTransactIsolationLevel;
published
property ZURL:RawByteString read FZURL write SetZURL;
property TransactIsolation:TZTransactIsolationLevel read GetTransactIsolation write SetTransactIsolation;
property ErrorCode:Integer read FNode.FErrorCode;
property Message:RawByteString read FNode.FMessage;
public
Function ZConnection:IZConnection;
Function GetPreparedCache:TZPreparedCacheSet;
Constructor Create; override;
Procedure Cleanup;override;
Procedure Open(Const _ZURL:RawByteString);
Procedure Close; inline;
Function Start:SizeInt; override;
Function Pause:SizeInt; override;
Function Stop:SizeInt; override;
function Send(Node:PQNode):Boolean;
end;
TQueryHandle=class
protected
FDbcCon:TDbcConnection;
Node:TQNode;
FState:Boolean;
public
Var
OnFinish:TFuncEvent;
protected
Procedure SetConnection(C:TDbcConnection);
Procedure BreakSend;
Procedure Finish;
Procedure SetProc(P:TOnDbcProc);
Function GetMessage:RawByteString;
Function ZConnection:IZConnection;
Function CreateStatement:IZStatement;
Function PrepareStatement(Const FSQL:RawByteString):IZPreparedStatement;
published
property DbcConnection:TDbcConnection read FDbcCon write SetConnection;
property OnDbcProc:TOnDbcProc read Node.Parent write SetProc;
property State:Boolean read FState;
property ErrorCode:Integer read Node.FErrorCode;
property Message:RawByteString read GetMessage;
public
Procedure Clear;
Constructor Create;
Destructor Destroy; override;
Procedure Send;
Procedure Cancel;
Function isCancel:Boolean; inline;
end;
TDbcQueryTask=class(TBaseTask)
protected
FHandle:TQueryHandle;
Function ZConnection:IZConnection;
Function CreateStatement:IZStatement;
Function PrepareStatement(Const FSQL:RawByteString):IZPreparedStatement;
function OnFinish:SizeInt;
public
property Handle:TQueryHandle read FHandle;
Constructor Create; override;
Procedure Cleanup; override;
Function Start:SizeInt; override;
Function Pause:SizeInt; override;
Function Stop:SizeInt; override;
end;
TDbcStatementTask=class(TDbcQueryTask)
protected
FSQL:RawByteString;
FRZ:TZResultSet;
FCount:Integer;
Procedure OnQuery;
Procedure OnUpdate;
Procedure OnExe;
function GetResultSet:TZResultSet;
Procedure SetResultSet(R:TZResultSet);
public
Procedure Cleanup; override;
Procedure ExecuteQuery(const SQL:RawByteString);
Procedure ExecuteUpdate(const SQL:RawByteString);
Procedure Execute(const SQL:RawByteString);
function UpdateCount:Integer;
function MoreResults:Boolean;
property ResultSet:TZResultSet read GetResultSet write SetResultSet;
end;
TDbcBatchTask=class(TDbcQueryTask)
protected
FBatch:AString;
FCount:Integer;
Procedure OnBatch;
public
Procedure AddSQL(const SQL:RawByteString);
Procedure Clear;
function GetCount:Integer;
end;
Function TranslateFBError(S:RawByteString):RawByteString;
function insert_into_sql(Const TableName,Options:RawByteString;Const Defs:TFields):RawByteString;
function insert_or_ignore_sql(Const TableName,Options:RawByteString;Const Defs:TFields):RawByteString;
function replace_into_sql(Const TableName,Options:RawByteString;Const Defs:TFields):RawByteString;
function select_sql(Const TableName,Opt1,Opt2:RawByteString;Const Defs:TFields):RawByteString;
function update_sql(Const TableName,Opt1,Opt2:RawByteString;Const Defs:TFields):RawByteString;
function delete_sql(Const TableName,Opt1,Opt2:RawByteString):RawByteString;
function where_sql(Const Defs:TFields):RawByteString;
function create_table_sql(Const TableName,Options:RawByteString;Const Defs:TFields):RawByteString;
function create_index_sql(Const TableName,Options,IndexName,Colums:RawByteString):RawByteString;
function create_unique_index_sql(Const TableName,Options,IndexName,Colums:RawByteString):RawByteString;
function SpaceToNull(Const S:RawByteString):RawByteString; inline;
function SpaceToNullQuoted(Const S:RawByteString):RawByteString; inline;
function QuotedStr(Const S:RawByteString):RawByteString; inline;
implementation
Var
GlobalGetConnLock:TRWLock;
class function TZPreparedCacheCompare.c(var a,b:TZPreparedCache):boolean;
begin
Result:=CompareStr(a.FKey,b.FKey)<0;
end;
type
TLogListen = class(TInterfacedObject,IZLoggingListener)
procedure LogEvent(Event:TZLoggingEvent);
end;
Var
LogListen:TLogListen;
procedure TLogListen.LogEvent(Event:TZLoggingEvent);
function getCat:String; inline;
begin
case Event.Category of
lcConnect: Result := 'Connect';
lcDisconnect: Result := 'Disconnect';
lcTransaction: Result := 'Transaction';
lcExecute: Result := 'Execute';
lcPrepStmt: Result := 'Prepare';
lcBindPrepStmt: Result := 'Bind prepared';
lcExecPrepStmt: Result := 'Execute prepared';
lcUnprepStmt: Result := 'Unprepare prepared';
else
Result := 'Other';
end;
end;
begin
if Assigned(Event) then
begin
if Event.Error='' then
begin
//if Info then
Log(dbc_event,Event.ErrorCodeOrAffectedRows,[getCat,', ',Event.Protocol,', ',Event.Message]);
end else
begin
//if Error then
Log(dbc_event,Event.ErrorCodeOrAffectedRows,[getCat,', ',Event.Protocol,', ',Event.Message,', ',Event.Error]);
end;;
end;
end;
Constructor TDbcThread.Create(F:TDbcConnection);
begin
FPingSupport:=false;
TransactIsolation:=tiReadCommitted;
FDbcCon:=F;
inherited Create;
end;
Constructor TDbcConnection.Create;
begin
inherited;
FThread:=TDbcThread.Create(Self);
if FThread.State then
begin
inherited Pause;
end;
end;
Procedure TDbcConnection.Cleanup;
begin
FreeAndNil(FThread);
inherited;
end;
Procedure TDbcConnection.SetZURL(Const _ZURL:RawByteString);
begin
Case State of
T_NEW,
T_RUN,
T_FIN:FZURL:=_ZURL;
end;
end;
Procedure TDbcConnection.SetTransactIsolation(V:TZTransactIsolationLevel);
begin
if Assigned(FThread) then FThread.TransactIsolation:=V;
end;
function TDbcConnection.GetTransactIsolation:TZTransactIsolationLevel;
begin
Result:=tiNone;
if Assigned(FThread) then Result:=FThread.TransactIsolation;
end;
Procedure TDbcConnection.FinishOpen;
begin
if FNode.FErrorCode=0 then
begin
inherited Start;
Notify.Call(T_SUC);
end else
begin
inherited Stop;
Notify.Call(T_ERR);
end;
end;
Procedure TDbcConnection.FinishClose;
begin
inherited Stop;
end;
Procedure TDbcConnection.FinishError;
begin
Notify.Call(T_ERR);
inherited Stop;
end;
Procedure TDbcConnection.Open(Const _ZURL:RawByteString);
begin
ZURL:=_ZURL;
Start;
end;
Procedure TDbcConnection.Close; inline;
begin
Stop;
end;
Function TDbcConnection.Start:SizeInt;
begin
Result:=R_ERR;
if State<>T_PAU then
if Trim(ZURL)<>'' then
begin
Acquire;
Result:=inherited Pause;
Release;
end;
if Result=R_SUC then
begin
FNode:=Default(TQNode);
FNode.Parent:=@FinishOpen;
FNode.FErrorCode:=0;
FNode.FOnDbcProc:=@FThread.DBOpen;
SendCmd(@FNode);
end;
end;
Function TDbcConnection.Pause:SizeInt;
begin
Result:=R_ERR;
end;
Function TDbcConnection.Stop:SizeInt;
begin
Result:=R_ERR;
Acquire;
if State<>T_PAU then Result:=inherited Pause;
if Result=R_SUC then
begin
FNode:=Default(TQNode);
FNode.Parent:=@FinishClose;
FNode.FErrorCode:=0;
FNode.FOnDbcProc:=@FThread.DBClose;
SendCmd(@FNode);
end;
if Release then Free;
end;
function TDbcConnection.Send(Node:PQNode):Boolean;
begin
Result:=FThread.Send(Node);
end;
function TDbcConnection.SendCmd(Node:PQNode):Boolean;
begin
Result:=FThread.SendCmd(Node);
end;
function TDbcConnection.ZConnection:IZConnection;
begin
Result:=nil;
if Assigned(FThread) then
if (FThread.FThreadID=system.ThreadID) then
begin
Result:=FThread.FConnection;
end;
end;
Function TDbcConnection.GetPreparedCache:TZPreparedCacheSet;
begin
Result:=nil;
if Assigned(FThread) then
if (FThread.FThreadID=system.ThreadID) then
begin
Result:=FThread.GetPreparedCache;
end;
end;
Procedure TDbcThread.DBOpen;
Var
F1,F2:Pointer;
begin
if (FThreadID=system.ThreadID) then
if Assigned(FDbcCon) then
begin
DBClose;
rwlock_wrlock(GlobalGetConnLock);
FConnection:=DriverManager.GetConnection(FDbcCon.FZURL);
rwlock_unlock(GlobalGetConnLock);
FConnection.Open;
FConnection.SetAutoCommit(True);
FConnection.SetTransactionIsolation(TransactIsolation);
F1:=TMethod(@((FConnection as TZAbstractDbcConnection).PingServer)).Code;
F2:=Pointer(@TZAbstractDbcConnection.PingServer);
FPingSupport:=F1<>F2;
end;
end;
Function TDbcThread.DBPing:Boolean;
begin
Result:=true;
if (FThreadID<>system.ThreadID) or (not FPingSupport) then Exit;
try
if Assigned(FConnection) then
begin
if FConnection.PingServer<>0 then
begin
Log(app_logic,1,['DbcThread.PingError']);
Result:=False;
end;
end;
except
on E:Exception do
begin
DumpExceptionCallStack(E);
Result:=False;
end;
end;
end;
Procedure TDbcThread.DBClose;
begin
if (FThreadID<>system.ThreadID) then Exit;
try
//not neeed iteration finalize
FreeAndNil(FPreparedCache);
except
on E:Exception do
begin
DumpExceptionCallStack(E);
end;
end;
try
FConnection:=nil;
except
on E:Exception do
begin
DumpExceptionCallStack(E);
FConnection:=nil;
end;
end;
end;
Procedure TDbcThread.InherBegin;
begin
FThreadID:=System.ThreadID;
end;
Procedure TDbcThread.InherUpdate;
Var
Node:PQNode;
rc:PtrUInt;
DbcProc:TOnDbcProc;
begin
Node:=nil;
rc:=0;
rc:=FCmdQueue.Count;
if rc>100 then rc:=100;
While FCmdQueue.Recv(Node) do
begin
try
Node^.FErrorCode:=0;
DbcProc:=Node^.FOnDbcProc;
if Assigned(DbcProc) then DbcProc();
except
on E:Exception do
begin
DumpExceptionCallStack(E);
if E.InheritsFrom(EZSQLThrowable) then
begin
Node^.FErrorCode:=EZSQLThrowable(E).ErrorCode;
end;
if Node^.FErrorCode=0 then Node^.FErrorCode:=-1;
Node^.FMessage:=E.Message;
end;
end;
SendFrom(Node);
if rc=0 then Break;
Dec(rc);
if rc=0 then Break;
if FState then Exit;
end;
rc:=FQueue.Count;
if rc>100 then rc:=100;
While FQueue.Recv(Node) do
begin
try
Node^.FErrorCode:=0;
if (not Assigned(FConnection)) then
begin
Node^.FErrorCode:=4;
Node^.FMessage:='Нет соединения с сервером!';
end else
if not DBPing then
begin
Node^.FErrorCode:=5;
Node^.FMessage:='Потеряно соединение с сервером!';
DBClose;
//asy
SyncFunc(@FDbcCon.FinishError);
end else
begin
DbcProc:=Node^.FOnDbcProc;
if Assigned(DbcProc) then
begin
DbcProc();
end;
end;
except
on E:Exception do
begin
DumpExceptionCallStack(E);
if E.InheritsFrom(EZSQLThrowable) then
begin
Node^.FErrorCode:=EZSQLThrowable(E).ErrorCode;
end;
if Node^.FErrorCode=0 then
begin
Node^.FErrorCode:=-1;
end;
Node^.FMessage:=E.Message;
end;
end;
SendFrom(Node);
if rc=0 then Break;
Dec(rc);
if rc=0 then Break;
if FState then Exit;
end;
if FQueue.IsEmpty and (not FState) then
begin
RTLeventWaitFor(FQueue.hEvent,RtlWaitTime);
if (not FState) and FPingSupport and FQueue.IsEmpty then
begin
if not DBPing then
begin
if Assigned(FDbcCon) then
begin
Node:=@FDbcCon.FNode;
Node^.FErrorCode:=5;
Node^.FMessage:='Потеряно соединение с сервером!';
end;
DBClose;
//asy
SyncFunc(@FDbcCon.FinishError);
end;
end;
end;
end;
Procedure TDbcThread.InherEnd;
Var
Node:PQNode;
begin
Node:=nil;
While FQueue.Recv(Node) do
begin
SendFrom(Node);
end;
DBClose;
end;
Function TZPreparedCacheSet.PrepareStatement(FConnection:IZConnection;Const FSQL:RawByteString):IZPreparedStatement;
Var
Z:TZPreparedCache;
FNode:TZPreparedCacheSet.PNode;
begin
Result:=nil;
Z.FKey:=Trim(FSQL);
Z.stmt:=nil;
FNode:=NFind(Z);
if Assigned(FNode) then
begin
Result:=FNode^.Data.stmt;
Result.ClearParameters;
end else
begin
Z.stmt:=FConnection.PrepareStatement(FSQL);
Insert(Z);
Result:=Z.stmt;
Result.SetResultSetConcurrency(rcReadOnly);
Result.SetFetchDirection(fdForward);
end;
end;
Function TZPreparedCacheSet.GetStatement(Const FSQL:RawByteString):IZPreparedStatement;
Var
Z:TZPreparedCache;
FNode:TZPreparedCacheSet.PNode;
begin
Result:=nil;
Z.FKey:=Trim(FSQL);
Z.stmt:=nil;
FNode:=NFind(Z);
if Assigned(FNode) then
begin
Result:=FNode^.Data.stmt;
end;
end;
Procedure TZPreparedCacheSet.DelStatement(Const FSQL:RawByteString);
Var
Z:TZPreparedCache;
begin
Z.FKey:=Trim(FSQL);
Z.stmt:=nil;
Delete(Z);
end;
Function TDbcThread.GetPreparedCache:TZPreparedCacheSet;
begin
Result:=nil;
if (FThreadID=system.ThreadID) then
begin
Result:=FPreparedCache;
end;
end;
Function TDbcThread.PrepareStatement(Const FSQL:RawByteString):IZPreparedStatement;
begin
Result:=nil;
if (FThreadID=system.ThreadID) then
if Assigned(FConnection) then
begin
if not Assigned(FPreparedCache) then
begin
FPreparedCache:=TZPreparedCacheSet.Create;
end;
Result:=FPreparedCache.PrepareStatement(FConnection,FSQL);
end;
end;
Procedure TQueryHandle.Clear;
begin
if FState then Exit;
OnFinish:=nil;
Node.FOnDbcProc:=nil;
Node.FErrorCode:=0;
Node.FMessage:='';
end;
Procedure TQueryHandle.SetConnection(C:TDbcConnection);
begin
if not State then
if FDbcCon<>C then
begin
if Assigned(FDbcCon) then
begin
FDbcCon.Free;
end;
FDbcCon:=C;
if Assigned(FDbcCon) then
begin
FDbcCon.Acquire;
end;
end;
end;
Constructor TQueryHandle.Create;
begin
Node:=Default(TQNode);
Node.Parent:=@Finish;
FState:=False;
end;
Destructor TQueryHandle.Destroy;
begin
Clear;
SetConnection(nil);
end;
Procedure TQueryHandle.BreakSend;
begin
FState:=True;
Node.FErrorCode:=0;
Node.Parent:=@Finish;
SendCurrQueue(@Node);
end;
Procedure TQueryHandle.Send;
begin
if not Assigned(Self) then
begin
Log(app_logic,1,['QueryHandle.Send,Assigned(Self):',Self]);
Exit;
end;
if FState then
begin
Log(app_logic,1,['QueryHandle.Send,FState:',FState]);
Exit;
end;
Node.Parent:=@Finish;
Node.FErrorCode:=0;
Node.FMessage:='';
if not Assigned(FDbcCon) then
begin
Log(app_logic,1,['QueryHandle.Send,Assigned(FDbcCon):',FDbcCon]);
Finish;
Exit;
end;
if FDbcCon.Send(@Node) then
begin
FState:=True;
end else
begin
Log(app_logic,1,['QueryHandle.SendRequest=false']);
BreakSend;
Exit;
end;
end;
Procedure TQueryHandle.Finish;
begin
FState:=False;
if not Assigned(OnFinish) then
begin
Log(app_logic,1,['QueryHandle.Send,Assigned(OnFinish):',TMethod(OnFinish).Code,TMethod(OnFinish).Data]);
Exit;
end;
Log(app_logic,0,['QueryHandle.Send,OnFinish:',TMethod(OnFinish).Code,TMethod(OnFinish).Data]);
OnFinish();
end;
Procedure TQueryHandle.Cancel;
begin
if FState then
begin
Node.FOnDbcProc:=nil;
end;
end;
Function TQueryHandle.isCancel:Boolean; inline;
begin
Result:=Node.FOnDbcProc=nil;
end;
Procedure TQueryHandle.SetProc(P:TOnDbcProc);
begin
if not FState then
begin
Node.FOnDbcProc:=P;
end;
end;
Function TQueryHandle.GetMessage:RawByteString;
begin
Result:='';
if FState then Exit;
Result:=Node.FMessage;
end;
Constructor TDbcQueryTask.Create;
begin
inherited;
FHandle:=TQueryHandle.Create;
end;
Procedure TDbcQueryTask.Cleanup;
begin
FHandle.Free;
inherited;
end;
function TDbcQueryTask.OnFinish:SizeInt;
begin
Result:=R_SUC;
if not FHandle.isCancel then
begin
if FHandle.ErrorCode=0 then
begin
Notify.Call(T_SUC);
end else
begin
Notify.Call(T_ERR);
end;
end;
Stop;
end;
function TDbcQueryTask.Start:SizeInt;
begin
Result:=R_ERR;
if not FHandle.State then Result:=inherited;
if Result=R_SUC then
begin
FHandle.OnFinish:=@OnFinish;
FHandle.Send;
end;
end;
function TDbcQueryTask.Pause:SizeInt;
begin
if FHandle.State then
begin
FHandle.OnFinish:=@Self.Pause;
FHandle.Cancel;
Result:=R_ASY;
end else
begin
Result:=inherited;
end;
end;
function TDbcQueryTask.Stop:SizeInt;
begin
if FHandle.State then
begin
FHandle.OnFinish:=@Self.Stop;
FHandle.Cancel;
Result:=R_ASY;
end else
begin
Result:=inherited;
end;
end;
///////
Function TQueryHandle.ZConnection:IZConnection;
begin
Result:=nil;
if Assigned(DbcConnection) then
begin
Result:=DbcConnection.ZConnection;
end;
end;
Function TQueryHandle.CreateStatement:IZStatement;
Var
Connection:IZConnection;
begin
Result:=nil;
Connection:=ZConnection;
if Assigned(Connection) then
begin
Result:=Connection.CreateStatement;
Result.SetResultSetConcurrency(rcReadOnly);
Result.SetFetchDirection(fdForward);
Result.SetResultSetType(rtForwardOnly);
end;
end;
Function TQueryHandle.PrepareStatement(Const FSQL:RawByteString):IZPreparedStatement;
begin
Result:=nil;
if Assigned(FDbcCon) then
if Assigned(FDbcCon.FThread) then
begin
Result:=FDbcCon.FThread.PrepareStatement(FSQL);
Result.SetResultSetType(rtForwardOnly);
end;
end;
////
Function TDbcQueryTask.ZConnection:IZConnection;
begin
Result:=nil;
if Assigned(FHandle) then
begin
Result:=FHandle.ZConnection;
end;
end;
Function TDbcQueryTask.CreateStatement:IZStatement;
begin
Result:=nil;
if Assigned(FHandle) then
begin
Result:=FHandle.CreateStatement;
end;
end;
Function TDbcQueryTask.PrepareStatement(Const FSQL:RawByteString):IZPreparedStatement;
begin
Result:=nil;
if Assigned(FHandle) then
Result:=FHandle.PrepareStatement(FSQL);
end;
//-----
Procedure TDbcStatementTask.OnQuery;
Var
stmt:IZStatement;
FR:IZResultSet;
FIndexPairList:TZIndexPairList;
begin
FreeAndNil(FRZ);
stmt:=CreateStatement;
FR:=stmt.ExecuteQuery(FSQL);
FRZ:=TZAsyncResultSet.Create(FR);
FIndexPairList:=_NewIndexPair(FRZ.ColumnsInfo.Count);
While TZAsyncResultSet(FRZ).Fetch(FR,FIndexPairList) and (not FHandle.isCancel) do;
FreeAndNil(FIndexPairList);
end;
Procedure TDbcStatementTask.OnUpdate;
Var
stmt:IZStatement;
begin
stmt:=CreateStatement;
FCount:=stmt.ExecuteUpdate(FSQL);
end;
Procedure TDbcStatementTask.OnExe;
Var
stmt:IZStatement;
R:Boolean;
begin
stmt:=CreateStatement;