-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsdb.pas
162 lines (136 loc) · 2.85 KB
/
jsdb.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
unit jsdb;
interface
uses
windows, classes, System.SysUtils, Generics.Collections, SQLiteTable3;
function ThreadedQuery(filename, query: string): string;
function query(id: Cardinal; query: string): string;
function opendb(filename: string): integer;
function closedb(id: Cardinal): boolean;
procedure killDb;
function exec(id: Cardinal; query: string): boolean;
implementation
uses
entrypoint, XSuperObject,console;
var
dbList: TDictionary<integer, TSQLiteDatabase>;
ids: integer;
procedure killDb;
var
Item: TPair<integer, TSQLiteDatabase>;
begin
exit;
for Item in dbList do
begin
// Item.Value.Close;
// Item.Value.Free;
end;
dbList.Clear;
end;
function opendb(filename: string): integer;
var
c: TSQLiteDatabase;
i: integer;
begin
if not FileExists(filename) then
begin
debug('db not found: ' + filename);
result := 0;
exit;
end;
c := TSQLiteDatabase.Create(filename);
Inc(ids);
i := ids;
result := i;
dbList.Add(i, c);
end;
function exec(id: Cardinal; query: string): boolean;
var
c: TSQLiteDatabase;
begin
result := false;
if dbList.TryGetValue(id, c) then
begin
try
c.ExecSQL(query);
finally
result := True;
end;
end;
end;
function ThreadedQuery(filename, query: string): string;
var
f: integer;
c: TSQLiteDatabase;
q: TSQLiteUniTable;
X: ISuperObject;
A: ISuperArray;
begin
if not FileExists(filename) then
begin
debug('db not found: ' + filename);
result := '[]';
exit;
end;
c := TSQLiteDatabase.Create(filename);
q := TSQLiteUniTable.Create(c, query);
A := SA();
while not q.EOF do
begin
X := SO;
for f := 0 to q.ColCount - 1 do
begin
X.S[q.Columns[f]] := q.FieldAsBlobText(q.FieldIndex[q.Columns[f]]);
end;
A.Add(X);
q.Next;
end;
result := A.AsJSON();
q.Free;
c.Free;
end;
function query(id: Cardinal; query: string): string;
var
f: integer;
c: TSQLiteDatabase;
q: TSQLiteUniTable;
X: ISuperObject;
A: ISuperArray;
begin
if dbList.TryGetValue(id, c) then
begin
q := TSQLiteUniTable.Create(c, query);
A := SA();
while not q.EOF do
begin
X := SO;
for f := 0 to q.ColCount - 1 do
begin
X.S[q.Columns[f]] := q.FieldAsBlobText(q.FieldIndex[q.Columns[f]]);
end;
A.Add(X);
q.Next;
end;
result := A.AsJSON();
q.Free;
end;
end;
function closedb(id: Cardinal): boolean;
var
c: TSQLiteDatabase;
begin
exit;
if dbList.TryGetValue(id, c) then
begin
c.Free;
dbList.Remove(id);
result := True;
end
else
result := false;
end;
initialization
dbList := TDictionary<integer, TSQLiteDatabase>.Create;
finalization
killDb;
dbList.Free;
end.