forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpFIBDataInfo.pas
2793 lines (2573 loc) · 73.2 KB
/
pFIBDataInfo.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:gdeatz@hlmdd.com }
{ }
{ Copyright (c) 1998-2013 Devrace Ltd. }
{ Written by Serge Buzadzhy (buzz@devrace.com) }
{ }
{ ------------------------------------------------------------- }
{ 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 pFIBDataInfo;
interface
{$I FIBPlus.inc}
uses
{$IFDEF WINDOWS}
Windows, // For inline functions
{$ENDIF}
SyncObjs,SysUtils,Classes,DB,FIBPlatforms,FIBDataSet,FIBDataBase,FIBQuery,
pFIBQuery,pFIBDataBase,pFIBProps
;
type
TExtBoolean= (eFalse,eTrue,eUnknown);
TpFIBFieldInfo=class
private
FIsComputed :boolean;
FDefaultValue:Ansistring;
FCanIncToWhereClause:boolean;
FDomainName:Ansistring;
FDefaultValueEmptyString: boolean;
// Info from FIB$FIELDS_INFO
FWithAdditionalInfo:boolean;
FDisplayLabel :Ansistring;
FVisible :boolean;
FEditFormat :Ansistring;
FDisplayFormat:Ansistring;
FIsTriggered :boolean;
FOtherInfo :TStrings;
FDisplayWidth :integer;
FCanBeBoolean :TExtBoolean;
FCanBeGUID :TExtBoolean;
FCharSet :integer;
function GetCanBeBoolean:boolean;
function GetCanBeGUID:boolean;
function GetCharSet:string;
public
constructor Create;
destructor Destroy;override;
procedure SaveToStream(Stream:TStream);
procedure LoadFromStream(Stream:TStream;FromBegin:boolean);
property IsComputed :boolean read FIsComputed;
property DefaultValue:Ansistring read FDefaultValue;
property DomainName :Ansistring read FDomainName;
property CanIncToWhereClause:boolean read FCanIncToWhereClause;
// Info from FIB$FIELDS_INFO
property WithAdditionalInfo:boolean read FWithAdditionalInfo;
property DisplayLabel :Ansistring read FDisplayLabel;
property Visible :boolean read FVisible;
property EditFormat :Ansistring read FEditFormat;
property DisplayFormat:Ansistring read FDisplayFormat;
property IsTriggered :boolean read FIsTriggered;
property OtherInfo :TStrings read FOtherInfo;
property DisplayWidth :integer read FDisplayWidth;
property DefaultValueEmptyString:boolean read FDefaultValueEmptyString;
property CanBeBoolean :boolean read GetCanBeBoolean;
property CanBeGuid :boolean read GetCanBeGUID;
property CharSetID :integer read FCharSet;
property CharSet :string read GetCharSet;
end;
TpFIBTableInfoCollect= class;
TpFIBTableInfo=class
private
FOwner:TpFIBTableInfoCollect;
FDBName :Ansistring;
FTableName :Ansistring;
FPrimaryKeyFields :Ansistring;
FFieldList :TStringList;
FFormatNumber :integer;
FFIVersion :integer;//FIB$FIELDS_INFO
FWithFieldRepositaryInfo :boolean;
FStreamVersion :integer;
FNonValidated :boolean;
FTableID :integer;
procedure GetInfoFields(const TableName:string;aTransaction:TFIBTransaction);
procedure GetRepositaryFieldInfos(const TableName:string;aTransaction:TFIBTransaction);
procedure ClearFieldList;
procedure GetAdditionalInfo(FromQuery:TFIBDataset;
const aFieldName:string;ToFieldInfo:TpFIBFieldInfo );
procedure FillInfo(DB:TFIBDataBase;Tr:TFIBTransaction; const ATableName:string);
function IsActualInfo(DB:TFIBDatabase) :boolean;
function GetPrimaryKeyFields(DB:TFIBDatabase): string;
function GetTableId(DB:TFIBDatabase):integer;
procedure LoadFromStreamVersion(Stream:TStream;aStreamVersion:integer);
public
constructor Create(AOwner:TpFIBTableInfoCollect);
destructor Destroy;override;
procedure SaveToStream(Stream:TStream);
procedure LoadFromStream(Stream:TStream;FromBegin:boolean);
function FieldInfo(const FieldName:string):TpFIBFieldInfo;
property TableName:Ansistring read FTableName ;
property PrimaryKeyFields[DB:TFIBDatabase]:string read GetPrimaryKeyFields;
property FieldList :TStringList read FFieldList;
property TableID[DB:TFIBDatabase]:integer read GetTableId;
end;
TpFIBTableInfoCollect= class(TComponent)
private
FDBNames :TStringList;
FListTabInfo:TStringList;
FLock: TCriticalSection;
FNeedValidate: boolean;
FLastTransactionId:Integer;
procedure PrepareInternalTransaction(aDataBase:TFIBDataBase);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
constructor Create(AOwner:TComponent); override;
destructor Destroy;override;
procedure SaveToStream(Stream:TStream);
procedure LoadFromStream(Stream:TStream;DB:TFIBDatabase=nil);
procedure SaveToFile(const FileName:string);
function LoadFromFile(const FileName:string;DB:TFIBDatabase=nil):boolean;
procedure ValidateSchema(aDataBase:TFIBDataBase;Proc:TpFIBAcceptCacheSchema);
function FindTableInfo(const ADBName,ATableName:string):TpFIBTableInfo;
function GetTableInfo(aDataBase:TFIBDataBase;const ATableName:string;
WithRepositaryInfo:boolean
):TpFIBTableInfo;
function
GetFieldInfo(aDataBase:TFIBDataBase; const ATableName,AFieldName:string;
RepositoryInfo:boolean
):TpFIBFieldInfo;
procedure Clear;
procedure ClearForDataBase(aDatabase:TFIBDataBase);
procedure ClearForTable(const TableName:string) ;
procedure CommitInternalTransaction ;
property NeedValidate :boolean read FNeedValidate write FNeedValidate;
end;
TpDataSetInfo= class
private
FDBName:Ansistring;
FVersion:integer;
FSelectSQL:TStrings;
FInsertSQL:TStrings;
FUpdateSQL:TStrings;
FDeleteSQL:TStrings;
FRefreshSQL:TStrings;
FKeyField:Ansistring;
FGeneratorName:Ansistring;
FDescription :Ansistring;
FUpdateTableName:Ansistring;
FUpdateOnlyModifiedFields:boolean;
FConditions :Ansistring;
FNonValidated :boolean;
public
constructor Create(DataSet:TFIBDataSet); overload;
constructor Create; overload;
destructor Destroy;override;
procedure SaveToStream(Stream:TStream);
procedure LoadFromStream(Stream:TStream);
end;
TpDataSetInfoCollect= class
private
FListDataSetInfo:TStringList;
FLock: TCriticalSection;
FNeedValidate: boolean;
function ActualVersionDSInfo(DataSet:TFIBDataSet):integer;
public
constructor Create;
destructor Destroy;override;
function FindDataSetInfo(DataSet:TFIBDataSet; var Index:integer):TpDataSetInfo; overload;
function FindDataSetInfo(const DBName:string;DS_ID:Integer ; var Index:integer):TpDataSetInfo; overload;
function GetDataSetInfo(DataSet:TFIBDataSet):TpDataSetInfo;
function LoadDataSetInfo(DataSet:TFIBDataSet):boolean;
procedure Clear;
procedure ClearDSInfo(DataSet:TFIBDataSet); overload;
procedure ClearDSInfo(DS_ID:integer); overload;
procedure SaveToStream(Stream:TStream);
procedure LoadFromStream(Stream:TStream);
procedure SaveToFile(const FileName:string);
function LoadFromFile(const FileName:string):boolean;
property NeedValidate :boolean read FNeedValidate write FNeedValidate;
end;
TpStoredProcCollect= class
private
FStoredProcNames:TStringList;
FSPParamTxt:TStringList;
FLock: TCriticalSection;
function IndexOfSP(DB:TFIBDatabase;const SPName:string;
ForceReQuery:boolean):integer;
function GetParamsText(DB:TFIBDatabase;const SPName:string):string;
public
constructor Create;
destructor Destroy;override;
procedure Clear;
function GetExecProcTxt(DB:TFIBDatabase;
const SPName:string; ForceReQuery:boolean
):string;
procedure ClearSPInfo(DB:TFIBDatabase);
end;
TpErrorMessage=class
private
FErrorMsg :Ansistring;
FVersion :integer;
public
constructor Create(const aErrorMsg:string; aVersion:integer);
public
property ErrorMsg :Ansistring read FErrorMsg ;
property Version :integer read FVersion;
end;
TpErrorMessagesCollect=class
private
FDatabases :TStringList;
FMaxVersion:integer;
FValidated :boolean;
FLock: TMultiReadExclusiveWriteSynchronizer;
function FindErrorMessage(const DBName,ErrorName:string;
var BaseIndex,ErrorIndex:integer):boolean;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
public
procedure SaveToStream(Stream:TStream);
procedure LoadFromStream(Stream:TStream);
procedure SaveToFile(const FileName:string);
procedure LoadFromFile(const FileName:string);
public
procedure AddErrorMessage(const DBName,ErrorName,ErrorMsg:string;
Version:integer);
function ErrorMessage(Tr:TFIBTransaction; const ErrorName:string):string;
end;
var
ListTableInfo :TpFIBTableInfoCollect;
ListDataSetInfo:TpDataSetInfoCollect;
ListSPInfo :TpStoredProcCollect;
ListErrorMessages:TpErrorMessagesCollect;
// Manage Developer Info tables
procedure CreateFRepositaryTable(DB:TFIBDatabase);
procedure CreateDRepositaryTable(DB:TFIBDatabase);
procedure CreateERepositaryTable(DB:TFIBDatabase);
function ExistFRepositaryTable(DB:TFIBDatabase):boolean;
function ExistDRepositaryTable(DB:TFIBDatabase):boolean;
function ExistERepositaryTable(DB:TFIBDatabase):boolean;
function ExistBooleanDomain(DB:TFIBDatabase):boolean;
function SaveFIBDataSetInfo(DataSet:TFibDataSet; const Name:string):boolean;
// Routine function
function GetFieldInfos(Field:TField;RepositaryInfo:boolean):TpFIBFieldInfo;
function GetOtherFieldInfo(Field:TField;const InfoName:string):string;
procedure Update1RepositaryTable(Tr:TFIBTransaction);
procedure Update2RepositaryTable(Tr:TFIBTransaction);
function DBPrimaryKeyFields(const TableName:string;
aTransaction:TFIBTransaction
):string;
implementation
uses pFIBDataSet,StrUtil,FIBConsts,pFIBCacheQueries,ibase,SqlTxtRtns
{$IFDEF D_XE3}
{$IFDEF MACOS}
,Posix.Unistd // for inline functions
{$ENDIF}
{$ENDIF}
;
type
TFriendDatabase= class(TFIBDatabase);
var
DatabaseRepositories:TStringList;
LockRepList: TCriticalSection;
const
SDefer='@FIB_DEFERRED';
FormatNumbersSQL=
'select a1.rdb$relation_name,a1.RDB$RELATION_ID VER from RDB$RELATIONS a1'+CLRF+
'where a1.RDB$SYSTEM_FLAG = 0'+CLRF+
'and not a1.rdb$view_blr is null'+CLRF+
'union'+CLRF+
'select R.rdb$relation_name,RDB$FORMAT from RDB$RELATIONS R'+CLRF+
'where R.RDB$SYSTEM_FLAG = 0'+CLRF+
'and R.rdb$view_blr is null order by 1';
function ExistRepositaryTable(DB:TFIBDatabase;Kind:byte):boolean;
var aTransaction:TFibTransaction;
qry :TFIBQuery;
Index :integer;
s :Ansistring;
cr :TExtBoolean;
function RepositaryIsRegistered:TExtBoolean;
begin
Index:=DatabaseRepositories.IndexOfObject(DB);
if Index>-1 then
begin
case DatabaseRepositories[Index][Kind] of
'0': Result:=eFalse;
'1': Result:=eTrue;
else
Result:=eUnknown;
end
end
else
Result:=eUnknown;
end;
begin
LockRepList.Acquire;
try
if csDesigning in DB.ComponentState then
begin
Index:=-1;
DatabaseRepositories.Clear;
cr :=eUnknown;
end
else
cr :=RepositaryIsRegistered;
Result:=cr =eTrue;
if cr<>eUnknown then Exit;
if not (TFIBUseRepository(Kind-1) in DB.UseRepositories) then Exit;
if Index<0 then
s:='222'
else
s:=DatabaseRepositories[Index];
aTransaction:=TFIBTransaction.Create(nil);
aTransaction.TimeoutAction :=TACommit;
aTransaction.DefaultDatabase:=DB;
qry:=TFIBQuery.Create(nil);
with qry do
try
ParamCheck:=True;
Database:=DB; Transaction:=aTransaction;
SQL.Text:='select COUNT(RDB$RELATION_NAME)'+CLRF+
'from RDB$RELATIONS where RDB$FLAGS = 1'+CLRF+
'and RDB$RELATION_NAME=?RT';
aTransaction.StartTransaction;
case Kind of
1: Params[0].asString:='FIB$FIELDS_INFO';
2: Params[0].asString:='FIB$DATASETS_INFO';
3: Params[0].asString:='FIB$ERROR_MESSAGES';
end;
ExecQuery;
if Fields[0].asInteger>0 then
s[Kind]:='1'
else
s[Kind]:='0';
Close;
if Index<0 then
DatabaseRepositories.AddObject(s,DB)
else
DatabaseRepositories[Index]:=s;
Result:=RepositaryIsRegistered=eTrue;
finally
aTransaction.Free;
Free;
end;
finally
LockRepList.Release;
end;
end;
function ExistDRepositaryTable(DB:TFIBDatabase):boolean;
begin
Result:=ExistRepositaryTable(DB,2)
end;
function ExistERepositaryTable(DB:TFIBDatabase):boolean;
begin
Result:=ExistRepositaryTable(DB,3)
end;
function ExistBooleanDomain(DB:TFIBDatabase):boolean;
var
Transaction:TFibTransaction;
qry:TFIBQuery;
begin
qry:=TFIBQuery.Create(nil);
Transaction:=TFIBTransaction.Create(nil);
try
Transaction.DefaultDatabase:=DB;
qry.Transaction:=Transaction;
with qry,qry.SQL do
begin
ParamCheck:=False;
Database:=DB;
ParamCheck:=False;
Transaction.StartTransaction;
Text:=
'Select Count(*) FROM RDB$FIELDS FLD'+CLRF +
'WHERE FLD.RDB$FIELD_NAME = ''FIB$BOOLEAN''' ;
ExecQuery;
Result:=qry.Fields[0].asInteger<>0;
end
finally
Transaction.Free;
qry.Free;
end;
end;
function ExistFRepositaryTable(DB:TFIBDatabase):boolean;
begin
Result:=ExistRepositaryTable(DB,1)
end;
procedure Update1RepositaryTable(Tr:TFIBTransaction);
var qry:TFIBQuery;
begin
qry:=TFIBQuery.Create(nil);
with qry,qry.SQL do
try
Database:=Tr.DefaultDatabase; Transaction:=Tr;
ParamCheck:=False;
if not Tr.InTransaction then Tr.StartTransaction;
try
Text:='CREATE GENERATOR FIB$FIELD_INFO_VERSION';
ExecQuery;
except
end;
try
Text:='ALTER TABLE FIB$FIELDS_INFO ADD DISPLAY_WIDTH INTEGER DEFAULT 0';
ExecQuery;
except
end;
try
Text:='ALTER TABLE FIB$FIELDS_INFO ADD FIB$VERSION INTEGER';
ExecQuery;
except
end;
try
Text:=
'CREATE TRIGGER FIB$FIELDS_INFO_BI FOR FIB$FIELDS_INFO '+
'ACTIVE BEFORE INSERT POSITION 0 as '+CLRF+
'begin '+#13#10+
'new.fib$version=gen_id(fib$field_info_version,1);'+CLRF+
'end';
ExecQuery;
except
end;
try
Text:=
'CREATE TRIGGER FIB$FIELDS_INFO_BU FOR FIB$FIELDS_INFO '+
'ACTIVE BEFORE UPDATE POSITION 0 as '+CLRF+
'begin '+CLRF+
'new.fib$version=gen_id(fib$field_info_version,1);'+CLRF+
'end';
ExecQuery;
except
end;
finally
Tr.CommitRetaining;
Free
end;
end;
procedure Update2RepositaryTable(Tr:TFIBTransaction);
var qry:TFIBQuery;
begin
qry:=TFIBQuery.Create(nil);
with qry,qry.SQL do
try
Database:=Tr.DefaultDatabase; Transaction:=Tr;
ParamCheck:=False;
if not ExistBooleanDomain(Transaction.DefaultDatabase) then
begin
Text:=
'CREATE DOMAIN FIB$BOOLEAN AS SMALLINT DEFAULT 1 NOT NULL CHECK (VALUE IN (0,1))';
ExecQuery;
end;
try
Text:='ALTER TABLE FIB$DATASETS_INFO ADD UPDATE_TABLE_NAME VARCHAR(68)';
ExecQuery;
except
end;
try
Text:='ALTER TABLE FIB$DATASETS_INFO ADD UPDATE_ONLY_MODIFIED_FIELDS FIB$BOOLEAN NOT NULL';
ExecQuery;
except
end;
try
Text:='ALTER TABLE FIB$DATASETS_INFO ADD CONDITIONS BLOB sub_type 1 segment size 80';
ExecQuery;
except
end;
if not Tr.InTransaction then Tr.StartTransaction;
try
Text:='CREATE GENERATOR FIB$FIELD_INFO_VERSION';
ExecQuery;
except
end;
try
Text:='ALTER TABLE FIB$DATASETS_INFO ADD fib$version INTEGER';
ExecQuery;
Text:=
'CREATE TRIGGER FIB$DATASETS_INFO_BI FOR FIB$DATASETS_INFO '+
'ACTIVE BEFORE INSERT POSITION 0 as '+CLRF +
'begin'+CLRF+
'new.fib$version=gen_id(fib$field_info_version,1);'+CLRF+
'end';
ExecQuery;
Text:=
'CREATE TRIGGER FIB$DATASETS_INFO_BU FOR FIB$DATASETS_INFO '+
'ACTIVE BEFORE UPDATE POSITION 0 as '+CLRF+
'begin'+CLRF+
'new.fib$version=gen_id(fib$field_info_version,1);'+CLRF+
'end';
ExecQuery;
except
end;
finally
Tr.CommitRetaining;
qry.Free
end
end;
procedure DoCreateRepositaryTable(DB:TFIBDatabase;Kind:byte);
var Index:integer;
Transaction:TFibTransaction;
qry:TFIBQuery;
begin
qry:=TFIBQuery.Create(nil);
Transaction:=TFibTransaction.Create(nil);
try
Transaction.DefaultDatabase:=DB;
qry.Database:=DB; qry.Transaction:=Transaction;
qry.ParamCheck:=False;
Transaction.StartTransaction;
with qry,qry.SQL do
case Kind of
1: begin
if not ExistBooleanDomain(DB) then
begin
Text:=
'CREATE DOMAIN FIB$BOOLEAN AS SMALLINT DEFAULT 1 NOT NULL CHECK (VALUE IN (0,1))';
ExecQuery;
end;
Text:=
'CREATE TABLE FIB$FIELDS_INFO (TABLE_NAME VARCHAR(31) NOT NULL,'+CLRF+
'FIELD_NAME VARCHAR(31) NOT NULL,'+CLRF+
'DISPLAY_LABEL VARCHAR(25),'+CLRF+
'VISIBLE FIB$BOOLEAN DEFAULT 1 NOT NULL,'+CLRF+
'DISPLAY_FORMAT VARCHAR(15),'+CLRF+
'EDIT_FORMAT VARCHAR(15),'+CLRF+
'TRIGGERED FIB$BOOLEAN DEFAULT 0 NOT NULL,'+CLRF+
'CONSTRAINT PK_FIB$FIELDS_INFO PRIMARY KEY (TABLE_NAME, FIELD_NAME))';
ExecQuery;
Text:='GRANT SELECT ON TABLE FIB$FIELDS_INFO TO PUBLIC';
ExecQuery;
Update1RepositaryTable(Transaction);
end;
2:
begin
Text:=
'CREATE TABLE FIB$DATASETS_INFO (DS_ID INTEGER NOT NULL,'+CLRF+
'DESCRIPTION VARCHAR(40),'+
'SELECT_SQL BLOB sub_type 1 segment size 80,'+CLRF+
'UPDATE_SQL BLOB sub_type 1 segment size 80,'+CLRF+
'INSERT_SQL BLOB sub_type 1 segment size 80,'+CLRF+
'DELETE_SQL BLOB sub_type 1 segment size 80,'+CLRF+
'REFRESH_SQL BLOB sub_type 1 segment size 80,'+CLRF+
'NAME_GENERATOR VARCHAR(68), '+
'KEY_FIELD VARCHAR(68),'+
'CONSTRAINT PK_FIB$DATASETS_INFO PRIMARY KEY (DS_ID))';
ExecQuery;
Text:=
'GRANT SELECT ON TABLE FIB$DATASETS_INFO TO PUBLIC';
ExecQuery;
Update2RepositaryTable(Transaction);
end;
3:
begin
Text:=
'CREATE TABLE FIB$ERROR_MESSAGES ('+
'CONSTRAINT_NAME VARCHAR(67) NOT NULL,'+
'MESSAGE_STRING VARCHAR(100),'+
'FIB$VERSION INTEGER,'+
'CONSTR_TYPE VARCHAR(11) DEFAULT ''UNIQUE'' NOT NULL,'+
'CONSTRAINT PK_FIB$ERROR_MESSAGES PRIMARY KEY (CONSTRAINT_NAME))'
;
ExecQuery;
Text:=
'GRANT SELECT ON TABLE FIB$ERROR_MESSAGES TO PUBLIC';
ExecQuery;
try
Text:='CREATE GENERATOR FIB$FIELD_INFO_VERSION';
ExecQuery;
except
end;
Text:=
'CREATE TRIGGER BI_FIB$ERROR_MESSAGES FOR FIB$ERROR_MESSAGES '+
'ACTIVE BEFORE INSERT POSITION 0 AS '+CLRF+
'begin '+CLRF+'new.fib$version=gen_id(fib$field_info_version,1);'+CLRF+' end';
ExecQuery;
Text:=
'CREATE TRIGGER BU_FIB$ERROR_MESSAGES FOR FIB$ERROR_MESSAGES '+
'ACTIVE BEFORE UPDATE POSITION 0 AS '+CLRF+
'begin '+CLRF+'new.fib$version=gen_id(fib$field_info_version,1);'+CLRF+' end';
ExecQuery;
end;
end;
Transaction.Commit;
finally
Transaction.Free;
qry.Free;
end;
with DatabaseRepositories do
begin
Index:=IndexOfObject(DB);
if Index=-1 then
AddObject(FastCopy('222',1,Kind-1)+'1'+FastCopy('222',1,Kind+1),DB)
else
DatabaseRepositories[Index]:=
FastCopy(DatabaseRepositories[Index],1,Kind-1)+'1'+FastCopy(DatabaseRepositories[Index],1,Kind+1);
end;
end;
procedure CreateDRepositaryTable(DB:TFIBDatabase);
begin
DoCreateRepositaryTable(DB,2)
end;
procedure CreateFRepositaryTable(DB:TFIBDatabase);
begin
DoCreateRepositaryTable(DB,1)
end;
procedure CreateERepositaryTable(DB:TFIBDatabase);
begin
DoCreateRepositaryTable(DB,3)
end;
function ExchangeDataSetInfo(DataSet:TFibDataSet;DS_Info:TpDataSetInfo;aDescription:string=''):boolean;
var
vTransaction:TFibTransaction;
DI:TpFIBDataset;
vDescription:string;
tf1:TFIBXSQLVAR;
q:TFIBQuery;
begin
Result:=True;
if DataSet is TpFIBDataSet then
with TpFIBDataSet (DataSet) do
begin
if DataSet_ID= 0 then
raise Exception.Create(Name + SCompEditDataSet_ID);
if DataBase=nil then raise Exception.Create(SDataBaseNotAssigned);
if not (urDataSetInfo in DataSet.Database.UseRepositories) then
raise Exception.Create(SCompEditDataSetInfoForbid);
if not ExistDRepositaryTable(DataSet.Database) then
raise Exception.Create(SCompEditDataSetInfoNotExists);
DI:=TpFIBDataset.Create(nil);
vTransaction:=TFibTransaction.Create(nil);
vTransaction.DefaultDatabase:=DataSet.DataBase;
q:=GetQueryForUse(vTransaction,'SELECT * FROM FIB$DATASETS_INFO WHERE DS_ID=:DS_ID');
try
vTransaction.StartTransaction;
if DS_Info<>nil then
begin
q.Params[0].asInteger:=DataSet_ID;
q.ExecQuery;
if q.RecordCount=0 then
Exit;
with DS_Info,q do
begin
FSelectSQL.Text :=FieldByName('SELECT_SQL').asString;
FUpdateSQL.Text :=FieldByName('UPDATE_SQL').asString;
FInsertSQL.Text :=FieldByName('INSERT_SQL').asString;
FDeleteSQL.Text :=FieldByName('DELETE_SQL').asString;
FRefreshSQL.Text:=FieldByName('REFRESH_SQL').asString;
FKeyField :=FieldByName('KEY_FIELD').asString;
FGeneratorName :=FieldByName('NAME_GENERATOR').asString;
FDescription :=FieldByName('DESCRIPTION') .asString;
tf1:=FindField('FIB$VERSION');
if Assigned(tf1) then
FVersion:=FieldByName('FIB$VERSION').asInteger;
tf1:=FindField('UPDATE_TABLE_NAME');
if Assigned(tf1) then
FUpdateTableName:=tf1.AsString;
tf1:=FindField('UPDATE_ONLY_MODIFIED_FIELDS');
if Assigned(tf1) then
FUpdateOnlyModifiedFields:=tf1.AsInteger=1;
tf1:=FindField('CONDITIONS');
if Assigned(tf1) then
begin
FConditions:=tf1.AsString;
end;
q.Close;
end;
end
else
begin
//For design only
DI.Database:=DataSet.DataBase;
DI.Transaction:=vTransaction;
DI.UpdateTransaction:=vTransaction;
DI.SelectSQL.Text:=
'SELECT * FROM FIB$DATASETS_INFO WHERE DS_ID='+IntToStr(DataSet_ID);
DI.InsertSQL.Text:=
'INSERT INTO FIB$DATASETS_INFO (DS_ID,UPDATE_ONLY_MODIFIED_FIELDS) VALUES('
+IntToStr(DataSet_ID)+',1)';
DI.UpdateSQL.Text:=
'UPDATE FIB$DATASETS_INFO SET SELECT_SQL=?SELECT_SQL,'+
'DESCRIPTION=?DESCRIPTION,'+
'UPDATE_SQL=?UPDATE_SQL,'+
'INSERT_SQL=?INSERT_SQL,'+
'DELETE_SQL=?DELETE_SQL,'+
'REFRESH_SQL=?REFRESH_SQL,'+
'NAME_GENERATOR=?NAME_GENERATOR,'+
'KEY_FIELD=?KEY_FIELD, '+
'UPDATE_TABLE_NAME=?UPDATE_TABLE_NAME, '+
'UPDATE_ONLY_MODIFIED_FIELDS=?UPDATE_ONLY_MODIFIED_FIELDS, '+
'CONDITIONS=?CONDITIONS '+
'WHERE DS_ID='+IntToStr(DataSet_ID)
;
try
DI.QUpdate.Prepare;
except
Update2RepositaryTable(vTransaction);
vTransaction.Commit;
vTransaction.StartTransaction
end;
DI.Open;
if DI.RecordCount=0 then
begin
DI.QInsert.ExecQuery;
DI.Close;DI.Open;
if DI.RecordCount=0 then
raise Exception.Create(SCompEditUnableInsertInfoRecord);
end;
if Length(aDescription)= 0 then
vDescription:=DI.FieldByName('DESCRIPTION') .asString
else
vDescription:=aDescription;
{ Result:=False;
if not InputQuery(SCompEditSaveDataSetProperty, SCompEditDataSetDesc, vDescription
) then Exit;}
DI.Edit;
DI.FieldByName('SELECT_SQL') .asString :=DataSet.SelectSQL.Text;
DI.FieldByName('INSERT_SQL') .asString :=DataSet.InsertSQL.Text;
DI.FieldByName('DELETE_SQL') .asString :=DataSet.DeleteSQL.Text;
DI.FieldByName('UPDATE_SQL') .asString :=DataSet.UpdateSQL.Text;
DI.FieldByName('REFRESH_SQL').asString :=DataSet.RefreshSQL.Text;
DI.FieldByName('KEY_FIELD') .asString :=AutoUpdateOptions.KeyFields;
DI.FieldByName('NAME_GENERATOR').asString :=AutoUpdateOptions.GeneratorName;
DI.FieldByName('DESCRIPTION') .asString :=vDescription;
DI.FieldByName('UPDATE_TABLE_NAME').asString :=AutoUpdateOptions.UpdateTableName;
DI.FieldByName('UPDATE_ONLY_MODIFIED_FIELDS').asInteger:=
Ord(AutoUpdateOptions.UpdateOnlyModifiedFields);
DI.FieldByName('CONDITIONS').asString :=Conditions.ExchangeString;
DI.Post;
end;
vTransaction.Commit;
Result:=True;
finally
FreeQueryForUse(q);
vTransaction.Free;
DI.Free;
end;
end;
end;
function SaveFIBDataSetInfo(DataSet:TFibDataSet;const Name:string):boolean;
begin
Result:=ExchangeDataSetInfo(DataSet,nil,Name);
end;
function DoGetTableId(const TableName:string; aTransaction:TFIBTransaction):integer;
var
q:TFIBQuery;
const
STabId = 'select RDB$RELATION_ID from RDB$RELATIONS '+
'where RDB$RELATION_NAME=?TN';
begin
if (aTransaction=nil) or (aTransaction.DefaultDatabase=nil) then
begin
Result:=-1;
Exit;
end;
q:=GetQueryForUse(aTransaction,STabId);
with q do
try
Params[0].AsString:=TableName;
if not Transaction.InTransaction then
Transaction.StartTransaction;
ExecQuery;
if Eof then
Result:=-1
else
Result:=Fields[0].asInteger
finally
FreeQueryForUse(q);
end
end;
function DBPrimaryKeyFields(const TableName:string;
aTransaction:TFIBTransaction
):string;
var
q:TFIBQuery;
const
SGetPrimary= 'select i.rdb$field_name'+CLRF+
'from rdb$relation_constraints r, rdb$index_segments i'+CLRF+
'where r.rdb$relation_name=:TN and'+CLRF+
'r.rdb$constraint_type=''PRIMARY KEY'' and'+CLRF+
'r.rdb$index_name=i.rdb$index_name'+CLRF +
'order by i.rdb$field_position';
begin
if Length(TableName) = 0 then
begin
result := '';
exit;
end;
if (aTransaction=nil) or (aTransaction.DefaultDatabase=nil) then
begin
Result:=SDefer;
Exit;
end;
q:=GetQueryForUse(aTransaction,SGetPrimary);
Result:='';
with q do
try
q.Options:=[qoStartTransaction];
if TableName[1]='"' then
ParamByName('TN').AsString:=Copy(TableName,2,Length(TableName)-2)
else
ParamByName('TN').AsString:=TableName;
ExecQuery;
Result:=FastTrim(Fields[0].asString); Next;
while not Eof do
begin
Result:=Result+';'+FastTrim(Fields[0].asString);
Next
end;
finally
FreeQueryForUse(q);
end
end;
//StreamRtn
procedure WriteStrToStream(const s:Ansistring;Stream: TStream);
var
L:integer;
begin
L:=Length(s);
with Stream do
begin
WriteBuffer(L,SizeOf(Integer));
if L>0 then
WriteBuffer(s[1],L);
end;
end;
procedure ReadStrFromStream(const Stream: TStream;var ResStr:Ansistring);
var
L:integer;
begin
with Stream do
begin
ReadBuffer(L,SizeOf(Integer));
SetLength(ResStr,L);
if L>0 then
ReadBuffer(ResStr[1],L);
end;
end;
constructor TpFIBFieldInfo.Create;
begin
inherited Create;
FWithAdditionalInfo:=False;
FIsTriggered :=False;
FOtherInfo :=TStringList.Create;
FDefaultValueEmptyString := False;
FDisplayWidth :=0;
FCanBeBoolean :=eUnknown;
FCanBeGUID :=eUnknown;
end;
destructor TpFIBFieldInfo.Destroy;
begin
FOtherInfo.Free;
inherited Destroy;
end;
function TpFIBFieldInfo.GetCanBeBoolean:boolean;
begin
case FCanBeBoolean of
eTrue : Result:=True ;
eFalse : Result:=False;
else
Result:=PosCI('BOOLEAN',DomainName)>0;
FCanBeBoolean :=TExtBoolean(Result);
end;
end;
function TpFIBFieldInfo.GetCanBeGUID:boolean;
begin
case FCanBeGuid of
eTrue : Result:=True ;
eFalse : Result:=False;
else
Result:=PosCI('GUID',DomainName)>0;
FCanBeGuid :=TExtBoolean(Result);
end;
end;
function TpFIBFieldInfo.GetCharSet:string;
begin
if (FCharSet<0) or (FCharSet>IBStdCharSetsCount-1) then
Result:=UnknownStr
else
Result:=IBStdCharacterSets[FCharSet]
end;
procedure TpFIBFieldInfo.SaveToStream(Stream:TStream);
begin
with Stream do
begin
WriteBuffer(FIsComputed,SizeOf(boolean));
WriteBuffer(FCanIncToWhereClause,SizeOf(boolean));
WriteBuffer(FDefaultValueEmptyString,SizeOf(boolean));
WriteBuffer(FWithAdditionalInfo,SizeOf(boolean));
WriteBuffer(FVisible,SizeOf(boolean));
WriteBuffer(FIsTriggered,SizeOf(boolean));
WriteBuffer(FDisplayWidth,SizeOf(integer));
WriteBuffer(FCharSet,SizeOf(integer));
WriteStrToStream(FDefaultValue,Stream);
WriteStrToStream(FDomainName,Stream);
WriteStrToStream(FDisplayLabel,Stream);
WriteStrToStream(FEditFormat,Stream);
WriteStrToStream(FDisplayFormat,Stream);
WriteStrToStream(FOtherInfo.Text,Stream);
end;