-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappdbcreate.pas
More file actions
523 lines (452 loc) · 18.9 KB
/
appdbcreate.pas
File metadata and controls
523 lines (452 loc) · 18.9 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
unit AppDbCreate;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms,
AppDb;
type
{ TCreateAppdatabase }
TCreateAppdatabase = class(TAppDatabase)
private
function GetError: Boolean;
procedure SetError(AValue: Boolean);
private
FError : Boolean;
FNewDatabaseCreated : Boolean;
function CheckDatabaseFile : boolean;
procedure SqliteAutoVacuum;
procedure SqliteJournalMode;
procedure SqliteSynchronous;
procedure SqliteTemStore;
procedure SqliteUserVersion;
procedure CreTable(TableName, SqlText, Version : String);
procedure AlterTable(TableName, SqlText, Version : String);
procedure InsertMeta(Version : String);
procedure UpdateMeta(Version : String);
function SelectMeta : Integer;
protected
public
constructor Create; overload;
destructor Destroy; override;
function CreateNewDatabase : boolean;
procedure CreateAllTables;
published
end;
implementation
uses Form_Main, DataModule, Dialogs, Db, Settings;
{ TCreateAppdatabase }
const
creTblSetmeta = 'CREATE TABLE IF NOT EXISTS ' + SETTINGS_META + ' (' +
'KEY VARCHAR(50), ' +
'VALUE VARCHAR(255));';
creTblFolderList = 'create table if not exists ' + FOLDER_LIST + ' (' +
'ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, ' +
'GUID VARCHAR(50) UNIQUE , ' +
'OBJECTTYPE INTEGER , ' +
'NAME VARCHAR(50) , ' +
'PARENT_FOLDER VARCHAR(50) , ' +
'DATE_CREATED DATE , ' +
'DATE_ALTERED DATE , ' +
'CREATED_BY VARCHAR(100) , ' +
'ALTERED_BY VARCHAR(100));';
creTblQueryList = 'create table if not exists ' + QUERY_LIST + ' (' +
'ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, ' +
'GUID VARCHAR(50) UNIQUE , ' +
'OBJECTTYPE VARCHAR(10) , ' +
'NAME VARCHAR(100) , ' +
'PARENT_FOLDER VARCHAR(50) , ' +
'URL VARCHAR(255) , ' +
'DESCRIPTION_SHORT VARCHAR(255) , ' +
'DESCRIPTION_LONG VARCHAR(10000) , ' +
'AUTHENTICATION BOOL , ' +
'AUTHENTICATION_USER VARCHAR(255) , ' + // 100 + encrytion = 200
'AUTHENTICATION_PWD VARCHAR(255) , ' + // 100 + encrytion = 200
'TOKEN VARCHAR(255) , ' + // 100 + encrytion = 200
'AUTHENTICATION_CHECK VARCHAR(255) , ' + // 100 + encrytion = 200
'PAGING_SEARCHTEXT VARCHAR(100) , ' +
'SALT VARCHAR(100) , ' +
'DATE_CREATED DATE , ' +
'DATE_ALTERED DATE , ' +
'CREATED_BY VARCHAR(100) , ' +
'ALTERED_BY VARCHAR(100));';
CreTblAppSetApp = 'create table if not exists ' + SETTINGS_APP + ' (' +
'ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, ' +
'GUID VARCHAR(50) UNIQUE , ' +
'GUID_NODE VARCHAR2(50) , ' +
'LOGGED_IN_USER VARCHAR2(50) , ' +
'ITEM_DATA VARCHAR2(100) , ' +
'ITEM_INFO VARCHAR2(50));';
AltTblQLReqb = 'alter table ' + QUERY_LIST + ' ' +
'add REQUEST_BODY VARCHAR(500);';
AltTblQLHttpMeth = 'alter table ' + QUERY_LIST + ' ' +
'add HTTP_METHOD VARCHAR(10);';
{%region properties}
function TCreateAppdatabase.GetError: Boolean;
begin
Result := FError;
end;
procedure TCreateAppdatabase.SetError(AValue: Boolean);
begin
FError := AValue;
end;
{%endregion properties}
{%region constructor - destructor}
constructor TCreateAppdatabase.Create;
begin
inherited;
FNewDatabaseCreated := False;
end;
destructor TCreateAppdatabase.Destroy;
begin
//..
inherited Destroy;
end;
{%endregion constructor - destructor}
function TCreateAppdatabase.CheckDatabaseFile: boolean;
begin
if dbFile = '' then begin
result := false;
end
else begin
if not FileExists(dbFile) then begin
FrmMain.Logging.WriteToLogInfo('Aanmaken nieuw leeg database bestand op de locatie: '+ dbFile );
try
DataModule1.SQLite3Connection.Close();
DataModule1.SQLite3Connection.DatabaseName := dbFile;
DataModule1.SQLite3Connection.Open; //creates the file
DataModule1.SQLite3Connection.Close(True);
FrmMain.Logging.WriteToLogInfo('Leeg database bestand is aangemaakt.');
FNewDatabaseCreated := True;
result := true;
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij het aanmaken van een leeg database bestand.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
FError := true;
result := false;
end;
end;
end
else begin
frmMain.Logging.WriteToLogInfo('Het database bestand bestaat al. ('+ dbFile +').');
FError := false;
result := true;
end;
end;
end;
procedure TCreateAppdatabase.SqliteAutoVacuum;
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 auto_vacuum = INCREMENTAL;');
SQLite3Connection.ExecuteDirect('Begin Transaction'); //Start a transaction for SQLdb to use
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('Database instelling: auto_vacuum = incremental.');
end;
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij maken van een database instelling.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Let op', 'Onverwachte fout bij het maken van een database instelling.', mtError, [mbOK],0);
end;
end;
end;
procedure TCreateAppdatabase.SqliteJournalMode;
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 journal_mode = WAL;');
SQLite3Connection.ExecuteDirect('Begin Transaction'); //Start a transaction for SQLdb to use
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('Database instelling: journal_mode = WAL.');
end;
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij maken van een database instelling.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Let op', 'Onverwachte fout bij het maken van een database instelling.', mtError, [mbOK],0);
end;
end;
end;
procedure TCreateAppdatabase.SqliteSynchronous;
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 synchronous = normal;');
SQLite3Connection.ExecuteDirect('Begin Transaction'); //Start a transaction for SQLdb to use
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('Database instelling: synchronous = normal.');
end;
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij maken van een database instelling.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Let op', 'Onverwachte fout bij het maken van een database instelling.', mtError, [mbOK],0);
end;
end;
end;
procedure TCreateAppdatabase.SqliteTemStore;
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 temp_store = memory;');
SQLite3Connection.ExecuteDirect('Begin Transaction'); //Start a transaction for SQLdb to use
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('Database instelling: temp_store = memory.');
end;
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij maken van een database instelling.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Let op', 'Onverwachte fout bij het maken van een database instelling.', mtError, [mbOK],0);
end;
end;
end;
procedure TCreateAppdatabase.SqliteUserVersion;
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 USER_VERSION = ' + Settings.DataBaseVersion + ';');
SQLite3Connection.ExecuteDirect('Begin Transaction'); //Start a transaction for SQLdb to use
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('Database instelling: USER_VERSION = ' + Settings.DatabaseVersion);
end;
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij maken van een database instelling.');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Let op', 'Onverwachte fout bij het maken van een database instelling.', mtError, [mbOK],0);
end;
end;
end;
function TCreateAppdatabase.CreateNewDatabase: boolean;
begin
if CheckDatabaseFile then begin // check if database file exists, if not then the file is created
if FNewDatabaseCreated then begin
//Set SQlite database settings
SqliteAutoVacuum;
SqliteJournalMode;
SqliteSynchronous;
SqliteTemStore;
SqliteUserVersion;
if not FError then CreTable(SETTINGS_META, creTblSetmeta, '0');
if not FError then InsertMeta('0');
end;
// Create the tables
CreateAllTables;
result := true;
end
else begin
result := true;
end;
end;
procedure TCreateAppdatabase.CreateAllTables;
var
Version : String;
begin
if FileExists(dbFile) then begin
if (StrToInt(Settings.DataBaseVersion) >= 1) and (SelectMeta = 0) then begin // (version 1 tables)
Version := '1';
if not FError then CreTable(FOLDER_LIST, creTblFolderList, Version);
if not FError then CreTable(QUERY_LIST, creTblQueryList, Version);
if not FError then CreTable(SETTINGS_APP, CreTblAppSetApp, Version);
if not FError then UpdateMeta(Version);
FrmMain.Logging.WriteToLogInfo('Het aanmaken/bijwerken van de database (tabellen) is gereed. (Versie: ' + Version + ').');
end;
if StrToInt(Settings.DataBaseVersion) > SelectMeta then begin
if SelectMeta < 3 then begin
Version := '2';
if not FError then AlterTable(QUERY_LIST, AltTblQLReqb, Version);
if not FError then AlterTable(QUERY_LIST, AltTblQLHttpMeth, Version);
if not FError then SqliteUserVersion;
if not FError then UpdateMeta(Version);
FrmMain.Logging.WriteToLogInfo('Het aanmaken/bijwerken van de database (tabellen) is gereed. (Versie: ' + Version + ').');
end;
{if SelectMeta < 4 then begin
Version := '3';
SqliteUserVersion;
//..
end; }
end;
if not FError then begin
messageDlg('Gereed.', 'Het aanmaken/bijwerken van de database (tabellen) is gereed.', mtInformation, [mbOK],0);
end;
end
else begin // database file does not exists
FrmMain.Logging.WriteToLogError('De database is niet gevonden.');
messageDlg('Let op', 'De database is niet gevonden.', mtError, [mbOK],0);
FError := true;
end;
end;
procedure TCreateAppdatabase.CreTable(TableName, SqlText, Version : String);
begin
try
FrmMain.Logging.WriteToLogInfo('Aanmaken tabel: ' + TableName + '. (Versie: ' + Version + ').');
With DataModule1 do begin
SQLite3Connection.Close();
SQLite3Connection.DatabaseName:=dbFile;
SQLite3Connection.Open;
SQLTransaction.Active:=True;
SQLite3Connection.ExecuteDirect(SqlText);
SQLTransaction.Commit;
SQLite3Connection.Close();
end;
except
on E : Exception do begin
FrmMain.Logging.WriteToLogError('Fout bij het aanmaken van de tabel: ' + TableName + '. (Versie: ' + Version + ').');
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Let op', 'Het aanmaken van de tabel "' + TableName + '" is mislukt.', mtError, [mbOK],0);
FError := true;
end;
end;
end;
procedure TCreateAppdatabase.AlterTable(TableName, SqlText, Version: String);
begin
try
With DataModule1 do begin
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 tabel ' + TableName + ' is gewijzigd.');
end;
except
on E: EDatabaseError do begin
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Fout.', 'Het wijzigen van de tabel ' + TableName + ' is mislukt.', mtError, [mbOK],0);
FError := true;
end;
end;
end;
procedure TCreateAppdatabase.InsertMeta(Version : String);
var
SqlString : String;
begin
SqlString := 'insert into ' + SETTINGS_META + ' (KEY, VALUE) values (:KEY, :VERSION)';
try
With DataModule1 do begin
SQLQuery.Close;
SQLite3Connection.Close();
SQLite3Connection.DatabaseName:=dbFile;
SQLQuery.SQL.Text :=SqlString;
SQLQuery.Params.ParamByName('KEY').AsString := 'Version';
SQLQuery.Params.ParamByName('VERSION').AsString := Version;
SQLite3Connection.Open;
SQLTransaction.Active:=True;
SQLQuery.ExecSQL;
SQLTransaction.Commit;
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('Versie is toegevoegd aan tabel '+ SETTINGS_META + '.');
end;
except
on E: EDatabaseError do begin
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Fout.', 'Het invoeren van "Versienummmer" is mislukt.', mtError, [mbOK],0);
FError := true;
end;
end;
end;
procedure TCreateAppdatabase.UpdateMeta(Version: String);
var
SqlString : String;
begin
SqlString := 'update ' + SETTINGS_META + ' set VALUE = :VALUE where KEY = :KEY;';
try
With DataModule1 do begin
SQLQuery.Close;
SQLite3Connection.Close();
SQLite3Connection.DatabaseName:=dbFile;
SQLQuery.SQL.Text :=SqlString;
SQLQuery.Params.ParamByName('KEY').AsString := 'Version';
SQLQuery.Params.ParamByName('VALUE').AsString := Version;
SQLite3Connection.Open;
SQLTransaction.Active:=True;
SQLQuery.ExecSQL;
SQLTransaction.Commit;
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('Versie is bijgewerkt in de tabel '+ SETTINGS_META + '.');
end;
except
on E: EDatabaseError do begin
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Fout.', 'Het actualiseren van het versienummer is mislukt.', mtError, [mbOK],0);
FError := true;
end;
end;
end;
function TCreateAppdatabase.SelectMeta: Integer;
var
SqlString : String;
Version : Integer;
begin
SqlString := 'select VALUE from ' + SETTINGS_META + ' where KEY = :KEY';
try
With DataModule1 do begin
SQLQuery.Close;
SQLite3Connection.Close();
SQLite3Connection.DatabaseName:=dbFile;
SQLQuery.SQL.Text := SqlString;
SQLQuery.Params.ParamByName('KEY').AsString := 'Version';
SQLite3Connection.Open;
SQLQuery.Open;
SQLQuery.First;
while not SQLQuery.Eof do begin
Version := SQLQuery.FieldByName('VALUE').AsInteger;
SQLQuery.Next;
end;
SQLQuery.Close;
SQLite3Connection.Close();
FrmMain.Logging.WriteToLogInfo('Versie is opgevraagd. (Tabel: '+ SETTINGS_META + ').');
Result := Version;
end;
except
on E: EDatabaseError do begin
FrmMain.Logging.WriteToLogError('Melding:');
FrmMain.Logging.WriteToLogError(E.Message);
messageDlg('Fout.', 'Opvragen versienummer is mislukt.', mtError, [mbOK],0);
FError := true;
Result := -1;
end;
end;
end;
end.