From 7d7ec2329de8941dada0cf0593e7a37b4b1e00da Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Sun, 20 Dec 2020 23:36:10 +0100 Subject: allow bool bind parameters and reading bool values --- sqlite.zig | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'sqlite.zig') diff --git a/sqlite.zig b/sqlite.zig index 82d2313..f769237 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -224,6 +224,10 @@ pub fn Iterator(comptime Type: type) type { debug.assert(columns == 1); return try self.readFloat(options); }, + .Bool => { + debug.assert(columns == 1); + return try self.readBool(options); + }, .Void => { debug.assert(columns == 1); }, @@ -280,6 +284,11 @@ pub fn Iterator(comptime Type: type) type { return @floatCast(Type, d); } + fn readBool(self: *Self, options: anytype) !Type { + const d = c.sqlite3_column_int64(self.stmt, 0); + return d > 0; + } + const ReadBytesMode = enum { Blob, Text, @@ -488,6 +497,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t else => switch (field_type_info) { .Int, .ComptimeInt => _ = c.sqlite3_bind_int64(self.stmt, column, @intCast(c_longlong, field_value)), .Float, .ComptimeFloat => _ = c.sqlite3_bind_double(self.stmt, column, field_value), + .Bool => _ = c.sqlite3_bind_int64(self.stmt, column, @boolToInt(field_value)), .Array => |arr| { switch (arr.child) { u8 => { @@ -653,6 +663,7 @@ fn addTestData(db: *Db) !void { \\ id integer PRIMARY KEY, \\ author_id integer, \\ data text, + \\ is_published integer, \\ FOREIGN KEY(author_id) REFERENCES user(id) \\) }; @@ -938,6 +949,42 @@ test "sqlite: read a single value into void" { _ = try stmt.one(void, .{}, .{ .id = @as(usize, 20) }); } +test "sqlite: read a single value into bool" { + var db: Db = undefined; + try db.init(testing.allocator, .{ .mode = dbMode() }); + try addTestData(&db); + + const query = "SELECT id FROM user WHERE id = ?{usize}"; + + var stmt: Statement(.{}, ParsedQuery.from(query)) = try db.prepare(query); + defer stmt.deinit(); + + const b = try stmt.one(bool, .{}, .{ .id = @as(usize, 20) }); + testing.expect(b != null); + testing.expect(b.?); +} + +test "sqlite: insert bool and bind bool" { + var db: Db = undefined; + try db.init(testing.allocator, .{ .mode = dbMode() }); + try addTestData(&db); + + try db.exec("INSERT INTO article(id, author_id, is_published) VALUES(?{usize}, ?{usize}, ?{bool})", .{ + .id = @as(usize, 1), + .author_id = @as(usize, 20), + .is_published = true, + }); + + const query = "SELECT id FROM article WHERE is_published = ?{bool}"; + + var stmt: Statement(.{}, ParsedQuery.from(query)) = try db.prepare(query); + defer stmt.deinit(); + + const b = try stmt.one(bool, .{}, .{ .is_published = true }); + testing.expect(b != null); + testing.expect(b.?); +} + test "sqlite: statement reset" { var db: Db = undefined; try db.init(testing.allocator, .{ .mode = dbMode() }); -- cgit v1.2.3