-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjanSQLExpression2.pas
1120 lines (1020 loc) · 22.7 KB
/
janSQLExpression2.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
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.mozilla.org/NPL/NPL-1_1Final.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: janSQLExpression2.pas, released March 24, 2002.
The Initial Developer of the Original Code is Jan Verhoeven
(jan1.verhoeven@wxs.nl or http://jansfreeware.com).
Portions created by Jan Verhoeven are Copyright (C) 2002 Jan Verhoeven.
All Rights Reserved.
Contributor(s):
* Zlatko Matić (matalab@gmail.com)
* ___________________.
Last Modified: 24-mar-2002
Current Version: 1.0
Notes: This is a very fast expression compiler and evaluator
Known Issues:
History:
1.2 16.08.2011 (by Zlatko Matić)
- Procedures proLOJ and procROJ added to handle Sybase-like operators ('*='; '=*') for outer joins.
1.1 26-mar-2002
- added functions:
- FORMAT, FORMATDATE, FORMATTIME
- YEAR, MONTH, DAY
- DATEADD, EASTER
- WEEKNUMBER
- ISNUMERIC, ISDATE function
- REPLACE function
- added constants:
- DATE, TIME
1.0 24-mar-2002 : original release
-----------------------------------------------------------------------------}
{$ifdef fpc}
{$mode delphi} {$H+}
{$endif}
unit janSQLExpression2;
interface
uses
{$IFDEF UNIX} clocale, cwstring,{$ENDIF}
Classes,SysUtils,Math,janSQLStrings,janSQLTokenizer;
const
delimiters=['+','-','*','/',' ','(',')','=','>','<'];
numberchars=['0'..'9','.'];
identchars=['a'..'z','A'..'Z','0'..'9','.','_'];
null = '';
type
TVariableEvent=procedure(sender:Tobject;const VariableName:string;var VariableValue:variant;var handled:boolean) of object;
{ TjanSQLExpression2 }
TjanSQLExpression2=class(TObject)
private
FInFix:TList;
FPostFix:TList;
FStack:TList;
Fsource: string;
VStack:array[0..100] of variant;
SP:integer;
idx: integer; // scan index
SL:integer; // source length
FToken:string;
FTokenKind:TTokenKind;
FTokenValue:variant;
FTokenOperator:TTokenOperator;
FTokenLevel:integer;
FTokenExpression:string;
FPC:integer;
FonGetVariable: TVariableEvent;
procedure Setsource(const Value: string);
function Parse:boolean;
procedure AddToken;
procedure ClearInfix;
procedure ClearPostFix;
procedure ClearStack;
function InFixToStack(index:integer):boolean;
function InfixToPostFix(index:integer):boolean;
function StackToPostFix:boolean;
function ConvertInFixToPostFix:boolean;
procedure procString;
procedure procNumber;
procedure procVariable;
procedure procEq;
procedure procLOJ; //To be used for left outer join (Sybase-like). Added by Zlatko Matić, 22.08.2011
procedure procROJ; //To be used for right outer join (Sybase-like). Added by Zlatko Matić, 22.08.2011
procedure procNe;
procedure procGt;
procedure procGe;
procedure procLt;
procedure procLe;
procedure procAdd;
procedure procSubtract;
procedure procMultiply;
procedure procDivide;
procedure procAnd;
procedure procOr;
procedure procNot;
procedure procLike;
// numerical functions
procedure procSin;
procedure procCos;
procedure procSqr;
procedure procSqrt;
procedure procCeil;
procedure procFloor;
procedure procIsNumeric;
procedure procIsDate;
procedure procsqlIn;
// string functions
procedure procUPPER;
procedure procLOWER;
procedure procTRIM;
procedure procSoundex;
procedure procLeft;
procedure procRight;
procedure procMid;
procedure procsubstr_after;
procedure procsubstr_before;
procedure procReplace;
procedure procLen;
procedure procFix;
procedure procFormat;
procedure procYear;
procedure procMonth;
procedure procDay;
procedure procDateAdd;
procedure procEaster;
procedure procWeekNumber;
// conversion functions
procedure procAsNumber;
function CloseStackToPostFix: boolean;
function OperatorsToPostFix(Level:integer): boolean;
function FlushStackToPostFix: boolean;
function runpop:variant;
procedure runpush(value:variant);
procedure SetonGetVariable(const Value: TVariableEvent);
function IsLike(v1,v2:variant):boolean;
procedure runOperator(op: TTokenOperator);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure getInFix(list:TStrings);
procedure getPostFix(list:TStrings);
function Evaluate:variant;
procedure GetTokenList(list:TList;from,till:integer);
property Expression:string read Fsource write Setsource;
property onGetVariable:TVariableEvent read FonGetVariable write SetonGetVariable;
end;
implementation
{ TjanSQLExpression2 }
procedure TjanSQLExpression2.AddToken;
var
tok:TToken;
begin
tok:=TToken.Create;
tok.name:=FToken;
tok.tokenkind:=FTokenKind;
tok.value:=FTokenValue;
tok._operator:=FTokenOperator;
tok.level:=FtokenLevel;
tok.expression:=FTokenExpression;
FInFix.Add(tok);
end;
procedure TjanSQLExpression2.Clear;
begin
ClearInfix;
ClearPostFix;
ClearStack;
end;
procedure TjanSQLExpression2.ClearInfix;
var
i,c:integer;
begin
c:=FInFix.Count;
if c=0 then exit;
for i:=c-1 downto 0 do
TObject(FInFix.items[i]).free;
FInFix.clear;
end;
procedure TjanSQLExpression2.ClearPostFix;
var
i,c:integer;
begin
c:=FPostFix.Count;
if c=0 then exit;
for i:=c-1 downto 0 do
TObject(FPostFix.items[i]).free;
FPostFix.clear;
end;
procedure TjanSQLExpression2.ClearStack;
var
i,c:integer;
begin
c:=FStack.Count;
if c=0 then exit;
for i:=c-1 downto 0 do
TObject(FStack.items[i]).free;
FStack.clear;
end;
{
For each token in INPUT do the following:
If the token is an operand, enqueue it in OUTPUT.
If the token is an open bracket - push it on STACK.
If the token is a closing bracket:
- pop operators off STACK and enqueue them in OUTPUT,
until you encounter an open bracket.
Discard the opening bracket. If you reach the bottom of STACK without seeing an open bracket this indicates that the parentheses in the infix expression do not match, and so you should indicate an error.
If the token is an operator - pop operators off STACK and enqueue them in OUTPUT, until one of the following occurs:
- STACK is empty
- the operator at the top of STACK has lower precedence than the token
- the operator at the top of the stack has the same precedence as the token and the token is right associative.
Once you have done that push the token on STACK.
When INPUT becomes empty pop any remaining operators from STACK and enqueue them in OUTPUT. If one of the operators on STACK happened to be an open bracket, that means that its closing bracket never came, so an an error should be indicated.
}
function TjanSQLExpression2.ConvertInFixToPostFix: boolean;
var
i,c,level:integer;
tok:TToken;
begin
result:=false;
c:=FInfix.count;
if c=0 then exit;
for i:=0 to c-1 do begin
tok:=TToken(FInfix[i]);
case tok.tokenkind of
tkOperand: if not InFixToPostFix(i) then exit;
tkOpen: if not InFixToStack(i) then exit;
tkClose: if not CloseStackToPostFix then exit;
tkOperator:
begin
if not OperatorsToPostFix(tok.level) then exit;
InFixToStack(i);
end;
end;
end;
result:=FlushStackToPostFix;
end;
{
If the token is a closing bracket:
- pop operators off STACK and enqueue them in OUTPUT,
until you encounter an open bracket.
Discard the opening bracket. If you reach the bottom of STACK without seeing an open bracket this indicates that the parentheses in the infix expression do not match, and so you should indicate an error.
}
function TjanSQLExpression2.CloseStackToPostFix: boolean;
begin
result:=false;
while (FStack.count<>0) and (TToken(Fstack[FStack.count-1]).tokenkind<>tkOpen) do
StackToPostFix;
if FStack.count<>0 then begin
TToken(FStack[FStack.count-1]).free;
Fstack.Delete(FStack.count-1);
result:=true;
end;
end;
{
If the token is an operator - pop operators off STACK and enqueue them in OUTPUT, until one of the following occurs:
- STACK is empty
- the operator at the top of STACK has lower precedence than the token
- the operator at the top of the stack has the same precedence as the token and the token is right associative.
Once you have done that push the token on STACK.
}
function TjanSQLExpression2.OperatorsToPostFix(Level:integer): boolean;
begin
while (FStack.count<>0) and (TToken(Fstack[FStack.count-1]).level>=level) do
StackToPostFix;
result:=true;
end;
{
When INPUT becomes empty pop any remaining operators from STACK and enqueue them in OUTPUT. If one of the operators on STACK happened to be an open bracket, that means that its closing bracket never came, so an an error should be indicated.
}
function TjanSQLExpression2.FlushStackToPostFix: boolean;
begin
result:=false;
while (FStack.count<>0) and (TToken(Fstack[FStack.count-1]).tokenkind<>tkOpen) do
StackToPostFix;
result:=FStack.count=0;
end;
constructor TjanSQLExpression2.Create;
begin
FInFix:=TList.create;
FPostFix:=TList.create;
FStack:=TList.create;
end;
destructor TjanSQLExpression2.Destroy;
begin
Clear;
FInFix.free;
FPostFix.free;
Fstack.free;
inherited;
end;
procedure TjanSQLExpression2.getInFix(list: TStrings);
var
i,c:integer;
begin
list.Clear;
c:=FInFix.Count;
if c=0 then exit;
for i:=0 to c-1 do begin
list.append(TToken(FInFix[i]).name);
end;
end;
procedure TjanSQLExpression2.getPostFix(list:Tstrings);
var
i,c:integer;
begin
list.Clear;
c:=FPostFix.Count;
if c=0 then exit;
for i:=0 to c-1 do begin
list.append(TToken(FPostFix[i]).name);
end;
end;
function TjanSQLExpression2.InfixToPostFix(index: integer): boolean;
begin
result:=false;
if (index<0) or (index>=FInFix.count) then exit;
FPostFix.add(TToken(FInfix[index]).copy);
result:=true;
end;
function TjanSQLExpression2.InFixToStack(index: integer): boolean;
begin
result:=false;
if (index<0) or (index>=FInFix.count) then exit;
FStack.add(TToken(FInfix[index]).copy);
result:=true;
end;
function TjanSQLExpression2.Parse;
var
tokenizer:TjanSQLTokenizer;
begin
clear;
try
tokenizer:=TjanSQLTokenizer.create;
result:=Tokenizer.Tokenize(FSource,FInfix);
finally
tokenizer.free;
end;
end;
procedure TjanSQLExpression2.procAdd;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2 + v1);
end;
procedure TjanSQLExpression2.procAnd;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2 and v1);
end;
procedure TjanSQLExpression2.procDivide;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2/v1);
end;
procedure TjanSQLExpression2.procEq;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2=v1);
end;
procedure TjanSQLExpression2.procLOJ;
//To be used for left outer join (Sybase-like). Added by Zlatko Matić, 22.08.2011
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush((v2=v1) or (v2<>v1)); //To be checked
end;
procedure TjanSQLExpression2.procROJ;
//To be used for left outer join (Sybase-like). Added by Zlatko Matić, 22.08.2011
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush((v1=v2) or(v1<>v2)); //To be ckecked
end;
procedure TjanSQLExpression2.procGe;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2>=v1);
end;
procedure TjanSQLExpression2.procGt;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2>v1);
end;
procedure TjanSQLExpression2.procLe;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2<=v1);
end;
procedure TjanSQLExpression2.procLt;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2<v1);
end;
procedure TjanSQLExpression2.procMultiply;
begin
runpush(runpop* runpop);
end;
procedure TjanSQLExpression2.procNe;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2<>v1);
end;
procedure TjanSQLExpression2.procNumber;
begin
runpush(TToken(FPostFix[FPC]).value);
end;
procedure TjanSQLExpression2.procOr;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2 or v1);
end;
procedure TjanSQLExpression2.procString;
begin
runpush(TToken(FPostFix[FPC]).value);
end;
procedure TjanSQLExpression2.procSubtract;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(v2-v1);
end;
procedure TjanSQLExpression2.Setsource(const Value: string);
begin
Fsource := Value;
SL:=length(FSource);
parse;
ConvertInFixToPostFix;
end;
function TjanSQLExpression2.StackToPostFix: boolean;
var
tok:TToken;
begin
result:=false;
if FStack.count=0 then exit;
tok:=TToken(FStack[FStack.count-1]);
FPostFix.Add(tok);
FStack.Delete(FStack.count-1);
result:=true;
end;
procedure TjanSQLExpression2.runOperator(op:TTokenOperator);
begin
case op of
toAString: procString;
toNumber: procNumber;
toVariable: procVariable;
toEq: procEq;
toNe: procNe;
toGt: procGt;
toGe: procGe;
toLt: procLt;
toLe: procLe;
toAdd: procAdd;
toSubtract: procSubtract;
toMultiply: procMultiply;
toDivide: procDivide;
toAnd: procAnd;
toOr: procOr;
toNot: procNot;
toLike: procLike;
toSin: procSin;
toCos: procCos;
toSqr: procSqr;
toSqrt: procSqrt;
tosqlIN: procsqlIN;
toUPPER: procUPPER;
toLOWER: procLOWER;
toTRIM: procTRIM;
toSoundex: procSoundex;
toLeft:procLeft;
toRight:procRight;
toMid:procMid;
toLen:procLen;
toFix:procFix;
toCeil:procCeil;
toFloor:procFloor;
toAsNumber: procAsNumber;
toFormat: procFormat;
toYear: procYear;
toMonth: procMonth;
toDay: procDay;
toDateAdd: procDateAdd;
toEaster: procEaster;
toWeekNumber:procWeekNumber;
toIsNumeric: procIsNumeric;
toIsDate: procIsDate;
toReplace:procReplace;
toSubstr_After:procSubstr_After;
toSubstr_Before:procSubstr_Before;
end;
end;
function TjanSQLExpression2.Evaluate: variant;
var
i,c:integer;
op:TTokenOperator;
begin
result:=null;
c:=FPostFix.Count;
if c=0 then exit;
SP:=0;
for i:=0 to c-1 do begin
FPC:=i;
op:=TToken(FPostFix[i])._operator;
try
runoperator(op);
except
exit;
end;
end;
result:=runpop;
end;
function TjanSQLExpression2.runpop: variant;
begin
if SP=0 then
result:=null
else begin
dec(SP);
result:=Vstack[sp];
end;
end;
procedure TjanSQLExpression2.runpush(value: variant);
begin
VStack[SP]:=value;
inc(SP);
end;
procedure TjanSQLExpression2.procVariable;
var
VariableName:string;
VariableValue:Variant;
handled:boolean;
begin
VariableName:=TToken(FPostFix[FPC]).name;
if assigned(onGetVariable) then begin
handled:=false;
ongetvariable(self,VariableName,VariableValue,handled);
if not handled then
VariableValue:=VariableName;
end
else
VariableValue:=VariableName;
runpush(VariableValue);
end;
procedure TjanSQLExpression2.SetonGetVariable(const Value: TVariableEvent);
begin
FonGetVariable := Value;
end;
procedure TjanSQLExpression2.procSin;
var
v1:variant;
begin
v1:=runpop;
runpush(sin(v1));
end;
procedure TjanSQLExpression2.procNot;
var
v1:variant;
begin
v1:=runpop;
runpush(not(v1));
end;
procedure TjanSQLExpression2.procLike;
var
v1,v2:variant;
begin
v1:=runpop;
v2:=runpop;
runpush(IsLike(v1,v2));
end;
function TjanSQLExpression2.IsLike(v1, v2: variant): boolean;
var
p1,p2:integer;
s1,s2:string;
begin
s1:=v1;
s2:=v2;
result := false;
if posstr('%',s1)=0 then begin
result:=ansisametext(s1,s2)
end
else if (copy(s1,1,1)='%') and (copy(s1,length(s1),1)='%') then begin
s1:=copy(s1,2,length(s1)-2);
result:=postext(s1,s2)>0;
end
else if (copy(s1,1,1)='%') then begin
s1:=copy(s1,2,maxint);
p1:=postext(s1,s2);
result:=p1=length(s2)-length(s1)+1;
end
else if (copy(s1,length(s1),1)='%') then begin
s1:=copy(s1,1,length(s1)-1);
result:=postext(s1,s2)=1;
end;
end;
procedure TjanSQLExpression2.procsqlIn;
var
v1:variant;
se,s2:string;
b:boolean;
p1,p2,L,L2:integer;
begin
v1:=runpop;
s2:=v1;
se:=TToken(FPostFix[FPC]).expression;
runpush(postext('['+s2+']',se)>0);
end;
procedure TjanSQLExpression2.GetTokenList(list: TList; from, till: integer);
var
tok:TToken;
i:integer;
begin
Clear;
for i:=from to till do
FInFix.Add(TToken(list[i]).copy);
ConvertInFixToPostFix;
end;
procedure TjanSQLExpression2.procLOWER;
var
v1:variant;
s1:string;
begin
v1:=runpop;
s1:=v1;
runpush(lowercase(s1));
end;
procedure TjanSQLExpression2.procTRIM;
var
v1:variant;
s1:string;
begin
v1:=runpop;
s1:=v1;
runpush(trim(s1));
end;
procedure TjanSQLExpression2.procUPPER;
var
v1:variant;
s1:string;
begin
v1:=runpop;
s1:=v1;
runpush(uppercase(s1));
end;
procedure TjanSQLExpression2.procSoundex;
var
v1:variant;
s1:string;
begin
v1:=runpop;
s1:=v1;
runpush(soundex(s1));
end;
procedure TjanSQLExpression2.procAsNumber;
var
v1:variant;
s1:string;
d1:double;
begin
v1:=runpop;
try
s1:=v1;
v1:=strtofloat(s1);
except
v1:=0;
end;
s1:=v1;
runpush(v1);
end;
procedure TjanSQLExpression2.procLeft;
var
asize,atext:variant;
s1:string;
p:integer;
begin
asize:=runpop;
atext:=runpop;
s1:=atext;
p:=asize;
s1:=copy(s1,1,p);
runpush(s1);
end;
procedure TjanSQLExpression2.procRight;
var
asize,atext:variant;
s1:string;
p:integer;
begin
asize:=runpop;
atext:=runpop;
s1:=atext;
p:=asize;
s1:=copy(s1,length(s1)-p+1,p);
runpush(s1);
end;
procedure TjanSQLExpression2.procMid;
var
vcount,vfrom,vtext:variant;
s1:string;
p,c:integer;
begin
vcount:=runpop;
vfrom:=runpop;
vtext:=runpop;
s1:=vtext;
p:=vfrom;
c:=vcount;
s1:=copy(s1,p,c);
runpush(s1);
end;
procedure TjanSQLExpression2.procCos;
var
v1:variant;
begin
v1:=runpop;
runpush(cos(v1));
end;
procedure TjanSQLExpression2.procSqr;
var
v1:variant;
begin
v1:=runpop;
runpush(sqr(v1));
end;
procedure TjanSQLExpression2.procSqrt;
var
v1:variant;
begin
v1:=runpop;
runpush(sqrt(v1));
end;
procedure TjanSQLExpression2.procLen;
var
v1:variant;
s1:string;
begin
v1:=runpop;
s1:=v1;
runpush(length(s1));
end;
procedure TjanSQLExpression2.procFix;
var
vfloat,vdecimals:variant;
s1,s2:string;
d1:double;
begin
vdecimals:=runpop;
vfloat:=runpop;
s1:=vfloat;
s2:=vdecimals;
try
d1:=strtofloat(s1);
s1:=format('%.'+s2+'f',[d1]);
except
end;
runpush(s1);
end;
procedure TjanSQLExpression2.procCeil;
var
v1:variant;
begin
v1:=runpop;
runpush(ceil(v1));
end;
procedure TjanSQLExpression2.procFloor;
var
v1:variant;
begin
v1:=runpop;
runpush(floor(v1));
end;
procedure TjanSQLExpression2.procFormat;
var
vfloat,vformat:variant;
s1,s2:string;
d1:double;
i1:integer;
begin
vformat:=runpop;
vfloat:=runpop;
s1:=vfloat;
s2:=vformat;
if s2='' then begin
runpush(s1);
exit;
end;
if s2[length(s2)] in ['d','x'] then
try
i1:=strtoint(s1);
s1:=format(s2,[i1]);
except
end
else if s2[length(s2)] in ['s'] then
try
s1:=format(s2,[s1]);
except
end
else
try
d1:=strtofloat(s1);
s1:=format(s2,[d1]);
except
end;
runpush(s1);
end;
procedure TjanSQLExpression2.procDay;
{return the day part as integer from a 'yyyy-mm-dd' string}
var
v1:variant;
s1:string;
i1:integer;
begin
v1:=runpop;
s1:=v1;
i1:=strtointdef(copy(s1,9,2),0);
runpush(i1);
end;
procedure TjanSQLExpression2.procMonth;
{return the month part as integer from a 'yyyy-mm-dd' string}
var
v1:variant;
s1:string;
i1:integer;
begin
v1:=runpop;
s1:=v1;
i1:=strtointdef(copy(s1,6,2),0);
runpush(i1);
end;
procedure TjanSQLExpression2.procYear;
{return the year part as integer from a 'yyyy-mm-dd' string}
var
v1:variant;
s1:string;
i1:integer;
begin
v1:=runpop;
s1:=v1;
i1:=strtointdef(copy(s1,1,4),0);
runpush(i1);
end;
procedure TjanSQLExpression2.procDateAdd;
{add number of intervals to date}
var
vinterval,vnumber,vdate:variant;
ayear,amonth,aday:word;
adate:TDateTime;
sinterval,sdate:string;
inumber:integer;
begin
vdate:=runpop;
vnumber:=runpop;
vinterval:=runpop;
sinterval:=lowercase(vinterval);
inumber:=vnumber;
sdate:=vdate;
try
ayear:=strtoint(copy(sdate,1,4));
amonth:=strtoint(copy(sdate,6,2));
aday:=strtoint(copy(sdate,9,2));
adate:=encodedate(ayear,amonth,aday);
if sinterval='d' then
adate:=adate+1
else if sinterval='m' then
adate:=incmonth(adate,inumber)
else if sinterval='y' then
adate:=encodedate(ayear+inumber,amonth,aday)
else if sinterval='w' then
adate:=adate+7*inumber
else if sinterval='q' then
adate:=incmonth(adate,inumber*3);
decodedate(adate,ayear,amonth,aday);
sdate:=format('%.4d',[ayear])+'-'+format('%.2d',[amonth])+'-'+format('%.2d',[aday]);
except