-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDbcScript.pas
2828 lines (2550 loc) · 61.4 KB
/
DbcScript.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
{ Dbc script similar transact-sql
Copyright (C) 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 DbcScript;
{$mode objfpc}{$H+}
interface
uses
SysUtils,
dateutils,
Classes,
gset,
gFastStack,
FmtBCD,
ZSysUtils,
ZClasses,
ZDbcIntfs,
ZCompatibility,
ZEncoding,
ZVariant,
ZFastCode,
ZTokenizer,
ZGenericSqlToken,
UAsyncResultSet,
DbcScriptUtils,
DbcScriptExp,
DbcEngine;
type
TSQLCommandType=(
qcIf,
qcGoto,
qcBeginTran,
qcCommitTran,
qcCommitWork,
qcRollbackTran,
qcRollbackWork,
qcSetVar,
qcPrint,
qcStatment,
qcDeclareVar,
qcDeclareCursor,
qcDeleteCursor,
qcOpenCursor,
qcCloseCursor,
qcFetchNext,
qcFetchPrior,
qcFetchFirst,
qcFetchLast,
qcFetchAbs,
qcFetchRel
);
//PSQLCommand=^TSQLCommand;
TSQLCommand=record
VType:TSQLCommandType;
Value:RawByteString;
SID:SizeInt;
end;
TParams=array of RawByteString;
TSQLStatment=object
FParamsID,FValuesID:SizeInt;
FResultTp:TZResultSetType;
end;
TSQLExp=object
OpID:SizeInt;
Node:PExpNode;
end;
TSQLBatchCmd=object
FCount:SizeUint;
FCommands:array of TSQLCommand;
end;
TSQLVariables=class(TStringList)
protected
Procedure SetValue(Const FName:RawByteString;Const V:TZVariant);
function GetValue(Const FName:RawByteString):TZVariant;
function GetMutable(Const FName:RawByteString):PZVariant;
public
destructor Destroy; override;
procedure Clear; override;
function GetRawByteString(Const FName:RawByteString):RawByteString;
function GetAsBoolean (Const FName:RawByteString):Boolean;
function GetAsBytes (Const FName:RawByteString):TBytes;
function GetAsInteger (Const FName:RawByteString):Int64;
function GetAsUInteger(Const FName:RawByteString):UInt64;
function GetAsDouble (Const FName:RawByteString):Double;
function GetAsCurrency(Const FName:RawByteString):Currency;
function GetAsDateTime(Const FName:RawByteString):TDateTime;
Procedure SetRawByteString(Const FName,FValue:RawByteString);
Procedure SetAsDateTime(Const FName:RawByteString;FValue:TDateTime);
Procedure SetAsNull(Const FName:RawByteString);
property ZValues[const FName:RawByteString]:TZVariant read GetValue write SetValue;
end;
PZCursor=^TZCursor;
TZCursor=record
FName:RawByteString;
FTag:SizeInt;
FOpen:Boolean;
VInterface:IZInterface;
end;
TZCursorCompare=class
class function c(var a,b:TZCursor):boolean; static;
end;
_TZCursorSet=specialize TSet<TZCursor,TZCursorCompare>;
TZCursorSet=class(_TZCursorSet)
procedure DeclareStatement(Const Name:RawByteString;Tag:SizeInt;stmt:IZPreparedStatement);
Function GetCursor(Const Name:RawByteString):PZCursor;
Procedure DelCursor(Const Name:RawByteString);
end;
PSQLContext=^TSQLContext;
TSQLContext=object
protected
FCursors:TZCursorSet;
public
FGlobal:IZResultSet;
FVariables:TSQLVariables;
FConnection:IZConnection;
FGlobalCache:TZPreparedCacheSet;
Function GetCursor(Const Name:RawByteString):PZCursor;
Procedure DelCursor(Const Name:RawByteString);
Procedure ClearCursors;
Procedure DeclareStatement(Const Name,FSQL:RawByteString;Tag:SizeInt;ResultTp:TZResultSetType);
procedure SetParams2stmt(Const FParams:TParams;Fstmt:IZPreparedStatement);
procedure SetValues2ctx(Const FValues:TParams;R:IZResultSet;withi:Longint=0);
function GetStatment(Const FValues:TParams):RawByteString;
Function PrepareStatement(Const FSQL:RawByteString):IZPreparedStatement;
Procedure Clear;
end;
TOnPrintCb=Procedure(Const S:RawByteString);
PSQLScript=^TSQLScript;
TSQLScriptIterator=object
private
Script:PSQLScript;
Context:PSQLContext;
b,c,i:Longint;
FCmd:TSQLCommand;
FTRANCOUNT:SizeUint;
FSaveAutoCommit:Boolean;
Procedure OpenCursor;
Procedure CloseCursor;
Procedure FetchCursor;
Procedure FetchCursorNum;
Procedure DeclareCursor;
Procedure ExecuteCmd;
procedure BeginTran;
procedure CommitTran;
procedure RollbackTran;
procedure CommitWork;
procedure RollbackWork;
procedure SetVar;
function GetQuery(VSID:Int64):IZResultSet;
procedure PrintExp;
procedure GotoId;
procedure OnIf;
public
FOnPrintCb:TOnPrintCb;
function Next:Boolean;
procedure Close(isCommit:Boolean);
end;
TGotoBackward=array of SizeInt;
TSQLScript=object
private
FStatmentList:array of TSQLStatment;
FBatchList:array of TSQLBatchCmd;
FParamsList:array of TParams;
FExps:array of TSQLExp;
function TryGetParams(i:SizeInt;var R:TParams):Boolean;
function TryGetStatment(i:SizeInt;var R:TSQLStatment):Boolean;
function AddParams(Const _Params:TParams):SizeInt;
function AddStatment(Const FParams,FValues:TParams;ResultTp:TZResultSetType):SizeInt;
procedure AddBath(LastCount:SizeUint);
procedure TrimBath;
procedure AddCmd(Const _Value:RawByteString;_Type:TSQLCommandType;_SID:SizeInt);
function GetLastCmdId:Sizeint;
function TrySetCmdOp(i,o:SizeInt):Boolean;
function TryGoto(b,i:SizeInt):Boolean;
function AddExp(OpID:SizeInt;Node:PExpNode):SizeInt;
function TryGetExp(i:SizeInt;var R:TSQLExp):Boolean;
function TrySetExpOp(i,o:SizeInt):Boolean;
function TryGetExpOp(i:SizeInt;var R:SizeInt):Boolean;
procedure SetGotoIds(id:SizeInt;Back:TGotoBackward);
public
Procedure Parse(Stream:TStream);
function Excecute(var Context:TSQLContext):TSQLScriptIterator;
end;
TDbcStatementScript=class(TDbcQueryTask)
protected
FRZ:TZResultSet;
FParams:TSQLVariables;
FScript:TSQLScript;
Procedure OnQuery;
function GetResultSet:TZResultSet;
Procedure SetResultSet(R:TZResultSet);
function GetParams:TSQLVariables;
Procedure SetParams(R:TSQLVariables);
public
Constructor Create; override;
Procedure Cleanup; override;
Procedure SetSctipt(Const FNew:TSQLScript);
Procedure ExecuteScript;
Procedure ExecuteScript(Stream:TStream);
property ResultSet:TZResultSet read GetResultSet write SetResultSet;
property Params:TSQLVariables read GetParams write SetParams;
end;
Const
vtCursor=vtInterface;
implementation
class function TZCursorCompare.c(var a,b:TZCursor):boolean;
begin
Result:=CompareStr(a.FName,b.FName)<0;
end;
procedure TZCursorSet.DeclareStatement(Const Name:RawByteString;Tag:SizeInt;stmt:IZPreparedStatement);
Var
Z:TZCursor;
FNode:TZCursorSet.PNode;
begin
Z.FName:=Name;
FNode:=NFind(Z);
if Assigned(FNode) then
begin
FNode^.Data.FTag :=Tag;
FNode^.Data.FOpen :=false;
FNode^.Data.VInterface:=stmt;
end else
begin
Z.FTag :=Tag;
Z.FOpen :=false;
Z.VInterface:=stmt;
Insert(Z);
end;
end;
Function TZCursorSet.GetCursor(Const Name:RawByteString):PZCursor;
Var
Z:TZCursor;
FNode:TZCursorSet.PNode;
begin
Result:=nil;
Z.FName:=Name;
FNode:=NFind(Z);
if Assigned(FNode) then
begin
Result:=@FNode^.Data;
end;
end;
Procedure TZCursorSet.DelCursor(Const Name:RawByteString);
Var
Z:TZCursor;
begin
Z.FName:=Name;
Delete(Z);
end;
Procedure DoPrint(Const S:RawByteString);
begin
//Writeln(S);
end;
Procedure TDbcStatementScript.OnQuery;
Var
FIndexPairList:TZIndexPairList;
FContext:TSQLContext;
FGlobal:IZResultSet;
I:TSQLScriptIterator;
begin
FContext:=Default(TSQLContext);
FContext.FVariables:=FParams;
FContext.FConnection:=ZConnection;
if Assigned(FHandle) then
if Assigned(FHandle.DbcConnection) then
FContext.FGlobalCache:=FHandle.DbcConnection.GetPreparedCache;
I:=FScript.Excecute(FContext);
I.FOnPrintCb:=@DoPrint;
While (I.Next) do
begin
if FHandle.isCancel then
begin
FContext.FGlobal:=nil;
I.Close(false);
Break;
end;
end;
I.Close(true);
FGlobal:=FContext.FGlobal;
FContext.ClearCursors;
FreeAndNil(FRZ);
if Assigned(FGlobal) then
begin
FRZ:=TZAsyncResultSet.Create(FGlobal);
FIndexPairList:=_NewIndexPair(FRZ.ColumnsInfo.Count);
While TZAsyncResultSet(FRZ).Fetch(FGlobal,FIndexPairList) and (not FHandle.isCancel) do;
FreeAndNil(FIndexPairList);
end;
end;
function TDbcStatementScript.GetResultSet:TZResultSet;
begin
Result:=nil;
if FHandle.State then Exit;
Result:=FRZ;
end;
Procedure TDbcStatementScript.SetResultSet(R:TZResultSet);
begin
if FHandle.State then Exit;
FRZ:=R;
end;
function TDbcStatementScript.GetParams:TSQLVariables;
begin
Result:=nil;
if FHandle.State then Exit;
Result:=FParams;
end;
Procedure TDbcStatementScript.SetParams(R:TSQLVariables);
begin
if FHandle.State then Exit;
FParams:=R;
end;
Constructor TDbcStatementScript.Create;
begin
inherited;
FParams:=TSQLVariables.Create;
end;
Procedure TDbcStatementScript.Cleanup;
begin
FreeAndNil(FParams);
FreeAndNil(FRZ);
inherited;
end;
Procedure TDbcStatementScript.SetSctipt(Const FNew:TSQLScript);
begin
if FHandle.State then Exit;
FScript:=FNew;
end;
Procedure TDbcStatementScript.ExecuteScript(Stream:TStream);
begin
if FHandle.State then Exit;
FScript.Parse(Stream);
FHandle.OnDbcProc:=@OnQuery;
end;
Procedure TDbcStatementScript.ExecuteScript;
begin
FHandle.OnDbcProc:=@OnQuery;
end;
///
Procedure TSQLVariables.SetValue(Const FName:RawByteString;Const V:TZVariant);
Var
i:Longint;
P:PZVariant;
begin
if (Self=nil) then Exit;
i:=IndexOf(FName);
if (i=-1) then
begin
P:=AllocMem(SizeOf(TZVariant));
P^:=V;
AddObject(FName,TObject(P));
end else
begin
P:=Pointer(Objects[i]);
P^:=V;
end;
end;
function TSQLVariables.GetValue(Const FName:RawByteString):TZVariant;
Var
i:Longint;
P:PZVariant;
begin
Result:=Default(TZVariant);
if (Self=nil) then Exit;
i:=IndexOf(FName);
if (i=-1) then Exit;
P:=Pointer(Objects[i]);
Result:=P^;
end;
function TSQLVariables.GetMutable(Const FName:RawByteString):PZVariant;
Var
i:Longint;
begin
Result:=nil;
if (Self=nil) then Exit;
i:=IndexOf(FName);
if (i=-1) then Exit;
Result:=Pointer(Objects[i]);
end;
////
function TSQLVariables.GetRawByteString(Const FName:RawByteString):RawByteString;
var
V:TZVariant;
begin
V:=GetValue(FName);
Result:=_GetAsRawByteString(V);
end;
function TSQLVariables.GetAsBoolean(Const FName:RawByteString):Boolean;
var
V:TZVariant;
begin
V:=GetValue(FName);
Result:=_GetAsBoolean(V);
end;
function TSQLVariables.GetAsBytes(Const FName:RawByteString):TBytes;
var
V:TZVariant;
begin
V:=GetValue(FName);
Result:=_GetAsBytes(V);
end;
function TSQLVariables.GetAsInteger(Const FName:RawByteString):Int64;
var
V:TZVariant;
begin
V:=GetValue(FName);
Result:=_GetAsInteger(V);
end;
function TSQLVariables.GetAsUInteger(Const FName:RawByteString):UInt64;
var
V:TZVariant;
begin
V:=GetValue(FName);
Result:=_GetAsUInteger(V);
end;
function TSQLVariables.GetAsDouble(Const FName:RawByteString):Double;
var
V:TZVariant;
begin
V:=GetValue(FName);
Result:=_GetAsDouble(V);
end;
function TSQLVariables.GetAsCurrency(Const FName:RawByteString):Currency;
var
V:TZVariant;
begin
V:=GetValue(FName);
Result:=_GetAsCurrency(V);
end;
function TSQLVariables.GetAsDateTime(Const FName:RawByteString):TDateTime;
var
V:TZVariant;
begin
V:=GetValue(FName);
Result:=_GetAsDateTime(V);
end;
//
Procedure TSQLVariables.SetRawByteString(Const FName,FValue:RawByteString);
begin
SetValue(FName,EncodeRawByteString(FValue));
end;
Procedure TSQLVariables.SetAsDateTime(Const FName:RawByteString;FValue:TDateTime);
begin
SetValue(FName,EncodeDateTime(FValue));
end;
Procedure TSQLVariables.SetAsNull(Const FName:RawByteString);
begin
SetValue(FName,EncodeNull);
end;
procedure TSQLVariables.Clear;
Var
i:Longint;
P:PZVariant;
begin
if (Self<>nil) and (Count<>0) then
For i:=0 to Count-1 do
begin
P:=Pointer(Objects[i]);
if Assigned(P) then
begin
Finalize(P^);
FreeMem(P);
end;
end;
inherited;
end;
destructor TSQLVariables.Destroy;
begin
Clear;
inherited;
end;
Procedure TSQLContext.ClearCursors;
begin
FGlobal:=nil;
FreeAndNil(FCursors);
end;
Procedure TSQLContext.DeclareStatement(Const Name,FSQL:RawByteString;Tag:SizeInt;ResultTp:TZResultSetType);
var
stmt:IZPreparedStatement;
begin
stmt:=FConnection.PrepareStatement(FSQL);
stmt.SetResultSetConcurrency(rcReadOnly);
stmt.SetResultSetType(ResultTp);
if (ResultTp=rtForwardOnly) then
stmt.SetFetchDirection(fdForward)
else
stmt.SetFetchDirection(fdUnknown);
if not Assigned(FCursors) then
begin
FCursors:=TZCursorSet.Create;
end;
FCursors.DeclareStatement(Name,Tag,stmt);
end;
Function TSQLContext.GetCursor(Const Name:RawByteString):PZCursor;
begin
Result:=nil;
if Assigned(FCursors) then Result:=FCursors.GetCursor(Name);
end;
Procedure TSQLContext.DelCursor(Const Name:RawByteString);
begin
if Assigned(FCursors) then FCursors.DelCursor(Name);
end;
Procedure TSQLContext.Clear;
begin
if Assigned(FVariables) then
begin
FVariables.Clear;
FreeAndNil(FVariables);
end;
ClearCursors;
end;
type
TQueryState=object
Data:RawByteString;
DataLen:SizeUint;
FParams,FValues:TParams;
Fbrackets:SizeUint;
Procedure AddChar(Ch:AnsiChar);
Procedure AddToken(Const _Token:TZToken);
Procedure AddParam(Const _Name:RawByteString);
Procedure AddValue(isVar:Boolean;Const _Name:RawByteString);
Procedure Add4String(TrimR:Boolean); inline;
Procedure Add2Param; inline;
function IsSpace:Boolean; inline;
procedure Clean;
procedure Finish;
procedure Parse(Const FToken:TZToken);
end;
PSQLParseContext=^TSQLParseContext;
TDeclareState=object
Context:PSQLParseContext;
FName:RawByteString;
FType:TZVariantType;
FResultTp:TZResultSetType;
procedure Clean;
procedure AddDeclare; inline;
procedure AddCursor; inline;
Function Finish:Boolean;
Function Parse(Const FToken:TZToken):Boolean;
end;
TFetchState=object
Context:PSQLParseContext;
FName:RawByteString;
FLastCmd:TSQLCommandType;
procedure Clean;
Function Finish:Boolean;
Function Parse(Const FToken:TZToken):Boolean;
end;
{TBlockState=record
_type,
VSID:SizeInt;
end;}
TBlockType=(btBegin,btIf1,btIf2,btElse,btWhile);
PBlockState=^TBlockState;
TBlockState=object
_type:TBlockType;
FBeginId:SizeInt;
GotoBackward:TGotoBackward;
Procedure ClearGotoId; inline;
Procedure AddGotoId(id:SizeInt);
end;
TBlockStack=specialize TFastStack<TBlockState>;
{IF () ->1 >-v
BEGIN ->0 |
END <-0 = 1 :=2 |
| >-v GOTO END
ELSE :=3 | |
--< |
BEGIN ->0 |
END <-0 = 3 --<
WHILE ->4 >-v
BEGIN ->0 |
END <-0 = 4 | GOTO WHILE
--<
}
TSQLParseCb=procedure of object;
TSQLParseContext=object
Script:PSQLScript;
FTokenizer:TZTokenizer;
FExpressionState:TExpressionState;
FQueryState:TQueryState;
FDeclareState:TDeclareState;
FFetchState:TFetchState;
FBlockStack:TBlockStack;
//FBlockStack:array of TBlockState;
FOpId:SizeInt;
FParamName:RawByteString;
FToken:TZToken;
State:SizeUint;
cb:TSQLParseCb;
function PushBlock(_type:TBlockType;_VSID:SizeInt):PBlockState;
function PopBlock:Boolean; inline;
//function TopBlock:TBlockState;
//procedure SetTopBlockType(o:SizeInt);
procedure AddCmd(Const FName:RawByteString;_Type:TSQLCommandType;_SID:SizeInt); inline;
procedure AddBath(LastCount:SizeUint); inline;
function AddStatment(ResultTp:TZResultSetType):SizeInt;
//Function GetUpperCaseToken:RawByteString; inline;
Function GetAsUInt64(def:UInt64):UInt64; inline;
Function GetType:TZTokenType; inline;
Function isEOC(Const _Token:TZToken):Boolean;
Procedure Reset;
Procedure SetCb(_cb:TSQLParseCb;new:SizeUint=0); inline;
Procedure ResetErr;
Procedure cbError;
Procedure cbNormal;
Procedure cbSet;
function SubQueryFinish:Int64;
Procedure cbPrint;
Procedure cbDeclare;
Procedure cbCursor;
Procedure cbFetch;
Procedure cbBegin;
//Procedure OnEOCCmd;
Procedure OnAfterAddCmd;
Procedure OnBeforeAddBath;
Procedure cbEnd;
Procedure cbCond;
Procedure cbBreak;
Procedure cbContinue;
Procedure cbElse;
Procedure cbCommit;
Procedure cbRollback;
Procedure BeginParse;
Procedure EndParse;
end;
Procedure TBlockState.ClearGotoId; inline;
begin
SetLength(GotoBackward,0);
end;
Procedure TBlockState.AddGotoId(id:SizeInt);
Var
i:SizeInt;
begin
i:=Length(GotoBackward);
SetLength(GotoBackward,i+1);
GotoBackward[i]:=id;
end;
Procedure TQueryState.AddChar(Ch:AnsiChar);
begin
if DataLen=Length(Data) then
begin
if Length(Data)<8 then
SetLength(Data,8)
else
SetLength(Data,DataLen+(DataLen shr 1));
end;
Inc(DataLen);
Data[DataLen]:=Ch;
end;
Procedure TQueryState.AddToken(Const _Token:TZToken);
Var
New,A:SizeUint;
begin
if IsSpace and (_Token.TokenType=ttWhitespace) then Exit;
A:=Length(Data);
New:=DataLen+_Token.L;
if (New>=A) then
begin
if A<8 then A:=8;
While (New>A) do A:=A+(A shr 1);
SetLength(Data,A);
end;
Move(_Token.P^,Data[DataLen+1],_Token.L);
DataLen:=New;
end;
Procedure TQueryState.AddParam(Const _Name:RawByteString);
Var
i:SizeUint;
begin
i:=Length(FParams);
SetLength(FParams,i+1);
FParams[i]:=_Name;
end;
Procedure TQueryState.AddValue(isVar:Boolean;Const _Name:RawByteString);
Var
i:SizeUint;
begin
i:=Length(FValues);
SetLength(FValues,i+1);
if isVar then
begin
FValues[i]:=':'+_Name;
end else
if (_Name<>'') and (_Name[1]=':') then
begin
FValues[i]:=' '+_Name;
end else
begin
FValues[i]:=_Name;
end;
end;
Procedure TQueryState.Add4String(TrimR:Boolean); inline;
Var
T:RawByteString;
begin
if DataLen=0 then Exit;
if Length(FValues)=0 then
begin
T:=TrimLeft(Copy(Data,1,DataLen));
end else
begin
T:=Copy(Data,1,DataLen);
end;
if TrimR then
begin
T:=TrimRight(T);
end;
AddValue(false,T);
DataLen:=0;
end;
Procedure TQueryState.Add2Param; inline;
begin
AddParam(Trim(Copy(Data,1,DataLen)));
DataLen:=0;
end;
function TQueryState.IsSpace:Boolean; inline;
begin
Result:=(DataLen=0) and (Length(FValues)=0);
end;
procedure TQueryState.Clean;
begin
DataLen:=0;
SetLength(FParams,0);
SetLength(FValues,0);
Fbrackets:=0;
end;
procedure TQueryState.Finish;
begin
Add4String(True);
end;
procedure TQueryState.Parse(Const FToken:TZToken);
begin
Case FToken.TokenType of
ttSymbol:
begin
AddToken(FToken);
Case FToken.P^ of
'(':Inc(Fbrackets);
')':Dec(Fbrackets);
end;
end;
ttWhitespace:if (DataLen<>0) then
AddToken(FToken);
ttSpecial:if GetVariableType(FToken.P,FToken.L)=3 then
begin
Add4String(False);
AddValue(true,DecodeVariable(FToken.P,FToken.L));
end else
begin
AddChar('?');
AddParam(DecodeVariable(FToken.P,FToken.L));
end;
else
AddToken(FToken);
end;
end;
function TSQLParseContext.PushBlock(_type:TBlockType;_VSID:SizeInt):PBlockState;
Var
B:TBlockState;
begin
B:=Default(TBlockState);
B._type:=_type;
B.FBeginId:=_VSID;
FBlockStack.Push(B);
Result:=FBlockStack.Top;
end;
{
procedure TSQLParseContext.PushBlock(_type,_VSID:SizeInt);
Var
i:SizeInt;
begin
i:=Length(FBlockStack);
SetLength(FBlockStack,i+1);
FBlockStack[i]._type:=_type;
FBlockStack[i].VSID:=_VSID;
end;
}
function TSQLParseContext.PopBlock:Boolean; inline;
begin
Result:=FBlockStack.Pop;
end;
{
procedure TSQLParseContext.PopBlock;
Var
i:SizeInt;
begin
i:=Length(FBlockStack);
if (i=0) then Exit;
Dec(i);
SetLength(FBlockStack,i);
end;
function TSQLParseContext.TopBlock:TBlockState;
Var
i:SizeInt;
begin
Result._type:=-1;
Result.VSID:=-1;
i:=Length(FBlockStack);
if (i=0) then Exit;
Dec(i);
Result:=FBlockStack[i];
end;
}
{
procedure TSQLParseContext.SetTopBlockType(o:SizeInt);
Var
i:SizeInt;
begin
i:=Length(FBlockStack);
if (i=0) then Exit;
Dec(i);
FBlockStack[i]._type:=o;
end;
}
procedure TSQLParseContext.AddCmd(Const FName:RawByteString;_Type:TSQLCommandType;_SID:SizeInt); inline;
begin
Script^.AddCmd(FName,_Type,_SID);
OnAfterAddCmd;
end;
procedure TSQLParseContext.AddBath(LastCount:SizeUint); inline;
begin
OnBeforeAddBath;
Script^.AddBath(LastCount);
end;
function TSQLParseContext.AddStatment(ResultTp:TZResultSetType):SizeInt;
begin
Result:=Script^.AddStatment(FQueryState.FParams,FQueryState.FValues,ResultTp);
FQueryState.Clean;
end;
{
Function TSQLParseContext.GetUpperCaseToken:RawByteString; inline;
begin
System.SetString(Result,FToken.P,FToken.L);
Result:=UpperCase(Result);
end;
}
Function TSQLParseContext.GetAsUInt64(def:UInt64):UInt64; inline;
begin
Result:=RawToUInt64Def(FToken.P,FToken.P+FToken.L,def)
end;
Function TSQLParseContext.GetType:TZTokenType; inline;
begin
Result:=FToken.TokenType;
end;
Function TSQLParseContext.isEOC(Const _Token:TZToken):Boolean;
begin
Result:=(_Token.TokenType=ttEOF) or
((_Token.TokenType=ttSymbol) and (_Token.L=1) and (_Token.P^=';'));
end;
Procedure TSQLParseContext.Reset;
begin
FOpId:=-1;
FParamName:='';