-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappdbmaintain.pas
More file actions
394 lines (332 loc) · 12.6 KB
/
appdbmaintain.pas
File metadata and controls
394 lines (332 loc) · 12.6 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
unit AppDbMaintain;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, Windows, fileutil, ComCtrls,
AppDb;
// Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
type
{ TAppDbMaintain }
TAppDbMaintain = class(TAppDatabase)
private
function IsFileInUse(FileName: TFileName): Boolean;
procedure SaveTrvStateInsert(TrvName, NodeState : string; Node : TTreeNode);
procedure SaveTrvStateUpdate(TrvName, NodeState : string; Node : TTreeNode);
protected
public
constructor Create; overload;
destructor Destroy; override;
procedure CompressAppDatabase;
function CopyDbFile: Boolean;
procedure SaveTreeViewState(Trv: TTreeView);
procedure ReadTreeViewState(Trv: TTreeView);
procedure Optimze;
procedure ResetAutoIncrementAll;
procedure ResetAutoIncrementTbl(aTblName : String); // Not used
published
end;
implementation
uses Form_Main, DataModule, Settings, Tablename, Db, appdbFQ;
{ TFolder }
function TAppDbMaintain.IsFileInUse(FileName: TFileName): Boolean;
var
HFileRes: HFILE;
begin
result := False;
if not FileExists(FileName) then exit;
HFileRes := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE, 0, nil,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
result := (HFileRes = INVALID_HANDLE_VALUE);
if not result then CloseHandle(HFileRes);
end;
constructor TAppDbMaintain.Create;
begin
inherited;
//..
end;
destructor TAppDbMaintain.Destroy;
begin
//..
inherited Destroy;
end;
procedure TAppDbMaintain.CompressAppDatabase;
begin
try
With DataModule1 do begin
SQLite3Connection.Close();
SQLite3Connection.DatabaseName := dbFile;
SQLite3Connection.Open;
SQLTransaction.Active := False;
SQLite3Connection.ExecuteDirect('End Transaction'); // End the transaction
SQLite3Connection.ExecuteDirect('VACUUM');
SQLite3Connection.ExecuteDirect('Begin Transaction'); //Start a transaction for SQLdb to use
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('De database is schoongemaakt en verkleind.');
end;
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij het comprimeren van de applicatie database.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Let op', 'Onverwachte fout bij het comprimeren van de applicatie database.', mtError, [mbOK],0);
end;
end;
end;
function TAppDbMaintain.CopyDbFile: Boolean;
var
Prefix : String;
SrcFilename, DestFilename : String;
begin
SrcFilename := ExtractFilePath(Application.ExeName) + Settings.DatabaseFolder + PathDelim + Settings.DatabaseName;
if not IsFileInUse(SrcFilename) then begin
Prefix := FormatDateTime('YYYYMMDD_', Now);
DestFilename := ExtractFilePath(Application.ExeName) + Settings.DatabaseFolder + PathDelim + Settings.BackupFolder + PathDelim + Prefix + Settings.DatabaseName;
if FileExists(SrcFilename) then begin
if not FileExists(DestFilename) then begin
CopyFile(SrcFilename, DestFilename);
FrmMain.Logging.WriteToLogInfo('Kopie van de applicatie database is gemaakt.');
FrmMain.Logging.WriteToLogInfo('Kopie is: ' + DestFilename);
Result := True;
end
else begin
if MessageDlg('Let op.', 'Het bestand bestaat al. Wilt u het overschrijven?' + sLineBreak +
sLineBreak +
'Bestand: ' + DestFilename,
mtWarning, [mbYes, mbNo], 0, mbNo) = mrYes then begin
CopyFile(SrcFilename, DestFilename, [cffOverwriteFile]);
FrmMain.Logging.WriteToLogInfo('Kopie van de applicatie database is gemaakt.');
FrmMain.Logging.WriteToLogInfo('Kopie is: ' + DestFilename);
Result := True;
end;
end;
end
else begin
messageDlg('Fout.', 'Het database bestand is niet gevonden.', mtError, [mbOK],0);
Result := False;
end;
end
else begin
messageDlg('Let op.', 'Het bestand is in gebruik (door iemand anders). ' +sLineBreak +
'Er wordt géén kopie gemaakt.' , mtWarning, [mbOK],0);
Result := False;
end;
end;
procedure TAppDbMaintain.SaveTreeViewState(Trv: TTreeView);
var
Node : TTreeNode;
begin
Node := Trv.Items.GetFirstNode;
while Assigned(Node) do
begin
if Node.Expanded then begin
SaveTrvStateInsert(Trv.Name, 'Expanded', Node);
SaveTrvStateUpdate(Trv.Name, 'Expanded', Node);
end
else begin
SaveTrvStateInsert(Trv.Name, 'Collapsed', Node);
SaveTrvStateUpdate(Trv.Name, 'Collapsed', Node);
end;
Node := Node.GetNext;
end;
end;
procedure TAppDbMaintain.ReadTreeViewState(Trv: TTreeView);
var
SqlText : String;
NodeGuid, NodeState : String;
i : Integer;
Node : TTreeNode;
s : String;
begin
SqlText := 'select GUID_NODE, ITEM_DATA, ITEM_INFO ';
SqlText += 'from ' + SETTINGS_APP + ' ';
SqlText += 'where ITEM_INFO = :ITEM_INFO;';
s := '';
try
With DataModule1 do begin
SQLQuery.Close;
SQLite3Connection.Close();
SQLite3Connection.DatabaseName := dbFile;
SQLQuery.SQL.Text := SqlText;
SQLQuery.Params.ParamByName('ITEM_INFO').AsString := Trv.Name;
s := Trv.Name;
SQLite3Connection.Open;
SQLQuery.Open;
SQLQuery.First;
while not SQLQuery.Eof do begin
NodeGuid := SQLQuery.FieldByName('GUID_NODE').AsString;
NodeState := SQLQuery.FieldByName('ITEM_DATA').AsString;
if NodeState = 'Expanded'then begin
s := Trv.Name + ' - ' + NodeGuid;
for i := 0 to trv.Items.Count -1 do begin
Node := trv.Items[i];
if PtrApiObject(Node.Data)^.Guid = NodeGuid then begin
trv.Items[i].Expand(false);
//if PtrApiObject(Node.Data)^.Guid = '{C7C1F61F-0E23-4D63-AA3F-E1F76A501752}' then
// s := '';
end
end;
end;
SQLQuery.next;
end;
SQLQuery.Close;
SQLite3Connection.Close();
end;
except
on E: EDatabaseError do
begin
FrmMain.Logging.WriteToLogInfo('Het inlezen van de Node States uit tabel ' + SETTINGS_APP + ' is mislukt.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Fout.', 'Het inlezen van de Node States is mislukt.', mtError, [mbOK],0);
end;
end;
end;
procedure TAppDbMaintain.Optimze;
begin
try
With DataModule1 do begin
SQLite3Connection.Close();
SQLite3Connection.DatabaseName := dbFile;
SQLite3Connection.Open;
SQLTransaction.Active := False;
SQLite3Connection.ExecuteDirect('End Transaction'); // End the transaction
SQLite3Connection.ExecuteDirect('pragma optimize;');
SQLite3Connection.ExecuteDirect('Begin Transaction'); //Start a transaction for SQLdb to use
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('De database is geoptimaliseerd.');
end;
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij het optimaliseren van de applicatie database.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Let op', 'Onverwachte fout bij het optimaliseren van de applicatie database.', mtError, [mbOK],0);
end;
end;
end;
procedure TAppDbMaintain.ResetAutoIncrementAll;
var
SqlText : String;
begin
SqlText := 'delete from sqlite_sequence';
With DataModule1 do begin
try
SQLQuery.Close;
SQLite3Connection.Close();
SQLite3Connection.DatabaseName := dbFile;
SQLQuery.SQL.Text := SqlText;
SQLite3Connection.Open;
SQLTransaction.Active:=True;
SQLQuery.ExecSQL;
SQLTransaction.Commit;
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('De ID''s van alle tabellen zijn gereset.');
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij resetten van alle id''s.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Fout.', 'Fout bij resetten van alle id''s.', mtError, [mbOK],0);
end;
end;
end;
end;
procedure TAppDbMaintain.ResetAutoIncrementTbl(aTblName: String);
var
SqlText : String;
begin
SqlText := 'delete from sqlite_sequence where name = :TABLE_NAME';
With DataModule1 do begin
try
SQLQuery.Close;
SQLite3Connection.Close();
SQLite3Connection.DatabaseName := dbFile;
SQLQuery.SQL.Text := SqlText;
SQLQuery.Params.ParamByName('TABLE_NAME').AsString := aTblName;
SQLite3Connection.Open;
SQLTransaction.Active:=True;
SQLQuery.ExecSQL;
SQLTransaction.Commit;
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('De ID''s van de tabel ' + aTblName + ' zijn gereset.');
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij resetten van de id''s.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Fout.', 'Fout bij resetten van de id''s.', mtError, [mbOK],0);
end;
end;
end;
end;
procedure TAppDbMaintain.SaveTrvStateInsert(TrvName, NodeState: string; Node : TTreeNode);
var
SqlText : String;
begin
SqlText := 'insert into ' + SETTINGS_APP + ' (GUID, GUID_NODE, LOGGED_IN_USER, ITEM_DATA, ITEM_INFO) ' +
'select :GUID, :GUID_NODE, :LOGGED_IN_USER, :ITEM_DATA, :ITEM_INFO ' +
'where not exists (select GUID_NODE from ' + SETTINGS_APP + ' where GUID_NODE = :GUID_NODE '+
'and ITEM_INFO = :ITEM_INFO);';
try
With DataModule1 do begin
SQLQuery.Close;
SQLite3Connection.Close();
SQLite3Connection.DatabaseName := dbFile;
SQLQuery.SQL.Text := SqlText;
SQLQuery.Params.ParamByName('GUID').AsString := TGUID.NewGuid.ToString();
SQLQuery.Params.ParamByName('GUID_NODE').AsString := PtrApiObject(Node.Data)^.Guid;
SQLQuery.Params.ParamByName('LOGGED_IN_USER').AsString := SysUtils.GetEnvironmentVariable('USERNAME');
SQLQuery.Params.ParamByName('ITEM_DATA').AsString := NodeState;
SQLQuery.Params.ParamByName('ITEM_INFO').AsString := TrvName;
SQLite3Connection.Open;
SQLTransaction.Active:=True;
SQLQuery.ExecSQL;
SQLTransaction.Commit;
SQLite3Connection.Close();
end;
except
on E: EDatabaseError do
begin
FrmMain.Logging.WriteToLogInfo('Het invoeren van een Node State in de tabel ' + SETTINGS_APP + ' is mislukt.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Fout.', 'Het opslaan van de TreeView staat is mislukt.', mtError, [mbOK],0);
end;
end;
end;
procedure TAppDbMaintain.SaveTrvStateUpdate(TrvName, NodeState: string; Node: TTreeNode);
var
SqlText : String;
begin
SqlText := 'update ' + SETTINGS_APP + ' ' +
'set ITEM_DATA = :ITEM_DATA, ' +
'ITEM_INFO = :ITEM_INFO, ' +
'LOGGED_IN_USER = :LOGGED_IN_USER ' +
'where GUID_NODE = :GUID_NODE and ITEM_INFO = :ITEM_INFO;';
try
With DataModule1 do begin
SQLQuery.Close;
SQLite3Connection.Close();
SQLite3Connection.DatabaseName := dbFile;
SQLQuery.SQL.Text := SqlText;
SQLQuery.Params.ParamByName('GUID_NODE').AsString := PtrApiObject(Node.Data)^.Guid;
SQLQuery.Params.ParamByName('LOGGED_IN_USER').AsString := SysUtils.GetEnvironmentVariable('USERNAME');
SQLQuery.Params.ParamByName('ITEM_DATA').AsString := NodeState;
SQLQuery.Params.ParamByName('ITEM_INFO').AsString := TrvName;
SQLite3Connection.Open;
SQLTransaction.Active:=True;
SQLQuery.ExecSQL;
SQLTransaction.Commit;
SQLite3Connection.Close();
end;
except
on E: EDatabaseError do
begin
FrmMain.Logging.WriteToLogInfo('Het invoeren van een Node State in de tabel ' + SETTINGS_APP + ' is mislukt.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Fout.', 'Het opslaan van de TreeView staat is mislukt.', mtError, [mbOK],0);
end;
end;
end;
end.