aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: ebb9fde4ba42b9c663a7ff9ee77052c122482832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const std = @import("std");
const log = @import("log.zig").scoped_log_t(.db);

/// Database abstraction layer
pub const Db = @This();

/// Type erased pointer to actual implementation, and reference to implementation functions
/// Inspired by std.mem.Allocator
ptr: *anyopaque,
vtable: VTable,

fn init(ptr: *anyopaque, vtable: VTable) Db {
    return Db{
        .ptr = ptr,
        .vtable = vtable,
    };
}

pub const OpenError = error{ Failed, NotThreadSafe } || std.mem.Allocator.Error;
pub const PrepareError = error{Failed} || std.mem.Allocator.Error;
pub const BindError = error{};
pub const StepError = error{Failed} || std.mem.Allocator.Error;
pub const ColumnError = error{WrongType} || std.fmt.ParseIntError;

// Dispatcher for concrete implementations. Inspired by std.mem.Allocator.
const VTable = struct {
    prepare: *const fn (*anyopaque, query: [:0]const u8) PrepareError!*anyopaque,
    // bind: *const fn (db: *anyopaque, stmt: *anyopaque, idx: usize, val: anytype) BindError!void,
    step: *const fn (db: *anyopaque, stmt: *anyopaque) StepError!bool,
    // TODO support more types
    // TODO think of something clever to avoid this function proliferation.
    // Values returned should live at least until next call to .step()
    column_i64: *const fn (db: *anyopaque, stmt: *anyopaque, idx: u31) ColumnError!?i64,
    column_slice_const_u8: *const fn (db: *anyopaque, stmt: *anyopaque, idx: u31) ColumnError!?[:0]const u8,
    close_stmt: *const fn (db: *anyopaque, stmt: *anyopaque) void,
    close_db: *const fn (db: *anyopaque) void,
};

pub const Stmt = struct {
    db: *Db,
    ptr: *anyopaque,

    fn bind(self: *Stmt, idx: u31, val: anytype) !void {
        try self.db.vtable.bind(self.db.ptr, self.ptr, idx, val);
    }
    fn bind_named(self: *Stmt, name: [:0]const u8, val: anytype) !void {
        try self.db.vtable.bind_named(self.db.ptr, self.ptr, name, val);
    }
    /// Advance the result set to the next row.
    pub fn step(self: *Stmt) !bool {
        return try self.db.vtable.step(self.db.ptr, self.ptr);
    }
    /// Read a column
    pub fn column(self: *Stmt, comptime T: type, index: u31) !?T {
        switch (@typeInfo(T)) {
            .Int => |intinfo| {
                if (intinfo.signedness != .signed) @compileError("integer type i64 only is supported in Stmt#column");
                if (intinfo.bits != 64) @compileError("integer type i64 only is supported in Stmt#column");
                return try self.db.vtable.column_i64(self.db.ptr, self.ptr, index);
            },
            .Pointer => |ptrinfo| {
                if (ptrinfo.size != .Slice) @compileError("pointer type []const u8 only is supported in Stmt#column");
                if (ptrinfo.child != u8) @compileError("pointer type []const u8 only is supported in Stmt#column");
                return try self.db.vtable.column_slice_const_u8(self.db.ptr, self.ptr, index);
            },
            else => @compileError("unhandled type " ++ @tagName(@typeInfo(T)) ++ " in Stmt#column"),
        }
    }
    pub fn close(self: *Stmt) void {
        self.db.vtable.close_stmt(self.db.ptr, self.ptr);
    }
};

/// Interface for running queries
/// params should be a tuple with values for placeholders.
pub fn query(self: *Db, comptime qry: [:0]const u8, params: anytype) !Stmt {
    const ti = @typeInfo(@TypeOf(params));
    if (ti != .Struct) @compileError("Db.query params must be a tuple struct but it's a " ++ @tagName(ti));
    const si = ti.Struct;
    if (!si.is_tuple) @compileError("Db.query params must be a tuple");

    const stmt_ptr = try self.vtable.prepare(self.ptr, qry);
    var stmt = Stmt{ .db = self, .ptr = stmt_ptr };
    inline for (0..si.fields.len) |idx| {
        try stmt.bind(idx, params[idx]);
    }
    return stmt;
}

/// Shortcut for query/step/close
pub fn exec(self: *Db, comptime qry: [:0]const u8) !void {
    var stmt = try self.query(qry, .{});
    defer stmt.close();
    _ = try stmt.step();
}

pub fn close(self: *Db) void {
    self.vtable.close_db(self.ptr);
}

test {
    _ = @import("test.zig");
}