From a4f6fca6759887abdec81195112eba3ecf3a299e Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Thu, 21 Jul 2022 21:18:43 -0700 Subject: [PATCH] Support optional types in sql --- src/sql/lib.zig | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/sql/lib.zig b/src/sql/lib.zig index ba62de4..ad509fd 100644 --- a/src/sql/lib.zig +++ b/src/sql/lib.zig @@ -80,7 +80,16 @@ pub const Row = struct { i64 => self.getI64(idx), Uuid => self.getUuid(idx), DateTime => self.getDateTime(idx), - else => @compileError("unknown type " ++ @typeName(T)), + + else => { + switch (@typeInfo(T)) { + .Optional => switch (c.sqlite3_column_type(self.stmt, idx)) { + c.SQLITE_NULL => return null, + else => return try self.getAlloc(std.meta.Child(T), idx, alloc), + }, + else => @compileError("unknown type " ++ @typeName(T)), + } + }, }; } }; @@ -91,7 +100,7 @@ pub const PreparedStmt = struct { pub fn bindNull(self: *PreparedStmt, idx: u15) !void { return switch (c.sqlite3_bind_null(self.stmt, idx)) { - .SQLITE_OK => {}, + c.SQLITE_OK => {}, else => error.UnknownError, }; } @@ -126,7 +135,10 @@ pub const PreparedStmt = struct { Uuid => self.bindUuid(idx, val), DateTime => self.bindDateTime(idx, val), @TypeOf(null) => self.bindNull(idx), - else => @compileError("Unknown Type"), + else => |T| switch (@typeInfo(T)) { + .Optional => if (val) |v| self.bind(idx, v) else self.bindNull(idx), + else => @compileError("Unknown Type" ++ @typeName(T)), + }, }; }