forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SqlTxtRtns.pas
3247 lines (2997 loc) · 77.3 KB
/
SqlTxtRtns.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
{***************************************************************}
{ 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 SqlTxtRtns;
{$I FIBPlus.inc}
interface
uses
SysUtils,Classes,FIBPlatforms,StrUtil,DB{$IFDEF D6+}, Variants{$ENDIF} ;
type
TSQLKind=(skUnknown,skSelect,skUpdate,skInsert,skDelete,skExecuteProc,skDDL,skExecuteBlock,skUpdateOrInsert);
TParserState =(sNormal,sQuote,sComment,sFBComment,sQuoteSingle);
TOnScanSQLText = procedure(Position:integer;var StopScan:boolean) of object;
TChars = set of AnsiChar;
TSQLSection = (stUnknown,stSelect, stFields,
stUpdate,stInsert,stDelete,stExecute, stSet, stComment,
stFrom, stWhere, stGroupBy, stHaving,
stUnion, stPlan, stOrderBy, stForUpdate
);
TSQLSections = array of TSQLSection;
TSQLParser = class
private
FState :TParserState;
FCurPos :integer;
FResultPos:integer;
FBracketOpened:integer;
FSQLText:string;
FLen: integer;
FReserved : integer;
FReserved1: integer;
FScanInBrackets:boolean;
FFirstPos :integer;
FSQLKind :TSQLKind;
FCanParamsCheck:boolean;
FTables :TStrings;
FBracketOpenedBeforeScan:integer;
protected
procedure SetSQLText(const Value: string);
function IsClause(Position:integer;const Clause:string):boolean;
function GetSQLKind:TSQLKind;
procedure IsOrderBegin(Position:integer;var Yes:boolean);
procedure IsGroupBegin(Position:integer;var Yes:boolean);
procedure IsByBegin(Position:integer;var Yes:boolean);
procedure IsHaving(Position:integer;var Yes:boolean);
procedure IsMainOrderBegin(Position:integer;var Yes:boolean);
procedure IsMainGroupBegin(Position:integer;var Yes:boolean);
procedure IsFromBegin(Position:integer;var Yes:boolean);
procedure IsWhereBegin(Position:integer;var Yes:boolean);
procedure IsWhereEnd(Position:integer;var Yes:boolean);
procedure IsMainWhereBegin(Position:integer;var Yes:boolean);
procedure IsMainFromBegin(Position:integer;var Yes:boolean);
procedure IsFromEnd(Position:integer;var Yes:boolean);
procedure IsPlanBegin(Position:integer;var Yes:boolean);
procedure IsPlanEnd(Position:integer;var Yes:boolean);
procedure IsSetBegin(Position:integer;var Yes:boolean);
procedure IsIntoBegin(Position:integer;var Yes:boolean);
procedure IsUpdateBegin(Position:integer;var Yes:boolean);
function IsSelect(Position:integer):boolean;
function IsUpdate(Position:integer):boolean;
function IsDelete(Position:integer):boolean;
function IsInsert(Position:integer):boolean;
function IsExecute(Position:integer):boolean;
function IsExecuteProc(Position:integer):boolean;
function IsExecuteBlock(Position:integer):boolean;
function IsDDL:boolean;
public
constructor Create; overload;
constructor Create(const aSQLText:string); overload;
destructor Destroy; override;
procedure FillMainTables(WithSP:boolean=False);
procedure ScanText(FromPosition:integer;Proc:TOnScanSQLText);
function PosInSections(Position:integer):TSQLSections; overload;
function PosInSections(ScanFrom,Position:integer; var EndScan:integer):TSQLSections; overload;
function DispositionMainFrom:TPosition;
function MainFrom:string;
function ModifiedTables:string;
function OrderText(var StartPos,EndPos:integer):string;
function OrderClause:string;
function SetOrderClause(const Value:string):string;
function GetOrderInfo(NullsFirstDef:boolean = False):Variant;
function GroupByText(var StartPos,EndPos:integer):string;
function GroupByClause:string;
function SetGroupClause(const Value:string):string;
function GetFieldsClause:string;
function SetFieldsClause(const NewFields:string):string;
function WhereClause(N:integer;var StartPos,EndPos:integer):string;
function SetWhereClause(N:Integer;const Value:string):string;
function SetMainWhereClause(const Value:string):string;
function MainWhereClause(var StartPos,EndPos:integer):string;
function WhereCount:integer;
function MainWhereIndex:integer;
function AddToMainWhere(const NewClause:string;ForceCLRF:boolean = True):string;
function PlanCount:integer;
function PlanText(N:integer;var StartPos,EndPos:integer):string;
function MainPlanIndex:integer;
function MainPlanText(var StartPos,EndPos:integer):string;
function SetMainPlan(const Text:string):string;
function GetMainPlan:string;
function RecCountSQLText:string;
function GetAllTables(WithSP:boolean=False):TStrings;
function GetWord(Position:integer; var StartWord:integer ;PartOnly:boolean=False ):string;
function GetPrevWord(CurPosition:integer; var PosPrevWord:Integer):string;
public
property SQLText:string read FSQLText write SetSQLText;
property Len :integer read FLen write FLen;
property FirstPos :integer read FFirstPos;
property MainPlanClause:string read GetMainPlan ;
property SQLKind :TSQLKind read FSQLKind;
property CanParamsCheck:boolean read FCanParamsCheck;
end;
const
SpaceStr=' ';
ForceNewStr=#13#10+SpaceStr;
QuotMarks=#39;
function DispositionFrom(const SQLText:string):TPosition;
procedure AllSQLTables(SQLText:string;aTables:TStrings; WithSP:boolean=False
;WithAliases:boolean =False
);
procedure AllTables(const SQLText:string;aTables:TStrings; WithSP:boolean =False
;WithAliases:boolean =False
);
function TableByAlias(const SQLText,Alias:string):string;
function AliasForTable(const SQLText,TableName:string):string;
function FullFieldName(const SQLText,aFieldName:string):string;
function FieldNameFromSelect(const SQLText, FieldName: string):string;
function AddToWhereClause(const SQLText,NewClause:string;
ForceCLRF:boolean = True
):string;
function GetWhereClause(const SQLText:string;N:integer;var
StartPos,EndPos:integer
):string;
function WhereCount(const SQLText:string):integer;
function MainWhereIndex(const SQLText:string):integer;
function MainFrom(const SQLText:string):string;
function MainWhereClause(const SQLText:string):string;
function GetOrderInfo(const SQLText:string;NullsFirstDef:boolean = False):variant;
function OrderStringTxt(const SQLText:string;
var StartPos,EndPos:integer
):string;
function SetOrderClause(const SQLText,NewClause:string):string;
procedure SetOrderString(SQLText:TStrings;const OrderTxt:string);
//
function PrepareConstraint(Src:Tstrings):string;
function CountSelect(const SrcSQL:string):string;
function FieldsClause(const SrcSQL:string):string;
function SetFirstClause(const SrcSQL:string;FirstCount:integer):string;
function GetModifyTable(const SQLText:string;WithAlias:boolean=False):string;
function GetSQLKind(const SQLText:string):TSQLKind;
//
function GetLinkFieldName(const SQLText,ParamName: string): string;
function GetFieldByAlias(const SQLText,FieldName:string):string;
function IsWhereBeginPos(const SQLText:string;Position:integer):boolean;
function IsWhereEndPos(const SQLText:string;Position:integer):boolean;
function PosInSections(const SQLText:string;Position:integer):TSQLSections;
function InvertOrderClause(const OrderText:string):string;
function IsEquelSQLNames(const Name1,Name2:string):boolean;
function CutTableName(const SQLString:string;AliasPosition:integer=0):string;
function CutAlias(const SQLString:string;AliasPosition:integer=0):string;
function PosAlias(const SQLString:string):integer;
function FieldNameForSQL(const TableAlias,FieldName:string):string;
function FieldUsedInClause(const TableAlias,FieldName,Clause:string):boolean;
function ChangeToSQLDecimalSeparator(const Source:string):string;
{ procedure GetExportDataScript(DataSet:TDataSet; const TableName:string;OutPut:TStrings; UseFieldNames:boolean =True;
FieldList:string=''; FileName:string =''
); }
procedure GetExportDataScript(DataSet:TDataSet; const TableName:string;OutPut:TStrings; UseFieldNames:boolean =True;
FieldList:string=''
);
procedure GetExportDataScriptToFile(DataSet:TDataSet; const TableName:string;const FileName:string;
UseFieldNames:boolean =True; FieldList:string=''
);
function GenerateInsertRow(DataSet:TDataSet;TableName:string;var fNames:string; FieldList:TList=nil):string ;
const
CharsAfterClause =[' ',#13,#9,#10,#0,';','(','/','-','"','^'];
CharsBeforeClause=[' ',#10,')',#9,#13,'"'];
endLexem=['+',')','(','*','/','|',',','=','>','<','-','!','^','~',',',';'];
IBStdCharSetsCount=61;
IBStdCollationsCount=136;
UnknownStr='UNKNOWN';
IBStdCharacterSets:array [0..IBStdCharSetsCount-1] of string =
('NONE','OCTETS','ASCII','UNICODE_FSS','UTF8','SJIS_0208',
'EUCJ_0208',UnknownStr,UnknownStr,
'DOS737','DOS437','DOS850','DOS865','DOS860',
'DOS863', 'DOS775','DOS858','DOS862','DOS864','NEXT',UnknownStr,'ISO8859_1',
'ISO8859_2','ISO8859_3',
UnknownStr,UnknownStr,UnknownStr,UnknownStr,UnknownStr,UnknownStr,
UnknownStr,UnknownStr,UnknownStr,UnknownStr,
'ISO8859_4','ISO8859_5','ISO8859_6',
'ISO8859_7','ISO8859_8','ISO8859_9','ISO8859_13',
UnknownStr,UnknownStr,UnknownStr,
'KSC_5601','DOS852','DOS857','DOS861','DOS866',
'DOS869','CYRL','WIN1250','WIN1251','WIN1252','WIN1253',
'WIN1254','BIG_5','GB_2312','WIN1255','WIN1256','WIN1257'
);
function ParseMacroString(const MacroString:string; aMacroChar:Char;var DefValue:string):string ;
function PosClause(const Clause,SQLText:string):integer;
const
BeginWhere =' WHERE ';
implementation
uses StdFuncs;
function InternalIsClause( const EtalonClause:string; const Source:string;
Position:integer
):boolean;
// EtalonClause must be in UpperCase
var
Len:Integer;
LenEtalon:Byte;
pSource:PChar;
pEtalon:PChar;
pEtalon1:PChar;
begin
if (Position>1) and not CharInSet(Source[Position-1],CharsBeforeClause) then
begin
Result:=False;
Exit
end;
Len:=Length(Source);
LenEtalon:=Length(EtalonClause);
if Len-Position+1<LenEtalon then
Result:=False
else
begin
pSource:=Pointer(Source);
Inc(pSource,Position-1);
pEtalon:=Pointer(EtalonClause);
pEtalon1:=Pointer(EtalonClause);
Inc(pEtalon1,LenEtalon);
Result:=True;
while (pEtalon<>pEtalon1) do
begin
if pSource^<>pEtalon^ then
begin
if Byte(pSource^)-32<>Byte(pEtalon^) then
begin
Result:=False;
Break;
end;
end;
Inc(pSource);
Inc(pEtalon);
end;
if Result and (Len-Position>=LenEtalon) then
// Result:=CharInSet(Source[Position+LenEtalon] , CharsAfterClause)
Result:=Source[Position+LenEtalon] in CharsAfterClause
end
end;
procedure SkipCommentsAndBlanks(const SQLText:string;Len:integer; var Position:integer);
begin
while (Position<Len) and
CharInSet(SQLText[Position] , CharsAfterClause-['"']) do
begin
while (Position<=Len) and CharInSet(SQLText[Position] , (CharsAfterClause -['/','-','"']) ) do
Inc(Position);
if (Position<Len) and (SQLText[Position]='-') and (SQLText[Position+1]='-') then
begin
while (Position<=Len) and not CharInSet(SQLText[Position] , [#13,#10]) do
Inc(Position);
end;
if (Position<Len) and(SQLText[Position]='/') and (SQLText[Position+1]='*') then
begin
Inc(Position,2);
while (Position<Len) and not((SQLText[Position]='*') and (SQLText[Position+1]='/')) do
Inc(Position);
Inc(Position,2);
end;
end;
end;
function ParseMacroString(const MacroString:string; aMacroChar:Char;var DefValue:string):string ;
var
PosDef:integer;
begin
// For FIBPlus Macro
PosDef:=PosCh('%',MacroString);
if PosDef=0 then
begin
if MacroString[1]=aMacroChar then
Result:=MacroString
else
Result:=aMacroChar+MacroString;
end
else
begin
Result:=Copy(MacroString,1,PosDef-1);
DefValue:=Copy(MacroString,PosDef+1,Length(MacroString)-PosDef);
if MacroString[1] <> aMacroChar then Result := aMacroChar + Result
end;
end;
function PosClause(const Clause,SQLText:string):integer;
begin
Result:=PosExtCI(Clause,SQLText,CharsBeforeClause,CharsAfterClause,False);
end;
function IsEquelSQLNames(const Name1,Name2:string):boolean;
begin
if (Length(Name1)>0) and (Length(Name2)>0) then
begin
case Name1[1] of
'"':
if Name2[1]<>'"' then
begin
Result:=Copy(Name1,2,Length(Name1)-2)=UpperCase(Name2)
end
else
Result:=Name1=Name2
else // else case
if Name2[1]='"' then
Result:= ('"'+Name1+'"'=Name2) or
('"'+UpperCase(Name1)+'"'=Name2)
else
// Quotes don't exist
Result:=UpperCase(Name1)=UpperCase(Name2)
end
end
else
Result := True; // Unknown
end;
function RemoveSP(const FromStr:string; OnlyArguments:boolean = False ):string;
var pBrIn,pBrOut:integer;
cBrIn,cBrOut:integer;
l:integer;
begin
Result:=FromStr;
pBrIn:=PosCh('(',FromStr);
if pBrIn=0 then Exit;
l:=Length(FromStr);
while pBrIn >0 do
begin
pBrOut:=pBrIn+1;
cBrIn :=1; cBrOut:=0;
while (cBrOut<cBrIn) do
begin
if Result[pBrOut]=')' then Inc(cBrOut)
else
if Result[pBrOut]='(' then Inc(cBrIn);
Inc(pBrOut);
if pBrOut>l then Exit;
end;
if not OnlyArguments then
begin
while (pBrIn>1) and not CharInSet(Result[pBrIn] , [',']) do
Dec(pBrIn);
while (pBrOut<=Length(Result)) and not CharInSet(Result[pBrOut] , [',']) do
Inc(pBrOut);
end;
System.Delete(Result,pBrIn,pBrOut-pBrIn);
pBrIn:=PosCh('(',Result);
end;
end;
(*
function OpenBracketCount(const Txt:Ansistring;Len:integer):integer;
var j:integer;
State:TParserState;
begin
result:=0; State:=sNormal;
for j := 1 to Len do
begin
case Txt[j] of
'(':if (State=sNormal) then
Inc(Result);
')': if (State=sNormal) then
Dec(result);
'"': case State of
sNormal: State:=sQuote;
sQuote : State:=sNormal;
end;
'''':case State of
sNormal : State:=sQuoteSingle;
sQuoteSingle : State:=sNormal;
end;
'*': case State of
sNormal:
if (j>1) and (Txt[j-1]='/') then
State:=sComment;
sComment:
if (j<len) and (Txt[j+1]='/') then
State:=sNormal
end;
'-': case State of
sNormal:
if (j>1) and (Txt[j-1]='-') then
State:=sFBComment;
end;
#13,#10:
if State=sFBComment then
State:=sNormal
end;
end;
end;
*)
function RemoveComments(const SQLText:string):string;
var
Len:integer;
Position,RLen:integer;
State:TParserState;
procedure WriteResultChar;
begin
Inc(RLen);
Result[RLen]:=SQLText[Position];
end;
begin
Len:=Length(SQLText);
Position:=1; RLen:=0;
SetLength(Result,Len);
State:=sNormal;
while (Position<=Len)do
begin
case State of
sNormal:
begin
case SQLText[Position] of
'"':
begin
State:=sQuote;
WriteResultChar;
end;
'''':
begin
State:=sQuoteSingle;
WriteResultChar;
end;
'-':
begin
if (Position<Len) and (SQLText[Position+1]='-') then
State:=sFBComment
else
WriteResultChar;
end;
'/':
begin
if (Position<Len) and (SQLText[Position+1]='*') then
State:=sComment
else
WriteResultChar;
end;
else
WriteResultChar;
end;
end;
sQuote:
begin
WriteResultChar;
if (SQLText[Position]='"') then
State:=sNormal;
end;
sQuoteSingle:
begin
WriteResultChar;
if (SQLText[Position]='''') then
State:=sNormal;
end;
sComment:
if (SQLText[Position]='*') then
begin
if (Position<Len) and (SQLText[Position+1]='/') then
begin
State:=sNormal;
Inc(Position);
end;
end;
sFBComment:
if (SQLText[Position]=#13) then
begin
State:=sNormal;
if (Position<Len) and (SQLText[Position+1]=#10) then
Inc(Position);
end;
end;
Inc(Position);
end;
SetLength(Result,RLen);
end;
function RemoveJoins(const FromStr:string):string;
var pON,pComa,pJOIN:integer;
tmpStr:string;
begin
Result:=FromStr;
pJOIN:=PosClause('JOIN',Result);
if pJOIN=0 then
Exit;
Result:=Copy(Result,1,pJOIN-1)+', '+Copy(Result,PJOIN+5,MaxInt);
Result:=ReplaceCIStr(Result, ' LEFT ' , ' ',False);
Result:=ReplaceCIStr(Result, ' RIGHT ', ' ',False);
Result:=ReplaceCIStr(Result, ' FULL ' , ' ',False);
Result:=ReplaceCIStr(Result, ' INNER ', ' ',False);
Result:=ReplaceCIStr(Result, ' OUTER ', ' ',False);
Result:=ReplaceCIStr(Result, ' JOIN ', ' , ',False);
pON:=PosClause('ON',Result);
tmpStr:='';
while pOn >0 do
begin
tmpStr:=Copy(Result,pOn+2,MaxInt);
pComa:=Pos(',',tmpStr);
SetLength(Result,pOn-1);
if pComa>0 then
begin
Result:=Result+Copy(tmpStr,pComa,MaxInt)
end;
pON:=PosClause('ON',Result);
end;
end;
function DispositionFrom(const SQLText:string):TPosition;
var
Parser:TSQLParser;
begin
Parser:=TSQLParser.Create(SQLText);
try
Result := Parser.DispositionMainFrom;
finally
Parser.Free
end
end;
function CountSelect(const SrcSQL:string):string;
var
Parser:TSQLParser;
begin
Parser:=TSQLParser.Create(SrcSQL);
try
Result:=Parser.RecCountSQLText;
finally
Parser.Free
end;
end;
function FieldsClause(const SrcSQL:string):string;
var
Parser:TSQLParser;
begin
Parser:=TSQLParser.Create(SrcSQL);
try
Result:=Parser.GetFieldsClause;
finally
Parser.Free
end;
end;
function SetFirstClause(const SrcSQL:string;FirstCount:integer):string;
var
Parser:TSQLParser;
pb:integer;
begin
// IF FIRST DON'T EXIST
Parser:=TSQLParser.Create(SrcSQL);
Result:=SrcSQL;
try
if Parser.SQLKind=skSelect then
if PosClause('FIRST',srcSQL)=0 then
begin
pb:=Parser.FFirstPos+6;
Insert(' FIRST '+IntToStr(FirstCount)+' ',Result,pb)
end
else
begin
//?
end;
finally
Parser.Free
end;
end;
function GetModifyTable(const SQLText:string;WithAlias:boolean=False):string;
var
StartCut,EndCut,L:integer;
Parser:TSQLParser;
begin
Parser:=TSQLParser.Create(SQLText);
try
Result:=FastTrim(Parser.ModifiedTables);
L:=Length(Result);
if not WithAlias and (L>0) then
begin
StartCut:=1;
if Result[StartCut]='"' then
begin
Inc(StartCut);
EndCut:=StartCut+1;
while EndCut<=L do
begin
if Result[EndCut]='"' then
begin
Dec(EndCut); Break
end
else
Inc(EndCut);
end;
Result:=FastCopy( Result,StartCut,EndCut-StartCut+1);
end
else
begin
EndCut:=StartCut;
while EndCut<L do
begin
if CharInSet(Result[EndCut+1], CharsAfterClause-['"']) then
begin
Break
end
else
Inc(EndCut);
end;
if L>EndCut then
SetLength(Result,EndCut);
Result:=FastUpperCase(Result);
end;
end;
finally
Parser.Free
end;
end;
function PosAlias(const SQLString:string):integer;
var
i:integer;
InQuote1,InQuote2:boolean;
begin
Result:=0;
InQuote1:=False;
InQuote2:=False;
for i:=1 to Length(SQLString) do
begin
case SQLString[i] of
' ',#10,#13,#9:
if not InQuote1 and not InQuote2 then
begin
Result:=i;
Break;
end;
'"':
if not InQuote2 then
InQuote1:=not InQuote1;
'''':
if not InQuote1 then
InQuote2:=not InQuote2;
end;
end;
if Result>0 then
while (Result<Length(SQLString)) and CharInSet(SQLString[Result] , CharsAfterClause-['"']) do
Inc(Result);
end;
function FieldUsedInClause(const TableAlias,FieldName,Clause:string):boolean;
begin
{ TODO : FieldUsedInClause }
Result:=False;
end;
function FieldNameForSQL(const TableAlias,FieldName:string):string;
begin
if Length(TableAlias) > 0 then
Result:=FormatIdentifier(3,TableAlias)+'.'+
FormatIdentifier(3,FieldName)
else
Result:=FormatIdentifier(3,FieldName);
end;
function CutTableName(const SQLString:string; AliasPosition:integer=0):string;
var
p:integer;
begin
if AliasPosition=0 then
p:=PosAlias(SQLString)
else
p:=AliasPosition;
if p=0 then
Result:=Trim(SQLString)
else
Result:=Trim(Copy(SQLString,1,p-1));
end;
function CutAlias(const SQLString:string; AliasPosition:integer=0):string;
var
p:integer;
begin
if AliasPosition=0 then
p:=PosAlias(SQLString)
else
p:=AliasPosition;
if p+3<=Length(SQLString) then
if CharInSet(SQLString[p] , ['A','a']) and CharInSet(SQLString[p+1] , ['S','s']) and
CharInSet(SQLString[p] , CharsAfterClause-['"'])
then
Inc(p,3);
if p=0 then
Result:=Trim(SQLString)
else
Result:=Trim(Copy(SQLString,p,MaxInt));
end;
procedure RemoveAliases(ListOfTables:TStrings);
var
i:integer;
begin
for i := 0 to ListOfTables.Count - 1 do
begin
ListOfTables[i]:=CutTableName(ListOfTables[i]);
end
end;
procedure AllTables(const SQLText:string;aTables:TStrings; WithSP:boolean =False
;WithAliases:boolean =False
);
var
Parser:TSQLParser;
begin
Parser:=TSQLParser.Create(SQLText);
with Parser do
try
FillMainTables(WithSP);
aTables.Assign(FTables);
if not WithAliases then
RemoveAliases(aTables);
finally
Parser.Free
end;
end;
procedure AllSQLTables(SQLText:string;aTables:TStrings; WithSP:boolean=False
;WithAliases:boolean =False
);
var
Parser:TSQLParser;
begin
Parser:=TSQLParser.Create(SQLText);
with Parser do
try
GetAllTables(WithSP);
aTables.Assign(FTables);
if not WithAliases then
RemoveAliases(aTables);
finally
Parser.Free
end;
end;
function AliasForTable(const SQLText,TableName:string):string;
var ts:TStrings;
i,p:integer;
tmpStr:string;
begin
Result:=TableName;
ts:=TStringList.Create;
try
AllTables(SQLText,ts,True,True);
for i:=0 to Pred(ts.Count) do
begin
p:=PosAlias(ts[i]);
if p>0 then
tmpStr:=CutTableName(ts[i],p)
else
tmpStr:=Trim(ts[i]);
if IsEquelSQLNames(tmpStr,TableName) then
// if not Found then
begin
// Is first table including
Result:=CutAlias(ts[i],p);
Exit;
end
{ else
begin
// Is second table including
// Table have 2 or more aliases
Result:=TableName;
Exit;
end}
end;
finally
ts.Free
end;
end;
function TableByAlias(const SQLText,Alias:string):string;
var ts:TStrings;
i,p:integer;
tmpStr:string;
tmpStr1:string;
begin
Result:='';
ts:=TStringList.Create;
tmpStr1:='';
try
AllTables(SQLText,ts, True,True);
for i:=0 to Pred(ts.Count) do
begin
p:=PosAlias(ts[i]);
if p>0 then
tmpStr:=CutAlias(ts[i],p)
else
tmpStr:=Trim(ts[i]);
if IsEquelSQLNames(tmpStr,Alias) then
begin
Result:=CutTableName(ts[i],p);
Break;
end
else
if Length(tmpStr1) = 0 then
begin
tmpStr1:=CutTableName(ts[i],p);
if not IsEquelSQLNames(Alias,tmpStr1) then
tmpStr1:='';
end;
end;
if (Length(Result) = 0) and (Length(tmpStr1) > 0) then
Result:=tmpStr1
finally
ts.Free
end;
end;
function FullFieldName(const SQLText,aFieldName:string):string;
var
p:integer;
TableName:string;
FieldName:string;
begin
p:=Pos('.',aFieldName);
if p=0 then
Result:=aFieldName
else
begin
TableName:= TableByAlias(SQLText,Copy(aFieldName,1,p-1));
if (Length(TableName)=0) then
Result:=aFieldName
else
if (Length(TableName)=0) then
Result:=aFieldName
else
begin
FieldName:=Trim(Copy(aFieldName,p+1,MaxInt));
if (Length(FieldName) > 0) then
if FieldName[1]='"' then
FieldName:=Copy(FieldName,2,Length(FieldName)-2)
else
FieldName:=UpperCase(FieldName);
if (TableName[1]<>'"') then
Result:=UpperCase(TableName)+'.'+FieldName
else
Result:=Copy(TableName,2,Length(TableName)-2)+'.'+FieldName
end;
end;
end;
function FieldNameFromSelect(const SQLText, FieldName: string):string;
var
p,pb,pe : integer;
begin
if Length(FieldName) = 0 then
Result := FieldName
else
if PosCh('.', FieldName) > 0 then
Result := FieldName
else
begin
if FieldName[1]='"' then
p:=PosInSubstrExt(FieldName, SQLText,
1,Length(SQLText),
endLexem+CharsAfterClause+['.'],endLexem+CharsAfterClause
)
else
p:=PosInSubstrCIExt(FieldName, SQLText,
1,Length(SQLText),
endLexem+CharsAfterClause+['.'],endLexem+CharsAfterClause
);
if p > 0 then
begin
pb:=p-1;
while (pb > 0) and CharInSet(SQLText[pb] , [' ',#10,#9,#13]) do
Dec(pb);
if (pb>0) and (SQLText[pb]='.') then
begin
pe:=pb-1;
while (pe > 0) and CharInSet(SQLText[pe] , [' ',#10,#9,#13]) do
Dec(pe);
if pe>0 then
begin
pb:=pe;
while (pb > 0) and not CharInSet(SQLText[pb] , ['(',' ',#10,#9,#13]) do
Dec(pb);
Inc(pb);
Result :=FastCopy(SQLText, pb, pe-pb+1)+
'.'+FastCopy(SQLText, p, Length(FieldName));
end
else
Result := FastCopy(SQLText, p, Length(FieldName));
end
else
Result := FastCopy(SQLText, p, Length(FieldName));
end
else
Result := FieldName;
end;
end;
function MainWhereIndex(const SQLText:string):integer;
var
Parser:TSQLParser;
begin
Parser:=TSQLParser.Create(SQLText);
try
Result:=Parser.MainWhereIndex;
finally
Parser.Free
end;
end;
function MainFrom(const SQLText:string):string;
var
Parser:TSQLParser;
begin
Parser:=TSQLParser.Create(SQLText);
try
Result:=Parser.MainFrom;
finally
Parser.Free
end;