forked from fmd-project-team/FMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLiteData.pas
417 lines (372 loc) · 10.1 KB
/
SQLiteData.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
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
unit SQLiteData;
{$mode objfpc}{$H+}
interface
uses
SysUtils, LazFileUtils, strutils, sqlite3conn, sqldb;
type
{ TSQLite3ConnectionH }
TSQLite3ConnectionH = class(TSQLite3Connection)
public
property Handle read GetHandle;
end;
TExceptionEvent = procedure(Sender: TObject; E: Exception) of object;
{ TSQliteData }
TSQliteData = class
private
FAutoVacuum: Boolean;
FConn: TSQLite3ConnectionH;
FFieldsParams: String;
FOnError: TExceptionEvent;
FTrans: TSQLTransaction;
FQuery: TSQLQuery;
FFilename: String;
FTableName: String;
FCreateParams: String;
FSelectParams: String;
FRecordCount: Integer;
procedure DoOnError(E: Exception);
function GetAutoApplyUpdates: Boolean;
procedure SetAutoApplyUpdates(AValue: Boolean);
procedure SetAutoVacuum(AValue: Boolean);
procedure SetCreateParams(AValue: String);
procedure SetFieldsParams(AValue: String);
procedure SetOnError(AValue: TExceptionEvent);
procedure SetSelectParams(AValue: String);
protected
function OpenDB: Boolean; virtual;
function CreateDB: Boolean; virtual;
function ConvertNewTableIF: Boolean; virtual;
procedure DoConvertNewTable;
procedure GetRecordCount; virtual;
procedure SetRecordCount(const AValue: Integer); virtual;
procedure IncRecordCount(const N: Integer = 1);
procedure Vacuum; virtual;
public
constructor Create;
destructor Destroy; override;
function Open(const AOpenTable: Boolean = True; const AGetRecordCount: Boolean = True): Boolean; virtual;
function OpenTable(const AGetRecordCount: Boolean = True): Boolean; virtual;
procedure Close; virtual;
procedure CloseTable; virtual;
procedure Refresh(RecheckDataCount: Boolean = False); virtual;
procedure Commit; virtual;
procedure CommitRetaining; virtual;
procedure Save; virtual;
function Connected: Boolean; inline;
property Connection: TSQLite3ConnectionH read FConn;
property Transaction: TSQLTransaction read FTrans;
property Table: TSQLQuery read FQuery;
property Filename: String read FFilename write FFilename;
property TableName: String read FTableName write FTableName;
property CreateParams: String read FCreateParams write SetCreateParams;
property SelectParams: String read FSelectParams write SetSelectParams;
property FieldsParams: String read FFieldsParams write SetFieldsParams;
property RecordCount: Integer read FRecordCount;
property AutoApplyUpdates: Boolean read GetAutoApplyUpdates write SetAutoApplyUpdates;
property AutoVacuum: Boolean read FAutoVacuum write SetAutoVacuum;
property OnError: TExceptionEvent read FOnError write SetOnError;
end;
function QuotedStr(const S: Integer): String; overload; inline;
function QuotedStr(const S: Boolean): String; overload; inline;
function QuotedStr(const S: TDateTime): String; overload; inline;
function QuotedStrD(const S: String): String; overload; inline;
function QuotedStrD(const S: Integer): String; overload; inline;
implementation
function QuotedStr(const S: Integer): String;
begin
Result := AnsiQuotedStr(IntToStr(S), '''');
end;
function QuotedStr(const S: Boolean): String;
begin
Result := AnsiQuotedStr(BoolToStr(S, '1', '0'), '''');
end;
function QuotedStr(const S: TDateTime): String;
begin
Result := AnsiQuotedStr(FormatDateTime('yyyy-mm-dd hh:mm:ss', S), '''');
end;
function QuotedStrD(const S: String): String;
begin
Result := AnsiQuotedStr(S, '"');
end;
function QuotedStrD(const S: Integer): String;
begin
Result := AnsiQuotedStr(IntToStr(S), '"');
end;
{ TSQliteData }
procedure TSQliteData.DoOnError(E: Exception);
begin
if Assigned(OnError) then
OnError(Self, E);
end;
function TSQliteData.GetAutoApplyUpdates: Boolean;
begin
Result := sqoAutoApplyUpdates in FQuery.Options;
end;
procedure TSQliteData.SetAutoApplyUpdates(AValue: Boolean);
begin
if AValue then
FQuery.Options := FQuery.Options + [sqoAutoApplyUpdates]
else
FQuery.Options := FQuery.Options - [sqoAutoApplyUpdates];
end;
procedure TSQliteData.SetAutoVacuum(AValue: Boolean);
begin
if FAutoVacuum = AValue then Exit;
FAutoVacuum := AValue;
end;
procedure TSQliteData.SetCreateParams(AValue: String);
begin
if FCreateParams = AValue then Exit;
FCreateParams := TrimSet(Trim(AValue), ['(', ')', ';']);
end;
procedure TSQliteData.SetFieldsParams(AValue: String);
begin
if FFieldsParams = AValue then Exit;
FFieldsParams := AValue;
end;
procedure TSQliteData.SetOnError(AValue: TExceptionEvent);
begin
if FOnError = AValue then Exit;
FOnError := AValue;
end;
procedure TSQliteData.SetSelectParams(AValue: String);
begin
if FSelectParams = AValue then Exit;
FSelectParams := AValue;
end;
function TSQliteData.OpenDB: Boolean;
begin
Result := False;
if FFilename = '' then Exit;
try
FConn.DatabaseName := FFilename;
FConn.Connected := True;
FTrans.Active := True;
except
end;
Result := FConn.Connected;
end;
function TSQliteData.CreateDB: Boolean;
begin
Result := False;
if (FTableName = '') or (FCreateParams = '') then Exit;
if not FConn.Connected then
if not OpenDB then Exit;
try
FConn.ExecuteDirect('DROP TABLE IF EXISTS ' + QuotedStrD(FTableName));
FConn.ExecuteDirect('CREATE TABLE ' + QuotedStrD(FTableName) + ' (' + FCreateParams + ')');
FTrans.Commit;
Result := True;
except
end;
end;
function TSQliteData.ConvertNewTableIF: Boolean;
begin
Result := False;
end;
procedure TSQliteData.DoConvertNewTable;
var
qactive: Boolean;
begin
if not ConvertNewTableIF then Exit;
if not FConn.Connected then Exit;
try
qactive := FQuery.Active;
if FQuery.Active then FQuery.Close;
with FConn do
begin
ExecuteDirect('DROP TABLE IF EXISTS ' + QuotedStrD('temp' + FTableName));
ExecuteDirect('CREATE TABLE ' + QuotedStrD('temp' + FTableName) + ' (' + FCreateParams + ')');
ExecuteDirect('INSERT INTO ' + QuotedStrD('temp' + FTableName) + ' (' + FieldsParams + ') SELECT ' + FieldsParams + ' FROM "' + FTableName + '"');
ExecuteDirect('DROP TABLE ' + QuotedStrD(FTableName));
ExecuteDirect('ALTER TABLE ' + QuotedStrD('temp' + FTableName) + ' RENAME TO ' + QuotedStrD(FTableName));
end;
FTrans.Commit;
if qactive <> FQuery.Active then
FQuery.Active := qactive;
except
FTrans.Rollback;
end;
end;
procedure TSQliteData.GetRecordCount;
begin
if FQuery.Active then
begin
FQuery.Last;
FRecordCount := FQuery.RecordCount;
FQuery.Refresh;
end
else FRecordCount := 0;
end;
procedure TSQliteData.SetRecordCount(const AValue: Integer);
begin
if FRecordCount = AValue then Exit;
FRecordCount := AValue;
end;
procedure TSQliteData.IncRecordCount(const N: Integer);
begin
Inc(FRecordCount, N);
end;
procedure TSQliteData.Vacuum;
var
qactive: Boolean;
begin
if not FConn.Connected then Exit;
try
qactive := FQuery.Active;
if FQuery.Active then FQuery.Close;
FConn.ExecuteDirect('END TRANSACTION');
try
FConn.ExecuteDirect('VACUUM');
finally
FConn.ExecuteDirect('BEGIN TRANSACTION');
if FQuery.Active <> qactive then
FQuery.Active := qactive;
end;
except
on E: Exception do
DoOnError(E);
end;
end;
constructor TSQliteData.Create;
begin
FConn := TSQLite3ConnectionH.Create(nil);
FTrans := TSQLTransaction.Create(nil);
FQuery := TSQLQuery.Create(nil);
FConn.CharSet := 'UTF8';
FConn.Transaction := FTrans;
FQuery.DataBase := FTrans.DataBase;
FQuery.Transaction := FTrans;
AutoApplyUpdates := True;
FAutoVacuum := True;
FRecordCount := 0;
FFilename := '';
FTableName := 'maintable';
FCreateParams := '';
end;
destructor TSQliteData.Destroy;
begin
Self.Close;
FConn.Free;
FQuery.Free;
FTrans.Free;
inherited Destroy;
end;
function TSQliteData.Open(const AOpenTable: Boolean;
const AGetRecordCount: Boolean): Boolean;
begin
Result := False;
if (FFilename = '') or (FCreateParams = '') then Exit;
if FileExistsUTF8(FFilename) then
Result := OpenDB
else
Result := CreateDB;
if Result and AOpenTable then
Result := OpenTable(AGetRecordCount);
end;
function TSQliteData.OpenTable(const AGetRecordCount: Boolean): Boolean;
begin
Result := False;
if not FConn.Connected then Exit;
try
if FQuery.Active then FQuery.Close;
if FSelectParams <> '' then
FQuery.SQL.Text := FSelectParams
else
FQuery.SQL.Text := 'SELECT * FROM ' + QuotedStrD(FTableName);
FQuery.Open;
if AGetRecordCount then
GetRecordCount
else
FRecordCount := FQuery.RecordCount;
except
on E: Exception do
DoOnError(E);
end;
Result := FQuery.Active;
if Result then DoConvertNewTable;
end;
procedure TSQliteData.Close;
begin
if not FConn.Connected then Exit;
try
Save;
if FAutoVacuum then
Vacuum;
CloseTable;
FTrans.Active := False;
FConn.Close;
except
on E: Exception do
DoOnError(E);
end;
end;
procedure TSQliteData.CloseTable;
begin
if not FConn.Connected then Exit;
if not FQuery.Active then Exit;
try
FQuery.Close;
FRecordCount := 0;
except
on E: Exception do
DoOnError(E);
end;
end;
procedure TSQliteData.Refresh(RecheckDataCount: Boolean);
begin
if not FConn.Connected then Exit;
if FQuery.Active then
FQuery.Refresh
else
FQuery.Open;
if RecheckDataCount then
GetRecordCount
else
FRecordCount := FQuery.RecordCount;
end;
procedure TSQliteData.Commit;
begin
if not FConn.Connected then Exit;
try
Transaction.Commit;
except
Transaction.Rollback;
end;
end;
procedure TSQliteData.CommitRetaining;
begin
if not FConn.Connected then Exit;
try
Transaction.CommitRetaining;
except
Transaction.RollbackRetaining;
end;
end;
procedure TSQliteData.Save;
var
qactive: Boolean;
begin
if not FConn.Connected then Exit;
try
qactive := FQuery.Active;
if FQuery.Active then
begin
FQuery.ApplyUpdates;
FQuery.Close;
end;
FTrans.Commit;
if qactive <> qactive then
FQuery.Active := FQuery.Active;
if FQuery.Active then
GetRecordCount;
except
on E: Exception do
DoOnError(E);
end;
end;
function TSQliteData.Connected: Boolean;
begin
Result := FConn.Connected and FQuery.Active;
end;
end.