Repository navigation
Expand file tree
/
Copy pathCcInterbase.pas
More file actions
2039 lines (1834 loc) · 77.3 KB
/
Copy pathCcInterbase.pas
File metadata and controls
2039 lines (1834 loc) · 77.3 KB
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
unit CcInterbase;
{$I CC.INC}
interface
uses Classes, Sysutils, CCat, DB, CcProviders, CcKeys;
type
TCcInterbaseBranch = (dbInterbase, dbFirebird);
//Database adaptor for Interbase and Firebird
//See also:
//TCcDBAdaptor
TCcInterbaseAdaptor = class (TCcDBAdaptor)
private
FDBBranch: TCcInterbaseBranch;
FBranchVersion: Integer;
procedure EmptyTempTable;
procedure InsertTempTable;
function GetDeclaration(DataType: TFieldType; nSize: Integer): String;
function TranslateDataType(LowLevelDataType: Integer): TFieldType;
function ConvertToOldGen(cPKGen: String; cNewOld: String): String;
function QuoteSQLData(cData: String; DataType: TFieldType;
lSQLStyle: Boolean): String;
function GetSQLDialect: Integer;
function StringToDate(cDate: String; lHasTimeInfo, lHasDateInfo: Boolean): TDateTime;
procedure GetReplicatingNode;
function ParseSQLCondition(cTable, cCondition: String; cNewOld: String): String;
procedure GetUniqueIndices(cTableName: String; list: TStringList);
function GetMillisecFractions(d: TDateTime): String;
function GetFieldTypeSQLText(FieldName, TableName: String): String;override;
function QuoteSQLDataLite(cData: String; DataType: TFieldType;
lSQLStyle: Boolean): String;
protected
FKeys : TCcKeyRing;
procedure DoRegisterNode(NodeName: String);override;
function GetQuoteMetadata: Boolean;override;
function CheckConnectionLossException(E: Exception): Boolean; override;
function GetInsertOrUpdateSQL(slFields: TStringList; sourceDBAdaptor: TCcDBAdaptor; keys: TCcCustomKeyRing; tableName: String): string;override;
procedure DoListTables(list: TStringList; IncludeTempTables: Boolean); override;
procedure DoListTableFields(cTableName: String; list: TStringList);override;
procedure DoListUpdatableTableFields(cTableName: String; list: TStringList);override;
procedure DoListPrimaryKeys(cTableName: String; list: TStringList);override;
procedure DoListTriggers(list: TStringList);override;
procedure DoListProcedures(list: TStringList);override;
procedure DoListAllProcedures(list: TStringList);override;
procedure DoListGenerators(list: TStringList);override;
procedure DoListFieldsForNoPK(cTableName: String; list: TStringList);override;
function MaxDDLNameLength: Integer;override;
function UDFDeclared(UDFName: String): Boolean;
function GetUseRowsAffected: Boolean; override;
procedure SetVersion(const Value: String);override;
//Called after a new connection has been started
procedure InitConnection;override;
//Called after a new transaction has been started
procedure InitTransaction;override;
//Called before a transaction is committed
procedure CleanupTransaction;override;
procedure BeforeConnect;override;
function GetCurrentTimeStampSQL:String;override;
procedure DoListKeywordsForbiddenAsFieldNames(list: TStringList);override;
function IndexDeclared(IndexName: String): Boolean;
function SupportsInsertOrUpdate: Boolean;override;
public
procedure DropGenerator(cGeneratorName: string); override;
procedure RemoveExtraCustomMetadata;override;
procedure RemoveCustomMetadata;override;
procedure DropProcedures;override;
procedure CheckExtraCustomMetadata;override;
function SupportsGenerators: Boolean;override;
class function GetAdaptorName: String;override;
function SQLFormatValue(Data: Variant; FieldType :TFieldType): String;override;
procedure CreateProcedures;override;
procedure CheckCustomMetadata;override;
function ConvertValue(Val: Variant; DataType: TFieldType): Variant;override;
function DeclareField(FieldName: String; FieldType: TFieldType;
Length: Integer; NotNull: Boolean; PK: Boolean; AutoInc: Boolean): String;override;
function DeclarePK(FieldNames: String): String;override;
procedure DeclareGenerator(GenName:String);override;
function GenDeclared(GenName:String): Boolean;override;
procedure RemoveTriggers(qTable: TCcQuery);override;
function GenerateTriggers(qTable:TCcQuery; qTableConf: TCcQuery; FailIfNoPK: Boolean; TrackFieldChanges: Boolean): Integer;override;
// procedure GetProcParams(ProcName: String;
// Params: TDataSet; InputParam: Boolean);override;
function GetGeneratorValue(GenName: String; Increment: Integer):
{$IFDEF CC_D2K9}
Int64;
{$ELSE}
Integer;
{$ENDIF}override;
function GetGenerator(GenName: String; Increment: Integer): String;override;
property SQLDialect: Integer read GetSQLDialect;
property DBBranch: TCcInterbaseBranch read FDBBranch;
property BranchVersion: Integer read FBranchVersion;
constructor Create(Conn: TCcConnection);override;
destructor Destroy;override;
end;
const
IB_SQL_VARYING = 448;
IB_SQL_TEXT = 452;
IB_SQL_DOUBLE = 480;
IB_SQL_FLOAT = 482;
IB_SQL_LONG = 496;
IB_SQL_SHORT = 500;
IB_SQL_TIMESTAMP = 510;
IB_SQL_BLOB = 520;
IB_SQL_D_FLOAT = 530;
IB_SQL_ARRAY = 540;
IB_SQL_QUAD = 550;
IB_SQL_TYPE_TIME = 560;
IB_SQL_TYPE_DATE = 570;
IB_SQL_INT64 = 580;
IB_SQL_DATE = IB_SQL_TIMESTAMP;
IB_SQL_BOOLEAN = 590;
implementation
uses {$IFDEF CC_UseVariants}Variants, FMTBcd, {$ENDIF}Math {$IFDEF CC_D6}, Strutils{$ENDIF};
procedure TCcInterbaseAdaptor.SetVersion(const Value: String);
var
cPrefix, cSuffix: String;
OldDecimalSeparator: Char;
begin
inherited;
if (Trim(Value) <> '') then begin
cPrefix := Copy(Value, 1, 2);
cSuffix := Copy(Value, 3, Length(Value));
if cPrefix = 'IB' then
FDBBranch := dbInterbase
else
FDBBranch := dbFirebird;
{$IFDEF CC_D2K13}
OldDecimalSeparator := FormatSettings.DecimalSeparator;
FormatSettings.DecimalSeparator := '.';
{$ELSE}
OldDecimalSeparator := DecimalSeparator;
DecimalSeparator := '.';
{$ENDIF}
try
FBranchVersion := Round(StrToFloat(Trim(cSuffix)) * 10);
finally
{$IFDEF CC_D2K13}
FormatSettings.DecimalSeparator := OldDecimalSeparator;
{$ELSE}
DecimalSeparator := OldDecimalSeparator;
{$ENDIF}
end;
end;
end;
function TCcInterbaseAdaptor.StringToDate(cDate: String; lHasTimeInfo, lHasDateInfo: Boolean): TDateTime;
var
cDateFormat: String;
nMilliSec: Integer;
nDashPos: Integer;
nDotPos: Integer;
function GetMonth(cMonth: String): String;
begin
//This function converts a three letter English abbreviation of the month
//name to its corresponding month number
cMonth := UpperCase(Trim(cMonth));
if cMonth = 'JAN' then
Result := '01'
else if cMonth = 'FEB' then
Result := '02'
else if cMonth = 'MAR' then
Result := '03'
else if cMonth = 'APR' then
Result := '04'
else if cMonth = 'MAY' then
Result := '05'
else if cMonth = 'JUN' then
Result := '06'
else if cMonth = 'JUL' then
Result := '07'
else if cMonth = 'AUG' then
Result := '08'
else if cMonth = 'SEP' then
Result := '09'
else if cMonth = 'OCT' then
Result := '10'
else if cMonth = 'NOV' then
Result := '11'
else if cMonth = 'DEC' then
Result := '12';
end;
begin
if Trim(cDate) = '' then begin
Result := 0;
Exit;
end;
if lHasDateInfo then begin
//Convert from string to date/time...
if Length(cDate) > 21 then begin
//This indicates that our date/time includes milliseconds
nMilliSec := StrToIntDef(Copy(cDate, Length(cDate) - 3, 4), 0);
cDate := Copy(cDate, 1, Length(cDate) - 5);
end
else
nMilliSec := 0;
{$IFDEF CC_D2K13}
cDate := ReplaceString(cDate, '-', FormatSettings.DateSeparator);
cDate := ReplaceString(cDate, '.', FormatSettings.DateSeparator);
cDate := ReplaceString(cDate, '/', FormatSettings.DateSeparator);
//Save current locale date format for later
cDateFormat := FormatSettings.ShortDateFormat;
{$ELSE}
cDate := ReplaceString(cDate, '-', DateSeparator);
cDate := ReplaceString(cDate, '.', DateSeparator);
cDate := ReplaceString(cDate, '/', DateSeparator);
//Save current locale date format for later
cDateFormat := ShortDateFormat;
{$ENDIF}
end else begin
if Length(cDate) > 8 then begin
//This indicates that our time includes milliseconds
nMilliSec := StrToIntDef(Copy(cDate, Length(cDate) - 3, 4), 0);
cDate := Copy(cDate, 1, Length(cDate) - 5);
end
else
nMilliSec := 0;
{$IFDEF CC_D2K13}
cDateFormat := FormatSettings.ShortDateFormat;
{$ELSE}
cDateFormat := ShortDateFormat;
{$ENDIF}
end;
try
if SQLDialect = 3 then
//Dialect 3 format: 'YYYY.MM.DD HH:NN:SS.ssss'
{$IFDEF CC_D2K13}
FormatSettings.ShortDateFormat := 'yyyy/mm/dd'
{$ELSE}
ShortDateFormat := 'yyyy/mm/dd'
{$ENDIF}
else begin
//Dialect 1 format: 'D-MMM-YYYY HH:NN:SS.ssss'
//converted (above) to: 'D.MMM.YYYY HH:NN:SS.ssss'
nDotPos := AnsiPos({$IFDEF CC_D2K13}FormatSettings.{$ENDIF}DateSeparator, cDate);
cDate := Copy(cDate, 1, nDotPos) + GetMonth(Copy(cDate, nDotPos+1, 3)) + Copy(cDate, nDotPos + 4, Length(cDate));
{$IFDEF CC_D2K13}
FormatSettings.ShortDateFormat := 'dd/mm/yyyy';
{$ELSE}
ShortDateFormat := 'dd/mm/yyyy';
{$ENDIF}
end;
//Perform the actual conversion
Result := StrToDateTime(cDate) + EncodeTime(0, 0, 0, nMilliSec div 10);
finally
//Restore the locale setting
{$IFDEF CC_D2K13}
FormatSettings.ShortDateFormat := cDateFormat;
{$ELSE}
ShortDateFormat := cDateFormat;
{$ENDIF}
end;
end;
function TCcInterbaseAdaptor.ConvertValue(Val: Variant; DataType: TFieldType): Variant;
var
vt: Word;
cOrigDecimalSeparator: Char;
begin
{$IFDEF CC_D2K13}
cOrigDecimalSeparator := FormatSettings.DecimalSeparator;
FormatSettings.DecimalSeparator := '.';
{$ELSE}
cOrigDecimalSeparator := DecimalSeparator;
DecimalSeparator := '.';
{$ENDIF}
try
vt := VarType(Val);
if {$IFDEF CC_D2K9} (vt = varUString) or {$ENDIF} (vt = varOleStr) or (vt = varString) then begin
if ((DataType = ftDateTime) or (DataType = ftDate) or (DataType = ftTime) {$IFDEF CC_D2K12} or (DataType = ftTimeStamp)
or (DataType = ftOraTimeStamp){$ENDIF}) then
Result := StringToDate(Val, (DataType <> ftDate), (DataType <> ftTime))
else if ((DataType = ftInteger) or (DataType = ftSmallInt)) then
Result := StrToInt(Val)
else if ((DataType = ftFloat) {$IFDEF CC_D2K12} or (DataType = ftSingle){$ENDIF} ) then
Result := StrToFloat(Val)
else if ((DataType = ftBcd) {$IFDEF CC_D2K12} or (DataType = ftFMTBcd){$ENDIF} ) then
Result := StrToCurr(Val)
else begin
Result := Val
end;
end
else
Result := Val;
finally
//Restore the locale setting
{$IFDEF CC_D2K13}
FormatSettings.DecimalSeparator := cOrigDecimalSeparator;
{$ELSE}
DecimalSeparator := cOrigDecimalSeparator;
{$ENDIF}
end;
end;
//This function is called in order to see if a certain predefined UDF is available on the
//database server.
function TCcInterbaseAdaptor.UDFDeclared(UDFName: String):Boolean;
begin
with FConnection.SelectQuery['IB_FINDUDF'] do begin
Close;
SQL.Text := 'select rdb$function_name from rdb$functions where upper(rdb$function_name) = ' + QuotedStr(UDFName);
Exec;
if RecordCount > 0 then Result := True
else Result := False;
end;
end;
////GrantProcedure is called in order to grant access to the given database procedure.
////If cUserName = 'PUBLIC', access should be granted to all users
//function TCcInterbaseAdaptor.GetGrantProcedure(cProcName, cUserName:String):String;
//begin
// Result := 'GRANT EXECUTE ON PROCEDURE ' + cProcName + ' TO ' + cUserName;
//end;
////GetGrantTable is called in order to grant access to the given database table.
////If cUserName = 'PUBLIC', access should be granted to all users
//function TCcInterbaseAdaptor.GetGrantTable(cTableName, cUserName:String): String;
//begin
// Result := 'GRANT ALL ON ' + cTableName + ' TO ' + cUserName;
//end;
function TCcInterbaseAdaptor.MaxDDLNameLength: Integer;
begin
Result := 31;
end;
destructor TCcInterbaseAdaptor.Destroy;
begin
FKeys.Free;
inherited;
end;
function TCcInterbaseAdaptor.DeclareField(FieldName: String; FieldType: TFieldType;
Length: Integer; NotNull, PK: Boolean; AutoInc: Boolean): String;
begin
Result := FieldName + ' ' + GetDeclaration(FieldType, Length);
if NotNull then
Result := Result + ' NOT NULL';
if PK then
Result := Result + ' PRIMARY KEY';
end;
procedure TCcInterbaseAdaptor.DeclareGenerator(GenName:String);
begin
Query.Clear;
Query.Add('CREATE GENERATOR ' + MetaQuote(GenName));
ExecConfQuery;
end;
//This function is called in order to see if a certain generator exists in the database
//If database does not handle or need generators (for example, if auto-increment fields are used instead)
//then simply return true.
function TCcInterbaseAdaptor.GenDeclared(GenName: String): Boolean;
begin
with FConnection.SelectQuery['IB_FINDGEN'] do begin
Close;
SQL.Text := 'select cast(rdb$generator_name as varchar(50)) from rdb$generators where %upper_case(cast(rdb$generator_name as varchar(50))) = %upper_case(:gen_name)';
if QuoteMetadata then
Macro['upper_case'].Value := ''
else
Macro['upper_case'].Value := 'upper';
Param['gen_name'].AsString := GenName;
Exec;
if RecordCount > 0 then Result := True
else Result := False;
end;
end;
procedure TCcInterbaseAdaptor.GetReplicatingNode;
begin
if (DBBranch = dbInterbase) or (BranchVersion < 20) then
Query.Add(' select replicating_node from rpl$vars into :replicating_node;')
else
Query.Add(' replicating_node = rdb$get_context(''USER_SESSION'', ''REPLICATING_NODE'');');
end;
function TCcInterbaseAdaptor.ParseSQLCondition(cTable, cCondition: String; cNewOld: String): String;
begin
Result := ConvertToOldGen(Trim(cCondition), cNewOld);
Result := ReplaceString(Result, '%%TABLE_NAME', Trim(cTable));
end;
function TCcInterbaseAdaptor.GenerateTriggers(qTable:TCcQuery; qTableConf: TCcQuery; FailIfNoPK: Boolean; TrackFieldChanges: Boolean): Integer;
var
nTriggerNumber: Integer;
cTriggerName: String;
TableName, ConfigName: String;
procedure DeclareVariables(cType: String);
begin
Query.Add('AS');
Query.Add('declare variable primary_key_values varchar(500);');
Query.Add('declare variable QuotedValue varchar(200);');
Query.Add('declare variable user_login varchar(50);');
Query.Add('declare variable repl_operation char(1);');
Query.Add('declare variable replicating_node varchar(50);');
Query.Add('declare variable primary_key_sync varchar(5000);');
Query.Add('declare variable unique_key_sync varchar(5000);');
Query.Add('declare variable operation_type char(1);');
if cType = 'U' then
Query.Add('declare variable old_primary_key_values varchar(500);');
Query.Add('begin');
end;
function CheckFieldRestrictions: Boolean;
var
I: Integer;
slIncludedFields: TStringList;
begin
Result := False;
if Assigned(qTableConf) and (qTableConf.RecordCount > 0) then begin
slIncludedFields := TStringList.Create;
try
slIncludedFields.CommaText := qTableConf.Field['included_fields'].AsString;
if (slIncludedFields.Count > 0) then begin
Query.Add('if ((rdb$get_context(''USER_TRANSACTION'', ''FORCE_REPLICATION'') = ''TRUE'') or ');
for I := 0 to slIncludedFields.Count -1 do begin
if (I > 0) then
Query.Add(' or ');
Query.Add('(old.' + slIncludedFields[i] + ' is distinct from new.' + slIncludedFields[i] + ')');
end;
Query.Add(') then begin');
Result := True;
end;
finally
slIncludedFields.Free;
end;
end;
end;
function GetPKVals(cNewOld: String): String;
var
I: Integer;
sl: TStringList;
begin
sl := TStringList.Create;
try
sl.Add(' primary_key_values = '''';');
for I := 0 to FKeys.Count - 1 do begin
if (FKeys[i].PrimaryKey) then begin
sl.Add(' if (' + cNewOld + '.' + MetaQuote(FKeys[i].KeyName) + ' is null) then QuotedValue = ''"'';');
sl.Add(' else select quoted_str from RPL$quote_str(' + cNewOld + '.' + MetaQuote(FKeys[i].KeyName) + ') into :QuotedValue;');
sl.Add(' primary_key_values = primary_key_values || QuotedValue || '';'';');
end;
end;
Result := sl.Text;
finally
sl.free;
end;
end;
function GetKeySyncVals: String;
var
cSync: String;
I: Integer;
lSync: Boolean;
sl2: TStringList;
begin
lSync := False;
sl2 := TStringList.Create;
try
sl2.Add(' primary_key_sync = '''';');
sl2.Add(' unique_key_sync = '''';');
for I := 0 to FKeys.Count - 1 do begin
cSync := FKeys[i].GenericSyncStatement;
if (Trim(cSync) = '') then
cSync := QuotedStr(cSync)
else
lSync := True;
sl2.Add(' select quoted_str from RPL$quote_str(' + cSync + ') into :QuotedValue;');
if (FKeys[i].PrimaryKey) then
sl2.Add(' primary_key_sync = primary_key_sync || QuotedValue || '';'';')
else
sl2.Add(' unique_key_sync = unique_key_sync || QuotedValue || '';'';');
end;
if lSync then
Result := sl2.Text;
finally
sl2.Free;
end;
end;
procedure GenerateChangedFieldsOld;
var
TriggerBody: TStringList;
slFields: TStringList;
I: Integer;
size: Integer;
ft: TFieldType;
q: TCcQuery;
cFieldVals, cFieldName : String;
sl: TStringList;
lFirst: Boolean;
cMainTriggerText: String;
cCondition: string;
nTriggerLength: Integer;
nFinalTriggerLength: Integer;
procedure CreateTrigger(lFinal: Boolean);
var
nPos: Integer;
cFullTrigName :String;
begin
if nTriggerNumber = 1 then
nPos := 32765
else if lFinal then
nPos := 32767
else
nPos := 32766;
if nTriggerNumber = 1 then
cFullTrigName := MetaQuote(cTriggerName)
else
cFullTrigName := MetaQuote(cTriggerName + IntToStr(nTriggerNumber));
Query.Add('create trigger ' + cFullTrigName + ' for ' + MetaQuote(TableName) + ' AFTER INSERT OR UPDATE OR DELETE position ' + IntToStr(nPos));
Query.Add('as');
if lFinal then begin
Query.Add('declare variable primary_key_values varchar(500);');
Query.Add('declare variable QuotedValue varchar(200);');
Query.Add('declare variable operation_type char(1);');
Query.Add('declare variable primary_key_sync varchar(5000);');
Query.Add('declare variable unique_key_sync varchar(5000);');
end;
Query.Add('declare variable user_login varchar(50);');
Query.Add('declare variable replicating_node varchar(50);');
Query.Add('declare variable change_number integer;');
Query.Add('begin');
Query.Add(' if (rdb$get_context(''USER_TRANSACTION'', ''RPL$NO_REPLICATION'') = ''TRUE'') then exit;');
if nTriggerNumber = 1 then begin
Query.Add(' change_number = gen_id(gen_rpl$log_change_number, 1);');
Query.Add(' rdb$set_context(''USER_TRANSACTION'', ''RPL$CHANGE_NUMBER'', change_number);');
end
else
Query.Add(' change_number = rdb$get_context(''USER_TRANSACTION'', ''RPL$CHANGE_NUMBER'');');
GetReplicatingNode;
if lFinal then begin
Query.Add('if (inserting) then begin');
Query.Add(GetPKVals('new'));
Query.Add('end else begin');
Query.Add(GetPKVals('old'));
Query.Add('end');
if GetKeySyncVals <> '' then begin
//Generate the synchronization statements for primary and unique keys
//We don't convert "new." to "old." in the GenericSyncStatement, which means we always take the new value of the fields, if applicable
//This doesn't matter because the synchronization statements are only used for inserting new key values, not for old records
//For this reason, the following code is also excluded from the delete triggers
Query.Add('if (not deleting) then begin');
Query.Add(GetKeySyncVals);
Query.Add('end');
end;
Query.Add('if (inserting) then operation_type = ''I'';');
Query.Add('else if (updating) then operation_type = ''U'';');
Query.Add('else operation_type = ''D'';');
end;
Query.Add(' for select u.login');
Query.Add(' from RPL$users u');
Query.Add(' where (u.login <> :replicating_node or :replicating_node is null)');
Query.Add(' and (u.config_name is null or u.config_name = ' + QuotedStr(ConfigName) + ')');
//User-definable SQL condition
if cCondition <> '' then
Query.Add(' and ((' + ParseSQLCondition(TableName, cCondition, 'old') + ') or (' + ParseSQLCondition(TableName, cCondition, 'new') + '))');
Query.Add(' into :user_login do');
Query.Add(' begin');
Query.AddStrings(TriggerBody);
if lFinal then begin
Query.Add(' if ((rdb$get_context(''USER_TRANSACTION'', ''FORCE_REPLICATION'') = ''TRUE'') or (rdb$get_context(''USER_TRANSACTION'', ''RPL$FIELDS_CHANGED'') = 1)) then begin');
Query.Add(' insert into RPL$log (code, change_number, login, operation_date, table_name, sent_from,');
Query.Add(' primary_key_values, procedure_statement, PRIMARY_KEY_SYNC, UNIQUE_KEY_SYNC, operation_type, transaction_number)');
Query.Add(' values (gen_id(gen_rpl$log, 1), :change_number, :user_login, current_timestamp, ' + QuotedStr(TableName) + ', :replicating_node,');
Query.Add(' :primary_key_values, null, :PRIMARY_KEY_SYNC, :UNIQUE_KEY_SYNC, :operation_type, current_transaction);');
Query.Add(' end');
end;
Query.Add(' end');
if lFinal then
Query.Add(' rdb$set_context(''USER_TRANSACTION'', ''RPL$FIELDS_CHANGED'', 0);');
Query.Add('end');
ExecConfQuery;
TriggerBody.Clear;
Inc(nTriggerNumber);
end;
begin
cCondition := Trim(qTableConf.Field['CONDITION'].AsString);
nTriggerLength := 700 + Length(cCondition);
nFinalTriggerLength := 1300 + Length(cCondition) + 2 * Length(GetPKVals('new')) + Length(GetKeySyncVals);
lFirst := True;
q := TCcQuery.Create(FConnection);
q.Connection := FConnection;
q.SelectStatement := True;
sl := TStringList.Create;
TriggerBody := TStringList.Create;
try
q.SQL.Text := 'select rf.rdb$field_name, rfs.rdb$field_type, rfs.rdb$field_length '+
' from rdb$relation_fields rf '+
' join rdb$fields rfs on rfs.rdb$field_name = rf.rdb$field_source ' +
' where rfs.rdb$computed_blr is null and rf.rdb$relation_name = :table_name order by rf.rdb$field_name';
q.Param['table_name'].Value := TableName;
q.Exec;
while not q.Eof do begin
cFieldName := q.Field['rdb$field_name'].AsString;
sl.Add('if (old.' + cFieldName + ' is distinct from new.' + cFieldName + ') then begin');
sl.Add(' insert into RPL$LOG_VALUES (CHANGE_NUMBER, NODE_NAME, OLD_VALUE, OLD_VALUE_BLOB,new_VALUE, new_VALUE_BLOB,FIELD_NAME,FIELD_TYPE)');
ft := TranslateDataType(q.Field['rdb$field_type'].AsInteger);
size := q.Field['rdb$field_length'].AsInteger;
if (ft = ftBlob) or (size > 200) then
cFieldVals := 'null, old.' + cFieldName + ', null, new.' + cFieldName
else
cFieldVals := 'old.' + cFieldName + ', null, new.' + cFieldName + ', null';
sl.Add(' values (:change_number, :user_login, ' + cFieldVals + ', ' + QuotedStr(cFieldName) + ', ' + IntToStr(Integer(ft)) + ');');
sl.Add(' rdb$set_context(''USER_TRANSACTION'', ''RPL$FIELDS_CHANGED'', 1);');
sl.Add('end');
//Check if trigger is too big...
if nTriggerLength + Length(TriggerBody.Text) + Length(sl.Text) > 30000 then
CreateTrigger(False);
TriggerBody.Append(sl.Text);
sl.Clear;
q.Next;
end;
if nFinalTriggerLength + Length(TriggerBody.Text) > 30000 then
CreateTrigger(False)
else
CreateTrigger(True);
nTriggerNumber := nTriggerNumber - 1;
finally
q.Free;
TriggerBody.Free;
sl.Free;
end;
end;
procedure GenerateChangedFields;
var
cCondition: string;
procedure DeclareVariables;
begin
Query.Add('declare variable field_name varchar(50);');
Query.Add('declare variable field_length integer;');
Query.Add('declare variable field_type integer;');
Query.Add('declare variable stmt varchar(5000);');
Query.Add('declare variable current_stmt varchar(500);');
Query.Add('declare variable val_blob blob;');
Query.Add('declare variable val varchar(250);');
Query.Add('declare variable counter integer;');
Query.Add('declare variable dbkey char(8) character set octets;');
Query.Add('declare variable change_number integer;');
end;
procedure GetFieldValues(cNewOld:String);
function GetStmtWhere: String;
var
I: Integer;
cKeyName: string;
begin
Result := '';
if (DBBranch = dbFirebird) and (BranchVersion >= 25) then begin
Result := 'rdb$db_key=:dbkey'''
end
else begin
for I := 0 to FKeys.Count-1 do begin
if Result <> '' then
Result := Result + ' || '' and '' || ''';
cKeyName := MetaQuote(FKeys[i].KeyName);
Result := Result + cKeyName + '='' || ' + QuoteSQLDataLite(cNewOld + '.' + cKeyName, FKeys[i].DataType, true);
end;
end;
end;
begin
Query.Add(' counter = 0;');
Query.Add(' for select trim(rf.rdb$field_name), coalesce(f.rdb$character_length, f.rdb$field_length), case f.rdb$field_type');
Query.Add(' when 7 then 3 when 8 then 3 when 9 then 3');
Query.Add(' when 10 then 6 when 27 then 6 when 12 then 9');
Query.Add(' when 13 then 10');
Query.Add(' when 35 then 11');
{ Query.Add(' when 14 then iif(rdb$character_set_id = 4, 38, 23)');
Query.Add(' when 37 then iif(rdb$character_set_id = 4, 24, 1)');
Query.Add(' when 40 then iif(rdb$character_set_id = 4, 24, 1)');
Query.Add(' when 261 then iif(rdb$field_sub_type = 0, 15, iif(rdb$character_set_id = 4, 39, 16))');}
Query.Add(' when 14 then case rdb$character_set_id when 4 then 38 else 23 end');
Query.Add(' when 37 then case rdb$character_set_id when 4 then 24 else 1 end');
Query.Add(' when 40 then case rdb$character_set_id when 4 then 24 else 1 end');
Query.Add(' when 261 then case rdb$field_sub_type when 0 then 15 else case rdb$character_set_id when 4 then 39 else 16 end end');
Query.Add(' when 16 then 8');
Query.Add(' when 23 then 5');
Query.Add(' end');
Query.Add(' from rdb$relation_fields rf');
Query.Add(' join rdb$fields f on f.rdb$field_name = rf.rdb$field_source');
Query.Add(' join rpl$tables_config t on t.table_name = rf.rdb$relation_name and t.config_name = ' + QuotedStr(ConfigName));
Query.Add(' where f.rdb$computed_blr is null and rf.rdb$relation_name = ' + QuotedStr(TableName));
Query.Add(' and ((coalesce(t.included_fields, '''') = '''') or rf.rdb$field_name in (select str from rpl$split_list(t.included_fields)))');
Query.Add(' and ((coalesce(t.excluded_fields, '''') = '''') or rf.rdb$field_name not in (select str from rpl$split_list(t.excluded_fields)))');
Query.Add(' into :field_name, :field_length, :field_type do');
Query.Add(' begin');
Query.Add(' if (field_length <= 250 and field_type not in (15, 16, 39)) then');
Query.Add(' current_stmt = ''select ''''''|| :field_name || '''''',cast('' || :field_name || '' as varchar(250)), cast(null as blob),'' || :field_type || '' from ' + MetaQuote(TableName) + ' where ' + GetStmtWhere + ';');
Query.Add(' else');
Query.Add(' current_stmt = ''select ''''''|| :field_name || '''''',cast(null as varchar(250)),cast('' || :field_name || '' as blob),'' || :field_type || '' from ' + MetaQuote(TableName) + ' where ' + GetStmtWhere + ';');
Query.Add('');
Query.Add(' if (stmt is null) then');
Query.Add(' stmt = current_stmt;');
Query.Add(' else');
Query.Add(' stmt = stmt || '' union all '' || current_stmt;');
Query.Add('');
Query.Add(' if (character_length(stmt) >= 4000 or octet_length(stmt) >= 10000 or counter >= 100) then begin');
if (DBBranch = dbFirebird) and (BranchVersion >= 25) then
Query.Add(' for execute statement (stmt) (dbkey := :dbkey) ')
else
Query.Add(' for execute statement (stmt) ');
Query.Add(' into :field_name, :val, :val_blob, :field_type do begin');
Query.Add(' if (val is not null or val_blob is not null) then');
Query.Add(' update or insert into rpl$tmp_values(field_name,field_type,'+ cNewOld + '_value,'+ cNewOld + '_blob, '+ cNewOld + '_blob_null, change_number) values (trim(:field_name), :field_type, :val, :val_blob, case when :val_blob is null then ''Y'' else ''N'' end, :change_number);');
Query.Add(' end');
Query.Add(' stmt = null;');
Query.Add(' counter = 0;');
Query.Add(' end');
Query.Add(' counter = counter + 1;');
Query.Add(' end');
Query.Add(' if (stmt is not null) then begin');
if (DBBranch = dbFirebird) and (BranchVersion >= 25) then
Query.Add(' for execute statement (stmt) (dbkey := :dbkey) ')
else
Query.Add(' for execute statement (stmt) ');
Query.Add(' into :field_name, :val, :val_blob, :field_type do begin');
Query.Add(' if (val is not null or val_blob is not null) then');
Query.Add(' update or insert into rpl$tmp_values(field_name,field_type,'+ cNewOld + '_value,'+ cNewOld + '_blob, '+ cNewOld + '_blob_null, change_number) values (trim(:field_name), :field_type, :val, :val_blob, case when :val_blob is null then ''Y'' else ''N'' end, :change_number);');
Query.Add(' end');
Query.Add(' end');
end;
begin
cCondition := Trim(qTableConf.Field['CONDITION'].AsString);
Query.Add('create trigger ' + cTriggerName + ' for ' + MetaQuote(TableName) + ' BEFORE INSERT OR UPDATE OR DELETE');
Query.Add('as');
DeclareVariables;
Query.Add('begin');
Query.Add(' if (rdb$get_context(''USER_TRANSACTION'', ''RPL$NO_REPLICATION'') = ''TRUE'') then exit;');
Query.Add(' change_number = gen_id(gen_rpl$log_change_number, 1);');
Query.Add(' insert into rpl$tmp_changes(change_number) values (:change_number);');
Query.Add(' if (inserting) then exit;');
Query.Add(' if (deleting) then');
Query.Add(' dbkey = old.rdb$db_key;');
Query.Add(' else');
Query.Add(' dbkey = new.rdb$db_key;');
GetFieldValues('old');
Query.Add('end');
ExecConfQuery;
Query.Add('create trigger ' + cTriggerName + '2 for ' + MetaQuote(TableName) + ' AFTER INSERT OR UPDATE OR DELETE');
Query.Add('as');
DeclareVariables;
Query.Add('declare variable primary_key_sync varchar(5000);');
Query.Add('declare variable unique_key_sync varchar(5000);');
Query.Add('declare variable user_login varchar(50);');
Query.Add('declare variable replicating_node varchar(50);');
Query.Add('declare variable primary_key_values varchar(500);');
Query.Add('declare variable operation_type char(1);');
Query.Add('begin');
Query.Add(' if (rdb$get_context(''USER_TRANSACTION'', ''RPL$NO_REPLICATION'') = ''TRUE'') then exit;');
GetReplicatingNode;
Query.Add(' if (deleting) then');
Query.Add(' dbkey = old.rdb$db_key;');
Query.Add(' else');
Query.Add(' dbkey = new.rdb$db_key;');
Query.Add(' select max(change_number) from rpl$tmp_changes into :change_number;');
Query.Add(' if (not deleting) then begin');
if GetKeySyncVals <> '' then begin
//Generate the synchronization statements for primary and unique keys
//We don't convert "new." to "old." in the GenericSyncStatement, which means we always take the new value of the fields, if applicable
//This doesn't matter because the synchronization statements are only used for inserting new key values, not for old records
//For this reason, the following code is also excluded from the delete triggers
Query.Add(GetKeySyncVals);
end;
GetFieldValues('new');
Query.Add(' end');
Query.Add(' select list((select quoted_str from RPL$QUOTE_STR(val)), '';'') || '';''');
Query.Add(' from (select coalesce(r.old_value, r.new_value) as val');
Query.Add(' from rdb$relation_constraints rel');
Query.Add(' join rdb$index_segments i on rel.rdb$index_name = i.rdb$index_name');
Query.Add(' join rpl$tmp_values r on r.field_name = i.rdb$field_name and r.change_number = :change_number');
Query.Add(' where rel.rdb$constraint_type = ''PRIMARY KEY''');
Query.Add(' and rel.rdb$relation_name = ' + QuotedStr(TableName));
Query.Add(' order by i.rdb$field_position) into :primary_key_values;');
Query.Add(' if (primary_key_values is null) then');
Query.Add(' select list(coalesce((select quoted_str from RPL$QUOTE_STR(val)), ''"''), '';'') || '';''');
Query.Add(' from (select coalesce(r.old_value, r.new_value) as val');
Query.Add(' from rdb$index_segments ins');
Query.Add(' left outer join rpl$tmp_values r on r.field_name = ins.rdb$field_name and r.change_number = :change_number');
Query.Add(' where ins.rdb$index_name = (select first 1 i.rdb$index_name as index_name');
Query.Add(' from rdb$indices i');
Query.Add(' where i.rdb$relation_name = ' + QuotedStr(TableName));
Query.Add(' and i.rdb$unique_flag = 1)');
Query.Add(' order by ins.rdb$field_position');
Query.Add(' ) into :primary_key_values;');
Query.Add(' if (primary_key_values is null) then');
Query.Add(' select list(coalesce((select quoted_str from RPL$QUOTE_STR(val)), ''"''), '';'') || '';''');
Query.Add(' from (select coalesce(r.old_value, r.new_value) as val');
Query.Add(' from rdb$relation_fields rf');
Query.Add(' join rdb$fields f on f.rdb$field_name = rf.rdb$field_source');
Query.Add(' left outer join rpl$tmp_values r on r.field_name = rf.rdb$field_name and r.change_number = :change_number');
Query.Add(' where rf.rdb$relation_name = ' + QuotedStr(TableName));
Query.Add(' and f.rdb$field_type <> 261 and f.rdb$field_length < 50');
Query.Add(' order by rf.rdb$field_name');
Query.Add(' ) into :primary_key_values;');
Query.Add(' if (inserting) then operation_type = ''I'';');
Query.Add(' else if (updating) then operation_type = ''U'';');
Query.Add(' else operation_type = ''D'';');
Query.Add(' if (exists(select field_name from rpl$tmp_values where change_number = :change_number and (old_value is distinct from new_value or old_blob is distinct from new_blob)) or (rdb$get_context(''USER_TRANSACTION'', ''FORCE_REPLICATION'') = ''TRUE'')) then begin');
Query.Add(' for select u.login');
Query.Add(' from RPL$users u');
Query.Add(' where (u.login <> :replicating_node or :replicating_node is null)');
Query.Add(' and (u.config_name is null or u.config_name = ' + QuotedStr(ConfigName) + ')');
//User-definable SQL condition
if cCondition <> '' then
Query.Add(' and ((' + ParseSQLCondition(TableName, cCondition, 'old') + ') or (' + ParseSQLCondition(TableName, cCondition, 'new') + '))');
Query.Add(' into :user_login do');
Query.Add(' begin');
Query.Add(' insert into RPL$log (code, change_number, login, operation_date, table_name, sent_from,');
Query.Add(' primary_key_values, procedure_statement, PRIMARY_KEY_SYNC, UNIQUE_KEY_SYNC, operation_type, transaction_number)');
Query.Add(' values (gen_id(gen_rpl$log, 1), :change_number, :user_login, current_timestamp, ' + QuotedStr(TableName) + ', :replicating_node,');
Query.Add(' :primary_key_values, null, :PRIMARY_KEY_SYNC, :UNIQUE_KEY_SYNC, :operation_type, current_transaction);');
Query.Add(' insert into rpl$log_values (CHANGE_NUMBER, NODE_NAME, OLD_VALUE, OLD_VALUE_BLOB,new_VALUE, new_VALUE_BLOB,FIELD_NAME,FIELD_TYPE, OLD_BLOB_NULL, NEW_BLOB_NULL)');
Query.Add(' select :change_number, :user_login, v.old_value, v.old_blob, v.new_value, v.new_blob, v.field_name, v.field_type, v.OLD_BLOB_NULL, v.NEW_BLOB_NULL');
Query.Add(' from rpl$tmp_values v');
Query.Add(' where v.change_number = :change_number and ((v.old_value is distinct from v.new_value or v.old_blob is distinct from v.new_blob) or (rdb$get_context(''USER_TRANSACTION'', ''FORCE_REPLICATION'') = ''TRUE''));');
Query.Add(' end');
Query.Add(' end');
Query.Add(' delete from rpl$tmp_values where change_number = :change_number;');
Query.Add(' delete from rpl$tmp_changes where change_number = :change_number;');
Query.Add('end');
ExecConfQuery;
nTriggerNumber := 2;
end;
procedure DoGenerateSQL(cNewOld: String; cOperationType: String);
var
I: Integer;
lConditionField: Boolean;
lCondition : Boolean;
// lCheckPKChanged : Boolean;
cSync: String;
begin
// lCheckPKChanged := (cNewOld = 'new') and (cOperationType = 'update');
lConditionField := (Trim(qTable.Field['CONDITION_FIELD'].AsString) <> '');
lCondition := (Trim(qTable.Field['CONDITION'].AsString) <> '');
Query.Add(' primary_key_values = '''';');
Query.Add(GetPKVals(cNewOld));
//Generate the synchronization statements for primary and unique keys
//We don't convert "new." to "old." in the GenericSyncStatement, which means we always take the new value of the fields, if applicable
//This doesn't matter because the synchronization statements are only used for inserting new key values, not for old records
//For this reason, the following code is also excluded from the delete triggers
if (cOperationType <> 'delete') then begin
Query.Add(GetKeySyncVals);
end;
Query.Add('if (inserting) then operation_type = ''I'';');
Query.Add('else if (updating) then operation_type = ''U'';');
Query.Add('else operation_type = ''D'';');
// if lCheckPKChanged then begin
// Query.Add(' if ((primary_key_values <> old_primary_key_values)');
// if lRefField then
// Query.Add(' or (old.' + MetaQuote(qTable.Field['REF_FIELD'].AsString) + ' <> new.' + MetaQuote(qTable.Field['REF_FIELD'].AsString) + ')');
// Query.Add(' ) then begin');
// end;
Query.Add(' for select u.login, t.repl_' + cOperationType + 's');
Query.Add(' from RPL$users u');
if (ConfigName <> '') then
Query.Add(' join rpl$tables_config t on t.table_name = ' + QuotedStr(TableName) + ' and t.config_name = ' + QuotedStr(ConfigName))
else
Query.Add(' join rpl$tables t on t.table_name = ' + QuotedStr(TableName));
Query.Add(' where (u.login <> :replicating_node or :replicating_node is null)');
if (ConfigName <> '') then
Query.Add(' and (u.config_name is null or u.config_name = ' + QuotedStr(ConfigName) + ')');
//User-definable SQL condition, configurable per table in RPL$TABLES
if lCondition then
Query.Add(' and (' + ParseSQLCondition(TableName, qTable.Field['CONDITION'].AsString, cNewOld) + ')');
if Assigned(qTableConf) then begin
if Trim(qTableConf.Field['CONDITION'].AsString) <> '' then
Query.Add(' and (' + ParseSQLCondition(TableName, qTableConf.Field['CONDITION'].AsString, cNewOld) + ')');
if Trim(qTableConf.Field[cOperationType + '_CONDITION'].AsString) <> '' then
Query.Add(' and (' + ParseSQLCondition(TableName, qTableConf.Field[cOperationType + '_CONDITION'].AsString, cNewOld) + ')');
end;
if lConditionField then
//If the condition field is null, the record is replicated to any user
//If RPL$USERS.Condition_value is null, the user gets all changes
Query.Add(' and ((' + cNewOld + '.' + MetaQuote(Trim(qTable.Field['CONDITION_FIELD'].AsString)) + ' = u.condition_value) or (u.condition_value is null))');
Query.Add(' into :user_login, :repl_operation do');
Query.Add(' begin');
Query.Add(' if (repl_operation = ''Y'') then');
Query.Add(' insert into RPL$log (code, login, operation_date, table_name, sent_from,');
Query.Add(' primary_key_values, procedure_statement, PRIMARY_KEY_SYNC, UNIQUE_KEY_SYNC, OPERATION_TYPE, transaction_number)');
Query.Add(' values (gen_id(gen_rpl$log, 1), :user_login, current_timestamp, ' + QuotedStr(TableName) + ', :replicating_node,');
Query.Add(' :primary_key_values, null, :PRIMARY_KEY_SYNC, :UNIQUE_KEY_SYNC, :OPERATION_TYPE, current_transaction);');
// Query.Add(' execute procedure RPL$generate_log (' + QuotedStr(TableName) + ', :primary_key_values, null, null, :user_login, :primary_key_sync, :unique_key_sync);');
Query.Add(' end');
// if lCheckPKChanged then
// Query.Add(' end');
end;
var
lFieldRestrictions : Boolean;
begin
nTriggerNumber := 1;
if not FConnection.InTransaction then
FConnection.StartTransaction;
TableName := Trim(qTable.Field['TABLE_NAME'].AsString);
FKeys.FailIfNoPK := FailIfNoPK;
FKeys.ClearTableKeys;
if Assigned(qTableConf) then begin
cTriggerName := Trim(qTableConf.Field['TRIG_BASE_NAME'].AsString);
ConfigName := Trim(qTableConf.Field['CONFIG_NAME'].AsString);
FKeys.LoadKeys(TableName, '', '', '', qTableConf.Field['PRIMARY_KEY_SYNC'].AsString,
qTableConf.Field['UNIQUE_KEY_NAMES'].AsString, qTableConf.Field['UNIQUE_KEY_SYNC'].AsString, koDelphi);
end
else begin
cTriggerName := Trim(qTable.Field['TRIG_BASE_NAME'].AsString);
FKeys.LoadKeys(TableName, '', '', '', qTable.Field['PRIMARY_KEY_SYNC'].AsString,
qTable.Field['UNIQUE_KEY_NAMES'].AsString, qTable.Field['UNIQUE_KEY_SYNC'].AsString, koDelphi);
end;
if TrackFieldChanges and (DBBranch = dbFirebird) and (BranchVersion >= 21) then begin
GenerateChangedFields;
end
else begin