Add boolean support to sql package

This commit is contained in:
jaina heartles 2022-12-06 01:43:03 -08:00
parent 4103dd384a
commit 7ea3048027
4 changed files with 29 additions and 2 deletions

View file

@ -124,6 +124,7 @@ pub fn parseValueNotNull(alloc: ?Allocator, comptime T: type, str: []const u8) !
return error.ResultTypeMismatch;
},
.Optional => try parseValueNotNull(alloc, std.meta.Child(T), str),
.Bool => return util.serialize.bool_map.get(str) orelse return error.ResultTypeMismatch,
else => @compileError("Type " ++ @typeName(T) ++ " not supported"),
},

View file

@ -341,6 +341,7 @@ fn getColumnInt(stmt: *c.sqlite3_stmt, comptime T: type, idx: u15) common.GetErr
std.log.err("SQLite column {}: Expected value of type {}, got {} (outside of range)", .{ idx, T, val });
return error.ResultTypeMismatch;
},
.Bool => if (val == 0) return false else return true,
else => {
std.log.err("SQLite column {}: Storage class INT cannot be parsed into type {}", .{ idx, T });
return error.ResultTypeMismatch;

View file

@ -24,6 +24,8 @@ pub const QueryRowError = errors.QueryRowError;
pub const BeginError = errors.BeginError;
pub const CommitError = errors.CommitError;
pub const DatabaseError = QueryError || RowError || QueryRowError || BeginError || CommitError;
pub const QueryOptions = common.QueryOptions;
pub const Engine = enum {
@ -37,6 +39,7 @@ pub const Engine = enum {
pub const QueryBuilder = struct {
array: std.ArrayList(u8),
where_clauses_appended: usize = 0,
set_statements_appended: usize = 0,
pub fn init(alloc: std.mem.Allocator) QueryBuilder {
return QueryBuilder{ .array = std.ArrayList(u8).init(alloc) };
@ -58,7 +61,7 @@ pub const QueryBuilder = struct {
/// interspersed with calls to appendSlice
pub fn andWhere(self: *QueryBuilder, comptime clause: []const u8) !void {
if (self.where_clauses_appended == 0) {
try self.array.appendSlice("WHERE ");
try self.array.appendSlice("\nWHERE ");
} else {
try self.array.appendSlice(" AND ");
}
@ -67,6 +70,17 @@ pub const QueryBuilder = struct {
self.where_clauses_appended += 1;
}
pub fn set(self: *QueryBuilder, comptime col: []const u8, comptime val: []const u8) !void {
if (self.set_statements_appended == 0) {
try self.array.appendSlice("\nSET ");
} else {
try self.array.appendSlice(", ");
}
try self.array.appendSlice(col ++ " = " ++ val);
self.set_statements_appended += 1;
}
pub fn str(self: *const QueryBuilder) []const u8 {
return self.array.items;
}
@ -523,6 +537,17 @@ fn Tx(comptime tx_level: u8) type {
return row;
}
pub fn queryRows(
self: Self,
comptime RowType: type,
q: [:0]const u8,
args: anytype,
max_items: ?usize,
alloc: std.mem.Allocator,
) QueryRowError![]RowType {
return try self.queryRowsWithOptions(RowType, q, args, max_items, .{ .allocator = alloc });
}
// Runs a query to completion and returns the results as a slice
pub fn queryRowsWithOptions(
self: Self,

View file

@ -242,7 +242,7 @@ pub fn DeserializerContext(comptime Result: type, comptime From: type, comptime
};
}
const bool_map = std.ComptimeStringMap(bool, .{
pub const bool_map = std.ComptimeStringMap(bool, .{
.{ "true", true },
.{ "t", true },
.{ "yes", true },