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