Support optional types in sql

This commit is contained in:
jaina heartles 2022-07-21 21:18:43 -07:00
parent cc44bbf12b
commit a4f6fca675
1 changed files with 15 additions and 3 deletions

View File

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