-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQLResource.pas
More file actions
462 lines (390 loc) · 12.8 KB
/
SQLResource.pas
File metadata and controls
462 lines (390 loc) · 12.8 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
unit SQLResource;
interface
uses
System.SysUtils, System.Classes, Data.DB, Data.SqlExpr;
const
DEFAULT_DESIGNER_FOLDER_NAME = 'Queries';
type
TOnResourceNotFoundEvent = procedure(AResourceName, AResourceType: string) of object;
TSQLResource = class(TSQLDataSet)
strict private
function LoadResourceString(const aResourceName: string; aResourceType: string = 'SQL' ): string;
private
FInitialParameters: TParams;
FDesignerUseSingleFile: Boolean;
FReplaceItems: TStringList;
FReplaceItemsWithRegex: TStringList;
FAddOnWhere: string;
FDesignerFolderName: string;
FOnResourceNotFound: TOnResourceNotFoundEvent;
FForceResourceName: string;
FForceLoadFile: string;
function resourceFilePath: string;
function getDesignerFile: string;
function getResourceName: string;
function getDesignerPath: string;
function formatSqlFileName(AType: string): string;
function getSql: string;
function DesignerFolderNameIsStored: Boolean;
procedure SetForceResourceName(const Value: string);
function forceResourceNameIsStored: Boolean;
procedure SetForceLoadFile(const Value: string);
function forceLoadFileIsStored: Boolean;
protected // Custom
function GetFromPosition(SqlClause: string): Integer; virtual;
function ReplaceTextsOnSQL(ASql: string): string; virtual;
function ReplaceTextsOnSQLWithRegex(ASql: string): string; virtual;
function loadSql: string; virtual;
function insertTextOnWhere(SqlClause, SqlFilter: string): string; virtual;
function getResourceType: string; virtual;
procedure loadInitialParameters; virtual;
protected // Override
procedure SetCommandText(const Value: string); override;
procedure OpenCursor(InfoQuery: Boolean); override;
procedure CloseCursor; override;
function PSGetTableName: string; override;
function PSGetCommandText: string; override;
public
constructor Create(AOwner: TComponent); overload; override;
destructor Destroy; override;
function ExecSQL(ExecDirect: Boolean = False): Integer; override;
published
property CommandText stored false;
property OnResourceNotFound: TOnResourceNotFoundEvent read FOnResourceNotFound write FOnResourceNotFound;
property AddOnWhere: string read FAddOnWhere write FAddOnWhere;
property ReplaceItems: TStringList read FReplaceItems write FReplaceItems;
property ReplaceItemsWithRegex: TStringList read FReplaceItemsWithRegex write FReplaceItemsWithRegex;
property ResourceName: string read getResourceName;
property ForceResourceName: string read FForceResourceName write SetForceResourceName stored forceResourceNameIsStored;
property ForceLoadFile: string read FForceLoadFile write SetForceLoadFile stored forceLoadFileIsStored;
property ResourceType: string read getResourceType;
property Sql: string read getSql;
property DesignerUseSingleFile: boolean read FDesignerUseSingleFile write FDesignerUseSingleFile default true;
property DesignerFile: string read getDesignerFile;
property DesignerPath: string read getDesignerPath;
property DesignerFolderName: string read FDesignerFolderName write FDesignerFolderName stored DesignerFolderNameIsStored;
end;
var
GetCurrentProjectPath: TFunc<String>;
implementation
uses
System.RegularExpressions,
System.StrUtils,
Data.DBCommon,
Vcl.Forms,
Winapi.Windows,
Vcl.Dialogs,
System.IOUtils, Data.DBXCommon;
function GetIdOption(Connection: TSQLConnection): IDENTIFIEROption;
var
MetaData: TDBXDatabaseMetaData;
begin
Result := idMixCase;
if (Assigned(Connection) and Assigned(Connection.DBXConnection)) then
begin
MetaData := Connection.MetaData;
if Assigned(MetaData) and (MetaData.MetaDataVersion = DBXVersion40) then
begin
if not MetaData.SupportsLowerCaseIdentifiers then
Result := idMakeUpperCase
else if not MetaData.SupportsUpperCaseIdentifiers then
Result := idMakeLowerCase;
end;
end;
end;
{ TSQLResource }
constructor TSQLResource.Create(AOwner: TComponent);
begin
inherited;
FInitialParameters := TParams.Create(Self);
FDesignerUseSingleFile := true;
FDesignerFolderName := DEFAULT_DESIGNER_FOLDER_NAME;
FReplaceItems := TStringList.Create;
FReplaceItems.Delimiter := '|';
FReplaceItems.QuoteChar := '"';
FReplaceItems.StrictDelimiter := true;
FReplaceItems.Sorted := false;
FReplaceItemsWithRegex := TStringList.Create;
FReplaceItemsWithRegex.Delimiter := '|';
FReplaceItemsWithRegex.QuoteChar := '"';
FReplaceItemsWithRegex.StrictDelimiter := true;
FReplaceItemsWithRegex.Sorted := false;
end;
function TSQLResource.DesignerFolderNameIsStored: Boolean;
begin
Result := FDesignerFolderName <> DEFAULT_DESIGNER_FOLDER_NAME;
end;
destructor TSQLResource.Destroy;
begin
if Assigned(FInitialParameters) then
FreeAndNil(FInitialParameters);
FreeAndNil(FReplaceItems);
FreeAndNil(FReplaceItemsWithRegex);
inherited;
end;
function TSQLResource.ExecSQL(ExecDirect: Boolean): Integer;
begin
CommandText := getSql;
loadInitialParameters;
Result := inherited ExecSQL(ExecDirect);
end;
procedure TSQLResource.CloseCursor;
begin
inherited;
loadInitialParameters;
end;
procedure TSQLResource.SetCommandText(const Value: string);
begin
FInitialParameters.Clear;
if Params.Count > 0 then
FInitialParameters.Assign(Params);
inherited;
end;
function TSQLResource.getResourceType: string;
begin
if self.SQLConnection = nil then
result := ''
else result := self.SQLConnection.DriverName;
end;
procedure TSQLResource.SetForceLoadFile(const Value: string);
begin
if Trim(Value) <> '' then
ForceResourceName := '';
FForceLoadFile := Trim(Value);
end;
procedure TSQLResource.SetForceResourceName(const Value: string);
begin
if Trim(Value) <> '' then
ForceLoadFile := '';
FForceResourceName := Trim(Value);
end;
function TSQLResource.PSGetCommandText: string;
begin
Result := sql;
end;
function TSQLResource.PSGetTableName: string;
begin
Result := GetTableNameFromSQLEx(Sql, GetIdOption(SQLConnection));
end;
procedure TSQLResource.loadInitialParameters;
begin
if FInitialParameters.Count > 0 then
begin
if ParamCount = 0 then
Params.Assign(FInitialParameters)
else Params.AssignValues(FInitialParameters);
end;
end;
function loadFromFile(AFileName: string): string;
var
sqlFile: TStringList;
begin
if (FileExists(AFileName)) then
begin
sqlFile := TStringList.Create;
try
sqlFile.LoadFromFile(AFileName);
Result := sqlFile.Text;
finally
FreeAndNil(sqlFile);
end;
end
else Result := Format('file "%s" not found.', [AFileName]);
end;
function TSQLResource.loadSql: string;
begin
Result := '';
if resourceType.IsEmpty then
result := 'set the property "resourceType"'
else if (FForceLoadFile <> '') then
result := loadFromFile(FForceLoadFile)
else if (csDesigning in ComponentState) then
result := loadFromFile(DesignerFile)
else Result := LoadResourceString(resourceName, resourceType);
end;
procedure TSQLResource.OpenCursor(InfoQuery: Boolean);
begin
if resourceType.IsEmpty then
raise Exception.Create('Property resourceType not defined');
CommandText := getSql;
loadInitialParameters;
inherited;
end;
function TSQLResource.getSql: string;
begin
Result := loadSql;
if AddOnWhere <> '' then
Result := insertTextOnWhere(Result, AddOnWhere);
Result := ReplaceTextsOnSQL(Result);
Result := ReplaceTextsOnSQLWithRegex(Result);
end;
function TSQLResource.insertTextOnWhere(SqlClause, SqlFilter: string): string;
var
FromPosition, WherePosition: Integer;
WhereClause: String;
begin
if (Trim(SqlClause) <> '') and (Trim(SqlFilter) <> '') then
begin
SqlClause := UpperCase(SqlClause);
FromPosition := GetFromPosition(SqlClause);
WherePosition := PosEx('WHERE', SqlClause, FromPosition);
if (WherePosition = 0) then
begin
WhereClause:= ' WHERE (' + SqlFilter + ' ) ';
WherePosition := PosEx('GROUP', SqlClause, FromPosition);
if (WherePosition = 0) then
begin
WherePosition := PosEx('ORDER', SqlClause, FromPosition);
end;
if (WherePosition = 0) then
begin
WherePosition := Length(SqlClause) + 1;
end;
end
else
begin
WhereClause:= ' ( ' + SqlFilter + ' ) AND ';
WherePosition := WherePosition + Length('WHERE');
end;
SqlClause:= Copy(SqlClause, 1, WherePosition - 1) + #13 + WhereClause + #13 + Copy(SqlClause, WherePosition, Length(SqlClause));
end;
Result:= SqlClause;
end;
function TSQLResource.GetFromPosition(SqlClause: string): Integer;
var
SelectPosition,
FromPosition: Integer;
begin
SelectPosition := PosEx('SELECT', SqlClause, 2);
FromPosition := Pos('FROM', SqlClause);
while (SelectPosition <> 0) and (SelectPosition < FromPosition) do
begin
SelectPosition := PosEx('SELECT', SqlClause, SelectPosition + 1);
FromPosition := PosEx('FROM', SqlClause, FromPosition + 1);
end;
Result := FromPosition;
end;
function TSQLResource.ReplaceTextsOnSQL(ASql: string): string;
var
I: Integer;
begin
Result := ASql;
for I := 0 to ReplaceItems.Count-1 do
Result := StringReplace(Result, ReplaceItems.Names[i], ReplaceItems.ValueFromIndex[i], [rfReplaceAll]);
end;
function TSQLResource.ReplaceTextsOnSQLWithRegex(ASql: string): string;
var
I: Integer;
Options: TRegExOptions;
RegexValue: string;
begin
Result := ASql;
for I := 0 to ReplaceItemsWithRegex.Count-1 do
begin
RegexValue := ReplaceItemsWithRegex.Names[i];
Options := [];
if (TRegex.IsMatch(RegexValue, '\/[msi]{1,3}$', [roIgnoreCase])) then
begin
if (TRegex.IsMatch(RegexValue, '\/.{0,2}i.{0,2}$')) then
Options := Options + [roIgnoreCase];
if (TRegex.IsMatch(RegexValue, '\/.{0,2}m.{0,2}$')) then
Options := Options + [roMultiLine];
if (TRegex.IsMatch(RegexValue, '\/.{0,2}s.{0,2}$')) then
Options := Options + [roSingleLine];
RegexValue := TRegex.replace(RegexValue, '\/[msi]{1,3}$', '', [roIgnoreCase]);
end;
Result := TRegEx.Replace(Result, RegexValue, ReplaceItemsWithRegex.ValueFromIndex[i], Options);
end;
end;
function TSQLResource.getDesignerPath: string;
begin
result := GetCurrentProjectPath;
end;
function TSQLResource.resourceFilePath: string;
begin
Result := TPath.Combine(GetCurrentProjectPath, FDesignerFolderName);
end;
function TSQLResource.getResourceName: string;
begin
if FForceLoadFile <> '' then
result := ''
else if FForceResourceName <> '' then
Result := FForceResourceName
else begin
Result := Format('%s%s', [
Owner.ClassName,
Name
]);
end;
end;
function TSQLResource.forceLoadFileIsStored: Boolean;
begin
Result := FForceLoadFile <> '';
end;
function TSQLResource.forceResourceNameIsStored: Boolean;
begin
Result := FForceResourceName <> '';
end;
function TSQLResource.formatSqlFileName(AType: string): string;
begin
if AType <> '' then
AType := '.' + AType;
Result := Format('%s%s.sql', [
ResourceName,
lowerCase(AType)
]);
end;
function TSQLResource.getDesignerFile: string;
var
fileNameSql: string;
fileNameWithType: string;
begin
if not(csDesigning in ComponentState) then
begin
Result := '';
exit;
end;
if resourceType.IsEmpty then
begin
result := 'set the property "resourceType"';
exit;
end;
fileNameWithType := TPath.Combine(resourceFilePath, formatSqlFileName(resourceType));
fileNameSql := TPath.Combine(resourceFilePath, formatSqlFileName(''));
if DesignerUseSingleFile then
begin
if not(FileExists(fileNameSql)) and (FileExists(fileNameWithType)) then
Result := fileNameWithType
else Result := fileNameSql;
end
else result := fileNameWithType;
end;
function TSQLResource.LoadResourceString(const aResourceName: string; aResourceType: string = 'SQL' ): string;
var
streamResource: TResourceStream;
SQL: TStringList;
begin
if FindResource(HInstance, PWideChar(aResourceName), pWidechar(aResourceType)) <> 0 then
begin
streamResource := TResourceStream.Create(HInstance, aResourceName, pchar(aResourceType));
try
SQL := TStringList.Create;
try
streamResource.Position := 0;
SQL.Clear;
SQL.LoadFromStream(streamResource);
Result := SQL.Text;
finally
SQL.DisposeOf;
end;
finally
streamResource.DisposeOf;
end;
end
else begin
if Assigned(FOnResourceNotFound) then
FOnResourceNotFound(resourceName, resourceType);
Result := '';
end;
end;
end.