-
Notifications
You must be signed in to change notification settings - Fork 58
/
DBParsers.pas
571 lines (543 loc) · 19 KB
/
DBParsers.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
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:[email protected] }
{ }
{ Copyright (c) 1998-2007 Devrace Ltd. }
{ Written by Serge Buzadzhy ([email protected]) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit DBParsers;
interface
{$I FIBPlus.inc}
uses SysUtils,Classes,DB,DBCommon,DbConsts,FIBPlatforms
{$IFDEF D6+} ,Variants,FMTBcd{$ENDIF}
;
type
TStrToDateFmt = function (const ADate,Fmt:string):TDateTime;
TMatchesMask = function (const S1, Mask: string): Boolean;
TExpressionParser = class(TExprParser)
private
FExpressionText :string;
FDataSet:TDataSet;
FStrToDateFmt :TStrToDateFmt;
FMatchesMask :TMatchesMask;
FFilteredFields :TStringList;
function FieldByName(const FieldName:string):TField;
function VarResult :Boolean;
public
constructor Create(DataSet: TDataSet; const Text: string;
Options: TFilterOptions; ParserOptions: TParserOptions;
const FieldName: string; DepFields: TBits; FieldMap: TFieldMap;
aStrToDateFmt:TStrToDateFmt=nil;
aMatchesMask :TMatchesMask=nil
);
destructor Destroy; override;
procedure ResetFields;
function BooleanResult: Boolean;
property StrToDateFmt:TStrToDateFmt read FStrToDateFmt write FStrToDateFmt;
property MatchesMask :TMatchesMask read FMatchesMask write FMatchesMask;
property ExpressionText :string read FExpressionText;
end;
implementation
{ TExpressionParser}
uses StrUtil {$IFDEF D6+} ,StdFuncs {$ENDIF};
constructor TExpressionParser.Create(DataSet: TDataSet; const Text: string;
Options: TFilterOptions; ParserOptions: TParserOptions;
const FieldName: string; DepFields: TBits; FieldMap: TFieldMap;
aStrToDateFmt:TStrToDateFmt=nil;
aMatchesMask :TMatchesMask=nil
);
begin
try
inherited Create(DataSet,Text, Options,ParserOptions, FieldName,DepFields,FieldMap);
except
on e: Exception do
begin
e.Message:='Can''t parse Filter for: '+CLRF+e.Message;
raise;
end;
end;
FExpressionText :=Text;
FDataSet:=DataSet;
FStrToDateFmt:=aStrToDateFmt;
FMatchesMask :=aMatchesMask;
FFilteredFields :=TStringList.Create;
with FFilteredFields do
begin
Sorted:=true;
Duplicates:=dupIgnore
end; // with
end;
destructor TExpressionParser.Destroy;
begin
FFilteredFields.Free;
inherited;
end;
function TExpressionParser.FieldByName(const FieldName:string):TField;
var
i:integer;
begin
if FFilteredFields.Find(FieldName,i) then
Result:=TField(FFilteredFields.Objects[i])
else
begin
Result:=FDataSet.FieldByName(FieldName);
FFilteredFields.AddObject(FieldName,Result)
end;
end;
{$WARNINGS OFF}
function TExpressionParser.VarResult :Boolean;
var
iLiteralStart: Word;
function ParseNode(pfdStart, pfd: PAnsiChar): Variant;
var
I, Z,AD: Integer;
Year, Mon, Day, Hour, Min, Sec, MSec: Word;
iClass: NODEClass;
iOperator: TCANOperator;
pArg1,pArg2: PAnsiChar;
Arg1,Arg2: Variant;
FieldName: String;
DataType: TFieldType;
DataOfs: integer;
ts: TTimeStamp;
Cur: Currency;
PartLength: Word;
IgnoreCase: Word;
S1,S2: Variant;
S :string;
{$IFDEF D2009+}
us:UnicodeString;
{$ENDIF}
null1,null2:boolean;
p,p1:integer;
type
PWordBool = ^WordBool;
begin
iClass := NODEClass(PInteger(@pfd[0])^);
iOperator := TCANOperator(PInteger(@pfd[4])^);
Inc(pfd, CANHDRSIZE);
case iClass of
nodeFIELD:
case iOperator of
coFIELD2:
begin
DataOfs := iLiteralStart + PWord(@pfd[2])^;
pArg1 := pfdStart;
Inc(pArg1, DataOfs);
FieldName := string(pArg1);
with FieldByName(FieldName) do
{$IFNDEF D6+}
Result := FieldByName(FieldName).Value
{$ELSE}
case DataType of
ftBCD:
if IsNull then
Result:=Null
else
Result:=FieldByName(FieldName).Value
else
Result := FieldByName(FieldName).Value
end
{$ENDIF}
end;
else
DatabaseError(SExprIncorrect);
end;
nodeCONST:
case iOperator of
coCONST2:
begin
{$IFDEF D2007+}
if PWord(@pfd[0])^ = $1007 then
DataType := ftWideString
else
{$ENDIF}
DataType := TFieldType(PWord(@pfd[0])^);
DataOfs := iLiteralStart + PWord(@pfd[4])^;
pArg1 := pfdStart;
Inc(pArg1, DataOfs);
case DataType of
ftSmallInt, ftWord:
Result := PWord(pArg1)^;
ftInteger, ftAutoInc:
Result := PInteger(pArg1)^;
ftFloat, ftCurrency:
Result := PDouble(pArg1)^;
ftString, ftFixedChar:
Result := string(pArg1);
{$IFDEF D2009+}
ftWideString:
begin
SetLength(us,PWord(pArg1)^ div 2);
Move(pArg1[2],us[1],PWord(pArg1)^) ;
Result:=us
end;
{$ENDIF}
ftDate:
begin
ts.Date := PInteger(pArg1)^;
ts.Time := 0;
{$IFDEF D6+}
Result := HookTimeStampToDateTime(ts);
{$ELSE}
Result := TimeStampToDateTime(ts);
{$ENDIF}
end;
ftTime:
begin
ts.Date := 0;
ts.Time := PInteger(pArg1)^;;
{$IFDEF D6+}
Result := HookTimeStampToDateTime(ts);
{$ELSE}
Result := TimeStampToDateTime(ts);
{$ENDIF}
end;
ftDateTime:
begin
ts :=MSecsToTimeStamp(PDouble(pArg1)^);
{$IFDEF D6+}
Result := HookTimeStampToDateTime(ts);
{$ELSE}
Result := TimeStampToDateTime(ts);
{$ENDIF}
end;
ftBoolean:
Result := PWordBool(pArg1)^;
ftBCD:
begin
BCDToCurr(PBCD(pArg1)^, Cur);
Result := Cur;
end;
{$IFDEF D6+}
ftLargeInt:
Result := PInt64(pArg1)^;
{$ENDIF}
else
DatabaseError(SExprIncorrect);
end;
end;
end;
nodeUNARY:
begin
pArg1 := pfdStart;
Inc(pArg1, CANEXPRSIZE + PWord(@pfd[0])^);
case iOperator of
coISBLANK,coNOTBLANK:
begin
Arg1 := ParseNode(pfdStart, pArg1);
Result := VarIsEmpty(Arg1) or VarIsNull(Arg1);
if iOperator = coNOTBLANK then
Result := not Result;
end;
coNOT:
Result := not WordBool(ParseNode(pfdStart, pArg1));
coMINUS:
Result := - ParseNode(pfdStart, pArg1);
coUPPER:
Result := AnsiUpperCase(VarToStr(ParseNode(pfdStart, pArg1)));
coLOWER:
Result := AnsiLowerCase(VarToStr(ParseNode(pfdStart, pArg1)));
end;
end;
nodeBINARY:
begin
pArg1 := pfdStart;
Inc(pArg1, CANEXPRSIZE + PWord(@pfd[0])^);
pArg2 := pfdStart;
Inc(pArg2, CANEXPRSIZE + PWord(@pfd[2])^);
case iOperator of
coAssign:Result := ParseNode(pfdStart, pArg1) ;
coEQ:
Result := ParseNode(pfdStart, pArg1) = ParseNode(pfdStart, pArg2);
coNE:
Result := ParseNode(pfdStart, pArg1) <> ParseNode(pfdStart, pArg2);
coGT:
Result := ParseNode(pfdStart, pArg1) > ParseNode(pfdStart, pArg2);
coGE:
Result := ParseNode(pfdStart, pArg1) >= ParseNode(pfdStart, pArg2);
coLT:
Result := ParseNode(pfdStart, pArg1) < ParseNode(pfdStart, pArg2);
coLE:
Result := ParseNode(pfdStart, pArg1) <= ParseNode(pfdStart, pArg2);
coOR:
Result := WordBool(ParseNode(pfdStart, pArg1)) or WordBool(ParseNode(pfdStart, pArg2));
coAND:
Result := WordBool(ParseNode(pfdStart, pArg1)) and WordBool(ParseNode(pfdStart, pArg2));
coADD:
Result := ParseNode(pfdStart, pArg1) + ParseNode(pfdStart, pArg2);
coSUB:
Result := ParseNode(pfdStart, pArg1) - ParseNode(pfdStart, pArg2);
coMUL:
Result := ParseNode(pfdStart, pArg1) * ParseNode(pfdStart,pArg2);
coDIV:
Result := ParseNode(pfdStart,pArg1) / ParseNode(pfdStart,pArg2);
coMOD,coREM:
Result := ParseNode(pfdStart,pArg1) mod ParseNode(pfdStart,pArg2);
coIN:
begin
Arg1 := ParseNode(PfdStart, pArg1);
Arg2 := ParseNode(PfdStart, pArg2);
if VarIsArray(Arg2) then
begin
Result := False;
AD:=VarArrayHighBound(Arg2, 1);
for I:=0 to AD do
begin
if VarIsEmpty(Arg2[I]) then break;
Result := (Arg1 = Arg2[I]);
if Result then break;
end;
end
else
Result := (Arg1 = Arg2);
end;
coLike:
if Assigned(FMatchesMask) then
Result :=
FMatchesMask(
VarToStr(ParseNode(pfdStart, pArg1)),
VarToStr(ParseNode(pfdStart, pArg2))
)
else
DatabaseError(SExprIncorrect);
else
DatabaseError(SExprIncorrect);
end;
end;
nodeCOMPARE:
begin
IgnoreCase := PWord(@pfd[0])^;
PartLength := PWord(@pfd[2])^;
pArg1 := pfdStart + CANEXPRSIZE + PWord(@pfd[4])^;
pArg2 := pfdStart + CANEXPRSIZE + PWord(@pfd[6])^;
S1 := ParseNode(pfdStart, pArg1);
S2 := ParseNode(pfdStart, pArg2);
null1:=VarIsNull(S1);
null2:=VarIsNull(S2);
if (null1 <> null2) then
begin
Result :=iOperator=coNE;
Exit;
end
else
if null1 then
begin
Result :=iOperator<>coNE;
Exit;
end ;
if IgnoreCase <> 0 then
begin
S1 := AnsiUpperCase(S1);
S2 := AnsiUpperCase(S2);
end;
if (PartLength > 0) and (iOperator<>coLIKE) then
begin
S1 := Copy(S1, 1, PartLength);
S2 := Copy(S2, 1, PartLength);
end;
case iOperator of
coEQ:
Result := S1 = S2;
coNE:
Result := S1 <> S2;
coLIKE:
if Assigned(FMatchesMask) then
Result := FMatchesMask(S1, S2)
else
DatabaseError(SExprIncorrect)
else
DatabaseError(SExprIncorrect);
end;
end;
nodeFUNC:
case iOperator of
coFUNC2:
begin
pArg1 := pfdStart;
Inc(pArg1, iLiteralStart + PWord(@pfd[0])^);
S :=AnsiUpperCase(pArg1);
if Length(S) = 0 then
DatabaseErrorFmt(SExprExpected, [S]);
pArg2 := pfdStart;
Inc(pArg2, CANEXPRSIZE + PWord(@pfd[2])^);
case S[1] of //
'D':if S = 'DAY' then
begin
DecodeDate(VarToDateTime(ParseNode(pfdStart, pArg2)), Year, Mon, Day);
Result := Day;
end
else
if S = 'DATE' then
begin
Result := ParseNode(pfdStart, pArg2);
if VarIsArray(Result) then
if Assigned(FStrToDateFmt) then
Result := FStrToDateFmt(VarToStr(Result[1]), VarToStr(Result[0]))
else
DatabaseError(SExprIncorrect)
else
Result := Integer(Trunc(VarToDateTime(Result)));
end
else
DatabaseErrorFmt(SExprExpected, [S]);
'G': if S = 'GETDATE' then Result := Now
else
DatabaseErrorFmt(SExprExpected, [S]);
'H':if S = 'HOUR' then
begin
DecodeTime(VarToDateTime(ParseNode(pfdStart, pArg2)), Hour, Min, Sec, MSec);
Result := Hour;
end
else
DatabaseErrorFmt(SExprExpected, [S]);
'M':if S = 'MONTH' then
begin
DecodeDate(VarToDateTime(ParseNode(pfdStart, pArg2)), Year, Mon, Day);
Result := Mon;
end
else
if S = 'MINUTE' then
begin
DecodeTime(VarToDateTime(ParseNode(pfdStart, pArg2)), Hour, Min, Sec, MSec);
Result := Min;
end
else
DatabaseErrorFmt(SExprExpected, [S]);
'U': if S = 'UPPER' then
Result := AnsiUpperCase(VarToStr(ParseNode(pfdStart, pArg2)))
else
DatabaseErrorFmt(SExprExpected, [S]);
'L': if S = 'LOWER' then
Result := AnsiLowerCase(VarToStr(ParseNode(pfdStart, pArg2)))
else
DatabaseErrorFmt(SExprExpected, [S]);
'Y': if S = 'YEAR' then
begin
DecodeDate(VarToDateTime(ParseNode(pfdStart, pArg2)), Year, Mon, Day);
Result := Year;
end
else
DatabaseErrorFmt(SExprExpected, [S]);
'S': if S = 'SUBSTRING' then
begin
Result := ParseNode(pfdStart, pArg2);
if VarType(Result[1]) in [varSmallint,varInteger,varDouble,varSingle] then
begin
p :=Integer(Result[1]);
p1:=Integer(Result[2]);
end
else
begin
S:=VarToStr(Result[1]);
p:=PosCh(',',S);
p1:=0;
if p>0 then
begin
p1:=StrToInt(FastCopy(S,p+1,1000));
p :=StrToInt(FastCopy(S,1,p-1));
end
else
DatabaseErrorFmt(SExprExpected, [S]);
end;
Result := FastCopy(VarToStr(Result[0]), p, p1);
end
else
if S = 'SECOND' then
begin
DecodeTime(VarToDateTime(ParseNode(pfdStart, pArg2)), Hour, Min, Sec, MSec);
Result := Sec;
end
else
DatabaseErrorFmt(SExprExpected, [S]);
'T':
case Length(S) of
4: if S = 'TRIM' then
Result := FastTrim(VarToStr(ParseNode(pfdStart, pArg2)))
else
DatabaseErrorFmt(SExprExpected, [S]);
8: if S = 'TRIMLEFT' then
Result := TrimLeft(VarToStr(ParseNode(pfdStart, pArg2)))
else
DatabaseErrorFmt(SExprExpected, [S]);
9: if S = 'TRIMRIGHT' then
Result := TrimRight(VarToStr(ParseNode(pfdStart, pArg2)))
else
DatabaseErrorFmt(SExprExpected, [S]);
end;
else
DatabaseErrorFmt(SExprExpected, [S]);
end
end
else
DatabaseError(SExprIncorrect);
end;
nodeLISTELEM:
case iOperator of
coLISTELEM2:
begin
Result := VarArrayCreate ([0, 50], VarVariant); // Create VarArray for ListElements Values
pArg1 := pfdStart;
Inc(pArg1, CANEXPRSIZE + PWord(@pfd[0])^);
I := 0;
repeat
Arg1 := ParseNode(PfdStart, pArg1);
if VarIsArray(Arg1) then
begin
Z:=0;
while not VarIsEmpty(Arg1[Z]) do
begin
Result[I] := Arg1[Z];
Inc(I); Inc(Z);
end;
end
else
begin
Result[I] := Arg1;
Inc(I);
end;
pArg1 := pfdStart;
Inc(pArg1, CANEXPRSIZE + PWord(@pfd[I*2])^);
until NODEClass(PInteger(@pArg1[0])^) <> NodeListElem;
if I<2 then
Result := VarAsType(Result[0], varString);
end;
else
DatabaseError(SExprIncorrect);
end;
end;
end;
var
pfdStart, pfd: PAnsiChar;
begin
pfdStart := Addr(FilterData[0]);
pfd := pfdStart;
iLiteralStart := PWord(@pfd[8])^;
Inc(pfd, 10);
Result := ParseNode(pfdStart, pfd);
end;
{$WARNINGS ON}
function TExpressionParser.BooleanResult: Boolean;
var
V:Variant;
begin
V:=VarResult;
Result :=WordBool(V)
end;
procedure TExpressionParser.ResetFields;
begin
FFilteredFields.Clear
end;
end.