forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pFIBMetaData.pas
4869 lines (4119 loc) · 139 KB
/
pFIBMetaData.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 pFIBMetaData;
interface
{$I FIBPlus.inc}
uses
Classes, SysUtils,ibase,DB,FIBPlatforms,
FIBDatabase,FIBQuery,FIBDataSet,pFIBProps,StrUtil,
pFIBInterfaces,fib{$IFDEF D6+}, Variants{$ENDIF}
;
const
DefTerminator = ';';
ProcTerminator = '^';
type
// Metadata Object Identifiers
TDBObjectType = (otDomain, otTable, otView, otProcedure, otGenerator,
otException, otUDF, otRole,otDBTrigger,otStoredFunctions,otPackage,otDatabase);
TDBObjectTypes = set of TDBObjectType;
// SubObjects
TTableChildType = (soTableFields, soPrimaryKey, soForeignKey, soTableTriggers,
soUniqueConstr,soIndex, soCheckConstr,soTableGrants);
TTableChildTypes = set of TTableChildType;
TViewChildType = (soViewFields, soViewTriggers,soViewGrants);
TViewChildTypes = set of TViewChildType;
TProcedureChild = (soProcFieldIn, soProcFieldOut,soProcGrants);
TProcedureChilds = set of TProcedureChild;
TUDFChild = (soUDFField);
TUDFChilds = set of TUDFChild;
TRoleChild=(soRoleGrants);
TTriggerType = (ttBefore, ttAfter,ttON);
TTriggerAction = (taInsert, taUpdate, taDelete,
taConnect,taDisconnect,taTransactionStart,taTransactionCommit,
taTransactionRollback
);
TTriggerActions = set of TTriggerAction;
TIndexOrder = (IoDescending, IoAscending);
TForeignKeyRule = (fkrRestrict, fkrCascade, fkrSetNull, fkrSetDefault);
TFIBFieldType = (fftUnKnown, fftNumeric, fftChar, fftVarchar, fftCstring, fftSmallint,
fftInteger, fftQuad, fftFloat, fftDoublePrecision, fftTimestamp, fftBlob, fftBlobId,
fftDate, fftTime, fftInt64 , fftBoolean);
TPrivilege=(goSelect,goInsert,goUpdate,goDelete,goReferences,goExecuteProc,goRole);
TPrivileges=set of TPrivilege;
TNotifyLoadMetaData=procedure (Sender:TObject; const Message:string; var Stop:boolean) of object;
TDDLTextOption=(dtoUseCreateDB,dtoUseSetClientLib,dtoUseSetTerm,dtoDecodeDomains,dtoIncludeGrants);
TDDLTextOptions=set of TDDLTextOption;
const
DefObjects = [otDomain, otTable, otView, otProcedure, otGenerator,
otException, otUDF, otRole,otDBTrigger];
DefTableChilds = [soTableFields, soPrimaryKey, soForeignKey, soTableTriggers,
soUniqueConstr,soIndex, soCheckConstr];
DefViewChilds = [soViewFields, soViewTriggers];
type
TCustomMetaObject = class;
TMetaObjectClass = class of TCustomMetaObject;
TMetaDomain = class;
TMetaTable = class;
TMetaDataItem = record
Childs: TList;
ClassID: TMetaObjectClass;
end;
TCustomMetaObject = class(TObject)
private
FName: string;
FFormatName:string;
FOwner: TCustomMetaObject;
FItems: array of TMetaDataItem;
FItemsCount: Integer;
FLoaded:boolean;
function GetChildObject(const ClassIndex, Index: Integer): TCustomMetaObject;
function GetChildObjectByName(const ClassIndex: Integer; const ObjectName:string): TCustomMetaObject;
function GetChildsCount(const ClassIndex: Integer): Integer;
function GetAsDDL: string;
procedure AddClass(ClassID: TMetaObjectClass);
function GenerateChildDDL(Stream: TFastStringStream; ChildType: Integer):integer;
function GetAsObjectDDL: string;
procedure SetName(const Value: string);
function GetItems(const Index: Integer): TMetaDataItem;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); virtual;
public
constructor Create(AOwner: TCustomMetaObject;aName:string=''); virtual;
destructor Destroy; override;
procedure Clear;
class function ClassIndex:integer; virtual;
class function IsSubObject:boolean; virtual;
procedure GenerateDDLText(Stream: TFastStringStream); virtual;
property Name: string read FName write SetName;
property AsDDL: string read GetAsDDL;
property AsObjectDDL: string read GetAsObjectDDL;
property ItemsCount: Integer read FItemsCount;
property Items[const Index: Integer]: TMetaDataItem read GetItems;
property Parent: TCustomMetaObject read FOwner;
property FormatName:string read FFormatName;
property Loaded:boolean read FLoaded;
end;
(* GRANT{
<privileges> ON [TABLE] {tablename | viewname}
TO {<object> | <userlist> | GROUP UNIX_group}
| EXECUTE ON PROCEDURE procname TO {<object> | <userlist>}
| <role_granted> TO {PUBLIC | <role_grantee_list>}};
<privileges> = {ALL [PRIVILEGES] | <privilege_list>}
<privilege_list> = {
SELECT
| DELETE
| INSERT
| UPDATE [(col [, col …])]
| REFERENCES [(col [, col …])]
[, <privilege_list> …]}}
<object> = {
PROCEDURE procname
| TRIGGER trigname
| VIEW viewname
| PUBLIC
[, <object> …]}
<userlist> = {
[USER] username
| rolename
| Unix_user}
[, <userlist> …]
[WITH GRANT OPTION]
<role_granted> = rolename [, rolename …]
*)
TCustomMetaGrant =class(TCustomMetaObject)
private
FObjectName:string;
FPrivileges:array of TPrivileges;
FFields :array of string;
FWithGrantOption:array of boolean;
FUserList :array of string;
procedure SetSize(Size:integer);
function GetSize:integer;
procedure LoadFromDataBase(QGrants:TFIBQuery; const Name: string;Pack:boolean=True);
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
property Count :integer read GetSize;
end;
TMetaViewGrant=class(TCustomMetaGrant)
public
class function ClassIndex:integer; override;
end;
TMetaTableGrant=class(TCustomMetaGrant)
public
class function ClassIndex:integer; override;
end;
TMetaProcGrant=class(TCustomMetaGrant)
public
class function ClassIndex:integer; override;
end;
TMetaRoleGrant=class(TCustomMetaGrant)
public
class function ClassIndex:integer; override;
end;
TMetaGenerator = class(TCustomMetaObject)
private
FValue: Integer;
procedure LoadFromDataBase(Transaction: TFIBTransaction; const aName: string);
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
property Value: Integer read FValue;
end;
TArrBounds = record
LOWER_BOUND: INTEGER ;
UPPER_BOUND: INTEGER
end;
TFieldDimensions = array of TArrBounds;
TCustomMetaField = class(TCustomMetaObject)
private
FScale: Word;
FLength: Smallint;
FPrecision: Smallint;
FFieldType: TFIBFieldType;
FCharSet: string;
FSegmentLength: Smallint;
FSubType: Smallint;
FBytesPerCharacter: Smallint;
FIsArray :boolean;
FDimensions:TFieldDimensions;
FIdentity :Integer;
procedure LoadFromDataBase(QField,QCharset:TFIBQuery); virtual;
property SegmentLength: Smallint read FSegmentLength;
function GetShortFieldType: string;
function GetDimensionStr:string;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function IsSubObject:boolean; override;
property Scale: Word read FScale;
property Length: Smallint read FLength;
property Precision: Smallint read FPrecision;
property FieldType: TFIBFieldType read FFieldType;
property CharSet: string read FCharSet;
property SubType: Smallint read FSubType;
property BytesPerCharacter: Smallint read FBytesPerCharacter;
property ShortFieldType: string read GetShortFieldType;
end;
TMetaField = class(TCustomMetaField)
private
procedure LoadFromDataBase(QField,QCharset:TFIBQuery); override;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex: integer; override;
property SegmentLength;
end;
TMetaProcField = class(TMetaField)
private
FDomain:string;
FMechanizm:Smallint;
FUseDomain:boolean;
FUseRelation:boolean;
FRelationObject:string;
FRelationField :string;
FDefaultValue: string;
procedure DoLoadFromQuery(QField ,QCharset:TFIBQuery;CanUseParamDomain,CanUseProcParamRelation:boolean);
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
end;
TMetaProcInField = class(TMetaProcField)
public
class function ClassIndex: integer; override;
end;
TMetaProcOutField = class(TMetaProcField)
public
class function ClassIndex: integer; override;
end;
TMetaTableField = class(TMetaField)
private
FDefaultValue: string;
FNotNull: Boolean;
FDomain: Integer;
FDomainName:string;
FComputedSource: string;
FValidationSource: string;
procedure LoadFromDataBase(Q: TFIBQuery;C:TFIBQuery); override;
function GetDomain: TMetaDomain;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
property DefaultValue: string read FDefaultValue;
property NotNull: Boolean read FNotNull;
property Domain: TMetaDomain read GetDomain;
property ComputedSource: string read FComputedSource;
end;
TMetaDomain = class(TMetaTableField)
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
class function IsSubObject:boolean; override;
procedure GenerateDDLText(Stream: TFastStringStream); override;
end;
TMetaConstraint = class(TCustomMetaObject)
private
FFields: array of Integer;
function GetFields(const Index: Word): TMetaTableField;
function GetFieldsCount: Word;
public
class function IsSubObject:boolean; override;
property Fields[const Index: Word]: TMetaTableField read GetFields;
property FieldsCount: Word read GetFieldsCount;
end;
TMetaPrimaryKey = class(TMetaConstraint)
private
procedure LoadFromDataBase(Q: TFIBQuery);
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
end;
TMetaUniqueConstraint = class(TMetaConstraint)
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
end;
TMetaForeignKey = class(TMetaConstraint)
private
FForTable: Integer;
FForFields: array of Integer;
FOnDelete: TForeignKeyRule;
FOnUpdate: TForeignKeyRule;
function GetForFields(const Index: Word): TMetaTableField;
function GetForFieldsCount: Word;
function GetForTable: TMetaTable;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
property ForTable: TMetaTable read GetForTable;
property ForFields[const Index: Word]: TMetaTableField read GetForFields;
property ForFieldsCount: Word read GetForFieldsCount;
property OnDelete: TForeignKeyRule read FOnDelete;
property OnUpdate: TForeignKeyRule read FOnUpdate;
end;
TMetaCheckConstraint = class(TCustomMetaObject)
private
FConstraint: string;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
property Constraint: string read FConstraint;
end;
TMetaIndex = class(TMetaConstraint)
private
FUnique: Boolean;
FActive: Boolean;
FOrder: TIndexOrder;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
property Unique: Boolean read FUnique;
property Active: Boolean read FActive;
property Order: TIndexOrder read FOrder;
end;
TCustomMetaTrigger = class(TCustomMetaObject)
private
FPrefix: TTriggerType;
FSuffix: TTriggerActions;
FPosition: Smallint;
FActive: Boolean;
FSource: string;
class function DecodePrefix(Value: Integer): TTriggerType;
class function DecodeSuffixes(Value: Integer): TTriggerActions;
procedure LoadFromDataBase(Q: TFIBQuery);
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
procedure SaveFakeDDL(Stream: TFastStringStream); // For Views
procedure SaveAlterDDL(Stream: TFastStringStream);
public
class function IsSubObject:boolean; override;
property Prefix: TTriggerType read FPrefix;
property Suffix: TTriggerActions read FSuffix;
property Position: Smallint read FPosition;
property Active: Boolean read FActive;
property Source: string read FSource;
end;
TMetaTableTrigger=class(TCustomMetaTrigger)
public
class function ClassIndex:integer; override;
end;
TMetaViewTrigger=class(TCustomMetaTrigger)
public
class function ClassIndex:integer; override;
end;
TMetaDBTrigger=class(TCustomMetaTrigger)
public
class function ClassIndex:integer; override;
class function IsSubObject:boolean; override;
end;
TMetaTable = class(TCustomMetaObject)
private
FRelationType:integer;
function GetFields(const Index: Integer): TMetaTableField;
function GetFieldsCount: Integer;
procedure LoadFromDataBase(QNames, QFields: TFIBQuery; QCharset:TFIBQuery; QPrimary,
QIndex, QForeign, QCheck, QTrigger: TFIBQuery; ChildTypes: TTableChildTypes);
function FindFieldIndex(const Name: string): Integer;
function GetUniques(const Index: Integer): TMetaUniqueConstraint;
function GetUniquesCount: Integer;
function GetPrimaryKey: TMetaPrimaryKey;
function GetPrimaryKeyExist: boolean;
function GetIndices(const Index: Integer): TMetaIndex;
function GetIndicesCount: Integer;
function GetForeign(const Index: Integer): TMetaForeignKey;
function GetForeignCount: Integer;
function GetChecks(const Index: Integer): TMetaCheckConstraint;
function GetChecksCount: Integer;
function GetTriggers(const Index: Integer): TCustomMetaTrigger;
function GetTriggersCount: Integer;
function GetGrants(const Index: Integer): TCustomMetaGrant;
function GetGrantsCount: Integer;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
procedure GenerateDDLTextOnlyTable(Stream: TFastStringStream);
public
constructor Create(AOwner: TCustomMetaObject;aName:string=''); override;
class function ClassIndex:integer; override;
function FindFieldName(const Name: string): TMetaTableField;
procedure GenerateDDLText(Stream: TFastStringStream); override;
property Fields[const Index: Integer]: TMetaTableField read GetFields;
property FieldsCount: Integer read GetFieldsCount;
property PrimaryKey: TMetaPrimaryKey read GetPrimaryKey;
property PrimaryKeyExist: boolean read GetPrimaryKeyExist;
property Uniques[const Index: Integer]: TMetaUniqueConstraint read GetUniques;
property UniquesCount: Integer read GetUniquesCount;
property Indices[const Index: Integer]: TMetaIndex read GetIndices;
property IndicesCount: Integer read GetIndicesCount;
property Foreign[const Index: Integer]: TMetaForeignKey read GetForeign;
property ForeignCount: Integer read GetForeignCount;
property Checks[const Index: Integer]: TMetaCheckConstraint read GetChecks;
property ChecksCount: Integer read GetChecksCount;
property Triggers[const Index: Integer]: TCustomMetaTrigger read GetTriggers;
property TriggersCount: Integer read GetTriggersCount;
end;
TMetaView = class(TCustomMetaObject)
private
FSource: string;
function GetFields(const Index: Integer): TMetaField;
function GetFieldsCount: Integer;
function GetTriggers(const Index: Integer): TCustomMetaTrigger;
function GetTriggersCount: Integer;
function GetGrants(const Index: Integer): TCustomMetaGrant;
function GetGrantsCount: Integer;
procedure LoadFromDataBase(QName, QFields, QTriggers: TFIBQuery;
QCharset:TFIBQuery; ChildTypes: TViewChildTypes);
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
constructor Create(AOwner: TCustomMetaObject;aName:string=''); override;
class function ClassIndex:integer; override;
procedure GenerateDDLText(Stream: TFastStringStream); override;
property Source: string read FSource;
property Fields[const Index: Integer]: TMetaField read GetFields;
property FieldsCount: Integer read GetFieldsCount;
property Triggers[const Index: Integer]: TCustomMetaTrigger read GetTriggers;
property TriggersCount: Integer read GetTriggersCount;
end;
TMetaProcedure = class(TCustomMetaObject)
private
FSource: string;
FPackageName:string;
procedure LoadFromDataBase(QNames, QFields: TFIBQuery; QCharset: TFIBQuery;
CanUseParamDomain,CanUseProcParamRelation:boolean );
function GetInputFields(const Index: Integer): TMetaProcInField;
function GetInputFieldsCount: Integer;
function GetOutputFields(const Index: Integer): TMetaProcOutField;
function GetOutputFieldsCount: Integer;
function GetGrants(const Index: Integer): TCustomMetaGrant;
function GetGrantsCount: Integer;
procedure InternalSaveToDDL(Stream: TFastStringStream; Operation: string);
procedure SaveToPostDDL(Stream: TFastStringStream);
procedure SaveToAlterDDL(Stream: TFastStringStream);
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
constructor Create(AOwner: TCustomMetaObject;aName:string=''); override;
class function ClassIndex:integer; override;
property Source: string read FSource;
property InputFields[const Index: Integer]: TMetaProcInField read GetInputFields;
property InputFieldsCount: Integer read GetInputFieldsCount;
property OutputFields[const Index: Integer]: TMetaProcOutField read GetOutputFields;
property OutputFieldsCount: Integer read GetOutputFieldsCount;
property PackageName:string read FPackageName;
end;
TMetaStoredFunc = class(TCustomMetaObject)
private
FPackageName:string;
FSource: string;
function GetInputFields(const Index: Integer): TMetaProcInField;
function GetInputFieldsCount: Integer;
procedure LoadFromDataBase(QNames, QFields: TFIBQuery; QCharset: TFIBQuery );
procedure InternalSaveToDDL(Stream: TFastStringStream; Operation: string);
function GetReturn: TMetaProcOutField;
protected
procedure SaveEmptyDDL(Stream: TFastStringStream);
procedure SaveAlterDDL(Stream: TFastStringStream);
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
constructor Create(AOwner: TCustomMetaObject;aName:string=''); override;
class function ClassIndex:integer; override;
property Source: string read FSource;
property InputFields[const Index: Integer]: TMetaProcInField read GetInputFields;
property InputFieldsCount: Integer read GetInputFieldsCount;
property PackageName:string read FPackageName;
property Return: TMetaProcOutField read GetReturn;
end;
TMetaPackage = class(TCustomMetaObject)
private
FSpec: string;
FBody: string;
procedure LoadFromDataBase(QNames: TFIBQuery );
procedure SaveSpecDDL(Stream: TFastStringStream);
procedure SaveBodyDDL(Stream: TFastStringStream);
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
property SpecSource: string read FSpec;
property BodySource: string read FBody;
end;
TMetaException = class(TCustomMetaObject)
private
FMessage: string;
FNumber: Integer;
procedure LoadFromDataBase(QName: TFIBQuery);
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
property Message: string read FMessage;
property Number: Integer read FNumber;
end;
TMetaUDFField = class(TCustomMetaField)
private
FPosition: Smallint;
FMechanism: Smallint;
procedure LoadFromDataBase(QField,QCharset:TFIBQuery); override;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
class function ClassIndex:integer; override;
property Position: Smallint read FPosition;
property Mechanism: Smallint read FMechanism;
end;
TMetaUDF = class(TCustomMetaObject)
private
FModule: string;
FEntry: string;
FReturn: Smallint;
procedure LoadFromDataBase(QNames, QFields: TFIBQuery; QCharset: TFIBQuery);
function GetFields(const Index: Integer): TMetaUDFField;
function GetFieldsCount: Integer;
protected
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
public
constructor Create(AOwner: TCustomMetaObject;aName:string=''); override;
class function ClassIndex:integer; override;
property Module: string read FModule;
property Entry: string read FEntry;
property Return: Smallint read FReturn;
property Fields[const Index: Integer]: TMetaUDFField read GetFields;
property FieldsCount: Integer read GetFieldsCount;
end;
TMetaRole = class(TCustomMetaObject)
private
FOwner: string;
protected
procedure LoadFromDataBase(QName: TFIBQuery);
procedure GenerateObjectDDL(Stream: TFastStringStream); override;
function GetGrants(const Index: Integer): TCustomMetaGrant;
function GetGrantsCount: Integer;
public
constructor Create(AOwner: TCustomMetaObject;aName:string=''); override;
class function ClassIndex:integer; override;
property Owner: string read FOwner;
end;
TMetaDataBase = class(TCustomMetaObject)
private
FInternalTransaction:TFIBTransaction;
private
// DBInfo
FPageSize:integer;
FDefaultCharset:string;
FConnectCharset:string;
FClientLib:string;
FProcParamDomainSupports:boolean;
FProcParamRelationSupports:boolean;
FIdentityFieldsSupports:boolean;
FStoredFunctionsSupports:boolean;
FPackagesSupports:boolean;
FTabRelTypeSupports:boolean;
FSQLDialect :integer;
//
FLoadObjects: TDBObjectTypes;
FLoadTableChilds: TTableChildTypes;
FLoadViewChilds: TViewChildTypes;
FSysInfos: Boolean;
//Flags
FNamesLoaded:boolean;
FDDLTextOptions:TDDLTextOptions;
FOnLoadMetaData:TNotifyLoadMetaData;
function GetGenerators(const Index: Integer): TMetaGenerator;
function GetGeneratorsCount: Integer;
function GetTables(const Index: Integer): TMetaTable;
function GetTablesCount: Integer;
function FindTableIndex(const TableName: string): Integer;
function FindDomainIndex(const DomainName: string): Integer;
function GetViews(const Index: Integer): TMetaView;
function GetViewsCount: Integer;
function GetDomains(const Index: Integer): TMetaDomain;
function GetDomainsCount: Integer;
function GetProcedures(const Index: Integer): TMetaProcedure;
function GetProceduresCount: Integer;
function GetFunctionsCount: Integer;
function GetFunctions(const Index: Integer): TMetaStoredFunc;
function GetExceptions(const Index: Integer): TMetaException;
function GetExceptionsCount: Integer;
function GetUDFS(const Index: Integer): TMetaUDF;
function GetUDFSCount: Integer;
function GetRoles(const Index: Integer): TMetaRole;
function GetRolesCount: Integer;
function GetPackageCount: Integer;
function GetPackages(const Index: Integer): TMetaPackage;
public
constructor Create(AOwner: TCustomMetaObject;aName:string=''); override;
destructor Destroy; override;
class function ClassIndex:integer; override;
procedure Clear;
procedure LoadFromDatabase(DB: TFIBDatabase; OnlyNames:boolean = False);
procedure GenerateDDLText(Stream: TFastStringStream);override;
procedure ExtGenerateDDLText(Stream: TFastStringStream;
Options:TDDLTextOptions=[dtoUseCreateDB,dtoUseSetTerm]);
function GetDBObject(DB:TFIBDatabase; ObjectType:TDBObjectType; const ObjectName:string; ForceLoad:boolean=True):TCustomMetaObject;
property Generators[const Index: Integer]: TMetaGenerator read GetGenerators;
property GeneratorsCount: Integer read GetGeneratorsCount;
property Tables[const Index: Integer]: TMetaTable read GetTables;
property TablesCount: Integer read GetTablesCount;
property Packages[const Index: Integer]: TMetaPackage read GetPackages;
property PackageCount: Integer read GetPackageCount;
property Views[const Index: Integer]: TMetaView read GetViews;
property ViewsCount: Integer read GetViewsCount;
property Domains[const Index: Integer]: TMetaDomain read GetDomains;
property DomainsCount: Integer read GetDomainsCount;
property Procedures[const Index: Integer]: TMetaProcedure read GetProcedures;
property ProceduresCount: Integer read GetProceduresCount;
property Exceptions[const Index: Integer]: TMetaException read GetExceptions;
property ExceptionsCount: Integer read GetExceptionsCount;
property UDFS[const Index: Integer]: TMetaUDF read GetUDFS;
property UDFSCount: Integer read GetUDFSCount;
property Roles[const Index: Integer]: TMetaRole read GetRoles;
property RolesCount: Integer read GetRolesCount;
property SysInfo:boolean read FSysInfos write FSysInfos;
property ViewInfo: TViewChildTypes read FLoadViewChilds write FLoadViewChilds default DefViewChilds;
property DBObjects: TDBObjectTypes read FLoadObjects write FLoadObjects default DefObjects;
property TableInfo: TTableChildTypes read FLoadTableChilds write FLoadTableChilds default DefTableChilds;
property DDLTextOptions:TDDLTextOptions read FDDLTextOptions write FDDLTextOptions;
property OnLoadMetaData:TNotifyLoadMetaData read FOnLoadMetaData write FOnLoadMetaData;
end;
TpFIBDBSchemaExtract=class;
TLoadOptions= class(TPersistent)
private
FLoadObjects: TDBObjectTypes;
FLoadTableChilds: TTableChildTypes;
FLoadViewChilds: TViewChildTypes;
FIncSysInfo: Boolean;
public
constructor Create;
published
property SysInfo:boolean read FIncSysInfo write FIncSysInfo default False;
property ViewInfo: TViewChildTypes read FLoadViewChilds write FLoadViewChilds default DefViewChilds;
property DBObjects: TDBObjectTypes read FLoadObjects write FLoadObjects default DefObjects;
property TableInfo: TTableChildTypes read FLoadTableChilds write FLoadTableChilds default DefTableChilds;
end;
TpFIBDBSchemaExtract=class (TComponent)
protected
FDatabase:TFIBDatabase;
FOnLoadMetaData:TNotifyLoadMetaData;
FDDLTextOptions:TDDLTextOptions;
FLoadOptions:TLoadOptions;
FMetaDatabase :TMetaDataBase;
FFullLoaded:boolean;
protected
function GetDomains(const Index: Integer): TMetaDomain;
function GetDomainsCount: Integer;
function GetExceptions(const Index: Integer): TMetaException;
function GetExceptionsCount: Integer;
function GetGenerators(const Index: Integer): TMetaGenerator;
function GetGeneratorsCount: Integer;
function GetProcedures(const Index: Integer): TMetaProcedure;
function GetProceduresCount: Integer;
function GetFunctions(const Index: Integer): TMetaStoredFunc;
function GetFunctionsCount: Integer;
function GetTables(const Index: Integer): TMetaTable;
function GetTablesCount: Integer;
function GetUDFS(const Index: Integer): TMetaUDF;
function GetUDFSCount: Integer;
function GetViews(const Index: Integer): TMetaView;
function GetViewsCount: Integer;
procedure SetDatabase(const Value: TFIBDatabase);
procedure SetLoadOptions(const Value: TLoadOptions);
function GetNamesLoaded: boolean;
private
function GetPackageCount: Integer;
function GetPackages(const Index: Integer): TMetaPackage;
protected
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure DoOnLoadMetaData(Sender:TObject; const Message:string; var Stop:boolean);
published
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure LoadMetaData(OnlyNames :boolean= False);
procedure LoadObjectNames;
function GetDDLText:string;
function GetMetaDataBase:TMetaDataBase;
procedure Clear;
function ObjectByName(ObjectType:TDBObjectType; const ObjectName:string; ForceLoad:boolean=True): TCustomMetaObject ;
function GetObjectDDL(ObjectType:TDBObjectType; const ObjectName:string; ForceLoad:boolean=True): string ;
property Generators[const Index: Integer]: TMetaGenerator read GetGenerators;
property GeneratorsCount: Integer read GetGeneratorsCount;
property Tables[const Index: Integer]: TMetaTable read GetTables;
property TablesCount: Integer read GetTablesCount;
property Packages[const Index: Integer]: TMetaPackage read GetPackages;
property PackagesCount: Integer read GetPackageCount;
property Views[const Index: Integer]: TMetaView read GetViews;
property ViewsCount: Integer read GetViewsCount;
property Domains[const Index: Integer]: TMetaDomain read GetDomains;
property DomainsCount: Integer read GetDomainsCount;
property Procedures[const Index: Integer]: TMetaProcedure read GetProcedures;
property ProceduresCount: Integer read GetProceduresCount;
property Functions[const Index: Integer]: TMetaStoredFunc read GetFunctions;
property FunctionsCount: Integer read GetFunctionsCount;
property Exceptions[const Index: Integer]: TMetaException read GetExceptions;
property ExceptionsCount: Integer read GetExceptionsCount;
property UDFS[const Index: Integer]: TMetaUDF read GetUDFS;
property UDFSCount: Integer read GetUDFSCount;
property MetaDataLoaded:boolean read FFullLoaded;
property NamesLoaded :boolean read GetNamesLoaded;
published
property Database:TFIBDatabase read FDatabase write SetDatabase;
property DDLTextOptions:TDDLTextOptions read FDDLTextOptions write FDDLTextOptions;
property LoadOptions:TLoadOptions read FLoadOptions write SetLoadOptions;
property OnLoadMetaData:TNotifyLoadMetaData read FOnLoadMetaData write FOnLoadMetaData;
end;
procedure FullExtractDDL(DB:TFIBDatabase; Dest: TStrings);
implementation
uses StdFuncs;
procedure FullExtractDDL(DB:TFIBDatabase; Dest: TStrings);
var
Extractor:TMetaDataBase;
begin
Extractor:=TMetaDataBase.Create(nil,'');
try
Extractor.LoadFromDatabase(DB);
Dest.Text:=Extractor.AsDDL
finally
Extractor.Free;
end;
end;
function CutDefaultValue(const Source:string):string;
begin
Result:=Trim(Source);
if Result <> '' then
Result := Copy(Result, 9,
System.Length(Result) - 8);
end;
function SavedMetaObjectName(const MetaObjectName:string):string;
begin
if (Length(MetaObjectName)>0) and (MetaObjectName[1]='"') then
Result:=FastCopy(MetaObjectName,2,Length(MetaObjectName)-2)
else
Result:=MetaObjectName
end;
const
TriggerPrefixes: array [TTriggerType] of PAnsiChar =
('BEFORE', 'AFTER','ON');
TriggerSuffixes: array [TTriggerAction] of PAnsiChar =
('INSERT', 'UPDATE', 'DELETE',
'CONNECT','DISCONNECT','TRANSACTION START','TRANSACTION COMMIT','TRANSACTION ROLLBACK'
);
FieldTypes: array [TFIBFieldType] of string =
('', 'NUMERIC', 'CHAR', 'VARCHAR', 'CSTRING', 'SMALLINT', 'INTEGER', 'QUAD',
'FLOAT', 'DOUBLE PRECISION', 'TIMESTAMP', 'BLOB', 'BLOBID', 'DATE', 'TIME',
'BIGINT' , 'BOOLEAN' );
QRYDB_INFO=
'SELECT RDB$CHARACTER_SET_NAME FROM RDB$DATABASE DBP ';
QRY_PACKAGE_SUPPORTS=
' SELECT 1 FROM RDB$RELATIONS RF WHERE RF.RDB$RELATION_NAME = ''RDB$RELATIONS''';
QRY_PROC_PARAMDOMAINS_SUPPORTS=
'SELECT 1 AS RESULT FROM RDB$DATABASE RD WHERE EXISTS ('+
' SELECT RF.*'+
' FROM RDB$RELATION_FIELDS RF'+
' WHERE (RF.RDB$RELATION_NAME = ''RDB$PROCEDURE_PARAMETERS'') AND'+
' (RF.RDB$FIELD_NAME = ''RDB$PARAMETER_MECHANISM'')'+
' )';
QRY_PROC_PARAMRELATION_SUPPORTS=
'SELECT 1 AS RESULT FROM RDB$DATABASE RD WHERE EXISTS ('+
' SELECT RF.*'+
' FROM RDB$RELATION_FIELDS RF'+
' WHERE (RF.RDB$RELATION_NAME = ''RDB$PROCEDURE_PARAMETERS'') AND'+
' (RF.RDB$FIELD_NAME = ''RDB$RELATION_NAME'')'+
' )';
QRY_TAB_RELATION_TYPE_SUPPORTS=
'SELECT 1 AS RESULT FROM RDB$DATABASE RD WHERE EXISTS ('+
' SELECT RF.*'+
' FROM RDB$RELATION_FIELDS RF'+
' WHERE (RF.RDB$RELATION_NAME = ''RDB$RELATIONS'') AND'+
' (RF.RDB$FIELD_NAME = ''RDB$RELATION_TYPE'')'+
' )';
QRY_FLD_BY_IDENTITY_SUPPORTS=
'SELECT 1 AS RESULT FROM RDB$DATABASE RD WHERE EXISTS ('+
'SELECT RF.*'+
' FROM RDB$RELATION_FIELDS RF'+
' WHERE (RF.RDB$RELATION_NAME = ''RDB$RELATION_FIELDS'')'+
' AND (RF.RDB$FIELD_NAME = ''RDB$IDENTITY_TYPE'')'+
' )';
QRY_STORED_FUNCS_SUPPORTS=
'SELECT 1 AS RESULT FROM RDB$DATABASE RD WHERE EXISTS ('+
'SELECT RF.*'+
' FROM RDB$RELATION_FIELDS RF'+
' WHERE (RF.RDB$RELATION_NAME = ''RDB$FUNCTIONS'')'+
' AND (RF.RDB$FIELD_NAME = ''RDB$FUNCTION_SOURCE'')'+
' )';
//FB3^^^^
QRYGenerators =
'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS GEN WHERE ' +
'(NOT GEN.RDB$GENERATOR_NAME STARTING WITH ''RDB$'') AND ' +
'(NOT GEN.RDB$GENERATOR_NAME STARTING WITH ''SQL$'') AND ' +
'((GEN.RDB$SYSTEM_FLAG IS NULL) OR (GEN.RDB$SYSTEM_FLAG <> 1)) ' +
'ORDER BY GEN.RDB$GENERATOR_NAME';
QRYTables =
'SELECT REL.RDB$RELATION_NAME FROM RDB$RELATIONS REL WHERE ' +
'(REL.RDB$SYSTEM_FLAG <> 1 OR REL.RDB$SYSTEM_FLAG IS NULL) AND ' +
'(NOT REL.RDB$FLAGS IS NULL) AND ' +
'(REL.RDB$VIEW_BLR IS NULL) AND ' +
'(REL.RDB$SECURITY_CLASS STARTING WITH ''SQL$'') ' +
'ORDER BY REL.RDB$RELATION_NAME';
QRYTablesRel =
'SELECT REL.RDB$RELATION_NAME,REL.RDB$RELATION_TYPE FROM RDB$RELATIONS REL WHERE ' +
'(REL.RDB$SYSTEM_FLAG <> 1 OR REL.RDB$SYSTEM_FLAG IS NULL) AND ' +
'(NOT REL.RDB$FLAGS IS NULL) AND ' +
'(REL.RDB$VIEW_BLR IS NULL) AND ' +
'(REL.RDB$SECURITY_CLASS STARTING WITH ''SQL$'') ' +
'ORDER BY REL.RDB$RELATION_NAME';
QRYSysTables =
'SELECT REL.RDB$RELATION_NAME FROM RDB$RELATIONS REL ' +
'WHERE REL.RDB$VIEW_BLR IS NULL ORDER BY REL.RDB$RELATION_NAME';