-
Notifications
You must be signed in to change notification settings - Fork 1
/
uFrmPrincipal.pas
274 lines (221 loc) · 7.73 KB
/
uFrmPrincipal.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
unit uFrmPrincipal;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
System.RegularExpressions;
const
REGEX_TSQLDATASET_MATCH = '((?:object|inherited) \w+: )TSQLDataSet\b$';
REGEX_CLASS_NAME = '(?:object|inherited) \w+: (?<className>T\w+)$';
REGEX_GET_TSQLDATASET_CONTENT = '(?:object|inherited) (?<componentName>\w+): TSQLDataSet\R(?<componentContent>[\s\S]*?)(?=\R.*?end)';
REGEX_GET_SQL_NO_FORMATTED = 'CommandText = {0,}\R?(?<componentContent>[\s\S]*?)(?=\R.*? {4}(?:DataSource|DbxCommandType|MaxBlobSize|Params))';
REGEX_REMOVE_DELPHI_IDENT = '^ {6}';
REGEX_JOIN_DELPHIS_LINE_BREAK_1 = '( {0,}\+$\R^ {0,})';
REGEX_JOIN_DELPHIS_LINE_BREAK_2 = '''''';
REGEX_REMOVE_SINGLE_QUOTE_1 = '('')?((?:#\d{1,3}))('')?';
REGEX_REMOVE_SINGLE_QUOTE_2 = '''';
REGEX_GET_CODE_ASCI = '(?<=#)\d+';
REGEX_GET_SQL_EXPRE = '(SqlExpr {0,})([,;])';
REGEX_REPLACE_TSQLDATASET = '(\w+: )TSQLDataSet(;)$';
type
TfrmPrincipal = class(TForm)
Panel1: TPanel;
edtDfmFilesPath: TLabeledEdit;
btnLoad: TButton;
edtSqlFilesPath: TLabeledEdit;
btnCreate: TButton;
Splitter1: TSplitter;
Panel2: TPanel;
lblTitleDfmFiles: TLabel;
mmoDfmFiles: TMemo;
Panel3: TPanel;
lblSqlFiles: TLabel;
mmoSqlFiles: TMemo;
Panel4: TPanel;
lblErrors: TLabel;
mmoErrors: TMemo;
Splitter2: TSplitter;
procedure btnLoadClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnCreateClick(Sender: TObject);
private
procedure setTitles;
procedure ConvertFile(ADfmFilename: string);
procedure CreateSqlFile(ClassName: string; groups: TGroupCollection; var sqlFileName: string);
function ConvertCodeAsciiToString(AText: string): string;
{ Private declarations }
public
{ Public declarations }
end;
var
frmPrincipal: TfrmPrincipal;
implementation
{$R *.dfm}
uses
System.IOUtils,
System.Types;
procedure TfrmPrincipal.FormCreate(Sender: TObject);
begin
setTitles;
edtDfmFilesPath.Text := TPath.Combine(TDirectory.GetCurrentDirectory, 'DfmFiles');
edtSqlFilesPath.Text := TPath.Combine(TDirectory.GetCurrentDirectory, 'SqlFiles');
end;
procedure TfrmPrincipal.setTitles;
begin
lblTitleDfmFiles.Caption := Format('dfm files founded (%d)', [mmoDfmFiles.Lines.Count]);
lblSqlFiles.Caption := Format('sql files created (%d)', [mmoSqlFiles.Lines.Count]);
lblErrors.Caption := Format('dfm files with errors (%d)', [mmoErrors.Lines.Count]);
end;
procedure TfrmPrincipal.btnCreateClick(Sender: TObject);
var
dfmFile: string;
begin
mmoSqlFiles.Clear;
mmoErrors.Clear;
if mmoDfmFiles.Lines.Count = 0 then
btnLoadClick(btnLoad);
for dfmFile in mmoDfmFiles.Lines do
begin
try
ConvertFile(dfmFile);
except
end;
setTitles;
end;
ShowMessage('Finished.');
end;
procedure ConvertPasFile(ADfmFilename: string);
var
pasFileName: string;
archiveContent : TStringList;
begin
archiveContent := TStringList.Create;
try
pasFileName := ChangeFileExt(ADfmFilename, '.pas');
archiveContent.LoadFromFile(pasFileName);
archiveContent.Text := TRegex.Replace(archiveContent.Text, REGEX_GET_SQL_EXPRE, '$1, SqlResource$2', [roIgnoreCase]);
archiveContent.Text := TRegex.Replace(archiveContent.Text, REGEX_REPLACE_TSQLDATASET, '$1TSQLResource$2', [roMultiLine, roIgnoreCase]);
archiveContent.SaveToFile(pasFileName, TEncoding.UTF8);
finally
FreeAndNil(archiveContent);
end;
end;
procedure TFrmPrincipal.CreateSqlFile(ClassName: string; groups: TGroupCollection; var sqlFileName: string);
var
ComponentName: string;
ComponentContent: string;
contentMatches: TMatchCollection;
sqlFile: TStringList;
begin
ForceDirectories(edtSqlFilesPath.Text);
ComponentName := Groups['componentName'].Value;
ComponentContent := Groups['componentContent'].Value;
sqlFileName := Format('%s%s.sql', [ClassName, ComponentName]);
if ComponentContent <> '' then
begin
sqlFile := TStringList.Create;
try
contentMatches := TRegex.Matches(ComponentContent, REGEX_GET_SQL_NO_FORMATTED, []);
if (contentMatches.Count = 0) then
exit;
groups := contentMatches.Item[0].Groups;
sqlFile.Text := Groups['componentContent'].Value;
sqlFile.Text := TRegex.Replace(sqlFile.Text, REGEX_REMOVE_DELPHI_IDENT, '', [roMultiLine]);
sqlFile.Text := TRegex.Replace(sqlFile.Text, REGEX_JOIN_DELPHIS_LINE_BREAK_1, '', [roMultiLine]);
sqlFile.Text := TRegex.Replace(sqlFile.Text, REGEX_JOIN_DELPHIS_LINE_BREAK_2, '', [roMultiLine]);
sqlFile.Text := TRegex.Replace(sqlFile.Text, REGEX_REMOVE_SINGLE_QUOTE_1, '@$2@', []);
sqlFile.Text := TRegex.Replace(sqlFile.Text, REGEX_REMOVE_SINGLE_QUOTE_2, '', []);
sqlFile.Text := ConvertCodeAsciiToString(sqlFile.Text);
sqlFile.SaveToFile(TPath.Combine(edtSqlFilesPath.Text, sqlFileName), TEncoding.UTF8);
finally
FreeAndNil(sqlFile);
end;
end;
FreeAndNil(sqlFile);
end;
procedure TfrmPrincipal.ConvertFile(ADfmFilename: string);
var
archiveContent: TStringList;
ClassName: string;
matches: TMatchCollection;
match: TMatch;
sqlFileName: string;
begin
archiveContent := TStringList.Create;
try
try
archiveContent.LoadFromFile(ADfmFilename, TEncoding.UTF8);
if archiveContent.Count <= 2 then
exit;
if (not TRegex.Match(archiveContent.Text, REGEX_TSQLDATASET_MATCH, [roIgnoreCase, roMultiLine]).Success) then
exit;
matches := TRegex.Matches(archiveContent.Strings[0], REGEX_CLASS_NAME);
if (matches.Count = 0) then
exit;
ClassName := matches.Item[0].Groups['className'].Value;
matches := TRegex.Matches(archiveContent.Text, REGEX_GET_TSQLDATASET_CONTENT, []);
if (matches.Count = 0) then
exit;
for match in Matches do
begin
if not match.Success then
Continue;
CreateSqlFile(ClassName, match.Groups, sqlFileName);
archiveContent.Text := TRegex.Replace(archiveContent.Text, REGEX_TSQLDATASET_MATCH, '$1TSQLResource', [roIgnoreCase, roMultiLine]);
archiveContent.SaveToFile(ADfmFilename, TEncoding.UTF8);
mmoSqlFiles.Lines.Add(sqlFileName);
setTitles;
Application.ProcessMessages;
end;
ConvertPasFile(ADfmFilename);
except
on E: Exception do
begin
mmoErrors.Lines.Add(Format('%s - %s', [ADfmFilename, E.Message]));
exit
end;
end;
finally
FreeAndNil(archiveContent);
end;
end;
function TfrmPrincipal.ConvertCodeAsciiToString(AText: string):string;
var
matches: TMatchCollection;
match: TMatch;
codeAscii: string;
charAscii: string;
begin
Result := AText;
matches := TRegex.Matches(AText, REGEX_GET_CODE_ASCI, []);
if (matches.Count = 0) then
exit;
for match in Matches do
begin
try
charAscii := Char(Ord(StrToInt(match.Value)));
codeAscii := Format('@#%s@',[match.Value]);
Result := TRegex.Replace(Result, codeAscii, charAscii);
except
ShowMessage(match.Value);
end;
end;
end;
procedure TfrmPrincipal.btnLoadClick(Sender: TObject);
var
LList: TStringDynArray;
fileFound: string;
begin
mmoDfmFiles.Clear;
mmoSqlFiles.Clear;
mmoErrors.Clear;
setTitles;
LList := TDirectory.GetFiles(edtDfmFilesPath.Text, '*.dfm', TSearchOption.soAllDirectories);
for fileFound in LList do
begin
mmoDfmFiles.Lines.Add(fileFound);
setTitles;
Application.ProcessMessages;
end;
end;
end.