| ... | ... | @@ -4,12 +4,12 @@ const tracer = @import("tracer"); |
| 4 | 4 | const extras = @import("extras"); |
| 5 | 5 | const builtin = @import("builtin"); |
| 6 | 6 | |
| 7 | | const Self = @This(); |
| 7 | const Driver = @This(); |
| 8 | 8 | |
| 9 | 9 | db: *c.sqlite3, |
| 10 | 10 | |
| 11 | | pub fn connect(allocator: std.mem.Allocator, path: [:0]const u8) !Self { |
| 12 | | var driver: Self = try .connect_only(allocator, path); |
| 11 | pub fn connect(allocator: std.mem.Allocator, path: [:0]const u8) !Driver { |
| 12 | var driver: Driver = try .connect_only(allocator, path); |
| 13 | 13 | errdefer driver.close(); |
| 14 | 14 | _ = try driver.first(allocator, void, "PRAGMA journal_mode = WAL", .{}); |
| 15 | 15 | _ = try driver.first(allocator, void, "PRAGMA busy_timeout = 5000", .{}); |
| ... | ... | @@ -21,7 +21,7 @@ pub fn connect(allocator: std.mem.Allocator, path: [:0]const u8) !Self { |
| 21 | 21 | _ = try driver.first(allocator, void, "PRAGMA auto_vacuum = INCREMENTAL", .{}); |
| 22 | 22 | return driver; |
| 23 | 23 | } |
| 24 | | pub fn connect_only(allocator: std.mem.Allocator, path: [:0]const u8) !Self { |
| 24 | pub fn connect_only(allocator: std.mem.Allocator, path: [:0]const u8) !Driver { |
| 25 | 25 | std.log.scoped(.zorm).info("connecting to {s} @ {s}", .{ "sqlite3", path }); |
| 26 | 26 | _ = allocator; |
| 27 | 27 | var db: ?*c.sqlite3 = null; |
| ... | ... | @@ -34,11 +34,11 @@ pub fn connect_only(allocator: std.mem.Allocator, path: [:0]const u8) !Self { |
| 34 | 34 | return .{ .db = db.? }; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | | pub fn close(self: *Self) void { |
| 37 | pub fn close(self: *Driver) void { |
| 38 | 38 | s.assert(c.sqlite3_close_v2(self.db)); |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | | pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]T { |
| 41 | pub fn collect(self: *Driver, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]T { |
| 42 | 42 | const t = tracer.trace(@src(), " {s}", .{query}); |
| 43 | 43 | defer t.end(); |
| 44 | 44 | |
| ... | ... | @@ -53,7 +53,7 @@ pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime |
| 53 | 53 | return list.toOwnedSlice(); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | | pub fn exec(self: *Self, alloc: std.mem.Allocator, comptime query: string, args: anytype) !void { |
| 56 | pub fn exec(self: *Driver, alloc: std.mem.Allocator, comptime query: string, args: anytype) !void { |
| 57 | 57 | const t = tracer.trace(@src(), " {s}", .{query}); |
| 58 | 58 | defer t.end(); |
| 59 | 59 | |
| ... | ... | @@ -63,7 +63,7 @@ pub fn exec(self: *Self, alloc: std.mem.Allocator, comptime query: string, args: |
| 63 | 63 | try stmt.exec(alloc); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | | pub fn first(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T { |
| 66 | pub fn first(self: *Driver, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T { |
| 67 | 67 | const t = tracer.trace(@src(), " {s}", .{query}); |
| 68 | 68 | defer t.end(); |
| 69 | 69 | |
| ... | ... | @@ -75,7 +75,7 @@ pub fn first(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime q |
| 75 | 75 | return iter.step(alloc, T); |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | | pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool { |
| 78 | pub fn doesTableExist(self: *Driver, alloc: std.mem.Allocator, name: string) !bool { |
| 79 | 79 | const t = tracer.trace(@src(), " {s}", .{name}); |
| 80 | 80 | defer t.end(); |
| 81 | 81 | |
| ... | ... | @@ -87,7 +87,7 @@ pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool |
| 87 | 87 | return false; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | | pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table: string, comptime column: string) !bool { |
| 90 | pub fn hasColumnWithName(self: *Driver, alloc: std.mem.Allocator, comptime table: string, comptime column: string) !bool { |
| 91 | 91 | const t = tracer.trace(@src(), " {s}.{s}", .{ table, column }); |
| 92 | 92 | defer t.end(); |
| 93 | 93 | |
| ... | ... | @@ -99,19 +99,19 @@ pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table: |
| 99 | 99 | return false; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | | pub fn createTable(self: *Self, alloc: std.mem.Allocator, comptime name: []const u8, comptime pk_name: []const u8, pk_type: type) !void { |
| 102 | pub fn createTable(self: *Driver, alloc: std.mem.Allocator, comptime name: []const u8, comptime pk_name: []const u8, pk_type: type) !void { |
| 103 | 103 | const t = tracer.trace(@src(), " {s} ({s})", .{ name, pk_name }); |
| 104 | 104 | defer t.end(); |
| 105 | 105 | try self.exec(alloc, comptime std.fmt.comptimePrint("create table {s}({s} {s} primary key not null)", .{ name, pk_name, nameForType2(pk_type) }), .{}); |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | | pub fn addColumn(self: *Self, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void { |
| 108 | pub fn addColumn(self: *Driver, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void { |
| 109 | 109 | const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name }); |
| 110 | 110 | defer t.end(); |
| 111 | 111 | try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s}", .{ table_name, col_name, nameForType(T) }), .{}); |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | | pub fn addColumnForeign(self: *Self, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type, comptime table_name2: []const u8, comptime col_name2: []const u8) !void { |
| 114 | pub fn addColumnForeign(self: *Driver, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type, comptime table_name2: []const u8, comptime col_name2: []const u8) !void { |
| 115 | 115 | const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name }); |
| 116 | 116 | defer t.end(); |
| 117 | 117 | try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s} references \"{s}\" (\"{s}\")", .{ table_name, col_name, nameForType(T), table_name2, col_name2 }), .{}); |
| ... | ... | @@ -184,7 +184,7 @@ pub const Pragma = struct { |
| 184 | 184 | }; |
| 185 | 185 | |
| 186 | 186 | pub const pragma = struct { |
| 187 | | pub fn table_info(self: *Self, alloc: std.mem.Allocator, comptime name: string) ![]const Pragma.TableInfo { |
| 187 | pub fn table_info(self: *Driver, alloc: std.mem.Allocator, comptime name: string) ![]const Pragma.TableInfo { |
| 188 | 188 | const t = tracer.trace(@src(), " {s}", .{name}); |
| 189 | 189 | defer t.end(); |
| 190 | 190 | |
| ... | ... | @@ -288,7 +288,7 @@ pub const Statement = struct { |
| 288 | 288 | db: *c.sqlite3, |
| 289 | 289 | stmt: *c.sqlite3_stmt, |
| 290 | 290 | |
| 291 | | pub fn prepare(driver: *Self, query: []const u8) !Statement { |
| 291 | pub fn prepare(driver: *Driver, query: []const u8) !Statement { |
| 292 | 292 | var stmt: ?*c.sqlite3_stmt = null; |
| 293 | 293 | var flags: c_uint = 0; |
| 294 | 294 | _ = &flags; |