authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-08-17 23:08:20-07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-08-17 23:08:20-07:00
logea2700127eb26ee9cef2bc5dac091494ef2fcfe5
tree8405b64cecbc75036c6ed49996518d60f748a8d9
parent8c99d8f6b67eb7c16d6bd7bf8f95153798b3240e
signature Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

sqlite3: Self -> Driver


1 files changed, 15 insertions(+), 15 deletions(-)

src/sqlite3.zig+15-15
......@@ -4,12 +4,12 @@ const tracer = @import("tracer");
44const extras = @import("extras");
55const builtin = @import("builtin");
66
7const Self = @This();
7const Driver = @This();
88
99db: *c.sqlite3,
1010
11pub fn connect(allocator: std.mem.Allocator, path: [:0]const u8) !Self {
12 var driver: Self = try .connect_only(allocator, path);
11pub fn connect(allocator: std.mem.Allocator, path: [:0]const u8) !Driver {
12 var driver: Driver = try .connect_only(allocator, path);
1313 errdefer driver.close();
1414 _ = try driver.first(allocator, void, "PRAGMA journal_mode = WAL", .{});
1515 _ = 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 {
2121 _ = try driver.first(allocator, void, "PRAGMA auto_vacuum = INCREMENTAL", .{});
2222 return driver;
2323}
24pub fn connect_only(allocator: std.mem.Allocator, path: [:0]const u8) !Self {
24pub fn connect_only(allocator: std.mem.Allocator, path: [:0]const u8) !Driver {
2525 std.log.scoped(.zorm).info("connecting to {s} @ {s}", .{ "sqlite3", path });
2626 _ = allocator;
2727 var db: ?*c.sqlite3 = null;
......@@ -34,11 +34,11 @@ pub fn connect_only(allocator: std.mem.Allocator, path: [:0]const u8) !Self {
3434 return .{ .db = db.? };
3535}
3636
37pub fn close(self: *Self) void {
37pub fn close(self: *Driver) void {
3838 s.assert(c.sqlite3_close_v2(self.db));
3939}
4040
41pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]T {
41pub fn collect(self: *Driver, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) ![]T {
4242 const t = tracer.trace(@src(), " {s}", .{query});
4343 defer t.end();
4444
......@@ -53,7 +53,7 @@ pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime
5353 return list.toOwnedSlice();
5454}
5555
56pub fn exec(self: *Self, alloc: std.mem.Allocator, comptime query: string, args: anytype) !void {
56pub fn exec(self: *Driver, alloc: std.mem.Allocator, comptime query: string, args: anytype) !void {
5757 const t = tracer.trace(@src(), " {s}", .{query});
5858 defer t.end();
5959
......@@ -63,7 +63,7 @@ pub fn exec(self: *Self, alloc: std.mem.Allocator, comptime query: string, args:
6363 try stmt.exec(alloc);
6464}
6565
66pub fn first(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T {
66pub fn first(self: *Driver, alloc: std.mem.Allocator, comptime T: type, comptime query: string, args: anytype) !?T {
6767 const t = tracer.trace(@src(), " {s}", .{query});
6868 defer t.end();
6969
......@@ -75,7 +75,7 @@ pub fn first(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime q
7575 return iter.step(alloc, T);
7676}
7777
78pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool {
78pub fn doesTableExist(self: *Driver, alloc: std.mem.Allocator, name: string) !bool {
7979 const t = tracer.trace(@src(), " {s}", .{name});
8080 defer t.end();
8181
......@@ -87,7 +87,7 @@ pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool
8787 return false;
8888}
8989
90pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table: string, comptime column: string) !bool {
90pub fn hasColumnWithName(self: *Driver, alloc: std.mem.Allocator, comptime table: string, comptime column: string) !bool {
9191 const t = tracer.trace(@src(), " {s}.{s}", .{ table, column });
9292 defer t.end();
9393
......@@ -99,19 +99,19 @@ pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table:
9999 return false;
100100}
101101
102pub fn createTable(self: *Self, alloc: std.mem.Allocator, comptime name: []const u8, comptime pk_name: []const u8, pk_type: type) !void {
102pub fn createTable(self: *Driver, alloc: std.mem.Allocator, comptime name: []const u8, comptime pk_name: []const u8, pk_type: type) !void {
103103 const t = tracer.trace(@src(), " {s} ({s})", .{ name, pk_name });
104104 defer t.end();
105105 try self.exec(alloc, comptime std.fmt.comptimePrint("create table {s}({s} {s} primary key not null)", .{ name, pk_name, nameForType2(pk_type) }), .{});
106106}
107107
108pub fn addColumn(self: *Self, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void {
108pub fn addColumn(self: *Driver, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void {
109109 const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name });
110110 defer t.end();
111111 try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s}", .{ table_name, col_name, nameForType(T) }), .{});
112112}
113113
114pub 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 {
114pub 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 {
115115 const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name });
116116 defer t.end();
117117 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 {
184184};
185185
186186pub 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 {
188188 const t = tracer.trace(@src(), " {s}", .{name});
189189 defer t.end();
190190
......@@ -288,7 +288,7 @@ pub const Statement = struct {
288288 db: *c.sqlite3,
289289 stmt: *c.sqlite3_stmt,
290290
291 pub fn prepare(driver: *Self, query: []const u8) !Statement {
291 pub fn prepare(driver: *Driver, query: []const u8) !Statement {
292292 var stmt: ?*c.sqlite3_stmt = null;
293293 var flags: c_uint = 0;
294294 _ = &flags;