| ... | ... | @@ -1,15 +1,91 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | |
| 3 | | const EngineType = enum { |
| 3 | pub const DriverType = enum { |
| 4 | 4 | sqlite3, |
| 5 | 5 | }; |
| 6 | 6 | |
| 7 | | pub fn engine(comptime etype: EngineType) type { |
| 7 | pub fn Driver(comptime etype: DriverType) type { |
| 8 | 8 | return switch (etype) { |
| 9 | 9 | .sqlite3 => @import("./sqlite3.zig"), |
| 10 | 10 | }; |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | pub 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 | |
| 19 | pub 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 | |
| 13 | 89 | pub const backer = struct { |
| 14 | 90 | pub const sqlite = @import("sqlite"); |
| 15 | 91 | }; |