-
Notifications
You must be signed in to change notification settings - Fork 58
/
pFIBExports.pas
477 lines (384 loc) · 12.6 KB
/
pFIBExports.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
unit pFIBExports;
interface
{$I FIBPlus.inc}
uses SysUtils,{$IFNDEF D6+}Windows,{$ELSE}FIBPlatforms,{$ENDIF}Classes,
pFIBInterfaces,FIBDatabase,FIBQuery,FIBDataSet,pFIBDataSet,pFIBQuery,
StrUtil,pFIBDatabase,SqlTxtRtns, pFIBMetaData;
type
TFIBStringer= class(TComponent, IFIBStringer)
private
function FormatIdentifier(Dialect: Integer; const Value: string): string;
procedure DispositionFromClause(const SQLText:string;var X,Y:integer);
function PosCh(aCh:Char; const s:string):integer;
function PosCh1(aCh:Char; const s:string; StartPos:integer):integer;
function PosCI(const Substr,Str:string):integer;
function EmptyStrings(SL:TStrings):boolean;
function EquelStrings(const s,s1:string; CaseSensitive:boolean):boolean;
procedure DeleteEmptyStr(Src:TStrings);//
procedure AllTables(const SQLText:string;aTables:TStrings; WithSP:boolean =False
;WithAliases:boolean =False
);
function WordCount(const S: string; const WordDelims: TCharSet): Integer;
function ExtractWord(Num:integer;const Str: string;const WordDelims:TCharSet):string;
function PosInRight(const substr,Str:string;BeginPos:integer):integer;
function LastChar(const Str:string):Char;
function TableByAlias(const SQLText,Alias:string):string;
function AliasForTable(const SQLText,TableName:string):string;
function SetOrderClause(const SQLText,Order:string):string;
function AddToMainWhereClause(const SQLText,Condition:string):string;
end;
TMetaDataExtractor=class(TpFIBDBSchemaExtract, IFIBMetaDataExtractor)
private
function DBPrimaryKeys(const TableName:string;Transaction:TObject):string;
function IsCalculatedField(const TableName,FieldName:string;Transaction:TObject):boolean;
function ObjectDDLTxt(Transaction:TObject;ObjectType:Integer; const ObjectName:string; ForceLoad:boolean=True): string ;
procedure SetDatabase(aDatabase:TObject);
function GetTableName(Index:integer): string;
function GetViewName(Index:integer): string;
function GetProcedureName(Index:integer): string;
function GetUDFName(Index:integer): string;
function GetGeneratorName(Index:integer): string;
function ObjectListFields(const ObjectName:string; var DDL:string ;ForceLoad:boolean=True;IncludeOBJName:boolean=False): string ;
public
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
end;
TiSQLParser=class(TSQLParser,ISQLParser)
protected
{ IInterface }
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
// procedure SetSQLText(const Value: string);
function QueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
function iPosInSections(Position:integer):TiSQLSections;
function SectionInArray(Section:TiSQLSection ;ArrS:TiSQLSections):boolean;
function CutAlias(const SQLString:string;AliasPosition:integer=0):string;
function CutTableName(const SQLString:string;AliasPosition:integer=0):string;
function AliasForTable(const SQLText,TableName:string):string;
function TableByAlias(const SQLText,Alias:string):string;
end;
TFIBClassesExporter =class (TComponent, IFIBClassesExporter,IFIBStringer,IFIBMetaDataExtractor,
ISQLParser
)
private
FStringer:TFIBStringer;
FMetaDataExtractor:TMetaDataExtractor;
FSQLParser :TiSQLParser;
function iGetStringer:IFIBStringer;
function iGetMetaExtractor:IFIBMetaDataExtractor;
function iGetSQLParser:ISQLParser;
public
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
property Stringer:TFIBStringer read FStringer implements IFIBStringer;
property MetaDataExtractor:TMetaDataExtractor read FMetaDataExtractor implements IFIBMetaDataExtractor;
property SQLParser:TiSQLParser read FSQLParser implements iSQLParser;
end
;
implementation
uses pFIBDataInfo,IB_Services;
var vFIBClassesExporter:TFIBClassesExporter;
{ TFIBClassesExporter }
constructor TFIBClassesExporter.Create(AOwner: TComponent);
begin
inherited;
FIBClassesExporter:=Self;
FStringer:=TFIBStringer.Create(Self);
FMetaDataExtractor:=TMetaDataExtractor.Create(Self);
FSQLParser:=TiSQLParser.Create;
end;
destructor TFIBClassesExporter.Destroy;
begin
if Self= vFIBClassesExporter then
vFIBClassesExporter:=nil;
FSQLParser.Free;
inherited;
end;
function TFIBClassesExporter.iGetMetaExtractor: IFIBMetaDataExtractor;
begin
Result:=FMetaDataExtractor
end;
function TFIBClassesExporter.iGetSQLParser: ISQLParser;
begin
Result:=FSQLParser
end;
function TFIBClassesExporter.iGetStringer: IFIBStringer;
begin
Result:=FStringer;
end;
procedure CreateExporter;
begin
vFIBClassesExporter:=TFIBClassesExporter.Create(nil);
with vFIBClassesExporter do
begin
Name:='FIBClassesExporter'
end;
end;
{ TFIBStringer }
function TFIBStringer.AddToMainWhereClause(const SQLText,
Condition: string): string;
begin
Result:=SqlTxtRtns.AddToWhereClause(SQLText,Condition)
end;
function TFIBStringer.AliasForTable(const SQLText,
TableName: string): string;
begin
Result:=SqlTxtRtns.AliasForTable(SQLText,TableName)
end;
procedure TFIBStringer.AllTables(const SQLText: string; aTables: TStrings;
WithSP, WithAliases: boolean);
begin
SqlTxtRtns.AllTables(SQLText,aTables,WithSP,WithAliases);
end;
procedure TFIBStringer.DeleteEmptyStr(Src: TStrings);
begin
StrUtil.DeleteEmptyStr(Src);
end;
procedure TFIBStringer.DispositionFromClause(const SQLText:string;var X,Y:integer);
var
p:TPosition;
begin
p:=SqlTxtRtns.DispositionFrom(SQLText);
X:=P.X;
Y:=P.Y
end;
function TFIBStringer.EmptyStrings(SL: TStrings): boolean;
begin
Result:=StrUtil.EmptyStrings(SL)
end;
function TFIBStringer.EquelStrings(const s, s1: string;
CaseSensitive: boolean): boolean;
begin
Result:=StrUtil.EquelStrings(s, s1, CaseSensitive)
end;
function TFIBStringer.ExtractWord(Num: integer; const Str: string;
const WordDelims: TCharSet): string;
begin
Result:=StrUtil.ExtractWord(Num, Str,WordDelims)
end;
function TFIBStringer.FormatIdentifier(Dialect: Integer;
const Value: string): string;
begin
Result:=StrUtil.FormatIdentifier(Dialect,Value)
end;
function TFIBStringer.LastChar(const Str: string): Char;
begin
Result:=StrUtil.LastChar(Str)
end;
function TFIBStringer.PosCh(aCh: Char; const s: string): integer;
begin
Result:=StrUtil.PosCh(aCh,s)
end;
function TFIBStringer.PosCh1(aCh: Char; const s: string;
StartPos: integer): integer;
begin
Result:=StrUtil.PosCh1(aCh,s,StartPos)
end;
function TFIBStringer.PosCI(const Substr, Str: string): integer;
begin
Result:=StrUtil.PosCI(Substr, Str)
end;
function TFIBStringer.PosInRight(const substr, Str: string;
BeginPos: integer): integer;
begin
Result:=StrUtil.PosInRight(substr, Str, BeginPos)
end;
function TFIBStringer.SetOrderClause(const SQLText, Order: string): string;
begin
Result:=SqlTxtRtns.SetOrderClause(SQLText,Order)
end;
function TFIBStringer.TableByAlias(const SQLText, Alias: string): string;
begin
Result:=SqlTxtRtns.TableByAlias(SQLText,Alias)
end;
function TFIBStringer.WordCount(const S: string;
const WordDelims: TCharSet): Integer;
begin
Result:=StrUtil.WordCount(S,WordDelims)
end;
{ TMetaDataExtractor }
constructor TMetaDataExtractor.Create(AOwner: TComponent);
begin
inherited;
end;
function TMetaDataExtractor.DBPrimaryKeys(const TableName: string;
Transaction: TObject): string;
begin
Result:=pFIBDataInfo.DBPrimaryKeyFields(TableName,TFIBTransaction(Transaction))
end;
destructor TMetaDataExtractor.Destroy;
begin
inherited;
end;
function TMetaDataExtractor.GetGeneratorName(Index: integer): string;
begin
Result:=Generators[Index].Name
end;
function TMetaDataExtractor.GetProcedureName(Index: integer): string;
begin
Result:=Procedures[Index].Name
end;
function TMetaDataExtractor.GetTableName(Index: integer): string;
begin
Result:=Tables[Index].Name
end;
function TMetaDataExtractor.GetUDFName(Index: integer): string;
begin
Result:=UDFS[Index].Name
end;
function TMetaDataExtractor.GetViewName(Index: integer): string;
begin
Result:=Views[Index].Name
end;
function TMetaDataExtractor.IsCalculatedField(const TableName,
FieldName: string; Transaction: TObject): boolean;
var
vpFIBTableInfo:TpFIBTableInfo;
vFi:TpFIBFieldInfo;
begin
ListTableInfo.Clear;
vpFIBTableInfo:=ListTableInfo.GetTableInfo(TFIBTransaction(Transaction).MainDatabase,TableName,False);
vFi:=vpFIBTableInfo.FieldInfo(FieldName);
Result:= (vFi=nil) or vFi.IsComputed or vFi.IsTriggered ;
end;
function TMetaDataExtractor.ObjectDDLTxt(Transaction:TObject;ObjectType: Integer;
const ObjectName: string; ForceLoad: boolean): string;
var
Extractor:TpFIBDBSchemaExtract;
begin
Result:='';
if (Transaction=nil ) or (TFIBTransaction(Transaction).DefaultDatabase=nil) then
Exit;
Extractor:=TpFIBDBSchemaExtract.Create(nil);
try
Extractor.Database:=TFIBTransaction(Transaction).DefaultDatabase;
Result:=Extractor.GetObjectDDL(TDBObjectType(ObjectType), ObjectName, ForceLoad);
finally
Extractor.Free
end;
end;
function TMetaDataExtractor.ObjectListFields(
const ObjectName: string; var DDL:string ;ForceLoad: boolean=True;IncludeOBJName:boolean=False): string;
var
obj:TCustomMetaObject;
i:integer;
begin
obj:=FMetaDatabase.GetDBObject(Database,otTable,ObjectName,ForceLoad);
if obj=nil then
obj:=FMetaDatabase.GetDBObject(Database,otView,ObjectName,ForceLoad);
if obj=nil then
obj:=FMetaDatabase.GetDBObject(Database,otProcedure,ObjectName,ForceLoad);
Result:='';
DDL:='';
if obj=nil then
Exit;
if obj is TMetaTable then
begin
with TMetaTable(obj) do
for i:=0 to FieldsCount-1 do
begin
if IncludeOBJName then
DDL:=Result+FormatIdentifier(3,ObjectName)+'.'+ Fields[i].FormatName+#13#10
else
DDL:=Result+Fields[i].FormatName+#13#10;
Result:=DDL
end;
end
else
if obj is TMetaView then
begin
with TMetaView(obj) do
for i:=0 to FieldsCount-1 do
begin
// DDL:=DDL+Fields[i].AsDDL;
if IncludeOBJName then
DDL:=Result+FormatIdentifier(3,ObjectName)+'.'+ Fields[i].FormatName+#13#10
else
DDL:=Result+Fields[i].FormatName+#13#10;
Result:=DDL
end;
end
else
if obj is TMetaProcedure then
begin
with TMetaProcedure(obj) do
for i:=0 to OutputFieldsCount-1 do
begin
// DDL:=DDL+Fields[i].AsDDL;
if IncludeOBJName then
DDL:=Result+FormatIdentifier(3,ObjectName)+'.'+ OutputFields[i].FormatName+#13#10
else
DDL:=Result+OutputFields[i].FormatName+#13#10;
Result:=DDL
end;
end
end;
procedure TMetaDataExtractor.SetDatabase(aDatabase: TObject);
begin
if Database<>aDatabase then
Database:=TFIBDatabase(aDatabase)
end;
{ TiSQLParser }
function TiSQLParser._AddRef: Integer;
begin
Result := -1 // -1 indicates no reference counting is taking place
end;
function TiSQLParser._Release: Integer;
begin
Result := -1 // -1 indicates no reference counting is taking place
end;
function TiSQLParser.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then Result := S_OK
else Result := E_NOINTERFACE
end;
{
procedure TiSQLParser.SetSQLText(const Value: string);
begin
SQLText:=Value
end;
}
function TiSQLParser.iPosInSections(Position: integer): TiSQLSections;
begin
Result:=TiSQLSections(PosInSections(Position))
end;
function TiSQLParser.SectionInArray(Section: TiSQLSection;
ArrS: TiSQLSections): boolean;
var
i,L:integer;
begin
Result:=False;
L:=Length(ArrS)-1;
for i:=0 to L do
if ArrS[i]=Section then
begin
Result:=True; Exit
end;
end;
function TiSQLParser.TableByAlias(const SQLText, Alias: string): string;
begin
Result:=SqlTxtRtns.TableByAlias(SQLText, Alias)
end;
function TiSQLParser.CutAlias(const SQLString: string;
AliasPosition: integer): string;
begin
Result:=SqlTxtRtns.CutAlias(SQLString,AliasPosition)
end;
function TiSQLParser.CutTableName(const SQLString: string;
AliasPosition: integer): string;
begin
Result:=SqlTxtRtns.CutTableName(SQLString,AliasPosition)
end;
function TiSQLParser.AliasForTable(const SQLText,
TableName: string): string;
begin
Result:=SqlTxtRtns.AliasForTable(SQLText,TableName)
end;
initialization
CreateExporter;
finalization
FIBClassesExporter:=nil;
if vFIBClassesExporter<>nil then
vFIBClassesExporter.Free
end.