-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDbcScriptExp.pas
1398 lines (1227 loc) · 29.9 KB
/
DbcScriptExp.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 expression parser
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 DbcScriptExp;
{$mode objfpc}{$H+}
interface
uses
SysUtils,
Classes,
gFastStack,
FmtBCD,
ZSysUtils,
ZDbcIntfs,
ZCompatibility,
ZEncoding,
ZVariant,
ZFastCode,
ZTokenizer,ZGenericSqlToken,
DbcScriptUtils;
//scalar_subquery variable operator Integer Double Currency Bcd Quoted
type
TZNodeType=(ntNull ,ntBoolean ,ntInteger ,
ntUInteger ,ntDouble ,ntCurrency,
ntBigDecimal,ntGUID ,ntBytes ,
ntQuoted ,ntDateTime,ntFunction,
ntOperator ,ntVariable,ntSubquery);
TExpData=record
nType:TZNodeType;
Token:RawByteString;
case TZNodeType of
ntBoolean :(VBoolean :Boolean);
ntInteger :(VInteger :Int64);
ntUInteger :(VUInteger :UInt64);
ntDouble :(VDouble :Double);
ntCurrency :(VCurrency :Currency);
ntDateTime :(VDateTime :TDateTime);
ntOperator :(VOP1,VOP2 :Byte);
ntSubquery :(VSID :Int64);
end;
PExpNode=^TExpNode;
TExpNode=record
Parent,Left,Right:PExpNode;
Data:TExpData;
end;
TCb_GetValue=function(Const FName:RawByteString):TZVariant of object;
TCb_GetQuery=function(VSID:Int64):IZResultSet of object;
TCb_SubQueryParse =procedure(Const FToken:TZToken) of object;
TCb_SubQueryFinish=function:Int64 of object;
TExpressionState=object
cb_parse:TCb_SubQueryParse;
cb_fin:TCb_SubQueryFinish;
Node:PExpNode;
subquery_count:SizeUint;
procedure Clean;
function Finish:Boolean;
function GetStr:RawByteString;
procedure Print;
Function Parse(Const FToken:TZToken):Boolean;
end;
TExpressionCalc=object
cb_GetValue:TCb_GetValue;
cb_GetQuery:TCb_GetQuery;
Node:PExpNode;
Function calc:TZVariant;
end;
Procedure FreeExpression(Node:PExpNode);
implementation
Function NewExpNode(Const FToken:TZToken):PExpNode;
procedure _EncodeNumber(var Node:TExpNode); inline;
Var
Len:Integer;
BCD:TBCD;
begin
if (FToken.L<>0) and (FToken.P^='$') then
begin
Len:=FToken.L-1;
Node.Data.VCurrency:=ValRawCurr(PByteArray(@FToken.P[1]),'.',Len);
if (Len=FToken.L-1) then Node.Data.nType:=ntCurrency;
end else
begin
BCD:=Default(TBCD);
if TryRawToBcd(FToken.P,FToken.L,BCD,'.') then
begin
SetLength(Node.Data.Token,SizeOf(TBCD));
Move(BCD,PChar(Node.Data.Token)^,SizeOf(TBCD));
Node.Data.nType:=ntBigDecimal;
end;
end;
end;
procedure _EncodeDouble(var Node:TExpNode); inline;
Var
E:Integer;
begin
Node.Data.VDouble:=ValRawDbl(PByteArray(FToken.P),'.',E);
if (E<>0) then Node.Data.nType:=ntDouble;
end;
Function TryHexToBin(HexValue:PChar;L:Integer;var _Result:RawByteString):Boolean; inline;
Var
TCH:array[0..1] of AnsiChar;
begin
if (L and 1)=0 then
begin
L:=L shr 1;
SetLength(_Result,L);
Result:=HexToBin(HexValue,PChar(_Result),L)=L;
end else
begin
L:=(L shr 1);
SetLength(_Result,L+1);
TCH[0]:='0';
TCH[1]:=HexValue[0];
HexToBin(@TCH,PChar(_Result),1);
Result:=HexToBin(@HexValue[1],@PChar(_Result)[1],L)=L;
end;
if not Result then _Result:='';
end;
procedure _EncodeHex(var Node:TExpNode); inline;
begin
if FToken.L>2 then
if TryHexToBin(@FToken.P[2],FToken.L-2,Node.Data.Token) then
begin
if Length(Node.Data.Token)>SizeOf(Node.Data.VUInteger) then
begin
if Length(Node.Data.Token)=SizeOf(TGUID) then
begin
Node.Data.nType:=ntGUID;
end else
begin
Node.Data.nType:=ntBytes;
end;
end else
begin
Node.Data.VUInteger:=0;
{$IFDEF ENDIAN_BIG}
With Node.Data do
Move(Token[1],VUInteger,Length(Token));
{$ELSE}
With Node.Data do
begin
Move(Token[1],PByte(@VUInteger)[SizeOf(VUInteger)-Length(Token)],Length(Token));
VUInteger:=SwapEndian(VUInteger);
end;
{$ENDIF}
Node.Data.nType:=ntUInteger;
Node.Data.Token:='';
end;
end;
end;
procedure _EncodeUInteger(var Node:TExpNode); inline;
var
P,PEnd:PAnsiChar;
BCD:TBCD;
begin
PEnd:=FToken.P+FToken.L;
P:=PEnd;
Node.Data.VUInteger:=ValRawUInt64(FToken.P,P);
if P=PEnd then
begin
Node.Data.nType:=ntUInteger;
end else
begin
BCD:=Default(TBCD);
if TryRawToBcd(FToken.P,FToken.L,BCD,'.') then
begin
SetLength(Node.Data.Token,SizeOf(TBCD));
Move(BCD,PChar(Node.Data.Token)^,SizeOf(TBCD));
Node.Data.nType:=ntBigDecimal;
end;
end;
end;
procedure _EncodeQuoted(var Node:TExpNode); inline;
begin
Node.Data.nType:=ntQuoted;
Node.Data.Token:=GetParamStr(FToken.P,FToken.L);
end;
procedure _EncodeBoolean(v:Boolean;var Node:TExpNode); inline;
begin
Node.Data.VBoolean:=v;
Node.Data.nType:=ntBoolean;
end;
procedure _EncodeVariable(var Node:TExpNode); inline;
begin
Node.Data.nType:=ntVariable;
Node.Data.Token:=DecodeVariable(FToken.P,FToken.L);
end;
function _EncodeOp(var Node:TExpNode):Boolean; inline;
Var
i:SizeInt;
begin
i:=_get_op_id(FToken.P,FToken.L);
if (i<>-1) then
begin
Result:=true;
Node.Data.nType:=ntOperator;
Node.Data.VOP1 :=i;
end;
end;
begin
Result:=AllocMem(SizeOf(TExpNode));
Case FToken.TokenType of
ttNumber :_EncodeNumber(Result^);
ttFloat :_EncodeDouble (Result^);
ttInteger :_EncodeUInteger(Result^);
ttHexDecimal:_EncodeHex (Result^);
ttQuoted,
ttQuotedIdentifier:_EncodeQuoted(Result^);
ttWord,ttKeyword:
begin
Case _get_const_keyword(FToken.P,FToken.L) of
0:Result^.Data.nType:=ntNull;
1:_EncodeBoolean(true ,Result^);
2:_EncodeBoolean(false,Result^);
else
_EncodeOp(Result^);
// if not _EncodeOp = function
end;
end;
ttSpecial:_EncodeVariable(Result^);
ttSymbol:_EncodeOp(Result^);
//ttDateTime:Result:=EncodeRawByteString(FToken^.Token);
end;
end;
Function NewExpZeroNode:PExpNode;
begin
Result:=AllocMem(SizeOf(TExpNode));
Result^.Data.nType:=ntUInteger;
Result^.Data.VUInteger:=0;
end;
Function NewExpNullNode:PExpNode;
begin
Result:=AllocMem(SizeOf(TExpNode));
Result^.Data.nType:=ntNull;
end;
Function GetFirstValue(R:IZResultSet):TZVariant; inline;
begin
Result:=Default(TZVariant);
if Assigned(R) and (R.Next) then
begin
Result:=R.GetValue(1);
end;
end;
procedure ResolveFirstValue(Var V:TZVariant); inline;
begin
if (V.VType=vtInterface) then
begin
V:=GetFirstValue(IZResultSet(V.VInterface));
end;
end;
function GetVariant(Node:PExpNode;cb_GetValue:TCb_GetValue;cb_GetQuery:TCb_GetQuery):TZVariant;
function _EncodeBCD(const Value:RawByteString):TZVariant; inline;
begin
Result.VType:=vtBigDecimal;
Move(PChar(Value)^,Result.VBigDecimal,Length(Value));
end;
function _EncodeGUID(const Value:RawByteString):TZVariant; inline;
begin
Result.VType:=vtGUID;
Move(PChar(Value)^,Result.VGUID,Length(Value));
end;
function _EncodeBytes(const Value:RawByteString):TZVariant; inline;
begin
Result.VType:=vtBytes;
Result.VRawByteString:=Value;
end;
begin
Result:=Default(TZVariant);
if Assigned(Node) then
begin
Case Node^.Data.nType of
ntBoolean :Result:=EncodeBoolean (Node^.Data.VBoolean);
ntInteger :Result:=EncodeInteger (Node^.Data.VInteger);
ntUInteger :Result:=EncodeUInteger(Node^.Data.VUInteger);
ntDouble :Result:=EncodeDouble (Node^.Data.VDouble);
ntCurrency :Result:=EncodeCurrency(Node^.Data.VCurrency);
ntBigDecimal:Result:=_EncodeBCD (Node^.Data.Token);
ntGUID :Result:=_EncodeGUID (Node^.Data.Token);
ntBytes :Result:=_EncodeBytes (Node^.Data.Token);
ntQuoted :Result:=EncodeRawByteString(Node^.Data.Token);
ntDateTime :Result:=EncodeDateTime(Node^.Data.VDateTime);
ntVariable :if Assigned(cb_GetValue) then
Result:=cb_GetValue(Node^.Data.Token)
else
Result:=EncodeRawByteString(':'+Node^.Data.Token);
ntFunction :Result:=EncodeRawByteString(Node^.Data.Token);
ntOperator :Result:=EncodeRawByteString(_get_op_name(Node^.Data.VOP1,Node^.Data.VOP2));
ntSubquery :if Assigned(cb_GetQuery) then
Result:=EncodeInterface(cb_GetQuery(Node^.Data.VSID))
else
Result:=EncodeRawByteString('('+IntToStr(Node^.Data.VSID)+')');
end;
//Writeln('GET:',Node^.nType,':',Node^.Token,':',_GetAsRawByteString(Result));
end;
end;
function pushLeft(Node:PExpNode;New:PExpNode):PExpNode;
Var
P:PExpNode;
begin
Result:=New;
if Assigned(Node) then
begin
P:=Node^.Parent;
Result^.Left:=Node;
Result^.Parent:=P;
Node^.Parent:=Result;
if Assigned(P) then
begin
if (P^.Right=Node) then
P^.Right:=Result
else
P^.Left:=Result;
end;
end;
end;
function pushRight(Node:PExpNode;New:PExpNode):PExpNode;
Var
P:PExpNode;
begin
Result:=New;
if Assigned(Node) then
begin
P:=Node^.Parent;
Result^.Right:=Node;
Result^.Parent:=P;
Node^.Parent:=Result;
if Assigned(P) then
begin
if (P^.Right=Node) then
P^.Right:=Result
else
P^.Left:=Result;
end;
end;
end;
function PopRight(Node:PExpNode):PExpNode;
Var
P,R:PExpNode;
begin
Result:=nil;
R:=Node^.Right;
P:=Node^.Parent;
if Assigned(R) then
begin
R^.Parent:=P;
Result:=R;
end;
if Assigned(P) then
begin
if (P^.Left=Node) then
begin
P^.Left:=R;
end else
begin
P^.Right:=R;
end;
Result:=P;
end;
Finalize(Node^);
FreeMem(Node);
end;
function PopLeft(Node:PExpNode):PExpNode;
Var
P,L:PExpNode;
begin
Result:=nil;
L:=Node^.Left;
P:=Node^.Parent;
if Assigned(L) then
begin
L^.Parent:=P;
Result:=L;
end;
if Assigned(P) then
begin
if (P^.Left=Node) then
begin
P^.Left:=L;
end else
begin
P^.Right:=L;
end;
Result:=P;
end;
Finalize(Node^);
FreeMem(Node);
end;
function SetLeft(var Node:PExpNode;New:PExpNode):PExpNode;
begin
if Assigned(Node) then
begin
Node^.Left:=New;
New^.Parent:=Node;
end else
begin
Node:=New;
end;
Result:=New;
end;
function SetRight(var Node:PExpNode;New:PExpNode):PExpNode;
begin
if Assigned(Node) then
begin
Node^.Right:=New;
New^.Parent:=Node;
end else
begin
Node:=New;
end;
Result:=New;
end;
function LeftIsExist(Node:PExpNode):Boolean; inline;
begin
Result:=Assigned(Node) and Assigned(Node^.Left);
end;
function RightIsExist(Node:PExpNode):Boolean; inline;
begin
Result:=Assigned(Node) and Assigned(Node^.Right);
end;
function ParentUp(Node:PExpNode):PExpNode;
begin
Result:=Node;
if Assigned(Result) then
While Assigned(Result^.Parent) do Result:=Result^.Parent;
end;
Function GetNodeOp(Node:PExpNode):SizeInt; inline;
begin
Result:=-1;
if Assigned(Node) then
if (Node^.Data.nType=ntOperator) then
begin
Result:=Node^.Data.VOP1;
end;
end;
Procedure FreeNode(Node:PExpNode); inline;
begin
if Assigned(Node) then
begin
Finalize(Node^);
FreeMem(Node);
end;
end;
Function _down(Node:PExpNode):PExpNode; inline;
begin
Result:=Node;
if Assigned(Result) then
repeat
if Assigned(Result^.Left) then
Result:=Result^.Left
else if Assigned(Result^.Right) then
Result:=Result^.Right
else
Break;
until false;
end;
Procedure FreeExpression(Node:PExpNode);
var
I,N:PExpNode;
begin
I:=_down(Node);
if Assigned(I) then
begin
repeat
if (I^.Parent=nil) then Break;
N:=I;
I:=I^.Parent;
FreeNode(N);
if (I^.Left=N) then
begin
if (I^.Right=nil) then Continue;
I:=I^.Right;
I:=_down(I);
end;
until false;
end;
FreeNode(I);
end;
procedure TExpressionState.Clean;
begin
FreeExpression(Node);
Node:=nil;
subquery_count:=0;
end;
{
function _GetStr(Node:PExpNode):RawByteString;
var
p,n:SizeInt;
begin
if Assigned(Node) then
begin
if Node^.nType=ntOperator then
begin
p:=GetNodeOp(Node^.Parent);
n:=GetNodeOp(Node);
if Assigned(Node^.Parent) and (_get_op_prior(n)>_get_op_prior(p)) then
begin
Case n of
26..34:Result:='('+_GetStr(Node^.Left)+' '+_get_op_name(n)+' '+_GetStr(Node^.Right)+')';
else
Result:='('+_GetStr(Node^.Left)+_get_op_name(n)+_GetStr(Node^.Right)+')';
end;
end else
begin
Case n of
26..34:Result:=_GetStr(Node^.Left)+' '+_get_op_name(n)+' '+_GetStr(Node^.Right);
else
Result:=_GetStr(Node^.Left)+_get_op_name(n)+_GetStr(Node^.Right);
end;
end;
end else
begin
Result:=_GetAsRawByteString(GetVariant(Node,nil,nil));
end;
end else
Result:='';
end;}
function TExpressionState.GetStr:RawByteString;
var
I,N:PExpNode;
procedure concat(Var S:RawByteString;I:PExpNode); inline;
var
n:SizeInt;
begin
n:=GetNodeOp(I);
Case n of
-1:Case I^.Data.nType of
ntQuoted:S:=S+''''+I^.Data.Token+'''';
else
S:=S+_GetAsRawByteString(GetVariant(I,nil,nil));
end;
26..35:S:=S+' '+_get_op_name(I^.Data.VOP1,I^.Data.VOP2)+' ';
else
S:=S+_get_op_name(I^.Data.VOP1,I^.Data.VOP2);
end;
end;
Function _down(Var S:RawByteString;Node:PExpNode):PExpNode; inline;
var
p,n:SizeInt;
begin
Result:=Node;
if Assigned(Result) then
repeat
p:=GetNodeOp(Result);
if Assigned(Result^.Left) then
Result:=Result^.Left
else if Assigned(Result^.Right) then
begin
concat(S,Result);
Result:=Result^.Right;
end else
Break;
n:=GetNodeOp(Result);
if (_get_op_prior(n)>_get_op_prior(p)) then
begin
S:=S+'(';
end;
until false;
end;
procedure _up_br(Var S:RawByteString;Node:PExpNode); inline;
var
p,n:SizeInt;
begin
p:=GetNodeOp(Node^.Parent);
n:=GetNodeOp(Node);
if Assigned(Node^.Parent) and (_get_op_prior(n)>_get_op_prior(p)) then
begin
S:=S+')';
end;
end;
begin
I:=ParentUp(Node);
//Result:=_GetStr(I);
Result:='';
I:=_down(Result,Node);
if Assigned(I) then
begin
concat(Result,I);
repeat
N:=I;
I:=I^.Parent;
if (I=nil) then Break;
if (I^.Left=N) then
begin
concat(Result,I);
if (I^.Right=nil) then Continue;
I:=I^.Right;
I:=_down(Result,I);
concat(Result,I);
end else
begin
_up_br(Result,I);
end;
until false;
end;
end;
procedure _print(Node,orig:PExpNode;L:Byte);
begin
if Assigned(Node) then
begin
_print(Node^.Right,orig,L+2);
if Assigned(Node^.Parent) then
if (Node^.Parent^.Left=Node) then
begin
Writeln(Space(L-1),'\');
end;
if Node=orig then
Writeln(Space(L),_GetAsRawByteString(GetVariant(Node,nil,nil))+' <---')
else
Writeln(Space(L),_GetAsRawByteString(GetVariant(Node,nil,nil)));
if Assigned(Node^.Parent) then
if (Node^.Parent^.Left<>Node) then
begin
Writeln(Space(L-1),'/');
end;
_print(Node^.Left,orig,L+2);
end;
end;
procedure TExpressionState.Print;
var
tmp:PExpNode;
begin
tmp:=ParentUp(Node);
_print(tmp,Node,0);
end;
function node_is_const(Node:PExpNode):Boolean; inline;
begin
if Assigned(Node) then
begin
Result:=False;
if (Node^.Right=nil) and (Node^.Left=nil) then
Case Node^.Data.nType of
ntNull..ntDateTime:Result:=True;
end;
end else
begin
Result:=True;
end;
end;
Function NewSubQueryNode(id:Int64):PExpNode; inline;
begin
Result:=AllocMem(SizeOf(TExpNode));
Result^.Data.nType:=ntSubquery;
Result^.Data.VSID :=id;
end;
Function NewExpNode(Const V:TZVariant):PExpNode;
begin
Result:=AllocMem(SizeOf(TExpNode));
Case V.VType of
vtNull:Result^.Data.nType:=ntNull;
vtBoolean:begin
Result^.Data.nType :=ntBoolean;
Result^.Data.VBoolean:=V.VBoolean;
end;
vtInteger:begin
Result^.Data.nType :=ntInteger;
Result^.Data.VInteger:=V.VInteger;
end;
vtUInteger:begin
Result^.Data.nType :=ntUInteger;
Result^.Data.VUInteger:=V.VUInteger;
end;
vtDouble:begin
Result^.Data.nType :=ntDouble;
Result^.Data.VDouble :=V.VDouble;
end;
vtCurrency:begin
Result^.Data.nType :=ntCurrency;
Result^.Data.VCurrency:=V.VCurrency;
end;
vtBigDecimal:begin
Result^.Data.nType :=ntBigDecimal;
SetLength(Result^.Data.Token,SizeOf(TBCD));
Move(V.VBigDecimal,PChar(Result^.Data.Token)^,SizeOf(TBCD));
end;
vtGUID:begin
Result^.Data.nType :=ntGUID;
SetLength(Result^.Data.Token,SizeOf(TGUID));
Move(V.VGUID,PChar(Result^.Data.Token)^,SizeOf(TGUID));
end;
vtBytes:begin
Result^.Data.nType :=ntBytes;
Result^.Data.Token :=V.VRawByteString;
end;
vtDate,
vtTime,
vtTimeStamp,
vtDateTime:begin
Result^.Data.nType :=ntDateTime;
Result^.Data.VDateTime:=_GetAsDateTime(V);
end;
{$IFNDEF UNICODE}vtString,{$ENDIF}
{$IFNDEF NO_ANSISTRING}
vtAnsiString,
{$ENDIF}
{$IFNDEF NO_UTF8STRING}
vtUTF8String,
{$ENDIF}
vtRawByteString:begin
Result^.Data.nType :=ntQuoted;
Result^.Data.Token :=V.VRawByteString;
end;
{$IFDEF UNICODE}vtString,{$ENDIF}
vtUnicodeString:begin
Result^.Data.nType :=ntQuoted;
Result^.Data.Token :=RawByteString(V.VUnicodeString);
end;
else
begin
FreeMem(Result);
Result:=nil;
end;
end;
end;
Procedure Simp_LRR(var Node:PExpNode);
var
n_op,l_op:SizeInt;
VL,VR:TZVariant;
new,left:PExpNode;
begin
n_op:=GetNodeOp(Node);
Case n_op of
-1,29..32:Exit;
end;
if not node_is_const(Node^.Right) then Exit;
left:=Node^.Left;
repeat
if not Assigned(left) then Exit;
l_op:=GetNodeOp(left);
if (l_op=-1) then Exit;
if (_get_op_prior(l_op)=_get_op_prior(n_op))
and node_is_const(left^.Right) then Break;
left:=Left^.Left;
until false;
VL:=GetVariant(left^.Right,nil,nil);
VR:=GetVariant(Node^.Right,nil,nil);
VL:=DoTokenOp(n_op,0,VL,VR);
new:=NewExpNode(VL);
if Assigned(new) then
begin
FreeNode(left^.Right);
PopLeft(left);
Node^.Data.VOP1:=l_op;
FreeNode(Node^.Right);
SetRight(Node,new);
end;
end;
Procedure Simp_RLR(var Node:PExpNode);
var
n_op,r_op:SizeInt;
VL,VR:TZVariant;
new,Right:PExpNode;
begin
n_op:=GetNodeOp(Node);
Case n_op of
-1,29..32:Exit;
end;
if not node_is_const(Node^.Left) then Exit;
Right:=Node^.Right;
repeat
if not Assigned(Right) then Exit;
r_op:=GetNodeOp(Right);
if (r_op=-1) then Exit;
if (_get_op_prior(r_op)=_get_op_prior(n_op))
and node_is_const(Right^.Right) then Break;
Right:=Right^.Right;
until false;
VL:=GetVariant(Node^.Left ,nil,nil);
VR:=GetVariant(Right^.Right,nil,nil);
VL:=DoTokenOp(r_op,0,VL,VR);
new:=NewExpNode(VL);
if Assigned(new) then
begin
FreeNode(Right^.Right);
PopLeft(Right);
FreeNode(Node^.Left);
SetLeft(Node,new);
end;
end;
Procedure Simp_LR(var Node:PExpNode);
var
n:SizeInt;
VL,VR:TZVariant;
new,old,parent:PExpNode;
begin
n:=GetNodeOp(Node);
Case n of
-1,29..32:Exit;
end;
if node_is_const(Node^.Left) and node_is_const(Node^.Right) then
begin
VL:=GetVariant(Node^.Left ,nil,nil);
VR:=GetVariant(Node^.Right,nil,nil);
VL:=DoTokenOp(n,0,VL,VR);
new:=NewExpNode(VL);
if Assigned(new) then
begin
old:=Node;
parent:=Node^.Parent;
Node:=new;
Node^.Parent:=parent;
if Assigned(parent) then
begin
if (parent^.Left=old) then
begin
parent^.Left:=new;
end else
begin
parent^.Right:=new;
end;
end;
FreeNode(old^.Left);
FreeNode(old^.Right);
FreeNode(old);
end;
end;
end;
Procedure Simplification(var Node:PExpNode);
var
I,N:PExpNode;
begin
I:=_down(Node);
if Assigned(I) then
begin
repeat
N:=I;
I:=I^.Parent;
if (I=nil) then Break;
if (I^.Left=N) then
begin
if (I^.Right=nil) then Continue;
I:=I^.Right;
I:=_down(I);
end else
begin
N:=I;
Simp_LR(I);
Simp_LRR(I);
Simp_RLR(I);
if (N<>I) and (Node=N) then
begin
Node:=I;
end;
end;
until false;
end;
end;
function DoExistsOp(R:IZResultSet):TZVariant;
begin
Result.VType :=vtBoolean;
try
Result.VBoolean:=R.Next;
except
end;
end;
function TExpressionState.Finish:Boolean;
begin
Result:=true;
if (subquery_count<>0) then
begin
if Assigned(cb_fin) then