Fix some bugs in sql engine

This commit is contained in:
jaina heartles 2022-12-03 06:36:31 -08:00
parent a97850964e
commit e27d0064ee
3 changed files with 12 additions and 8 deletions

View file

@ -88,7 +88,7 @@ pub fn prepareParamText(arena: *std.heap.ArenaAllocator, val: anytype) !?[:0]con
else => |T| switch (@typeInfo(T)) {
.Enum => return @tagName(val),
.Optional => if (val) |v| try prepareParamText(arena, v) else null,
.Int => try std.fmt.allocPrintZ(arena.allocator(), "{}", .{val}),
.Bool, .Int => try std.fmt.allocPrintZ(arena.allocator(), "{}", .{val}),
.Union => loop: inline for (std.meta.fields(T)) |field| {
// Have to do this in a roundabout way to satisfy comptime checker
const Tag = std.meta.Tag(T);

View file

@ -193,6 +193,7 @@ pub const Db = struct {
.Null => return self.bindNull(stmt, idx),
.Int => return self.bindInt(stmt, idx, std.math.cast(i64, val) orelse unreachable),
.Float => return self.bindFloat(stmt, idx, val),
.Bool => return self.bindInt(stmt, idx, if (val) 1 else 0),
else => @compileError("Unable to serialize type " ++ @typeName(T)),
}
}
@ -251,18 +252,20 @@ pub const Results = struct {
db: *c.sqlite3,
pub fn finish(self: Results) void {
switch (c.sqlite3_finalize(self.stmt)) {
c.SQLITE_OK => {},
else => |err| {
handleUnexpectedError(self.db, err, self.getGeneratingSql()) catch {};
},
}
_ = c.sqlite3_finalize(self.stmt);
}
pub fn row(self: Results) common.RowError!?Row {
return switch (c.sqlite3_step(self.stmt)) {
c.SQLITE_ROW => Row{ .stmt = self.stmt, .db = self.db },
c.SQLITE_DONE => null,
c.SQLITE_CONSTRAINT_UNIQUE => return error.UniqueViolation,
c.SQLITE_CONSTRAINT_CHECK => return error.CheckViolation,
c.SQLITE_CONSTRAINT_NOTNULL => return error.NotNullViolation,
c.SQLITE_CONSTRAINT_FOREIGNKEY => return error.ForeignKeyViolation,
c.SQLITE_CONSTRAINT => return error.ConstraintViolation,
else => |err| handleUnexpectedError(self.db, err, self.getGeneratingSql()),
};
}

View file

@ -431,6 +431,7 @@ fn Tx(comptime tx_level: u8) type {
pub fn rollback(self: Self) void {
(if (tx_level < 2) self.rollbackTx() else self.rollbackSavepoint()) catch |err| {
std.log.err("Failed to rollback transaction: {}", .{err});
std.log.err("{any}", .{@errorReturnTrace()});
@panic("TODO: more gracefully handle rollback failures");
};
}
@ -628,7 +629,7 @@ fn Tx(comptime tx_level: u8) type {
}
fn rollbackUnchecked(self: Self) !void {
try self.exec("ROLLBACK", {}, null);
try self.execInternal("ROLLBACK", {}, null, false);
}
};
}