-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.pas
More file actions
91 lines (79 loc) · 1.79 KB
/
Database.pas
File metadata and controls
91 lines (79 loc) · 1.79 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
unit Database;
interface
uses
// FireDAC
FireDAC.Comp.Client,
FireDAC.DApt,
FireDAC.FMXUI.Wait,
FireDAC.Phys.SQLite,
FireDAC.Stan.Async,
FireDAC.Stan.Def;
type
TDatabase = class
private
FConnection: TFDConnection;
class function FileName: string;
public
constructor Create;
destructor Destroy; override;
function Connection: TFDConnection;
function Execute(const SQL: string): LongInt;
function TableExists(const TableName: string): Boolean;
end;
implementation
uses
// Delphi
System.IOUtils,
System.SysUtils;
constructor TDatabase.Create;
begin
inherited Create;
FConnection := nil;
end;
destructor TDatabase.Destroy;
begin
if Assigned(FConnection) then
try
if FConnection.Connected then FConnection.Close;
finally
FConnection.Free;
end;
inherited Destroy;
end;
class function TDatabase.FileName: string;
begin
Result := TPath.Combine(TPath.GetDocumentsPath, 'Diabether.sqlite');
end;
function TDatabase.Connection: TFDConnection;
begin
if not Assigned(FConnection) then
begin
FConnection := TFDConnection.Create(nil);
FConnection.Params.Values['DriverID'] := 'SQLite';
FConnection.Params.Values['Database'] := FileName;
end;
Result := FConnection;
end;
function TDatabase.Execute(const SQL: string): LongInt;
begin
Result := Connection.ExecSQL(SQL);
end;
function TDatabase.TableExists(const TableName: string): Boolean;
begin
Result := False;
const Q = TFDQuery.Create(nil);
try
Q.Connection := Connection;
Q.Open(Format('SELECT name FROM sqlite_master WHERE type=''table'' AND name=''%s''', [TableName]));
if Q.Active then
begin
Q.First;
const F = Q.FindField('name');
if Assigned(F) then
Result := F.AsString <> '';
end;
finally
Q.Free;
end;
end;
end.