2022-07-15 00:58:08 +00:00
|
|
|
const std = @import("std");
|
2022-09-11 11:38:46 +00:00
|
|
|
const util = @import("util");
|
2022-07-15 00:58:08 +00:00
|
|
|
|
2022-09-11 08:55:20 +00:00
|
|
|
const postgres = @import("./postgres.zig");
|
|
|
|
const sqlite = @import("./sqlite.zig");
|
2022-09-15 01:12:07 +00:00
|
|
|
const common = @import("./common.zig");
|
2022-09-11 08:55:20 +00:00
|
|
|
const Allocator = std.mem.Allocator;
|
2022-07-16 19:00:33 +00:00
|
|
|
|
2022-09-15 01:12:07 +00:00
|
|
|
pub const QueryOptions = common.QueryOptions;
|
|
|
|
|
2022-09-25 08:10:30 +00:00
|
|
|
pub const Engine = enum {
|
2022-09-11 08:55:20 +00:00
|
|
|
postgres,
|
|
|
|
sqlite,
|
2022-07-15 00:58:08 +00:00
|
|
|
};
|
|
|
|
|
2022-09-25 08:10:30 +00:00
|
|
|
pub const Config = union(Engine) {
|
2022-09-11 08:55:20 +00:00
|
|
|
postgres: struct {
|
2022-09-15 01:12:07 +00:00
|
|
|
pg_conn_str: [:0]const u8,
|
2022-09-11 08:55:20 +00:00
|
|
|
},
|
|
|
|
sqlite: struct {
|
2022-09-15 01:12:07 +00:00
|
|
|
sqlite_file_path: [:0]const u8,
|
2022-09-11 08:55:20 +00:00
|
|
|
},
|
2022-07-15 00:58:08 +00:00
|
|
|
};
|
|
|
|
|
2022-09-11 08:55:20 +00:00
|
|
|
//pub const OpenError = sqlite.OpenError | postgres.OpenError;
|
2022-09-25 08:10:30 +00:00
|
|
|
const RawResults = union(Engine) {
|
2022-09-11 08:55:20 +00:00
|
|
|
postgres: postgres.Results,
|
|
|
|
sqlite: sqlite.Results,
|
2022-07-15 00:58:08 +00:00
|
|
|
|
2022-09-11 11:38:46 +00:00
|
|
|
fn finish(self: RawResults) void {
|
2022-09-11 08:55:20 +00:00
|
|
|
switch (self) {
|
|
|
|
.postgres => |pg| pg.finish(),
|
|
|
|
.sqlite => |lite| lite.finish(),
|
|
|
|
}
|
2022-07-16 19:00:33 +00:00
|
|
|
}
|
2022-09-11 11:38:46 +00:00
|
|
|
};
|
2022-07-16 19:00:33 +00:00
|
|
|
|
2022-09-11 11:38:46 +00:00
|
|
|
// Represents a set of results.
|
|
|
|
// row() must be called until it returns null, or the query may not complete
|
|
|
|
// Must be deallocated by a call to finish()
|
|
|
|
pub fn Results(comptime result_types: []const type) type {
|
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
|
|
|
const RowTuple = std.meta.Tuple(result_types);
|
2022-07-15 00:58:08 +00:00
|
|
|
|
2022-09-11 11:38:46 +00:00
|
|
|
underlying: RawResults,
|
|
|
|
|
|
|
|
pub fn finish(self: Self) void {
|
|
|
|
self.underlying.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
// can be used as an optimization to reduce memory reallocation
|
|
|
|
// only works on postgres
|
|
|
|
pub fn rowCount(self: Self) ?usize {
|
|
|
|
return switch (self.underlying) {
|
|
|
|
.postgres => |pg| pg.rowCount(),
|
|
|
|
.sqlite => null, // not possible without repeating the query
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn row(self: *Self, alloc: ?Allocator) !?RowTuple {
|
|
|
|
const row_val = switch (self.underlying) {
|
|
|
|
.postgres => |*pg| if (try pg.row()) |r| Row{ .postgres = r } else return null,
|
|
|
|
.sqlite => |*lite| if (try lite.row()) |r| Row{ .sqlite = r } else return null,
|
|
|
|
};
|
|
|
|
|
|
|
|
var result: RowTuple = undefined;
|
|
|
|
var fields_allocated = [_]bool{false} ** result.len;
|
|
|
|
errdefer {
|
|
|
|
inline for (result_types) |_, i| {
|
|
|
|
if (fields_allocated[i]) util.deepFree(alloc, result[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline for (result_types) |T, i| {
|
|
|
|
result[i] = try row_val.get(T, i, alloc);
|
|
|
|
fields_allocated[i] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-07-24 04:14:46 +00:00
|
|
|
|
2022-09-11 08:55:20 +00:00
|
|
|
// Row is invalidated by the next call to result.row()
|
2022-09-25 08:10:30 +00:00
|
|
|
const Row = union(Engine) {
|
2022-09-11 08:55:20 +00:00
|
|
|
postgres: postgres.Row,
|
|
|
|
sqlite: sqlite.Row,
|
|
|
|
|
|
|
|
// Returns a value of type T from the zero-indexed column given by idx.
|
|
|
|
// Not all types require an allocator to be present. If an allocator is needed but
|
|
|
|
// not required, it will return error.AllocatorRequired.
|
|
|
|
// The caller is responsible for deallocating T, if relevant.
|
2022-09-11 11:38:46 +00:00
|
|
|
fn get(self: Row, comptime T: type, idx: u15, alloc: ?Allocator) anyerror!T {
|
2022-09-11 08:55:20 +00:00
|
|
|
return switch (self) {
|
|
|
|
.postgres => |pg| pg.get(T, idx, alloc),
|
|
|
|
.sqlite => |lite| lite.get(T, idx, alloc),
|
2022-07-15 00:58:08 +00:00
|
|
|
};
|
|
|
|
}
|
2022-09-11 08:55:20 +00:00
|
|
|
};
|
2022-07-15 00:58:08 +00:00
|
|
|
|
2022-09-25 08:10:30 +00:00
|
|
|
const DbUnion = union(Engine) {
|
2022-09-11 08:55:20 +00:00
|
|
|
postgres: postgres.Db,
|
|
|
|
sqlite: sqlite.Db,
|
2022-09-11 11:38:46 +00:00
|
|
|
};
|
2022-07-15 00:58:08 +00:00
|
|
|
|
2022-09-25 08:10:30 +00:00
|
|
|
pub const ConstraintMode = enum {
|
|
|
|
deferred,
|
|
|
|
immediate,
|
|
|
|
};
|
|
|
|
|
2022-09-11 11:38:46 +00:00
|
|
|
pub const Db = struct {
|
2022-09-25 08:10:30 +00:00
|
|
|
tx_open: bool = false,
|
2022-09-11 11:38:46 +00:00
|
|
|
underlying: DbUnion,
|
2022-09-25 08:10:30 +00:00
|
|
|
|
2022-09-11 08:55:20 +00:00
|
|
|
pub fn open(cfg: Config) !Db {
|
|
|
|
return switch (cfg) {
|
|
|
|
.postgres => |postgres_cfg| Db{
|
2022-09-11 11:38:46 +00:00
|
|
|
.underlying = .{
|
2022-09-15 01:12:07 +00:00
|
|
|
.postgres = try postgres.Db.open(postgres_cfg.pg_conn_str),
|
2022-09-11 11:38:46 +00:00
|
|
|
},
|
2022-09-08 01:55:55 +00:00
|
|
|
},
|
2022-09-11 08:55:20 +00:00
|
|
|
.sqlite => |lite_cfg| Db{
|
2022-09-11 11:38:46 +00:00
|
|
|
.underlying = .{
|
2022-09-15 01:12:07 +00:00
|
|
|
.sqlite = try sqlite.Db.open(lite_cfg.sqlite_file_path),
|
2022-09-11 11:38:46 +00:00
|
|
|
},
|
2022-09-08 01:55:55 +00:00
|
|
|
},
|
2022-09-11 08:55:20 +00:00
|
|
|
};
|
|
|
|
}
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-09-25 08:10:30 +00:00
|
|
|
pub fn close(self: *Db) void {
|
2022-09-11 11:38:46 +00:00
|
|
|
switch (self.underlying) {
|
2022-09-11 08:55:20 +00:00
|
|
|
.postgres => |pg| pg.close(),
|
|
|
|
.sqlite => |lite| lite.close(),
|
2022-09-08 01:55:55 +00:00
|
|
|
}
|
2022-07-15 00:58:08 +00:00
|
|
|
}
|
2022-07-22 04:18:20 +00:00
|
|
|
|
2022-09-15 01:12:07 +00:00
|
|
|
pub fn queryWithOptions(
|
2022-09-25 08:10:30 +00:00
|
|
|
self: *Db,
|
2022-09-15 01:12:07 +00:00
|
|
|
comptime result_types: []const type,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
opt: QueryOptions,
|
|
|
|
) !Results(result_types) {
|
2022-09-25 08:10:30 +00:00
|
|
|
if (self.tx_open) return error.TransactionOpen;
|
2022-09-15 01:12:07 +00:00
|
|
|
// Create fake transaction to use its functions
|
2022-09-25 08:10:30 +00:00
|
|
|
return (Tx{ .db = self }).queryWithOptions(result_types, sql, args, opt);
|
2022-09-15 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 11:38:46 +00:00
|
|
|
pub fn query(
|
2022-09-25 08:10:30 +00:00
|
|
|
self: *Db,
|
2022-09-11 11:38:46 +00:00
|
|
|
comptime result_types: []const type,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?Allocator,
|
|
|
|
) !Results(result_types) {
|
2022-09-25 08:10:30 +00:00
|
|
|
if (self.tx_open) return error.TransactionOpen;
|
2022-09-11 11:38:46 +00:00
|
|
|
// Create fake transaction to use its functions
|
2022-09-25 08:10:30 +00:00
|
|
|
return (Tx{ .db = self }).query(result_types, sql, args, alloc);
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exec(
|
2022-09-25 08:10:30 +00:00
|
|
|
self: *Db,
|
2022-09-11 11:38:46 +00:00
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?Allocator,
|
|
|
|
) !void {
|
2022-09-25 08:10:30 +00:00
|
|
|
if (self.tx_open) return error.TransactionOpen;
|
2022-09-11 11:38:46 +00:00
|
|
|
// Create fake transaction to use its functions
|
2022-09-25 08:10:30 +00:00
|
|
|
return (Tx{ .db = self }).exec(sql, args, alloc);
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn queryRow(
|
2022-09-25 08:10:30 +00:00
|
|
|
self: *Db,
|
2022-09-11 11:38:46 +00:00
|
|
|
comptime result_types: []const type,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?Allocator,
|
|
|
|
) !?Results(result_types).RowTuple {
|
2022-09-25 08:10:30 +00:00
|
|
|
if (self.tx_open) return error.TransactionOpen;
|
2022-09-11 11:38:46 +00:00
|
|
|
// Create fake transaction to use its functions
|
2022-09-25 08:10:30 +00:00
|
|
|
return (Tx{ .db = self }).queryRow(result_types, sql, args, alloc);
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(
|
2022-09-25 08:10:30 +00:00
|
|
|
self: *Db,
|
2022-09-11 11:38:46 +00:00
|
|
|
comptime table: []const u8,
|
|
|
|
value: anytype,
|
|
|
|
) !void {
|
2022-09-25 08:10:30 +00:00
|
|
|
if (self.tx_open) return error.TransactionOpen;
|
2022-09-11 11:38:46 +00:00
|
|
|
// Create fake transaction to use its functions
|
2022-09-25 08:10:30 +00:00
|
|
|
return (Tx{ .db = self }).insert(table, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn sqlEngine(self: *Db) Engine {
|
|
|
|
return self.underlying;
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Begins a transaction
|
2022-09-25 08:10:30 +00:00
|
|
|
pub fn begin(self: *Db) !Tx {
|
|
|
|
const tx = Tx{ .db = self };
|
2022-09-11 11:38:46 +00:00
|
|
|
try tx.exec("BEGIN", .{}, null);
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Tx = struct {
|
2022-09-25 08:10:30 +00:00
|
|
|
db: *Db,
|
2022-09-11 11:38:46 +00:00
|
|
|
|
|
|
|
// internal helper fn
|
|
|
|
fn queryInternal(
|
|
|
|
self: Tx,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
2022-09-15 01:12:07 +00:00
|
|
|
opt: QueryOptions,
|
2022-09-11 11:38:46 +00:00
|
|
|
) !RawResults {
|
2022-09-25 08:10:30 +00:00
|
|
|
return switch (self.db.underlying) {
|
2022-09-15 01:12:07 +00:00
|
|
|
.postgres => |pg| RawResults{ .postgres = try pg.exec(sql, args, opt.prep_allocator) },
|
|
|
|
.sqlite => |lite| RawResults{ .sqlite = try lite.exec(sql, args, opt) },
|
2022-09-11 11:38:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-15 01:12:07 +00:00
|
|
|
pub fn queryWithOptions(
|
|
|
|
self: Tx,
|
|
|
|
comptime result_types: []const type,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
options: QueryOptions,
|
|
|
|
) !Results(result_types) {
|
|
|
|
return Results(result_types){ .underlying = try self.queryInternal(sql, args, options) };
|
|
|
|
}
|
|
|
|
|
2022-09-11 11:38:46 +00:00
|
|
|
// Executes a query and returns the result set
|
|
|
|
pub fn query(
|
|
|
|
self: Tx,
|
|
|
|
comptime result_types: []const type,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?Allocator,
|
|
|
|
) !Results(result_types) {
|
2022-09-15 01:12:07 +00:00
|
|
|
return self.queryWithOptions(result_types, sql, args, .{ .prep_allocator = alloc });
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Executes a query without returning results
|
|
|
|
pub fn exec(
|
|
|
|
self: Tx,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?Allocator,
|
|
|
|
) !void {
|
2022-09-15 01:12:07 +00:00
|
|
|
_ = try self.queryRow(&.{}, sql, args, alloc);
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs a query and returns a single row
|
|
|
|
pub fn queryRow(
|
|
|
|
self: Tx,
|
|
|
|
comptime result_types: []const type,
|
|
|
|
q: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?Allocator,
|
|
|
|
) !?Results(result_types).RowTuple {
|
|
|
|
var results = try self.query(result_types, q, args, alloc);
|
|
|
|
defer results.finish();
|
|
|
|
|
|
|
|
const row = (try results.row(alloc)) orelse return null;
|
|
|
|
errdefer util.deepFree(alloc, row);
|
|
|
|
|
|
|
|
var more_rows = false;
|
|
|
|
while (try results.row(alloc)) |r| {
|
|
|
|
util.deepFree(alloc, r);
|
|
|
|
more_rows = true;
|
|
|
|
}
|
|
|
|
if (more_rows) return error.TooManyRows;
|
|
|
|
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inserts a single value into a table
|
|
|
|
pub fn insert(
|
|
|
|
self: Tx,
|
|
|
|
comptime table: []const u8,
|
|
|
|
value: anytype,
|
|
|
|
) !void {
|
|
|
|
const ValueType = comptime @TypeOf(value);
|
2022-09-15 01:12:07 +00:00
|
|
|
|
|
|
|
const fields = std.meta.fields(ValueType);
|
|
|
|
comptime var types: [fields.len]type = undefined;
|
|
|
|
comptime var table_spec: []const u8 = table ++ "(";
|
|
|
|
comptime var value_spec: []const u8 = "(";
|
|
|
|
inline for (fields) |field, i| {
|
|
|
|
types[i] = field.field_type;
|
|
|
|
table_spec = comptime (table_spec ++ field.name ++ ",");
|
|
|
|
value_spec = comptime value_spec ++ std.fmt.comptimePrint("${},", .{i + 1});
|
|
|
|
}
|
|
|
|
table_spec = comptime table_spec[0 .. table_spec.len - 1] ++ ")";
|
|
|
|
value_spec = comptime value_spec[0 .. value_spec.len - 1] ++ ")";
|
2022-09-11 11:38:46 +00:00
|
|
|
const q = comptime std.fmt.comptimePrint(
|
|
|
|
"INSERT INTO {s} VALUES {s}",
|
|
|
|
.{ table_spec, value_spec },
|
|
|
|
);
|
2022-09-15 01:12:07 +00:00
|
|
|
|
|
|
|
var args_tuple: std.meta.Tuple(&types) = undefined;
|
|
|
|
inline for (fields) |field, i| {
|
|
|
|
args_tuple[i] = @field(value, field.name);
|
|
|
|
}
|
|
|
|
try self.exec(q, args_tuple, null);
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 08:10:30 +00:00
|
|
|
pub fn sqlEngine(self: Tx) Engine {
|
|
|
|
return self.db.underlying;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn setConstraintMode(self: Tx, mode: ConstraintMode) !void {
|
|
|
|
switch (self.db.underlying) {
|
|
|
|
.sqlite => try self.exec(
|
|
|
|
switch (mode) {
|
|
|
|
.immediate => "PRAGMA defer_foreign_keys = FALSE",
|
|
|
|
.deferred => "PRAGMA defer_foreign_keys = TRUE",
|
|
|
|
},
|
|
|
|
.{},
|
|
|
|
null,
|
|
|
|
),
|
|
|
|
.postgres => try self.exec(
|
|
|
|
switch (mode) {
|
|
|
|
.immediate => "SET CONSTRAINTS ALL IMMEDIATE",
|
|
|
|
.deferred => "SET CONSTRAINTS ALL DEFERRED",
|
|
|
|
},
|
|
|
|
.{},
|
|
|
|
null,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 11:38:46 +00:00
|
|
|
pub fn rollback(self: Tx) void {
|
|
|
|
self.exec("ROLLBACK", .{}, null) catch |err| {
|
|
|
|
std.log.err("Error occured during rollback operation: {}", .{err});
|
2022-09-11 08:55:20 +00:00
|
|
|
};
|
2022-07-22 04:18:20 +00:00
|
|
|
}
|
2022-09-11 11:38:46 +00:00
|
|
|
|
|
|
|
pub fn commit(self: Tx) !void {
|
|
|
|
try self.exec("COMMIT", .{}, null);
|
|
|
|
}
|
2022-07-15 00:58:08 +00:00
|
|
|
};
|