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 {...@@ -25,8 +25,15 @@ pub fn close(self: *Self) void {
25 self.db.deinit();25 self.db.deinit();
26}26}
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
29pub fn collect(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]const T {35pub 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);
30 defer stmt.deinit();37 defer stmt.deinit();
31 var iter = try stmt.iteratorAlloc(T, alloc, args);38 var iter = try stmt.iteratorAlloc(T, alloc, args);
32 var list = std.ArrayList(T).init(alloc);39 var list = std.ArrayList(T).init(alloc);
...@@ -37,13 +44,13 @@ pub fn collect(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptim...@@ -37,13 +44,13 @@ pub fn collect(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptim
37}44}
3845
39pub fn exec(self: *Self, alloc: *std.mem.Allocator, comptime query: string, args: anytype) !void {46pub 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);
41 defer stmt.deinit();48 defer stmt.deinit();
42 try stmt.execAlloc(.{ .allocator = alloc }, args);49 try stmt.execAlloc(.{ .allocator = alloc }, args);
43}50}
4451
45pub fn first(self: *Self, alloc: *std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T {52pub 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);
47 defer stmt.deinit();54 defer stmt.deinit();
48 return try stmt.oneAlloc(T, alloc, .{}, args);55 return try stmt.oneAlloc(T, alloc, .{}, args);
49}56}