authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-21 20:32:31 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-21 20:32:31 -07:00
log47c7f105c1752823b3d62d78283c713d729cacba
treedb4c90977130167fdc19338ab194756a9b6ce2f2
parent3f56910018b4b6b067ff5ce2891fd858c3bc0ebf

sqlite3- add prepare function that catches sqlite errors


1 files changed, 10 insertions(+), 3 deletions(-)

src/sqlite3.zig+10-3
......@@ -25,8 +25,15 @@ pub fn close(self: *Self) void {
2525 self.db.deinit();
2626}
2727
28 var stmt = try self.db.prepare(query);
28fn prepare(self: *Self, comptime query: string) !sqlite.StatementType(.{}, query) {
29 return self.db.prepare(query) catch |err| switch (err) {
30 error.SQLiteError => std.debug.panic("{s}", .{self.db.getDetailedError()}),
31 else => return err,
32 };
33}
34
2935pub fn collect(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]const T {
36 var stmt = try self.prepare(query);
3037 defer stmt.deinit();
3138 var iter = try stmt.iteratorAlloc(T, alloc, args);
3239 var list = std.ArrayList(T).init(alloc);
......@@ -37,13 +44,13 @@ pub fn collect(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptim
3744}
3845
3946pub fn exec(self: *Self, alloc: *std.mem.Allocator, comptime query: string, args: anytype) !void {
40 var stmt = try self.db.prepare(query);
47 var stmt = try self.prepare(query);
4148 defer stmt.deinit();
4249 try stmt.execAlloc(.{ .allocator = alloc }, args);
4350}
4451
4552pub fn first(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T {
46 var stmt = try self.db.prepare(query);
53 var stmt = try self.prepare(query);
4754 defer stmt.deinit();
4855 return try stmt.oneAlloc(T, alloc, .{}, args);
4956}