forked from sirodcar/commonx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBModuleCommands.pas
More file actions
300 lines (246 loc) · 7.54 KB
/
DBModuleCommands.pas
File metadata and controls
300 lines (246 loc) · 7.54 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
unit DBModuleCommands;
interface
uses
mysqlstoragestring, DatabaseconnectionDm, stringx, systemx, typex, commandprocessor, sysutils, dir, classes, storageenginetypes, debug, betterobject, variants;
type
TDBCommand = class(TCommand)
private
FRecordResults: boolean;
FDBcontext: string;
protected
slWriteLog, slProblemLog: TStringlist;
dbm: TBestDatabaseDM;
procedure DoExecute; override;
procedure DBExecute;virtual;abstract;
public
procedure Detach; override;
property DBcontext: string read FDBcontext write FDBcontext;
property RecordResults: boolean read FRecordResults write FRecordResults;
procedure RecordQuery(sQuery: string);overload;
procedure RecordQuery(sl: TStringlist);overload;//ONE QUERY PER LINE NO CRLFS IN QUERY!
procedure InitExpense; override;
procedure WriteAndLog(sQuery: string);
procedure WriteQuery(sQuery: string);
function ReadQuery(sQuery: string): TSERowSet;
function ReadQueryH(sQuery: string): IHolder<TSERowSet>;
function FunctionQuery(sQuery: string; default: ni): ni;overload;
function FunctionQuery(sQuery: string; default: string): string;overload;
function GetCreateTable(sTable: string): string;overload;
procedure Problem(sProblem: string);
procedure BackupAndFlag(tblFrom: string; tblTo: string = '');
procedure RestoreAndUnflag(tblOriginal, tblBackup: string);
procedure UnflagBackup(tblOriginal, tblBackup: string);
procedure RestoreUnfinishedOperations;
procedure CreateBackupSystem;
procedure AssertUncommitted;
function TableHasColumn(sTable, sColumn: string): boolean;
function GetColumnsEndingWith(sTable, sColumnEnd: string): TArray<string>;
end;
var
datafolder: string;
contextfile: string;
implementation
{ TDBCommand }
procedure TDBCommand.AssertUncommitted;
var
i: ni;
begin
i := FunctionQuery('select count(*) from backup_table_log', 0);
if i>0 then
raise ECritical.create('***** THERE ARE UNCOMMITTED OPERATIONS, RUN COMMIT or ROLLBACK before continuing!!*********');
end;
procedure TDBCommand.BackupAndFlag(tblFrom, tblTo: string);
begin
if tblTo = '' then
tblTo := '_rollback_'+tblFrom;
var cnt := functionquery('select count(*) from backup_table_log where tbl_original='+gvs(tblFrom)+' and tbl_backup='+gvs(tblTo),0);
if cnt > 0 then
raise ECritical.create('rollback first! there are unsucessful operations that have not been rolled back');
WriteQuery('drop table if exists '+tblTo);
dbm.CopyTable(tblFrom, tblTo);
// WriteQuery('create table '+tblTo+' like '+tblFrom);
// WriteQuery('insert into '+tblto+' select * from '+tblFrom);
WriteQuery('insert into backup_table_log values (now(),'+gvs(tblFrom)+','+gvs(tblTo)+')');
end;
procedure TDBCommand.CreateBackupSystem;
begin
WriteQuery('create table if not exists backup_table_log (ts timestamp, tbl_original varchar(255), tbl_backup varchar(255))');
end;
procedure TDBCommand.Detach;
begin
if detached then exit;
slWriteLog.free;
slWriteLog := nil;
slProblemLog.free;
slProblemLog := nil;
inherited;
end;
procedure TDBCommand.DoExecute;
begin
inherited;
slWriteLog := TStringlist.create;
slProblemLog := TStringlist.create;
dbm := TBestDatabaseDM.create;
try
// dbm.Context := 'simple;db=mmm_crimp_hd;host=crimp-hd.mysql.database.azure.com;user=crimpdbadmin@crimp-hd;pass=B@W@v}B5y^ue';
dbm.Context := DBContext;
// dbm.ConfigureFromContext;
dbm.ConnectWrite;
CreateBackupSystem;
DBExecute;
var finalfile := datafolder+'_import.sql';
var problemfile := datafolder+'_problems.txt';
slProblemLog.savetoFile(problemfile);
slWriteLog.savetoFile(finalfile);
finally
dbm.Free;
end;
end;
function TDBCommand.FunctionQuery(sQuery, default: string): string;
begin
var rows := ReadQueryH(sQuery);
if rows.o.RowCount < 1 then
exit(default)
else begin
if varisnull(rows.o.values[0,0]) then
exit('')
else
exit(rows.o.values[0,0]);
end;
end;
function TDBCommand.GetColumnsEndingWith(sTable, sColumnEnd: string): TArray<string>;
var
s, sJunk: string;
t: ni;
sl: IHolder<TStringList>;
begin
s := GetCreateTable(sTable);
s := lowercase(s);
splitstringNocase(s, 'KEY ', s, sJunk);
sl := ParseStringH(s, '`');
for t := sl.o.count-1 downto 0 do begin
if (t and 1)= 0 then
sl.o.Delete(t);
end;
setlength(result,0);
for t := 0 to sl.o.count-1 do begin
if comparetext(zcopy(sl.o[t], length(sl.o[t])-length(sColumnEnd), length(sColumnEnd)), sColumnEnd)=0 then begin
setlength(result,length(result)+1);
result[high(result)] := sl.o[t];
end;
end;
end;
function TDBCommand.GetCreateTable(sTable: string): string;
begin
var rows := ReadQueryH('show create table '+sTable);
if rows.o.RowCount < 1 then
exit('')
else begin
if rows.o.FieldCount < 2 then
exit('');
if varisnull(rows.o.values[1,0]) then
exit('')
else
exit(rows.o.values[1,0]);
end;
end;
function TDBCommand.FunctionQuery(sQuery: string; default: ni): ni;
begin
var rows := ReadQueryH(sQuery);
if rows.o.RowCount < 1 then
exit(default)
else
exit(rows.o.values[0,0]);
end;
procedure TDBCommand.InitExpense;
begin
inherited;
memoryexpense := 1.0;
end;
procedure TDBCommand.Problem(sProblem: string);
begin
slProblemLog.add(sProblem);
end;
function TDBCommand.ReadQuery(sQuery: string): TSERowSet;
begin
result := nil;
dbm.ExecuteRead(sQuery, result);
end;
function TDBCommand.ReadQueryH(sQuery: string): IHolder<TSERowSet>;
begin
result := THolder<TSERowSet>.create;
result.o := ReadQuery(sQuery);
end;
procedure TDBCommand.RecordQuery(sl: TStringlist);
var
t: ni;
begin
for t:= 0 to sl.count-1 do begin
RecordQuery(sl[t]);
end;
end;
procedure TDBCommand.RestoreAndUnflag(tblOriginal, tblBackup: string);
var
cnt: ni;
begin
try
cnt := FunctionQuery('select count(*) from '+tblBackup,0);
except
cnt := 0;
end;
if cnt > 0 then begin
// if cnt = 0 then
// raise ECritical.create('cannot restore from an empty backup');
WriteQuery('drop table if exists '+tblOriginal);
dbm.CopyTable(tblBackup, tblOriginal);
// WriteQuery('create table '+tblOriginal+' like '+tblBackup);
// WriteQuery('insert into '+tblOriginal+' select * from '+tblBackup);
UnFlagBackUp(tblOriginal, tblBackup);
WriteQuery('drop table if exists '+tblBackup);
end;
end;
procedure TDBCommand.RestoreUnfinishedOperations;
begin
var rs := ReadQueryH('select * from backup_table_log order by ts desc');
for var t := 0 to rs.o.RowCount-1 do begin
rs.o.Cursor := t;
RestoreAndUnflag(rs.o['tbl_original'],rs.o['tbl_backup']);
end;
end;
function TDBCommand.TableHasColumn(sTable, sColumn: string): boolean;
var
s: string;
t: ni;
begin
s := GetCreateTable(sTable);
s := lowercase(s);
result := zpos(lowercase('`'+sColumn+'`'), s) >= 0;
end;
procedure TDBCommand.UnflagBackup(tblOriginal, tblBackup: string);
begin
WriteQuery('delete from backup_table_log where tbl_original='+gvs(tblOriginal)+' and tbl_backup='+gvs(tblBackup));
end;
procedure TDBCommand.RecordQuery(sQuery: string);
begin
exit;
if sQuery = '' then
exit;
if sQuery[high(sQuery)] <> ';' then
sQuery := sQuery + ';';
var sdir := datafolder;
forcedirectories(sdir);
// var sFile := GetNextSequentialFileName(sDir,'.sql');
// stringx.SaveStringAsFile(sFile, sQuery);
slWriteLog.add(sQuery);
end;
procedure TDBCommand.WriteAndLog(sQuery: string);
begin
// Debug.Log(sQuery);
WriteQuery(sQuery);
RecordQuery(sQuery);
end;
procedure TDBCommand.WriteQuery(sQuery: string);
begin
dbm.ExecuteWrite(sQuery);
end;
end.