Fix some bugs in sql engine
This commit is contained in:
parent
a97850964e
commit
e27d0064ee
3 changed files with 12 additions and 8 deletions
|
@ -88,7 +88,7 @@ pub fn prepareParamText(arena: *std.heap.ArenaAllocator, val: anytype) !?[:0]con
|
||||||
else => |T| switch (@typeInfo(T)) {
|
else => |T| switch (@typeInfo(T)) {
|
||||||
.Enum => return @tagName(val),
|
.Enum => return @tagName(val),
|
||||||
.Optional => if (val) |v| try prepareParamText(arena, v) else null,
|
.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| {
|
.Union => loop: inline for (std.meta.fields(T)) |field| {
|
||||||
// Have to do this in a roundabout way to satisfy comptime checker
|
// Have to do this in a roundabout way to satisfy comptime checker
|
||||||
const Tag = std.meta.Tag(T);
|
const Tag = std.meta.Tag(T);
|
||||||
|
|
|
@ -193,6 +193,7 @@ pub const Db = struct {
|
||||||
.Null => return self.bindNull(stmt, idx),
|
.Null => return self.bindNull(stmt, idx),
|
||||||
.Int => return self.bindInt(stmt, idx, std.math.cast(i64, val) orelse unreachable),
|
.Int => return self.bindInt(stmt, idx, std.math.cast(i64, val) orelse unreachable),
|
||||||
.Float => return self.bindFloat(stmt, idx, val),
|
.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)),
|
else => @compileError("Unable to serialize type " ++ @typeName(T)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -251,18 +252,20 @@ pub const Results = struct {
|
||||||
db: *c.sqlite3,
|
db: *c.sqlite3,
|
||||||
|
|
||||||
pub fn finish(self: Results) void {
|
pub fn finish(self: Results) void {
|
||||||
switch (c.sqlite3_finalize(self.stmt)) {
|
_ = c.sqlite3_finalize(self.stmt);
|
||||||
c.SQLITE_OK => {},
|
|
||||||
else => |err| {
|
|
||||||
handleUnexpectedError(self.db, err, self.getGeneratingSql()) catch {};
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn row(self: Results) common.RowError!?Row {
|
pub fn row(self: Results) common.RowError!?Row {
|
||||||
return switch (c.sqlite3_step(self.stmt)) {
|
return switch (c.sqlite3_step(self.stmt)) {
|
||||||
c.SQLITE_ROW => Row{ .stmt = self.stmt, .db = self.db },
|
c.SQLITE_ROW => Row{ .stmt = self.stmt, .db = self.db },
|
||||||
c.SQLITE_DONE => null,
|
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()),
|
else => |err| handleUnexpectedError(self.db, err, self.getGeneratingSql()),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -431,6 +431,7 @@ fn Tx(comptime tx_level: u8) type {
|
||||||
pub fn rollback(self: Self) void {
|
pub fn rollback(self: Self) void {
|
||||||
(if (tx_level < 2) self.rollbackTx() else self.rollbackSavepoint()) catch |err| {
|
(if (tx_level < 2) self.rollbackTx() else self.rollbackSavepoint()) catch |err| {
|
||||||
std.log.err("Failed to rollback transaction: {}", .{err});
|
std.log.err("Failed to rollback transaction: {}", .{err});
|
||||||
|
std.log.err("{any}", .{@errorReturnTrace()});
|
||||||
@panic("TODO: more gracefully handle rollback failures");
|
@panic("TODO: more gracefully handle rollback failures");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -628,7 +629,7 @@ fn Tx(comptime tx_level: u8) type {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rollbackUnchecked(self: Self) !void {
|
fn rollbackUnchecked(self: Self) !void {
|
||||||
try self.exec("ROLLBACK", {}, null);
|
try self.execInternal("ROLLBACK", {}, null, false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue