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)), + }, }; }