From 47c7f105c1752823b3d62d78283c713d729cacba Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 21 Oct 2021 20:32:31 -0700 Subject: [PATCH] sqlite3- add prepare function that catches sqlite errors --- src/sqlite3.zig | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/sqlite3.zig b/src/sqlite3.zig index fdfc22bc56672de5f1e8fef9e371844b40b5efa8..2f04155e1825a3f96a3a7fccb650f66105c3edb2 100644 --- a/src/sqlite3.zig +++ b/src/sqlite3.zig @@ -25,8 +25,15 @@ pub fn close(self: *Self) void { self.db.deinit(); } - var stmt = try self.db.prepare(query); +fn prepare(self: *Self, comptime query: string) !sqlite.StatementType(.{}, query) { + return self.db.prepare(query) catch |err| switch (err) { + error.SQLiteError => std.debug.panic("{s}", .{self.db.getDetailedError()}), + else => return err, + }; +} + pub fn collect(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]const T { + var stmt = try self.prepare(query); defer stmt.deinit(); var iter = try stmt.iteratorAlloc(T, alloc, args); var list = std.ArrayList(T).init(alloc); @@ -37,13 +44,13 @@ pub fn collect(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptim } pub fn exec(self: *Self, alloc: *std.mem.Allocator, comptime query: string, args: anytype) !void { - var stmt = try self.db.prepare(query); + var stmt = try self.prepare(query); defer stmt.deinit(); try stmt.execAlloc(.{ .allocator = alloc }, args); } pub fn first(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T { - var stmt = try self.db.prepare(query); + var stmt = try self.prepare(query); defer stmt.deinit(); return try stmt.oneAlloc(T, alloc, .{}, args); } -- 2.54.0