-
Notifications
You must be signed in to change notification settings - Fork 39
/
sqlite3.d
387 lines (344 loc) · 8.32 KB
/
sqlite3.d
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
/**
* Higher-level wrapper over etc.c.sqlite3
*
* License:
* This Source Code Form is subject to the terms of
* the Mozilla Public License, v. 2.0. If a copy of
* the MPL was not distributed with this file, You
* can obtain one at http://mozilla.org/MPL/2.0/.
*
* Authors:
* Vladimir Panteleev <ae@cy.md>
*/
module ae.sys.sqlite3;
pragma(lib, "sqlite3");
import etc.c.sqlite3;
import std.exception;
import std.string : toStringz;
import std.conv : to;
import std.traits;
import std.typecons : Nullable;
/// `sqlite3*` wrapper.
final class SQLite
{
/// C `sqlite3*` object.
sqlite3* db;
this(string fn, bool readOnly = false)
{
sqenforce(sqlite3_open_v2(toStringz(fn), &db, readOnly ? SQLITE_OPEN_READONLY : SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, null));
} ///
~this() /*@nogc*/
{
sqlite3_close(db);
}
/// Run a simple query, provided as an SQL string.
auto query(string sql)
{
struct Iterator
{
alias int delegate(ref const(char)[][] args, ref const(char)[][] columns) F;
string sql;
sqlite3* db;
F dg;
int fres;
Throwable throwable = null;
int opApply(F dg)
{
this.dg = dg;
auto res = sqlite3_exec(db, toStringz(sql), &callback, &this, null);
if (res == SQLITE_ABORT)
{
if (throwable)
throw throwable;
else
return fres;
}
else
if (res != SQLITE_OK)
throw new SQLiteException(db, res);
return 0;
}
static /*nothrow*/ extern(C) int callback(void* ctx, int argc, char** argv, char** colv)
{
auto i = cast(Iterator*)ctx;
static const(char)[][] args, cols;
args.length = cols.length = argc;
foreach (n; 0..argc)
args[n] = to!(const(char)[])(argv[n]),
cols[n] = to!(const(char)[])(colv[n]);
try
return i.fres = i.dg(args, cols);
catch (Exception e)
{
i.throwable = e;
return 1;
}
}
}
return Iterator(sql, db);
}
/// Run a simple query, discarding the result.
void exec(string sql)
{
foreach (cells, columns; query(sql))
break;
}
/// Return the ID of the last inserted row.
/// (`sqlite3_last_insert_rowid`)
@property long lastInsertRowID()
{
return sqlite3_last_insert_rowid(db);
}
/// Return the number of changed rows.
/// (`sqlite3_changes`)
@property int changes()
{
return sqlite3_changes(db);
}
/// `sqlite3_stmt*` wrapper.
final class PreparedStatement
{
private sqlite3_stmt* stmt;
/// `sqlite3_bind_XXX` wrapper.
void bind(int idx, int v)
{
sqlite3_bind_int(stmt, idx, v);
}
void bind(int idx, long v)
{
sqlite3_bind_int64(stmt, idx, v);
} /// ditto
void bind(int idx, double v)
{
sqlite3_bind_double(stmt, idx, v);
} /// ditto
void bind(int idx, in char[] v)
{
sqlite3_bind_text(stmt, idx, v.ptr, to!int(v.length), SQLITE_TRANSIENT);
} /// ditto
void bind(int idx, in wchar[] v)
{
sqlite3_bind_text16(stmt, idx, v.ptr, to!int(v.length*2), SQLITE_TRANSIENT);
} /// ditto
void bind(int idx, void* n)
{
assert(n is null);
sqlite3_bind_null(stmt, idx);
} /// ditto
void bind(int idx, const(void)[] v)
{
sqlite3_bind_blob(stmt, idx, v.ptr, to!int(v.length), SQLITE_TRANSIENT);
} /// ditto
void bind(T)(int idx, Nullable!T v)
if (is(typeof(bind(idx, v.get()))))
{
if (v.isNull)
sqlite3_bind_null(stmt, idx);
else
bind(v.get());
} /// ditto
/// Bind all arguments according to their type, in order.
void bindAll(T...)(T args)
{
foreach (int n, arg; args)
bind(n+1, arg);
}
/// Return "true" if a row is available, "false" if done.
bool step()
{
auto res = sqlite3_step(stmt);
if (res == SQLITE_DONE)
{
reset();
return false;
}
else
if (res == SQLITE_ROW)
return true;
else
{
scope(exit) sqlite3_reset(stmt);
sqenforce(res);
return false; // only on SQLITE_OK, which shouldn't happen
}
}
/// Calls `sqlite3_reset`.
void reset()
{
sqenforce(sqlite3_reset(stmt));
}
/// Binds the given arguments and executes the prepared statement, discarding the result.
void exec(T...)(T args)
{
static if (T.length)
bindAll!T(args);
while (step()) {}
}
/// Binds the given arguments and executes the prepared statement, returning the results as an iterator.
static struct Iterator
{
PreparedStatement stmt; ///
@trusted int opApply(U...)(int delegate(ref U args) @system dg)
{
int res = 0;
while (stmt.step())
{
scope(failure) stmt.reset();
static if (U.length == 1 && is(U[0] V : V[]) &&
!is(U[0] : string) && !is(Unqual!V == void) && !is(V == ubyte))
{
U[0] result;
result.length = stmt.columnCount();
foreach (int c, ref r; result)
r = stmt.column!V(c);
res = dg(result);
}
else
static if (U.length == 1 && is(U[0] V : V[string]))
{
U[0] result;
foreach (c; 0..stmt.columnCount())
result[stmt.columnName(c)] = stmt.column!V(c);
res = dg(result);
}
else
{
U columns;
stmt.columns(columns);
res = dg(columns);
}
if (res)
{
stmt.reset();
break;
}
}
return res;
} ///
}
/// ditto
Iterator iterate(T...)(T args)
{
static if (T.length)
bindAll!T(args);
return Iterator(this);
}
/// Returns the value of a column by its index, as the given D type.
T column(T)(int idx)
{
static if (is(T == Nullable!U, U))
return sqlite3_column_type(stmt, idx) == SQLITE_NULL
? T.init
: T(column!U(idx));
else
static if (is(T == string))
return (cast(char*)sqlite3_column_blob(stmt, idx))[0..sqlite3_column_bytes(stmt, idx)].idup;
else
static if (is(T V : V[]) && (is(Unqual!V == void) || is(V == ubyte)))
{
auto arr = (cast(V*)sqlite3_column_blob(stmt, idx))[0..sqlite3_column_bytes(stmt, idx)];
static if (isStaticArray!T)
{
enforce(arr.length == T.length, "Wrong size for static array column");
return arr[0..T.length];
}
else
return arr.dup;
}
else
static if (is(T == int))
return sqlite3_column_int(stmt, idx);
else
static if (is(T == long))
return sqlite3_column_int64(stmt, idx);
else
static if (is(T == bool))
return sqlite3_column_int(stmt, idx) != 0;
else
static if (is(T == double))
return sqlite3_column_double(stmt, idx);
else
static assert(0, "Can't get column with type " ~ T.stringof);
}
debug(ae_unittest) unittest
{
PreparedStatement s;
if (false)
{
s.column!(void[])(0);
s.column!(ubyte[])(0);
s.column!(void[16])(0);
s.column!(ubyte[16])(0);
s.column!(Nullable!(ubyte[16]))(0);
}
}
/// Returns the value of all columns, as the given D types.
void columns(T...)(ref T args)
{
foreach (i, arg; args)
args[i] = column!(typeof(arg))(i);
}
/// Returns the value of all columns, as an array of the given D type (`string` by default).
T[] getArray(T=string)()
{
T[] result = new T[dataCount()];
foreach (i, ref value; result)
value = column!T(i);
return result;
}
/// Returns the value of all columns as an associative array,
/// with the column names as the key,
/// and the values with given D type (`string` by default).
T[string] getAssoc(T=string)()
{
T[string] result;
foreach (i; 0..dataCount())
result[columnName(i)] = column!T(i);
return result;
}
/// `sqlite3_column_count` wrapper.
int columnCount()
{
return sqlite3_column_count(stmt);
}
/// `sqlite3_data_count` wrapper.
int dataCount()
{
return sqlite3_data_count(stmt);
}
/// Returns the column name by its index, as a D string.
string columnName(int idx)
{
return to!string(sqlite3_column_name(stmt, idx));
}
~this() /*@nogc*/
{
sqlite3_finalize(stmt);
}
}
/// Construct a prepared statement.
PreparedStatement prepare(string sql)
{
auto s = new PreparedStatement;
const(char)* tail;
auto sqlz = toStringz(sql);
sqenforce(sqlite3_prepare_v2(db, sqlz, -1, &s.stmt, &tail));
assert(tail == sqlz + sql.length, "Trailing SQL not compiled: " ~ sql[tail - sqlz .. $]);
return s;
}
private void sqenforce(int res)
{
if (res != SQLITE_OK)
throw new SQLiteException(db, res);
}
}
/// Exception class thrown on SQLite errors.
class SQLiteException : Exception
{
int code; ///
this(sqlite3* db, int code)
{
this.code = code;
super(to!string(sqlite3_errmsg(db)) ~ " (" ~ to!string(code) ~ ")");
} ///
}