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");...@@ -4,12 +4,12 @@ const tracer = @import("tracer");
4const extras = @import("extras");4const extras = @import("extras");
5const builtin = @import("builtin");5const builtin = @import("builtin");
66
7const Self = @This();7const Driver = @This();
88
9db: *c.sqlite3,9db: *c.sqlite3,
1010
11pub fn connect(allocator: std.mem.Allocator, path: [:0]const u8) !Self {11pub fn connect(allocator: std.mem.Allocator, path: [:0]const u8) !Driver {
12 var driver: Self = try .connect_only(allocator, path);12 var driver: Driver = try .connect_only(allocator, path);
13 errdefer driver.close();13 errdefer driver.close();
14 _ = try driver.first(allocator, void, "PRAGMA journal_mode = WAL", .{});14 _ = try driver.first(allocator, void, "PRAGMA journal_mode = WAL", .{});
15 _ = try driver.first(allocator, void, "PRAGMA busy_timeout = 5000", .{});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,7 +21,7 @@ pub fn connect(allocator: std.mem.Allocator, path: [:0]const u8) !Self {
21 _ = try driver.first(allocator, void, "PRAGMA auto_vacuum = INCREMENTAL", .{});21 _ = try driver.first(allocator, void, "PRAGMA auto_vacuum = INCREMENTAL", .{});
22 return driver;22 return driver;
23}23}
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 {
25 std.log.scoped(.zorm).info("connecting to {s} @ {s}", .{ "sqlite3", path });25 std.log.scoped(.zorm).info("connecting to {s} @ {s}", .{ "sqlite3", path });
26 _ = allocator;26 _ = allocator;
27 var db: ?*c.sqlite3 = null;27 var db: ?*c.sqlite3 = null;
...@@ -34,11 +34,11 @@ pub fn connect_only(allocator: std.mem.Allocator, path: [:0]const u8) !Self {...@@ -34,11 +34,11 @@ pub fn connect_only(allocator: std.mem.Allocator, path: [:0]const u8) !Self {
34 return .{ .db = db.? };34 return .{ .db = db.? };
35}35}
3636
37pub fn close(self: *Self) void {37pub fn close(self: *Driver) void {
38 s.assert(c.sqlite3_close_v2(self.db));38 s.assert(c.sqlite3_close_v2(self.db));
39}39}
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 {
42 const t = tracer.trace(@src(), " {s}", .{query});42 const t = tracer.trace(@src(), " {s}", .{query});
43 defer t.end();43 defer t.end();
4444
...@@ -53,7 +53,7 @@ pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime...@@ -53,7 +53,7 @@ pub fn collect(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime
53 return list.toOwnedSlice();53 return list.toOwnedSlice();
54}54}
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 {
57 const t = tracer.trace(@src(), " {s}", .{query});57 const t = tracer.trace(@src(), " {s}", .{query});
58 defer t.end();58 defer t.end();
5959
...@@ -63,7 +63,7 @@ pub fn exec(self: *Self, alloc: std.mem.Allocator, comptime query: string, args:...@@ -63,7 +63,7 @@ pub fn exec(self: *Self, alloc: std.mem.Allocator, comptime query: string, args:
63 try stmt.exec(alloc);63 try stmt.exec(alloc);
64}64}
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 {
67 const t = tracer.trace(@src(), " {s}", .{query});67 const t = tracer.trace(@src(), " {s}", .{query});
68 defer t.end();68 defer t.end();
6969
...@@ -75,7 +75,7 @@ pub fn first(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime q...@@ -75,7 +75,7 @@ pub fn first(self: *Self, alloc: std.mem.Allocator, comptime T: type, comptime q
75 return iter.step(alloc, T);75 return iter.step(alloc, T);
76}76}
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 {
79 const t = tracer.trace(@src(), " {s}", .{name});79 const t = tracer.trace(@src(), " {s}", .{name});
80 defer t.end();80 defer t.end();
8181
...@@ -87,7 +87,7 @@ pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool...@@ -87,7 +87,7 @@ pub fn doesTableExist(self: *Self, alloc: std.mem.Allocator, name: string) !bool
87 return false;87 return false;
88}88}
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 {
91 const t = tracer.trace(@src(), " {s}.{s}", .{ table, column });91 const t = tracer.trace(@src(), " {s}.{s}", .{ table, column });
92 defer t.end();92 defer t.end();
9393
...@@ -99,19 +99,19 @@ pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table:...@@ -99,19 +99,19 @@ pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table:
99 return false;99 return false;
100}100}
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 {
103 const t = tracer.trace(@src(), " {s} ({s})", .{ name, pk_name });103 const t = tracer.trace(@src(), " {s} ({s})", .{ name, pk_name });
104 defer t.end();104 defer t.end();
105 try self.exec(alloc, comptime std.fmt.comptimePrint("create table {s}({s} {s} primary key not null)", .{ name, pk_name, nameForType2(pk_type) }), .{});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}
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 {
109 const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name });109 const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name });
110 defer t.end();110 defer t.end();
111 try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s}", .{ table_name, col_name, nameForType(T) }), .{});111 try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s}", .{ table_name, col_name, nameForType(T) }), .{});
112}112}
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 {
115 const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name });115 const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name });
116 defer t.end();116 defer t.end();
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 }), .{});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,7 +184,7 @@ pub const Pragma = struct {
184};184};
185185
186pub const pragma = struct {186pub 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 const t = tracer.trace(@src(), " {s}", .{name});188 const t = tracer.trace(@src(), " {s}", .{name});
189 defer t.end();189 defer t.end();
190190
...@@ -288,7 +288,7 @@ pub const Statement = struct {...@@ -288,7 +288,7 @@ pub const Statement = struct {
288 db: *c.sqlite3,288 db: *c.sqlite3,
289 stmt: *c.sqlite3_stmt,289 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 {
292 var stmt: ?*c.sqlite3_stmt = null;292 var stmt: ?*c.sqlite3_stmt = null;
293 var flags: c_uint = 0;293 var flags: c_uint = 0;
294 _ = &flags;294 _ = &flags;