2022-07-15 00:58:08 +00:00
|
|
|
const std = @import("std");
|
2022-07-18 07:37:10 +00:00
|
|
|
const util = @import("util");
|
2022-07-15 00:58:08 +00:00
|
|
|
const c = @cImport({
|
|
|
|
@cInclude("sqlite3.h");
|
|
|
|
});
|
|
|
|
|
2022-07-18 07:37:10 +00:00
|
|
|
const Uuid = util.Uuid;
|
|
|
|
const DateTime = util.DateTime;
|
2022-07-16 19:00:33 +00:00
|
|
|
|
2022-07-24 04:14:46 +00:00
|
|
|
pub fn ByteArray(comptime n: usize) type {
|
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
data: [n]u8,
|
|
|
|
|
|
|
|
pub fn bindToSqlite(self: Self, stmt: *PreparedStmt, idx: u15) !void {
|
|
|
|
return stmt.bindBlob(idx, &self.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getFromSqlite(row: *Row, idx: u15, _: std.mem.Alloc) !Self {
|
|
|
|
var self: Self = undefined;
|
|
|
|
_ = try row.getBlob(idx, &self.data);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const ByteSlice = struct {
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
data: []const u8,
|
|
|
|
|
|
|
|
pub fn bindToSqlite(self: Self, stmt: *PreparedStmt, idx: u15) !void {
|
|
|
|
return stmt.bindBlob(idx, self.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getFromSqlite(row: *Row, idx: u15, alloc: std.mem.Alloc) !void {
|
|
|
|
return Self{
|
|
|
|
.data = try row.getBlobAlloc(idx, alloc),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-15 00:58:08 +00:00
|
|
|
pub const Sqlite = struct {
|
|
|
|
db: *c.sqlite3,
|
|
|
|
|
|
|
|
pub fn open(path: [:0]const u8) !Sqlite {
|
|
|
|
var db: ?*c.sqlite3 = undefined;
|
|
|
|
const err = c.sqlite3_open_v2(path, &db, c.SQLITE_OPEN_READWRITE | c.SQLITE_OPEN_CREATE, null);
|
|
|
|
if (err != c.SQLITE_OK) return error.UnknownError;
|
|
|
|
|
|
|
|
return Sqlite{
|
|
|
|
.db = db.?,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(self: *Sqlite) void {
|
2022-07-15 07:27:27 +00:00
|
|
|
_ = c.sqlite3_close_v2(self.db);
|
2022-07-15 00:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn prepare(self: *Sqlite, sql: []const u8) !PreparedStmt {
|
2022-07-15 07:27:27 +00:00
|
|
|
var stmt: ?*c.sqlite3_stmt = undefined;
|
|
|
|
const err = c.sqlite3_prepare_v2(self.db, sql.ptr, @intCast(c_int, sql.len), &stmt, null);
|
2022-07-16 18:41:09 +00:00
|
|
|
if (err != c.SQLITE_OK) {
|
2022-07-22 04:18:20 +00:00
|
|
|
std.log.debug("sql error {}: {s}", .{ err, c.sqlite3_errmsg(self.db) });
|
|
|
|
std.log.debug("Failed on SQL:\n==========\n{s}\n==========", .{sql});
|
2022-07-16 18:41:09 +00:00
|
|
|
return error.UnknownError;
|
|
|
|
}
|
2022-07-15 00:58:08 +00:00
|
|
|
|
2022-07-15 07:27:27 +00:00
|
|
|
return PreparedStmt{ .stmt = stmt.?, .db = self.db };
|
2022-07-15 00:58:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Row = struct {
|
|
|
|
stmt: *c.sqlite3_stmt,
|
2022-07-15 07:27:27 +00:00
|
|
|
db: *c.sqlite3,
|
2022-07-15 00:58:08 +00:00
|
|
|
|
2022-07-15 07:27:27 +00:00
|
|
|
pub fn getI64(self: Row, idx: u15) !i64 {
|
2022-07-15 00:58:08 +00:00
|
|
|
return @intCast(i64, c.sqlite3_column_int64(self.stmt, idx));
|
|
|
|
}
|
|
|
|
|
2022-07-15 07:27:27 +00:00
|
|
|
pub fn getText(self: Row, idx: u15, buf: []u8) ![]u8 {
|
2022-07-15 00:58:08 +00:00
|
|
|
const ptr = c.sqlite3_column_text(self.stmt, idx);
|
|
|
|
|
2022-07-15 07:27:27 +00:00
|
|
|
const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, idx));
|
2022-07-15 00:58:08 +00:00
|
|
|
if (size > buf.len) return error.StreamTooLong;
|
|
|
|
for (ptr[0..size]) |ch, i| buf[i] = ch;
|
|
|
|
|
|
|
|
return buf[0..size];
|
|
|
|
}
|
|
|
|
|
2022-07-15 07:27:27 +00:00
|
|
|
pub fn getTextAlloc(self: Row, idx: u15, alloc: std.mem.Allocator) ![]u8 {
|
2022-07-15 00:58:08 +00:00
|
|
|
const size = c.sqlite3_column_bytes(self.stmt, idx);
|
2022-07-15 07:27:27 +00:00
|
|
|
var buf = try alloc.alloc(u8, @intCast(usize, size));
|
2022-07-15 00:58:08 +00:00
|
|
|
errdefer alloc.free(buf);
|
|
|
|
|
|
|
|
return self.getText(idx, buf);
|
|
|
|
}
|
2022-07-17 23:21:03 +00:00
|
|
|
|
2022-07-24 04:14:46 +00:00
|
|
|
pub fn getBlob(self: Row, idx: u15, buf: []u8) ![]u8 {
|
|
|
|
const ptr = c.sqlite3_column_blob(self.stmt, idx);
|
|
|
|
|
|
|
|
const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, idx));
|
|
|
|
if (size > buf.len) return error.StreamTooLong;
|
|
|
|
for (ptr[0..size]) |ch, i| buf[i] = ch;
|
|
|
|
|
|
|
|
return buf[0..size];
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getBlobAlloc(self: Row, idx: u15, alloc: std.mem.Allocator) ![]u8 {
|
|
|
|
const size = c.sqlite3_column_bytes(self.stmt, idx);
|
|
|
|
var buf = try alloc.alloc(u8, @intCast(usize, size));
|
|
|
|
errdefer alloc.free(buf);
|
|
|
|
|
|
|
|
return self.getBlob(idx, buf);
|
|
|
|
}
|
|
|
|
|
2022-07-17 23:21:03 +00:00
|
|
|
pub fn getUuid(self: Row, idx: u15) !Uuid {
|
|
|
|
var buf: [Uuid.string_len + 1]u8 = undefined;
|
|
|
|
_ = try self.getText(idx, &buf);
|
|
|
|
return try Uuid.parse(buf[0..Uuid.string_len]);
|
|
|
|
}
|
2022-07-18 07:37:10 +00:00
|
|
|
|
|
|
|
pub fn getDateTime(self: Row, idx: u15) !DateTime {
|
|
|
|
return DateTime{ .seconds_since_epoch = try self.getI64(idx) };
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn getAlloc(self: Row, comptime T: type, idx: u15, alloc: std.mem.Allocator) !T {
|
|
|
|
// TODO: handle optionals
|
|
|
|
return switch (T) {
|
|
|
|
[]u8, []const u8 => self.getTextAlloc(idx, alloc),
|
|
|
|
i64 => self.getI64(idx),
|
|
|
|
Uuid => self.getUuid(idx),
|
|
|
|
DateTime => self.getDateTime(idx),
|
2022-07-22 04:18:43 +00:00
|
|
|
|
|
|
|
else => {
|
|
|
|
switch (@typeInfo(T)) {
|
|
|
|
.Optional => switch (c.sqlite3_column_type(self.stmt, idx)) {
|
|
|
|
c.SQLITE_NULL => return null,
|
|
|
|
else => return try self.getAlloc(std.meta.Child(T), idx, alloc),
|
|
|
|
},
|
2022-07-24 04:14:46 +00:00
|
|
|
.Struct, .Union, .Enum, .Opaque => if (@hasDecl(T, "getFromSqlite")) T.getFromSqlite(self, idx, alloc),
|
2022-07-22 04:18:43 +00:00
|
|
|
else => @compileError("unknown type " ++ @typeName(T)),
|
|
|
|
}
|
|
|
|
},
|
2022-07-18 07:37:10 +00:00
|
|
|
};
|
|
|
|
}
|
2022-07-15 00:58:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub const PreparedStmt = struct {
|
|
|
|
stmt: *c.sqlite3_stmt,
|
2022-07-15 07:27:27 +00:00
|
|
|
db: *c.sqlite3,
|
2022-07-15 00:58:08 +00:00
|
|
|
|
|
|
|
pub fn bindNull(self: *PreparedStmt, idx: u15) !void {
|
|
|
|
return switch (c.sqlite3_bind_null(self.stmt, idx)) {
|
2022-07-22 04:18:43 +00:00
|
|
|
c.SQLITE_OK => {},
|
2022-07-15 00:58:08 +00:00
|
|
|
else => error.UnknownError,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:00:33 +00:00
|
|
|
pub fn bindUuid(self: *PreparedStmt, idx: u15, id: Uuid) !void {
|
|
|
|
const str = id.toCharArray();
|
|
|
|
return self.bindText(idx, &str);
|
|
|
|
}
|
|
|
|
|
2022-07-15 00:58:08 +00:00
|
|
|
pub fn bindText(self: *PreparedStmt, idx: u15, str: []const u8) !void {
|
2022-07-15 07:27:27 +00:00
|
|
|
return switch (c.sqlite3_bind_text(self.stmt, idx, str.ptr, @intCast(c_int, str.len), c.SQLITE_TRANSIENT)) {
|
|
|
|
c.SQLITE_OK => {},
|
2022-07-15 00:58:08 +00:00
|
|
|
else => error.UnknownError,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-24 04:14:46 +00:00
|
|
|
pub fn bindBlob(self: *PreparedStmt, idx: u15, blob: []const u8) !void {
|
|
|
|
return switch (c.sqlite3_bind_blob64(self.stmt, idx, blob.ptr, blob.len, c.SQLITE_TRANSIENT)) {
|
|
|
|
c.SQLITE_OK => {},
|
|
|
|
else => error.UnknownError,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-15 00:58:08 +00:00
|
|
|
pub fn bindI64(self: *PreparedStmt, idx: u15, val: i64) !void {
|
|
|
|
return switch (c.sqlite3_bind_int64(self.stmt, idx, val)) {
|
2022-07-18 07:37:10 +00:00
|
|
|
c.SQLITE_OK => {},
|
2022-07-15 00:58:08 +00:00
|
|
|
else => error.UnknownError,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-18 07:37:10 +00:00
|
|
|
pub fn bindDateTime(self: *PreparedStmt, idx: u15, val: DateTime) !void {
|
|
|
|
return self.bindI64(idx, val.seconds_since_epoch);
|
|
|
|
}
|
|
|
|
|
2022-07-16 19:00:33 +00:00
|
|
|
pub fn bind(self: *PreparedStmt, idx: u15, val: anytype) !void {
|
|
|
|
return switch (@TypeOf(val)) {
|
2022-07-18 07:37:10 +00:00
|
|
|
[]u8, []const u8 => self.bindText(idx, val),
|
2022-07-16 19:00:33 +00:00
|
|
|
i64 => self.bindI64(idx, val),
|
|
|
|
Uuid => self.bindUuid(idx, val),
|
2022-07-18 07:37:10 +00:00
|
|
|
DateTime => self.bindDateTime(idx, val),
|
|
|
|
@TypeOf(null) => self.bindNull(idx),
|
2022-07-22 04:18:43 +00:00
|
|
|
else => |T| switch (@typeInfo(T)) {
|
|
|
|
.Optional => if (val) |v| self.bind(idx, v) else self.bindNull(idx),
|
2022-07-24 04:14:46 +00:00
|
|
|
.Struct, .Union, .Enum, .Opaque => if (@hasDecl(T, "bindToSqlite")) val.bindToSqlite(self, idx),
|
2022-07-22 04:18:43 +00:00
|
|
|
else => @compileError("Unknown Type" ++ @typeName(T)),
|
|
|
|
},
|
2022-07-16 19:00:33 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-15 00:58:08 +00:00
|
|
|
pub fn step(self: *PreparedStmt) !?Row {
|
|
|
|
return switch (c.sqlite3_step(self.stmt)) {
|
2022-07-15 07:27:27 +00:00
|
|
|
c.SQLITE_ROW => Row{ .stmt = self.stmt, .db = self.db },
|
|
|
|
c.SQLITE_DONE => null,
|
2022-07-15 00:58:08 +00:00
|
|
|
|
2022-07-16 05:29:08 +00:00
|
|
|
else => |err| blk: {
|
2022-07-22 04:18:20 +00:00
|
|
|
std.log.debug("sql error {}: {s}", .{ err, c.sqlite3_errmsg(self.db) });
|
|
|
|
std.log.debug("Failed on SQL:\n==========\n{s}\n==========", .{self.getGeneratingSql()});
|
2022-07-16 05:29:08 +00:00
|
|
|
break :blk error.UnknownError;
|
|
|
|
},
|
2022-07-15 00:58:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn finalize(self: *PreparedStmt) void {
|
|
|
|
_ = c.sqlite3_finalize(self.stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(self: *PreparedStmt) void {
|
|
|
|
_ = c.sqlite3_reset(self.stmt);
|
|
|
|
}
|
2022-07-22 04:18:20 +00:00
|
|
|
|
|
|
|
fn getGeneratingSql(self: *PreparedStmt) ?[*:0]const u8 {
|
|
|
|
return c.sqlite3_sql(self.stmt);
|
|
|
|
}
|
2022-07-15 00:58:08 +00:00
|
|
|
};
|