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
|
const std = @import("std");
const build_options = @import("build_options");
const debug = std.debug;
const mem = std.mem;
const testing = std.testing;
const c = @cImport({
@cInclude("sqlite3.h");
});
const logger = std.log.scoped(.sqlite);
/// Db is a wrapper around a SQLite database, providing high-level functions to executing queries.
/// A Db can be opened with a file database or a in-memory database:
///
/// // File database
/// var db: sqlite.Db = undefined;
/// try db.init(allocator, .{ .File = "/tmp/data.db" });
///
/// // In memory database
/// var db: sqlite.Db = undefined;
/// try db.init(allocator, .{ .Memory={} });
///
pub const Db = struct {
const Self = @This();
allocator: *mem.Allocator,
db: *c.sqlite3,
/// Mode determines how the database will be opened.
pub const Mode = union(enum) {
File: []const u8,
Memory,
};
/// init creates a database with the provided `mode`.
pub fn init(self: *Self, allocator: *mem.Allocator, mode: Mode) !void {
self.allocator = allocator;
switch (mode) {
.File => |path| {
logger.info("opening {}", .{path});
// Need a null-terminated string here.
const pathZ = try allocator.dupeZ(u8, path);
defer allocator.free(pathZ);
var db: ?*c.sqlite3 = undefined;
const result = c.sqlite3_open_v2(
pathZ,
&db,
c.SQLITE_OPEN_READWRITE | c.SQLITE_OPEN_CREATE,
null,
);
if (result != c.SQLITE_OK or db == null) {
logger.warn("unable to open database, result: {}", .{result});
return error.CannotOpenDatabase;
}
self.db = db.?;
},
.Memory => {
logger.info("opening in memory", .{});
var db: ?*c.sqlite3 = undefined;
const result = c.sqlite3_open_v2(
":memory:",
&db,
c.SQLITE_OPEN_READWRITE | c.SQLITE_OPEN_MEMORY,
null,
);
if (result != c.SQLITE_OK or db == null) {
logger.warn("unable to open database, result: {}", .{result});
return error.CannotOpenDatabase;
}
self.db = db.?;
},
}
}
/// deinit closes the database.
pub fn deinit(self: *Self) void {
_ = c.sqlite3_close(self.db);
}
/// exec is a convenience function which prepares a statement and executes it directly.
pub fn exec(self: *Self, comptime query: []const u8, values: anytype) !void {
var stmt = try self.prepare(query, values);
defer stmt.deinit();
try stmt.exec();
}
/// prepare prepares a statement for the `query` provided.
///
/// The query is analysed at comptime to search for bind markers.
/// prepare enforces having as much fields in the `values` tuple as there are bind markers.
///
/// Example usage:
///
/// var stmt = try db.prepare("INSERT INTO foo(id, name) VALUES(?, ?)", .{
/// .id = 3540,
/// .name = "Eminem",
/// });
/// defer stmt.deinit();
///
/// Note that the name of the fields in the tuple are irrelevant, only the types are.
pub fn prepare(self: *Self, comptime query: []const u8, values: anytype) !Statement {
return Statement.prepare(self, 0, query, values);
}
/// rowsAffected returns the number of rows affected by the last statement executed.
pub fn rowsAffected(self: *Self) usize {
return @intCast(usize, c.sqlite3_changes(self.db));
}
};
/// Statement is a wrapper around a SQLite statement, providing high-level functions to execute
/// a statement and retrieve rows for SELECT queries.
///
/// The exec function can be used to execute a query which does not return rows:
///
/// var stmt = try db.prepare("UPDATE foo SET id = ? WHERE name = ?", .{
/// .id = 200,
/// .name = "José",
/// });
/// defer stmt.deinit();
///
/// The one function can be used to select a single row:
///
/// var stmt = try db.prepare("SELECT name FROM foo WHERE id = ?", .{ .id = 200 });
/// defer stmt.deinit();
///
/// const Row = struct { id: usize };
/// const row = try stmt.one(Row .{});
///
/// The all function can be used to select all rows:
///
/// var stmt = try db.prepare("SELECT name FROM foo", .{});
/// defer stmt.deinit();
///
/// const Row = struct { id: usize };
/// const rows = try stmt.all(Row .{});
///
/// Look at aach function for more complete documentation.
///
pub const Statement = struct {
const Self = @This();
stmt: *c.sqlite3_stmt,
fn prepare(db: *Db, flags: c_uint, comptime query: []const u8, values: anytype) !Self {
const StructType = @typeInfo(@TypeOf(values)).Struct;
comptime {
const bind_parameter_count = std.mem.count(u8, query, "?");
if (bind_parameter_count != StructType.fields.len) {
@compileError("bind parameter count != number of fields in tuple/struct");
}
}
// prepare
var stmt = blk: {
var tmp: ?*c.sqlite3_stmt = undefined;
const result = c.sqlite3_prepare_v3(
db.db,
query.ptr,
@intCast(c_int, query.len),
flags,
&tmp,
null,
);
if (result != c.SQLITE_OK) {
logger.warn("unable to prepare statement, result: {}", .{result});
return error.CannotPrepareStatement;
}
break :blk tmp.?;
};
// Bind
inline for (StructType.fields) |struct_field, _i| {
const i = @as(usize, _i);
const field_type_info = @typeInfo(struct_field.field_type);
const field_value = @field(values, struct_field.name);
const column = i + 1;
switch (struct_field.field_type) {
[]const u8, []u8 => {
_ = c.sqlite3_bind_text(stmt, column, field_value.ptr, @intCast(c_int, field_value.len), null);
},
else => switch (field_type_info) {
.Int, .ComptimeInt => _ = c.sqlite3_bind_int64(stmt, column, @intCast(c_longlong, field_value)),
.Float, .ComptimeFloat => _ = c.sqlite3_bind_double(stmt, column, field_value),
.Array => |arr| {
switch (arr.child) {
u8 => {
const data: []const u8 = field_value[0..field_value.len];
_ = c.sqlite3_bind_text(stmt, column, data.ptr, @intCast(c_int, data.len), null);
},
else => @compileError("cannot populate field " ++ field.name ++ " of type array of " ++ @typeName(arr.child)),
}
},
else => @compileError("cannot bind field " ++ struct_field.name ++ " of type " ++ @typeName(struct_field.field_type)),
},
}
}
return Self{
.stmt = stmt,
};
}
pub fn deinit(self: *Self) void {
const result = c.sqlite3_finalize(self.stmt);
if (result != c.SQLITE_OK) {
logger.err("unable to finalize prepared statement, result: {}", .{result});
}
}
pub fn exec(self: *Self) !void {
const result = c.sqlite3_step(self.stmt);
switch (result) {
c.SQLITE_DONE => {},
c.SQLITE_BUSY => return error.SQLiteBusy,
else => std.debug.panic("invalid result {}", .{result}),
}
}
/// one reads a single row from the result set of this statement.
///
/// The data in the row is used to populate a value of the type `Type`.
/// This means that `Type` must have as many fields as is returned in the query
/// executed by this statement.
/// This also means that the type of each field must be compatible with the SQLite type.
///
/// Here is an example of how to use an anonymous struct type:
///
/// const row = try stmt.one(
/// struct {
/// id: usize,
/// name: []const u8,
/// age: usize,
/// },
/// .{ .allocator = allocator },
/// );
///
/// The `options` tuple is used to provide additional state in some cases, for example
/// an allocator used to read text and blobs.
///
pub fn one(self: *Self, comptime Type: type, options: anytype) !?Type {
const TypeInfo = @typeInfo(Type);
var result = c.sqlite3_step(self.stmt);
switch (TypeInfo) {
.Int => return switch (result) {
c.SQLITE_ROW => try self.readInt(Type, options),
c.SQLITE_DONE => null,
else => std.debug.panic("invalid result {}", .{result}),
},
.Struct => return switch (result) {
c.SQLITE_ROW => try self.readStruct(Type, options),
c.SQLITE_DONE => null,
else => std.debug.panic("invalid result {}", .{result}),
},
else => @compileError("cannot read into type " ++ @typeName(Type)),
}
}
/// all reads all rows from the result set of this statement.
///
/// The data in each row is used to populate a value of the type `Type`.
/// This means that `Type` must have as many fields as is returned in the query
/// executed by this statement.
/// This also means that the type of each field must be compatible with the SQLite type.
///
/// Here is an example of how to use an anonymous struct type:
///
/// const rows = try stmt.all(
/// struct {
/// id: usize,
/// name: []const u8,
/// age: usize,
/// },
/// .{ .allocator = allocator },
/// );
///
/// The `options` tuple is used to provide additional state in some cases.
/// Note that for this function the allocator is mandatory.
///
pub fn all(self: *Self, comptime Type: type, options: anytype) ![]Type {
const TypeInfo = @typeInfo(Type);
var rows = std.ArrayList(Type).init(options.allocator);
var result = c.sqlite3_step(self.stmt);
while (result == c.SQLITE_ROW) : (result = c.sqlite3_step(self.stmt)) {
const columns = c.sqlite3_column_count(self.stmt);
var value = switch (TypeInfo) {
.Int => blk: {
debug.assert(columns == 1);
break :blk try self.readInt(Type, options);
},
.Struct => blk: {
std.debug.assert(columns == @typeInfo(Type).Struct.fields.len);
break :blk try self.readStruct(Type, options);
},
else => @compileError("cannot read into type " ++ @typeName(Type)),
};
try rows.append(value);
}
if (result != c.SQLITE_DONE) {
logger.err("unable to iterate, result: {}", .{result});
return error.SQLiteStepError;
}
return rows.span();
}
fn readInt(self: *Self, comptime Type: type, options: anytype) !Type {
const n = c.sqlite3_column_int64(self.stmt, 0);
return @intCast(Type, n);
}
fn readStruct(self: *Self, comptime Type: type, options: anytype) !Type {
var value: Type = undefined;
inline for (@typeInfo(Type).Struct.fields) |field, _i| {
const i = @as(usize, _i);
const field_type_info = @typeInfo(field.field_type);
switch (field.field_type) {
[]const u8, []u8 => {
const data = c.sqlite3_column_blob(self.stmt, i);
if (data == null) {
@field(value, field.name) = "";
} else {
const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i));
var tmp = try options.allocator.alloc(u8, size);
mem.copy(u8, tmp, @ptrCast([*c]const u8, data)[0..size]);
@field(value, field.name) = tmp;
}
},
else => switch (field_type_info) {
.Int => {
const n = c.sqlite3_column_int64(self.stmt, i);
@field(value, field.name) = @intCast(field.field_type, n);
},
.Float => {
const f = c.sqlite3_column_double(self.stmt, i);
@field(value, field.name) = f;
},
.Void => {
@field(value, field.name) = {};
},
.Array => |arr| {
switch (arr.child) {
u8 => {
const data = c.sqlite3_column_blob(self.stmt, i);
const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i));
if (size > @as(usize, arr.len)) return error.ArrayTooSmall;
mem.copy(u8, @field(value, field.name)[0..], @ptrCast([*c]const u8, data)[0..size]);
},
else => @compileError("cannot populate field " ++ field.name ++ " of type array of " ++ @typeName(arr.child)),
}
},
else => @compileError("cannot populate field " ++ field.name ++ " of type " ++ @typeName(field.field_type)),
},
}
}
return value;
}
};
test "sqlite: statement exec" {
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
var allocator = &arena.allocator;
var db: Db = undefined;
try db.init(testing.allocator, dbMode());
// Create the tables
comptime const all_ddl = &[_][]const u8{
\\CREATE TABLE user(
\\ id integer PRIMARY KEY,
\\ name text,
\\ age integer
\\)
,
\\CREATE TABLE article(
\\ id integer PRIMARY KEY,
\\ author_id integer,
\\ data text,
\\ FOREIGN KEY(author_id) REFERENCES user(id)
\\)
};
inline for (all_ddl) |ddl| {
var stmt = try db.prepare(ddl, .{});
defer stmt.deinit();
try stmt.exec();
}
// Add data
const User = struct {
id: usize,
name: []const u8,
age: usize,
};
const users = &[_]User{
.{ .id = 20, .name = "Vincent", .age = 33 },
.{ .id = 40, .name = "Julien", .age = 35 },
.{ .id = 60, .name = "José", .age = 40 },
};
for (users) |user| {
try db.exec("INSERT INTO user(id, name, age) VALUES(?, ?, ?)", user);
const rows_inserted = db.rowsAffected();
testing.expectEqual(@as(usize, 1), rows_inserted);
}
// Read a single user
{
var stmt = try db.prepare("SELECT id, name, age FROM user WHERE id = ?", .{ .id = 20 });
defer stmt.deinit();
var rows = try stmt.all(User, .{ .allocator = allocator });
for (rows) |row| {
testing.expectEqual(users[0].id, row.id);
testing.expectEqualStrings(users[0].name, row.name);
testing.expectEqual(users[0].age, row.age);
}
}
// Read all users
{
var stmt = try db.prepare("SELECT id, name, age FROM user", .{});
defer stmt.deinit();
var rows = try stmt.all(User, .{ .allocator = allocator });
testing.expectEqual(@as(usize, 3), rows.len);
for (rows) |row, i| {
const exp = users[i];
testing.expectEqual(exp.id, row.id);
testing.expectEqualStrings(exp.name, row.name);
testing.expectEqual(exp.age, row.age);
}
}
// Test with anonymous structs
{
var stmt = try db.prepare("SELECT id, name, age FROM user WHERE id = ?", .{ .id = 20 });
defer stmt.deinit();
var row = try stmt.one(
struct {
id: usize,
name: []const u8,
age: usize,
},
.{ .allocator = allocator },
);
testing.expect(row != null);
const exp = users[0];
testing.expectEqual(exp.id, row.?.id);
testing.expectEqualStrings(exp.name, row.?.name);
testing.expectEqual(exp.age, row.?.age);
}
// Test with a single integer
{
var stmt = try db.prepare("SELECT age FROM user WHERE id = ?", .{ .id = 20 });
defer stmt.deinit();
var age = try stmt.one(usize, .{});
testing.expect(age != null);
testing.expectEqual(@as(usize, 33), age.?);
}
}
pub fn dbMode() Db.Mode {
return if (build_options.is_ci) blk: {
break :blk .{ .Memory = {} };
} else blk: {
const path = "/tmp/zig-sqlite.db";
std.fs.cwd().deleteFile(path) catch {};
break :blk .{ .File = path };
};
}
|