| ... | ... | @@ -1,6 +1,7 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const string = []const u8; |
| 3 | 3 | const sqlite = @import("sqlite"); |
| 4 | const tracer = @import("tracer"); |
| 4 | 5 | |
| 5 | 6 | const Self = @This(); |
| 6 | 7 | |
| ... | ... | @@ -33,6 +34,9 @@ fn prepare(self: *Self, comptime query: string) !sqlite.StatementType(.{}, query |
| 33 | 34 | } |
| 34 | 35 | |
| 35 | 36 | pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]T { |
| 37 | const t = tracer.trace(@src(), " {s}", .{query}); |
| 38 | defer t.end(); |
| 39 | |
| 36 | 40 | var stmt = try self.prepare(query); |
| 37 | 41 | defer stmt.deinit(); |
| 38 | 42 | var iter = try stmt.iteratorAlloc(T, alloc, args); |
| ... | ... | @@ -45,12 +49,18 @@ pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime |
| 45 | 49 | } |
| 46 | 50 | |
| 47 | 51 | pub fn exec(self: *Self, alloc: std.mem.Allocator, comptime query: string, args: anytype) !void { |
| 52 | const t = tracer.trace(@src(), " {s}", .{query}); |
| 53 | defer t.end(); |
| 54 | |
| 48 | 55 | var stmt = try self.prepare(query); |
| 49 | 56 | defer stmt.deinit(); |
| 50 | 57 | try stmt.execAlloc(alloc, .{}, args); |
| 51 | 58 | } |
| 52 | 59 | |
| 53 | 60 | pub fn first(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T { |
| 61 | const t = tracer.trace(@src(), " {s}", .{query}); |
| 62 | defer t.end(); |
| 63 | |
| 54 | 64 | var stmt = try self.prepare(query); |
| 55 | 65 | defer stmt.deinit(); |
| 56 | 66 | return try stmt.oneAlloc(T, alloc, .{}, args); |
| ... | ... | @@ -61,6 +71,9 @@ pub fn prepareDynamic(self: *Self, query: string) !sqlite.DynamicStatement { |
| 61 | 71 | } |
| 62 | 72 | |
| 63 | 73 | pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool { |
| 74 | const t = tracer.trace(@src(), " {s}", .{name}); |
| 75 | defer t.end(); |
| 76 | |
| 64 | 77 | for (try self.collect(alloc, string, "select name from sqlite_master where type=? AND name=?", .{ .type = "table", .name = name })) |item| { |
| 65 | 78 | if (std.mem.eql(u8, item, name)) { |
| 66 | 79 | return true; |
| ... | ... | @@ -70,6 +83,9 @@ pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool |
| 70 | 83 | } |
| 71 | 84 | |
| 72 | 85 | pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table: string, comptime column: string) !bool { |
| 86 | const t = tracer.trace(@src(), " {s}.{s}", .{ table, column }); |
| 87 | defer t.end(); |
| 88 | |
| 73 | 89 | for (try pragma.table_info(self, alloc, table)) |item| { |
| 74 | 90 | if (std.mem.eql(u8, item.name, column)) { |
| 75 | 91 | return true; |
| ... | ... | @@ -91,6 +107,9 @@ pub const Pragma = struct { |
| 91 | 107 | |
| 92 | 108 | pub const pragma = struct { |
| 93 | 109 | pub fn table_info(self: *Self, alloc: std.mem.Allocator, comptime name: string) ![]const Pragma.TableInfo { |
| 110 | const t = tracer.trace(@src(), " {s}", .{name}); |
| 111 | defer t.end(); |
| 112 | |
| 94 | 113 | return try self.collect(alloc, Pragma.TableInfo, "pragma table_info(" ++ name ++ ")", .{}); |
| 95 | 114 | } |
| 96 | 115 | }; |