| ... | ... | @@ -25,8 +25,15 @@ pub fn close(self: *Self) void { |
| 25 | 25 | self.db.deinit(); |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | | var stmt = try self.db.prepare(query); |
| 28 | fn 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 | |
| 29 | 35 | pub 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 | 37 | defer stmt.deinit(); |
| 31 | 38 | var iter = try stmt.iteratorAlloc(T, alloc, args); |
| 32 | 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 | 44 | } |
| 38 | 45 | |
| 39 | 46 | pub 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 | 48 | defer stmt.deinit(); |
| 42 | 49 | try stmt.execAlloc(.{ .allocator = alloc }, args); |
| 43 | 50 | } |
| 44 | 51 | |
| 45 | 52 | pub 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 | 54 | defer stmt.deinit(); |
| 48 | 55 | return try stmt.oneAlloc(T, alloc, .{}, args); |
| 49 | 56 | } |