authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-03-05 21:33:03 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-03-05 21:33:03 -08:00
log97990e70fb31a34d3782213af160e9add005184a
tree2055752d38277f880364703e4714702cc6b33de8
parentc3bcc53de6ce937c74c927d9b146e226f0fc23b0
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

further separation from sqlite being the only driver


2 files changed, 86 insertions(+), 2 deletions(-)

src/lib.zig+78-2
...@@ -1,15 +1,91 @@...@@ -1,15 +1,91 @@
1const std = @import("std");1const std = @import("std");
22
3const EngineType = enum {3pub const DriverType = enum {
4 sqlite3,4 sqlite3,
5};5};
66
7pub fn engine(comptime etype: EngineType) type {7pub fn Driver(comptime etype: DriverType) type {
8 return switch (etype) {8 return switch (etype) {
9 .sqlite3 => @import("./sqlite3.zig"),9 .sqlite3 => @import("./sqlite3.zig"),
10 };10 };
11}11}
1212
13pub fn connect(_type: DriverType, allocator: std.mem.Allocator, connection: [:0]const u8) !Engine {
14 return switch (_type) {
15 .sqlite3 => .{ .sqlite3 = try .connect(allocator, connection) },
16 };
17}
18
19pub const Engine = union(DriverType) {
20 sqlite3: Driver(.sqlite3),
21
22 pub fn close(engine: *Engine) void {
23 return switch (engine.*) {
24 inline else => |*e| e.close(),
25 };
26 }
27
28 pub fn lock(engine: *Engine) void {
29 return switch (engine.*) {
30 inline else => |*e| e.lock(),
31 };
32 }
33
34 pub fn unlock(engine: *Engine) void {
35 return switch (engine.*) {
36 inline else => |*e| e.unlock(),
37 };
38 }
39
40 pub fn exec(engine: *Engine, alloc: std.mem.Allocator, comptime query: []const u8, args: anytype) !void {
41 return switch (engine.*) {
42 inline else => |*e| e.exec(alloc, query, args),
43 };
44 }
45
46 pub fn first(engine: *Engine, alloc: std.mem.Allocator, comptime T: type, comptime query: []const u8, args: anytype) !?T {
47 return switch (engine.*) {
48 inline else => |*e| e.first(alloc, T, query, args),
49 };
50 }
51
52 pub fn collect(engine: *Engine, alloc: std.mem.Allocator, comptime T: type, comptime query: []const u8, args: anytype) ![]T {
53 return switch (engine.*) {
54 inline else => |*e| e.collect(alloc, T, query, args),
55 };
56 }
57
58 pub fn collectDyn(engine: *Engine, alloc: std.mem.Allocator, comptime T: type, query: []const u8, args: anytype) ![]T {
59 return switch (engine.*) {
60 inline else => |*e| {
61 var stmt = try e.prepareDynamic(query);
62 defer stmt.deinit();
63 var iter = try stmt.iteratorAlloc(T, alloc, args);
64 var list = std.ArrayList(T).init(alloc);
65 errdefer list.deinit();
66 while (try iter.nextAlloc(alloc, .{})) |row| {
67 try list.append(row);
68 }
69 return list.toOwnedSlice();
70 },
71 };
72 }
73
74 //
75
76 pub fn doesTableExist(engine: *Engine, alloc: std.mem.Allocator, name: []const u8) !bool {
77 return switch (engine.*) {
78 inline else => |*e| e.doesTableExist(alloc, name),
79 };
80 }
81
82 pub fn hasColumnWithName(engine: *Engine, alloc: std.mem.Allocator, comptime table: []const u8, comptime column: []const u8) !bool {
83 return switch (engine.*) {
84 inline else => |*e| e.hasColumnWithName(alloc, table, column),
85 };
86 }
87};
88
13pub const backer = struct {89pub const backer = struct {
14 pub const sqlite = @import("sqlite");90 pub const sqlite = @import("sqlite");
15};91};
src/sqlite3.zig+8
...@@ -27,6 +27,14 @@ pub fn close(self: *Self) void {...@@ -27,6 +27,14 @@ pub fn close(self: *Self) void {
27 self.db.deinit();27 self.db.deinit();
28}28}
2929
30pub fn lock(self: *Self) void {
31 self.mutex.lock();
32}
33
34pub fn unlock(self: *Self) void {
35 self.mutex.unlock();
36}
37
30fn prepare(self: *Self, comptime query: string) !sqlite.StatementType(.{}, query) {38fn prepare(self: *Self, comptime query: string) !sqlite.StatementType(.{}, query) {
31 return self.db.prepare(query) catch |err| switch (err) {39 return self.db.prepare(query) catch |err| switch (err) {
32 error.SQLiteError => std.debug.panic("{s}", .{self.db.getDetailedError()}),40 error.SQLiteError => std.debug.panic("{s}", .{self.db.getDetailedError()}),