forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pFIBScripter.pas
3095 lines (2869 loc) · 81.8 KB
/
pFIBScripter.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-2013 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 pFIBScripter;
interface
{$I FIBPlus.inc}
{$IFDEF D6+}
{$A2}
{$ENDIF}
uses SysUtils,Classes{$IFDEF D6+},Types{$ENDIF}
,FIBPlatforms,pFIBDatabase,pFIBQuery,FIBQuery,fib,pFIBInterfaces
;
//{$DEFINE BEZBAZY}
type
TStmtType = (sUnknown,sInvalid,sDML,
sConnect, sDisconnect, sReconnect,
sCreateDatabase,sDropDatabase,
sCommit, sRollBack,
sCreate, sAlter,sRecreate, sDrop, sSet, sSetGenerator,sSetStatistics,sDescribe,sDeclare,
sComment,sGrant,sRunFromFile,sBatch {Temp Type},sBatchStart,sBatchExecute, sExecute,
sInsert,sReinsert , sDirective
);
TObjectType =
(otNone,otDatabase,otDomain,otTable,otView,otProcedure,otTrigger,
otUDF,otException,otGenerator,otIndex,
otConstraint, otFilter,otField,otParameter,otRole,otBlock,otUser ,otPackage,otPackageBody,otFunction
);
TStmtCoord=record
X: Word;
Y: Integer;
end;
PStmtCoord=^TStmtCoord;
TValidationInfo=record
smdEnd :TStmtCoord;
Active :boolean;
BeginExist:boolean;
BegCount:integer;
end;
PValidationInfo=^TValidationInfo;
TStatementDesc = record
smdBegin:TStmtCoord;
smdEnd :TStmtCoord;
smtType :TStmtType;
objType :TObjectType;
objName :string;
DirectiveNum:integer ;
DirectiveElse:boolean;
end;
PStatementDesc=^TStatementDesc;
TScriptMap= array of TStatementDesc;
TOnParseStmt = procedure(Sender: TObject;StatementNo: Integer; Coord:TStatementDesc;
SQLText:TStrings) of object;
TDirectiveState = (dsUnknown,dsTrue,dsFalse);
TDirectiveDesc = record
dBegin:TStmtCoord;
dConditionClose:TStmtCoord;
dElse :TStmtCoord;
dEnd :TStmtCoord;
dState: TDirectiveState;
OwnerDirectiveNum:integer;
OwnerDirectiveElse:boolean;
end;
PDirectiveDesc=^TDirectiveDesc;
TDirectivesMap= array of TDirectiveDesc;
PDirectivesMap= array of PDirectiveDesc;
TpFIBScriptParser = class
private
FScript:TStrings;
FTemp :TStrings;
FCurDBName:string;
FMakeConnectInScript:boolean;
FHaveDMLStatements :boolean;
FHaveUnknownStatements:boolean;
FValidationInfo:TValidationInfo;
function NextTokenPos(TokenPos:TStmtCoord; EndCoord:TStmtCoord ):TStmtCoord;
function GetToken(TokenPos:TStmtCoord;IgnoreQuote:boolean=True):string;
procedure SearchObjectType(var stmtDesc:TStatementDesc;var BegSearch:TStmtCoord;ForGrant:boolean=False);
function ValidateStatement(var stmtDesc:TStatementDesc;const NeedCheckCanEnd:boolean ):boolean;
function StmtTypeNameToType(const TestString:string; Position:integer):TStmtType;
function TypeNameToObjectType(const TestString:string; Position:integer):TObjectType;
public
constructor Create;
destructor Destroy; override;
procedure ParseScript(AScript:TStrings;var Terminator:string;
var FScriptMap:TScriptMap; var FCDMap: TDirectivesMap;IgnoreLastTerm:boolean=True
);
end;
TOnStatementExecute = procedure(Sender: TObject;Line:Integer; StatementNo: Integer; Desc:TStatementDesc;
Statement: TStrings) of object;
TOnSQLScriptExecError = procedure(Sender: TObject; StatementNo: Integer;
Line:Integer; Statement: TStrings; SQLCode: Integer; const Msg: string;
var doRollBack:boolean; var Stop: Boolean) of object;
TDynStringArray= array of Ansistring;
//TDynStringArray= array of string;
TpFIBScripter=class(TComponent,IFIBScripter)
private
FPrepared:boolean;
FMakeConnectInScript:boolean;
FParser:TpFIBScriptParser;
FScript:TStrings;
FScriptMap:TScriptMap;
FDirectivesMap:TDirectivesMap;
FLineCountInFile:integer;
procedure DoOnChangeScript(Sender: TObject);
private
FDatabase :TpFIBDatabase;
FTransaction:TpFIBTransaction;
FQuery :TpFIBQuery;
FPaused :Boolean;
FSkipStatement:Boolean;
FStopStatementNo:integer;
FSQLDialect :integer;
FLibraryName:string;
FCharSet :string;
FAutoDDL :boolean;
FBlobFile :string;
FBlobFileStream:TFileStream;
vInternalDatabase:boolean;
FExternalTransaction:TpFIBTransaction;
FHaveDMLStatements :boolean;
FHaveUnknownStatements:boolean;
FNeedRestoreForceWrite:boolean;
vReinsPrepared:boolean;
vLastInsertStmt:string;
private
// AutoExecBlock support
FUseExecBlockForDML:boolean;
vBlockContextCount:integer;
vBlockSize:integer;
FExecBlockStatement:TStrings;
procedure RestartBlock;
procedure CloseBlock;
function AddStatementToExecuteBlock(Stmt:TStrings):boolean;
private
FOnExecuteError:TOnSQLScriptExecError;
FBeforeStatementExecute:TOnStatementExecute;
FAfterStatementExecute :TOnStatementExecute;
procedure SetDatabase(const Value: TpFIBDatabase);
procedure SetTransaction(const Value: TpFIBTransaction);
function GetTransaction:TpFIBTransaction;
procedure DoReconnect;
procedure SetConnectParams(StartToken:TStmtCoord;EndCoord:TStmtCoord);
procedure TryFillBlobParams;
function PrepareReinsert(const InsTxt,ReInsTxt:string):string;
private
//IB2007
FInBatchCollect:boolean;
FBatchSQLs :TDynStringArray;
FInternalOnStatementExec:TOnScriptStatementExec;
procedure SetOnStatExec(CallBack:TOnScriptStatementExec);
//Directives
private
FDefines:TStrings;
FDirectiveConsts:TStrings;
procedure SetScript(const Value: TStrings);
procedure SetDefines(const Value: TStrings);
function DirectiveForbid(DirNum:integer; InElse:boolean):boolean;
function CalcDirective(Directive:TDirectiveDesc):TDirectiveState;
function CalcExists(Condition:TStrings):TDirectiveState;
function CalcIF(Condition:TStrings):TDirectiveState;
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure CreateInternalDatabase;
procedure CopyFragment(BegPos,EndPos:TStmtCoord;Dest:TStrings);
procedure DoQueryExecute(SQL:TStrings;ParamValues:array of variant);
public
constructor Create(AOwner:TComponent); override;
destructor Destroy;override;
procedure Parse(Terminator:string=';');
procedure ExecuteScript(FromStmt:integer=1);
procedure ExecuteFromFile(const FileName: string;Terminator:string=';');
procedure ExecuteStatement(StmtTxt:TStrings;stmt:PStatementDesc;StmtNo:integer;
TmpSQL:TStrings=nil;LineInFile:integer=-1
);
procedure ClearPrepared;
function StatementsCount:integer;
function GetStatement(StmtNo:integer;Text:TStrings):PStatementDesc;
function LineCountInCurrentFile:integer;
procedure AddDefine(const Def:string);
procedure DeleteDefine(const Def:string);
procedure PreparePreDefines;
property Prepared:boolean read FPrepared;
property StopStatementNo:Integer read FStopStatementNo;
property Query:TpFIBQuery read FQuery;
property MakeConnectInScript:boolean read FMakeConnectInScript;
property SkipStatement: Boolean read FSkipStatement write FSkipStatement;
property Paused: Boolean read FPaused write FPaused;
property Defines:TStrings read FDefines write SetDefines;
published
property Script:TStrings read FScript write SetScript;
property Database:TpFIBDatabase read FDatabase write SetDatabase;
property Transaction: TpFIBTransaction read FExternalTransaction write SetTransaction;
property OnExecuteError:TOnSQLScriptExecError
read FOnExecuteError write FOnExecuteError;
property AutoDDL :boolean read FAutoDDL write FAutoDDL default True;
property BeforeStatementExecute:TOnStatementExecute read FBeforeStatementExecute write
FBeforeStatementExecute;
property AfterStatementExecute:TOnStatementExecute read FAfterStatementExecute write
FAfterStatementExecute ;
property UseExecBlockForDML:boolean read FUseExecBlockForDML write FUseExecBlockForDML default false;
end;
function StatementTypeName(tn:TStmtType):string;
// Possible Directives
// $IFDEF,$IFNDEF,$IFEXISTS,$IFNEXISTS,$IFNOTEXISTS,$ELSE,$ENDIF
//$IF,$DEFINE,$UNDEF
const
dIfDef='{$IFDEF';
dIfNDef='{$IFNDEF';
dIf ='{$IF';
dIfExists ='{$IFEXISTS';
dIfNExists ='{$IFNEXISTS';
dIfNotExists ='{$IFNOTEXISTS';
dElse='{$ELSE';
dEndIf='{$ENDIF';
dExecBlock='{$EXECUTE_BLOCK';
dDefine='{$DEFINE';
dUnDefine='{$UNDEF';
dSetVar='{$SET';
//predefined defnames
const srvIsFirebird='IS_FIREBIRD';
//predefined defVARS
srvMajorVer='SERVER_MAJOR_VER';
srvMinorVer='SERVER_MINOR_VER';
dbODSMajorVersion='ODS_MAJOR_VER';
dbODSMinorVersion='ODS_MINOR_VER';
implementation
uses StrUtil,SqlTxtRtns,StdFuncs;
{ TpFIBScripter }
type
TParseDisposition= (pdBetweenStatements,pdInStatement,pdInDirective);
TParserState =(psNormal,psInComment,psInFBComment,psInQuote,psInDoubleQuote,psInConditional);
procedure RaiseParserDirectiveError(const DirName:string;Line:integer);
begin
raise
Exception.Create('Parse script error.'+CLRF+'Can''t resolve directive "'+DirName+'"'+CLRF+
'Line ' +IntToStr(Line)
);
end;
function StatementTypeName(tn:TStmtType):string;
begin
case tn of
sUnknown: Result:='Unknown';
sInvalid: Result:='Invalid';
sDML: Result:='DML';
sConnect: Result:='Connect';
sDisconnect: Result:='Disconnect';
sReconnect: Result:='Reconnect';
sCreateDatabase: Result:='Create database';
sDropDatabase: Result:='Drop database';
sCommit: Result:='Commit';
sRollBack: Result:='Rollback';
sCreate: Result:='Create ';
sAlter: Result:='Alter ';
sRecreate:Result:='Recreate ';
sDrop:Result:='Drop ';
sSet:Result:='Set ';
sSetGenerator: Result:='Set generator';
sSetStatistics: Result:='Set statistics';
sDescribe :Result:='Describe ';
sDeclare :Result:='Declare ';
sComment :Result:='Comment ';
sGrant : Result:='Grant ';
sRunFromFile: Result:='Run from file ';
sInsert,sReinsert : Result:='DML'
else
Result:='Unknown';
end
end;
function StmtCoord(X:Word;Y:Integer):TStmtCoord;
begin
Result.X := X;
Result.Y := Y;
end;
function IsClause( const EtalonClause:string; const Source:string;
Position:integer
):boolean;
// EtalonClause must be if UpperCase
var
Len:Integer;
LenEtalon:Byte;
pSource:PChar;
pEtalon:PChar;
pEtalon1:PChar;
begin
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:=Source[Position+LenEtalon] in [' ',#13,#9,#10,'/','-',';','^',',','}']
end
end;
function StrIsIfDirective(const CheckStr:string;X:integer):boolean;
begin
{ dIfDef='{$IFDEF';
dIfNDef='{$IFNDEF';
dIf ='{$IF';
dIfExists ='{$IFEXISTS';
dIfNExists ='{$IFNEXISTS';
dIfNotExists ='{$IFNOTEXISTS';
}
Result:=IsClause(dIfDef,CheckStr,x) or IsClause(dIfNDef,CheckStr,x) or
IsClause(dIf,CheckStr,x) or IsClause(dIfExists,CheckStr,x) or
IsClause(dIfNExists,CheckStr,x) or IsClause(dIfNotExists,CheckStr,x)
end;
procedure TpFIBScripter.ClearPrepared;
begin
SetLength(FScriptMap,0);
SetLength(FDirectivesMap,0);
FPrepared:=False;
end;
procedure TpFIBScripter.CopyFragment(BegPos, EndPos: TStmtCoord;
Dest: TStrings);
var
y1:Integer;
L:Word;
CurStr:String;
begin
if Assigned(Dest) then
begin
Dest.Clear;
if (EndPos.X=0) and (FScript.Count>0) then
begin
// End term don't exist
EndPos.Y:=FScript.Count-1;
EndPos.X:=Length(FScript[FScript.Count-1])
end;
for y1:=BegPos.Y to EndPos.Y do
begin
CurStr:=FScript[y1];
if y1=BegPos.Y then // First line
begin
if BegPos.Y=EndPos.Y then
L:=EndPos.X-BegPos.X+1
else
L:=Length(CurStr)-BegPos.X+1;
if L<>Length(CurStr) then
Dest.Add(Copy(CurStr,BegPos.X,L))
else
Dest.Add(CurStr)
end
else
if y1=EndPos.Y then // Last line
begin
L:=EndPos.X;
if L<>Length(CurStr) then
Dest.Add(Copy(CurStr,1,L))
else
Dest.Add(CurStr)
end
else
Dest.Add(CurStr)
end;
end;
end;
constructor TpFIBScripter.Create(AOwner:TComponent);
begin
inherited;
// FUseExecBlockForDML:=True;
FScript:=TStringList.Create;
TStringList(FScript).OnChanging:=DoOnChangeScript;
FParser:=TpFIBScriptParser.Create;
FDefines:=TStringList.Create;
FDirectiveConsts:=TStringList.Create;
// FDirectiveConsts.Add('A= 11');
FTransaction:=TpFIBTransaction.Create(Self);
FQuery :=TpFIBQuery.Create(Self);
FQuery.Transaction:=FTransaction;
FSQLDialect:=3;
FAutoDDL:=True;
FBlobFileStream:=nil;
end;
procedure TpFIBScripter.CreateInternalDatabase;
begin
if not Assigned(FDatabase) then
begin
Database:=TpFIBDatabase.Create(Self);
vInternalDatabase:=True
end
end;
destructor TpFIBScripter.Destroy;
begin
FParser.Free;
FScript.Free;
FDefines.Free;
FDirectiveConsts.Free;
SetLength(FScriptMap,0);
SetLength(FDirectivesMap,0);
if Assigned(FExecBlockStatement) then
FExecBlockStatement.Free;
if Assigned(FBlobFileStream) then
FBlobFileStream.Free;
inherited;
end;
procedure TpFIBScripter.DoOnChangeScript(Sender: TObject);
begin
ClearPrepared
end;
procedure TpFIBScripter.DoQueryExecute(SQL:TStrings;
ParamValues: array of variant);
begin
end;
procedure TpFIBScripter.DoReconnect;
begin
if Assigned(FDatabase) then
begin
if GetTransaction.InTransaction then
GetTransaction.Commit;
FDatabase.Connected := False;
FDatabase.Connected := True
end;
end;
{$IFNDEF D6+}
// Copy from SysUtils (D6+)
function AnsiDequotedStr(const S: string; AQuote: Char): string;
var
LText: PChar;
begin
LText := PChar(S);
Result := AnsiExtractQuotedStr(LText, AQuote);
if Result = '' then
Result := S;
end;
{$ENDIF}
function TpFIBScripter.CalcIF(Condition:TStrings):TDirectiveState;
type
TState=(sConstName,sOperator,sValue);
var
i,L:integer;
tmpStr:string;
cName:string;
cOper:string;
cValue:string;
Value:string;
cValueF:Double;
ValueF:Double;
Expr:string;
State:TState;
begin
// only simple expressions
Result:=dsUnknown;
tmpStr:=Condition[0];
L:=Length(tmpStr);
I:=3;
while I <= L do
begin
if tmpStr[I] in [' ',#9,#13,#10] then
Break;
Inc(I)
end;
tmpStr:=Trim(Copy(Condition.Text,I,MaxInt));
SetLength(tmpStr,Length(tmpStr)-1);
Expr:=Trim(tmpStr);
if Length(Expr)=0 then
Exit;
I:=1;
State:=sConstName;
cOper:=''; cValue:='';
while I<=Length(Expr) do
begin
case State of
sConstName:
if (Expr[I] in [' ',#9,#10,#13]) then
begin
cName:=UpperCase(Copy(Expr,1,I-1));
while (I<=Length(Expr)) and (Expr[I] in [' ',#9,#10,#13]) do
Inc(I);
if (I>Length(Expr)) or not (Expr[I] in ['=','>','<']) then
raise Exception.Create('Parse script error.'+CLRF+'Can''t resolve condition "$IF"'+CLRF+
Expr
);
State:=sOperator;
end
else
if (Expr[I] in ['=','>','<']) then
begin
cName:=UpperCase(Copy(Expr,1,I-1));
State:=sOperator;
end
else
Inc(I);
sOperator:
if (Expr[I] in ['=','>','<']) then
begin
cOper:=cOper+Expr[I];
Inc(I)
end
else
State:=sValue;
sValue:
begin
while (I<=Length(Expr)) and (Expr[I] in [' ',#9,#10,#13]) do
Inc(I);
cValue:=Copy(Expr,I,MaxInt);
Break;
end;
end;
end;
//
Value:=FDirectiveConsts.Values[cName];
if Trim(Value)='' then
raise Exception.Create('Parse script error.'+CLRF+'Constant don''t exists'+CLRF+
cName
);
if cOper='=' then
begin
if Value=cValue then
Result:=dsTrue
else
Result:=dsFalse
end
else
if cOper='<>' then
begin
if Value<>cValue then
Result:=dsTrue
else
Result:=dsFalse
end
else
begin
// May be floats
Value :=StringReplace(Value,'.',{$IFDEF D_XE3}FormatSettings.{$ENDIF} DecimalSeparator,[]);
Value :=StringReplace(Value,',',{$IFDEF D_XE3}FormatSettings.{$ENDIF} DecimalSeparator,[]);
cValue:=StringReplace(cValue,'.',{$IFDEF D_XE3}FormatSettings.{$ENDIF} DecimalSeparator,[]);
cValue:=StringReplace(cValue,',',{$IFDEF D_XE3}FormatSettings.{$ENDIF} DecimalSeparator,[]);
cValueF:=StrToFloat(cValue);
ValueF:=StrToFloat(Value);
case cOper[1] of
'>':
if ValueF<cValueF then
Result:=dsFalse
else
if cValueF>cValueF then
Result:=dsTrue
else
if cOper='>=' then
Result:=dsTrue
else
Result:=dsFalse;
'<':
if ValueF>cValueF then
Result:=dsFalse
else
if ValueF<cValueF then
Result:=dsTrue
else
if cOper='<=' then
Result:=dsTrue
else
Result:=dsFalse;
end;
end;
end;
const
QRYDomainExist =
'select RDB$FIELD_TYPE FROM RDB$FIELDS WHERE RDB$FIELD_NAME=:NAME';
QRYTableExist =
'SELECT REL.RDB$RELATION_NAME FROM RDB$RELATIONS REL WHERE REL.RDB$RELATION_NAME=:NAME and REL.RDB$VIEW_BLR is null';
QRYViewExist =
'SELECT REL.RDB$RELATION_NAME FROM RDB$RELATIONS REL WHERE REL.RDB$RELATION_NAME=:NAME and NOT REL.RDB$VIEW_BLR is null';
QRYTriggerExist =
'SELECT T.RDB$TRIGGER_NAME from RDB$TRIGGERS T WHERE T.RDB$TRIGGER_NAME=:NAME';
QRYProcedureExist =
'SELECT RDB$PROCEDURE_NAME FROM RDB$PROCEDURES WHERE RDB$PROCEDURE_NAME=:NAME';
QRYPackageExist =
'SELECT RDB$PACKAGE_NAME FROM RDB$PACKAGES WHERE RDB$PACKAGE_NAME=:NAME';
QRYExceptionExist =
'SELECT RDB$EXCEPTION_NAME FROM RDB$EXCEPTIONS WHERE RDB$EXCEPTION_NAME=:NAME';
QRYGeneratorExist =
'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE RDB$GENERATOR_NAME=:NAME ';
QRYUdfExist =
'SELECT RDB$FUNCTION_NAME FROM RDB$FUNCTIONS WHERE RDB$FUNCTION_NAME=:NAME';
QRYFunctionExist =
'SELECT RDB$FUNCTION_NAME FROM RDB$FUNCTIONS WHERE RDB$FUNCTION_NAME=:NAME';
QRYRoleExist =
'SELECT RDB$ROLE_NAME FROM RDB$ROLES WHERE RDB$ROLE_NAME =:NAME ';
function TpFIBScripter.CalcExists(Condition:TStrings):TDirectiveState;
var
CheckExists:boolean;
i,L:integer;
tmpStr:string;
chObjectType:TObjectType;
chObjName:string;
chQryTxt:string;
begin
Result:=dsUnknown;
tmpStr:=Condition[0];
CheckExists:=IsClause(dIfExists, tmpStr,1) ;
if not CheckExists and not IsClause(dIfNExists, tmpStr,1)
and not IsClause(dIfNotExists, tmpStr,1)
then
Exit;
L:=Length(tmpStr);
I:=10;
while I <= L do
begin
if tmpStr[I] in [' ',#9,#13,#10] then
Break;
Inc(I)
end;
tmpStr:=Trim(Copy(Condition.Text,I,MaxInt));
if Length(tmpStr)=0 then
Exit;
chObjectType:=otNone;
case tmpStr[1] of
'D','d': if IsClause('DOMAIN', tmpStr,1) then
begin
chObjectType:=otDomain;
chQryTxt:=QRYDomainExist;
L:=7
end;
'E','e':
if IsClause('EXCEPTION', tmpStr,1) then
begin
chObjectType:=otException;
chQryTxt:=QRYExceptionExist;
L:=10
end;
'F','f':
if IsClause('FUNCTION', tmpStr,1) then
begin
chObjectType:=otFunction;
chQryTxt:=QRYFunctionExist;
L:=9
end;
'G','g':
if IsClause('GENERATOR', tmpStr,1) then
begin
chObjectType:=otGenerator;
chQryTxt:=QRYGeneratorExist;
L:=10
end;
'P','p':
if IsClause('PROCEDURE', tmpStr,1) then
begin
chObjectType:=otProcedure;
chQryTxt:=QRYProcedureExist;
L:=10
end
else
if IsClause('PACKAGE', tmpStr,1) then
begin
chObjectType:=otPackage;
chQryTxt:=QRYPackageExist;
L:=8
end;
'R','r':
if IsClause('ROLE', tmpStr,1) then
begin
chObjectType:=otRole;
chQryTxt:=QRYRoleExist;
L:=5
end;
'T','t':
if IsClause('TABLE', tmpStr,1) then
begin
chObjectType:=otTable;
chQryTxt:=QRYTableExist;
L:=6
end
else
if IsClause('TRIGGER', tmpStr,1) then
begin
chObjectType:=otTrigger;
chQryTxt:=QRYTriggerExist;
L:=8
end;
'U','u':
if IsClause('UDF', tmpStr,1) then
begin
chObjectType:=otUDF;
chQryTxt:=QRYUdfExist;
L:=4
end;
'V','v':
if IsClause('VIEW', tmpStr,1) then
begin
chObjectType:=otView;
chQryTxt:=QRYViewExist;
L:=5
end;
end; // case
if chObjectType<>otNone then
begin
chObjName:=Trim(Copy(tmpStr,L,MaxInt));
SetLength(chObjName,Length(chObjName)-1);
chObjName:=Trim(chObjName);
if (chObjName<>'') and (chObjName[1]='"') then
chObjName:=Copy(chObjName,2,Length(chObjName)-2)
else
chObjName:=UpperCase(chObjName);
FQuery.SQL.Text:=chQryTxt;
{$IFNDEF BEZBAZY}
if Assigned(FDatabase) and FDatabase.Connected then
begin
FQuery.Params[0].AsString:=chObjName;
if not GetTransaction.InTransaction then
GetTransaction.StartTransaction;
FQuery.Close;
try
FQuery.ExecQuery;
if FQuery.Eof xor CheckExists then
Result:= dsTrue
else
Result:= dsFalse
finally
FQuery.Close;
end;
end;
{$ENDIF}
end
else
if IsClause('SELECT', tmpStr,1) then
begin
chQryTxt:=tmpStr;
SetLength(chQryTxt,Length(chQryTxt)-1);
{$IFNDEF BEZBAZY}
if Assigned(FDatabase) and FDatabase.Connected then
begin
FQuery.SQL.Text:=chQryTxt;
if not GetTransaction.InTransaction then
GetTransaction.StartTransaction;
FQuery.Close;
try
FQuery.ExecQuery;
if FQuery.Eof xor CheckExists then
Result:= dsTrue
else
Result:= dsFalse
finally
FQuery.Close;
end;
end;
{$ENDIF}
end;
end;
function TpFIBScripter.CalcDirective(Directive:TDirectiveDesc):TDirectiveState;
var
TmpSQL:TStrings;
s:string;
begin
Result:=dsUnknown;
TmpSQL:=TStringList.Create;
try
CopyFragment(Directive.dBegin,Directive.dConditionClose,TmpSQL);
if TmpSQL.Count>0 then
if IsClause(dIfDef, TmpSQL[0],1) then
begin
s:=Trim(Copy(TmpSQL.Text,8,MaxInt));
SetLength(s,Length(s)-1);
s:=Trim(s);
if FDefines.IndexOf(UpperCase(s))>=0 then
Result:=dsTrue
else
Result:=dsFalse
end
else
if IsClause(dIfNDef, TmpSQL[0],1) then
begin
s:=Trim(Copy(TmpSQL.Text,9,MaxInt));
SetLength(s,Length(s)-1);
s:=Trim(s);
if FDefines.IndexOf(UpperCase(s))>=0 then
Result:=dsFalse
else
Result:=dsTrue
end
else
if IsClause(dIf, TmpSQL[0],1) then
begin
Result:=CalcIF(TmpSQL)
end
else
if IsClause(dIfExists, TmpSQL[0],1) then
begin
Result:=CalcExists(TmpSQL)
end
else
if IsClause(dIfNExists, TmpSQL[0],1) or IsClause(dIfNotExists, TmpSQL[0],1) then
begin
Result:=CalcExists(TmpSQL)
end
else
Result:=dsFalse
finally
TmpSQL.Free
end;
end;
function TpFIBScripter.DirectiveForbid(DirNum:integer; InElse:boolean):boolean;
var
NeedCalc:PDirectivesMap;
i,j:Integer;
vInElse:boolean;
begin
if (DirNum>=0) and (DirNum<Length(FDirectivesMap)) then
begin
case FDirectivesMap[DirNum].dState of
dsTrue: Result:= not InElse;
dsFalse: Result:= InElse;
else
//dsUnknown
begin
Result:=True;
SetLength(NeedCalc,1000);
i:=0;
j:=DirNum;
while (j>-1) and (FDirectivesMap[j].dState=dsUnknown) do
begin
NeedCalc[i]:=@FDirectivesMap[j];
j:=FDirectivesMap[j].OwnerDirectiveNum;
Inc(i)
end;
SetLength(NeedCalc,I);
Dec(I) ;
//
if j>-1 then
begin
Result:=(FDirectivesMap[j].dState=dsTrue) xor (NeedCalc[I].OwnerDirectiveElse);
end;
//
while (i>=0) and Result do
begin
if i>0 then
vInElse:=NeedCalc[i-1].OwnerDirectiveElse
else
vInElse:=InElse;
NeedCalc[i].dState:=CalcDirective(NeedCalc[i]^);
Result:= (NeedCalc[i].dState=dsTrue) xor vInElse;
Dec(I)
end;
if not Result then
if InElse then
FDirectivesMap[DirNum].dState:=dsTrue
else
FDirectivesMap[DirNum].dState:=dsFalse;
end;
end;
end
else
Result:=True
end;
procedure TpFIBScripter.ExecuteStatement(StmtTxt:TStrings;stmt:PStatementDesc;
StmtNo:integer; TmpSQL:TStrings=nil;LineInFile:integer=-1);
var
vToken:TStmtCoord;
tmpStr,tmpStr1:string;
vIsInternalTmpSQL:boolean;
doRollBack:boolean;
MayBeInBlock:boolean;
skip:boolean;
i:Integer;
procedure ApplyCommand;
begin
try
{$IFNDEF BEZBAZY}
if not GetTransaction.InTransaction then
GetTransaction.StartTransaction;
if Length(FBlobFile)>0 then
if (FQuery.ParamCount>0) then
TryFillBlobParams;
FQuery.ExecQuery;
if Assigned(FInternalOnStatementExec) then
begin
if LineInFile=-1 then
FInternalOnStatementExec(stmt.smdBegin.Y+1,StmtNo+1)
else
begin
FInternalOnStatementExec(LineInFile,StmtNo+1);
end;
end;
if Assigned(FAfterStatementExecute) then