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-10-01 09:05:33 +00:00
|
|
|
const postgres = @import("./engines/postgres.zig");
|
2022-10-04 02:41:59 +00:00
|
|
|
//const postgres = @import("./engines/null.zig");
|
2022-10-01 09:05:33 +00:00
|
|
|
const sqlite = @import("./engines/sqlite.zig");
|
2022-10-04 02:41:59 +00:00
|
|
|
//const sqlite = @import("./engines/null.zig");
|
2022-10-01 09:05:33 +00:00
|
|
|
const common = @import("./engines/common.zig");
|
2022-09-11 08:55:20 +00:00
|
|
|
const Allocator = std.mem.Allocator;
|
2022-07-16 19:00:33 +00:00
|
|
|
|
2022-10-01 09:05:33 +00:00
|
|
|
const errors = @import("./errors.zig").library_errors;
|
|
|
|
|
|
|
|
pub const OpenError = errors.OpenError;
|
|
|
|
pub const QueryError = errors.QueryError;
|
|
|
|
pub const RowError = errors.RowError;
|
|
|
|
pub const QueryRowError = errors.QueryRowError;
|
|
|
|
pub const BeginError = errors.BeginError;
|
|
|
|
pub const CommitError = errors.CommitError;
|
|
|
|
|
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-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-29 21:52:01 +00:00
|
|
|
|
2022-10-04 02:41:59 +00:00
|
|
|
fn columnCount(self: RawResults) !u15 {
|
|
|
|
return try switch (self) {
|
2022-09-29 21:52:01 +00:00
|
|
|
.postgres => |pg| pg.columnCount(),
|
|
|
|
.sqlite => |lite| lite.columnCount(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-04 02:41:59 +00:00
|
|
|
fn columnIndex(self: RawResults, name: []const u8) error{ NotFound, Unexpected }!u15 {
|
|
|
|
return switch (self) {
|
2022-10-01 09:05:33 +00:00
|
|
|
.postgres => |pg| pg.columnIndex(name),
|
|
|
|
.sqlite => |lite| lite.columnIndex(name),
|
2022-10-04 02:41:59 +00:00
|
|
|
} catch |err| switch (err) {
|
|
|
|
error.OutOfRange => error.Unexpected,
|
|
|
|
error.NotFound => error.NotFound,
|
2022-09-29 21:52:01 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-01 09:05:33 +00:00
|
|
|
fn row(self: *RawResults) RowError!?Row {
|
2022-09-29 21:52:01 +00:00
|
|
|
return switch (self.*) {
|
|
|
|
.postgres => |*pg| if (try pg.row()) |r| Row{ .postgres = r } else null,
|
|
|
|
.sqlite => |*lite| if (try lite.row()) |r| Row{ .sqlite = r } else null,
|
|
|
|
};
|
|
|
|
}
|
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()
|
2022-09-29 21:52:01 +00:00
|
|
|
pub fn Results(comptime T: type) type {
|
|
|
|
// would normally make this a declaration of the struct, but it causes the compiler to crash
|
2022-10-04 02:41:59 +00:00
|
|
|
const fields = if (T == void) .{} else std.meta.fields(T);
|
2022-09-11 11:38:46 +00:00
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
2022-07-15 00:58:08 +00:00
|
|
|
|
2022-09-11 11:38:46 +00:00
|
|
|
underlying: RawResults,
|
2022-09-29 21:52:01 +00:00
|
|
|
column_indices: [fields.len]u15,
|
|
|
|
|
2022-10-01 09:05:33 +00:00
|
|
|
fn from(underlying: RawResults) QueryError!Self {
|
2022-10-04 02:41:59 +00:00
|
|
|
if (std.debug.runtime_safety and fields.len != underlying.columnCount() catch unreachable) {
|
|
|
|
std.log.err("Expected {} columns in result, got {}", .{ fields.len, underlying.columnCount() catch unreachable });
|
2022-10-01 09:05:33 +00:00
|
|
|
return error.ColumnMismatch;
|
|
|
|
}
|
|
|
|
|
2022-09-29 21:52:01 +00:00
|
|
|
return Self{ .underlying = underlying, .column_indices = blk: {
|
|
|
|
var indices: [fields.len]u15 = undefined;
|
|
|
|
inline for (fields) |f, i| {
|
2022-10-04 02:41:59 +00:00
|
|
|
indices[i] = if (!std.meta.trait.isTuple(T))
|
|
|
|
underlying.columnIndex(f.name) catch {
|
|
|
|
std.log.err("Could not find column index for field {s}", .{f.name});
|
|
|
|
return error.ColumnMismatch;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
i;
|
2022-09-29 21:52:01 +00:00
|
|
|
}
|
|
|
|
break :blk indices;
|
|
|
|
} };
|
|
|
|
}
|
2022-09-11 11:38:46 +00:00
|
|
|
|
|
|
|
pub fn finish(self: Self) void {
|
|
|
|
self.underlying.finish();
|
|
|
|
}
|
|
|
|
|
2022-09-29 21:52:01 +00:00
|
|
|
// Returns the next row of results, or null if there are no more rows.
|
|
|
|
// Caller owns all memory allocated. The entire object can be deallocated with a
|
|
|
|
// call to util.deepFree
|
2022-10-01 09:05:33 +00:00
|
|
|
pub fn row(self: *Self, alloc: ?Allocator) RowError!?T {
|
2022-09-29 21:52:01 +00:00
|
|
|
if (try self.underlying.row()) |row_val| {
|
|
|
|
var result: T = undefined;
|
|
|
|
var fields_allocated: usize = 0;
|
|
|
|
errdefer inline for (fields) |f, i| {
|
|
|
|
// Iteration bounds must be defined at comptime (inline for) but the number of fields we could
|
|
|
|
// successfully allocate is defined at runtime. So we iterate over the entire field array and
|
|
|
|
// conditionally deallocate fields in the loop.
|
|
|
|
if (i < fields_allocated) util.deepFree(alloc, @field(result, f.name));
|
|
|
|
};
|
|
|
|
|
|
|
|
inline for (fields) |f, i| {
|
2022-10-04 02:41:59 +00:00
|
|
|
// TODO: Causes compiler segfault. why?
|
|
|
|
//const F = f.field_type;
|
|
|
|
const F = @TypeOf(@field(result, f.name));
|
|
|
|
@field(result, f.name) = row_val.get(F, self.column_indices[i], alloc) catch |err| {
|
|
|
|
std.log.err("SQL: Error getting column {s} of type {}", .{ f.name, F });
|
|
|
|
return err;
|
|
|
|
};
|
2022-09-29 21:52:01 +00:00
|
|
|
fields_allocated += 1;
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 21:52:01 +00:00
|
|
|
return result;
|
|
|
|
} else return null;
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
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-10-04 02:41:59 +00:00
|
|
|
fn get(self: Row, comptime T: type, idx: u15, alloc: ?Allocator) common.GetError!T {
|
|
|
|
if (T == void) return;
|
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
|
|
|
pub const ConstraintMode = enum {
|
|
|
|
deferred,
|
|
|
|
immediate,
|
|
|
|
};
|
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
pub const Conn = struct {
|
|
|
|
engine: union(Engine) {
|
|
|
|
postgres: postgres.Db,
|
|
|
|
sqlite: sqlite.Db,
|
|
|
|
},
|
|
|
|
current_tx_level: u8 = 0,
|
|
|
|
is_tx_failed: bool = false,
|
2022-10-04 02:41:59 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
pub fn open(cfg: Config) OpenError!Conn {
|
2022-09-11 08:55:20 +00:00
|
|
|
return switch (cfg) {
|
2022-10-04 05:41:22 +00:00
|
|
|
.postgres => |postgres_cfg| Conn{
|
2022-10-01 09:05:33 +00:00
|
|
|
.engine = .{
|
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-10-04 05:41:22 +00:00
|
|
|
.sqlite => |lite_cfg| Conn{
|
2022-10-01 09:05:33 +00:00
|
|
|
.engine = .{
|
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-10-04 05:41:22 +00:00
|
|
|
pub fn close(self: *Conn) void {
|
2022-10-01 09:05:33 +00:00
|
|
|
switch (self.engine) {
|
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-10-04 05:41:22 +00:00
|
|
|
pub fn acquire(self: *Conn) !Db {
|
|
|
|
if (self.current_tx_level != 0) return error.BadTransactionState;
|
|
|
|
return Db{ .conn = self };
|
2022-09-15 01:12:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
pub fn sqlEngine(self: *Conn) Engine {
|
|
|
|
return self.engine;
|
2022-09-11 11:38:46 +00:00
|
|
|
}
|
2022-10-04 05:41:22 +00:00
|
|
|
};
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
pub const Db = Tx(0);
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
/// When tx_level == 0, the DB is operating in "implied transaction" mode where
|
|
|
|
/// every command is its own transaction
|
|
|
|
/// When tx_level >= 1, the DB has an explicit transaction open
|
|
|
|
/// When tx_level >= 2, the DB has (tx_level - 1) levels of transaction savepoints open
|
|
|
|
/// (like nested transactions)
|
|
|
|
fn Tx(comptime tx_level: u8) type {
|
|
|
|
return struct {
|
|
|
|
const Self = @This();
|
|
|
|
const savepoint_name = if (tx_level == 0)
|
|
|
|
@compileError("Transaction not started")
|
|
|
|
else
|
|
|
|
std.fmt.comptimePrint("save_{}", .{tx_level});
|
|
|
|
const next_savepoint_name = Tx(tx_level + 1).savepoint_name;
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
conn: *Conn,
|
2022-09-25 08:10:30 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
/// The type of SQL engine being used. Use of this function should be discouraged
|
|
|
|
pub fn sqlEngine(self: Self) Engine {
|
|
|
|
return self.conn.sqlEngine();
|
|
|
|
}
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
// ********* Transaction management functions **********
|
2022-10-01 09:05:33 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
/// Start an explicit transaction
|
|
|
|
pub fn begin(self: Self) !Tx(1) {
|
|
|
|
if (tx_level != 0) @compileError("Transaction already started");
|
|
|
|
if (self.conn.current_tx_level != 0) return error.BadTransactionState;
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
try self.exec("BEGIN", {}, null);
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
self.conn.current_tx_level = 1;
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
return Tx(1){ .conn = self.conn };
|
|
|
|
}
|
2022-10-04 02:41:59 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
/// Create a savepoint (nested transaction)
|
|
|
|
pub fn savepoint(self: Self) !Tx(tx_level + 1) {
|
|
|
|
if (tx_level == 0) @compileError("Cannot place a savepoint on an implicit transaction");
|
|
|
|
if (self.conn.current_tx_level != tx_level) return error.BadTransactionState;
|
2022-09-15 01:12:07 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
try self.exec("SAVEPOINT " ++ next_savepoint_name, {}, null);
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
self.conn.current_tx_level = tx_level + 1;
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
return Tx(tx_level + 1){ .conn = self.conn };
|
|
|
|
}
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
/// Commit the entire transaction
|
|
|
|
pub fn commit(self: Self) !void {
|
|
|
|
if (tx_level == 0) @compileError("Transaction not started");
|
|
|
|
if (tx_level >= 2) @compileError("Cannot commit a savepoint");
|
|
|
|
if (self.conn.current_tx_level == 0) return error.BadTransactionState;
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
try self.exec("COMMIT", {}, null);
|
2022-09-25 08:10:30 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
self.conn.current_tx_level = 0;
|
2022-09-25 08:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
/// Release the current savepoint and all savepoints created from it.
|
|
|
|
pub fn release(self: Self) !void {
|
|
|
|
if (tx_level == 0) @compileError("Transaction not started");
|
|
|
|
if (tx_level == 1) @compileError("Cannot release a transaction");
|
|
|
|
if (self.conn.current_tx_level < tx_level) return error.BadTransactionState;
|
2022-09-11 11:38:46 +00:00
|
|
|
|
2022-10-04 05:41:22 +00:00
|
|
|
try self.exec("RELEASE SAVEPOINT " ++ savepoint_name, {}, null);
|
|
|
|
|
|
|
|
self.conn.current_tx_level = tx_level - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Rolls back the entire transaction
|
|
|
|
pub fn rollbackTx(self: Self) !void {
|
|
|
|
if (tx_level == 0) @compileError("Transaction not started");
|
|
|
|
if (tx_level >= 2) @compileError("Cannot rollback a transaction using a savepoint");
|
|
|
|
if (self.conn.current_tx_level == 0) return error.BadTransactionState;
|
|
|
|
|
|
|
|
try self.exec("ROLLBACK", {}, null);
|
|
|
|
|
|
|
|
self.conn.current_tx_level = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to roll back to a savepoint
|
|
|
|
pub fn rollbackSavepoint(self: Self) !void {
|
|
|
|
if (tx_level == 0) @compileError("Transaction not started");
|
|
|
|
if (tx_level == 1) @compileError("Cannot rollback a savepoint on the entire transaction");
|
|
|
|
if (self.conn.current_tx_level < tx_level) return error.BadTransactionState;
|
|
|
|
|
|
|
|
try self.exec("ROLLBACK TO " ++ savepoint_name, {}, null);
|
|
|
|
|
|
|
|
self.conn.current_tx_level = tx_level - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Perform whichever rollback is appropriate for the situation
|
|
|
|
pub fn rollback(self: Self) void {
|
|
|
|
(if (tx_level < 2) self.rollbackTx() else self.rollbackSavepoint()) catch |err| {
|
|
|
|
std.log.err("Failed to rollback transaction: {}", .{err});
|
|
|
|
@panic("TODO: more gracefully handle rollback failures");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const beginOrSavepoint = if (tx_level == 0) begin else savepoint;
|
|
|
|
pub const commitOrRelease = if (tx_level < 2) commit else release;
|
|
|
|
|
|
|
|
// Allows relaxing *some* constraints for the lifetime of the transaction.
|
|
|
|
// You should generally not do this, but it's useful when bootstrapping
|
|
|
|
// the initial admin community and cluster operator user.
|
|
|
|
pub fn setConstraintMode(self: Self, mode: ConstraintMode) !void {
|
|
|
|
if (tx_level == 0) @compileError("Transaction not started");
|
|
|
|
if (tx_level >= 2) @compileError("Cannot set constraint mode on a savepoint");
|
|
|
|
switch (self.sqlEngine()) {
|
|
|
|
.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,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ********** Query Helpers ************
|
|
|
|
|
|
|
|
/// Runs a command without returning results
|
|
|
|
pub fn exec(
|
|
|
|
self: Self,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?std.mem.Allocator,
|
|
|
|
) !void {
|
|
|
|
try self.execInternal(sql, args, alloc, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn queryWithOptions(
|
|
|
|
self: Self,
|
|
|
|
comptime RowType: type,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
options: QueryOptions,
|
|
|
|
) QueryError!Results(RowType) {
|
|
|
|
return Results(RowType).from(try self.runSql(sql, args, options, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn query(
|
|
|
|
self: Self,
|
|
|
|
comptime RowType: type,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?Allocator,
|
|
|
|
) QueryError!Results(RowType) {
|
|
|
|
return self.queryWithOptions(RowType, sql, args, .{ .prep_allocator = alloc });
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs a query to completion and returns a row of results, unless the query
|
|
|
|
/// returned a different number of rows.
|
|
|
|
pub fn queryRow(
|
|
|
|
self: Self,
|
|
|
|
comptime RowType: type,
|
|
|
|
q: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?Allocator,
|
|
|
|
) QueryRowError!RowType {
|
|
|
|
var results = try self.query(RowType, q, args, alloc);
|
|
|
|
defer results.finish();
|
|
|
|
|
|
|
|
const row = (try results.row(alloc)) orelse return error.NoRows;
|
|
|
|
errdefer util.deepFree(alloc, row);
|
|
|
|
|
|
|
|
// execute query to completion
|
|
|
|
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: Self,
|
|
|
|
comptime table: []const u8,
|
|
|
|
value: anytype,
|
|
|
|
alloc: ?std.mem.Allocator,
|
|
|
|
) !void {
|
|
|
|
const ValueType = comptime @TypeOf(value);
|
|
|
|
|
|
|
|
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| {
|
|
|
|
// This causes a compile error. Why?
|
|
|
|
//const F = field.field_type;
|
|
|
|
const F = @TypeOf(@field(std.mem.zeroes(ValueType), field.name));
|
|
|
|
// causes issues if F is @TypeOf(null), use dummy type
|
|
|
|
types[i] = if (F == @TypeOf(null)) ?i64 else F;
|
|
|
|
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] ++ ")";
|
|
|
|
const q = comptime std.fmt.comptimePrint(
|
|
|
|
"INSERT INTO {s} VALUES {s}",
|
|
|
|
.{ table_spec, value_spec },
|
|
|
|
);
|
|
|
|
|
|
|
|
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, alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal helpers
|
|
|
|
|
|
|
|
fn runSql(
|
|
|
|
self: Self,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
opt: QueryOptions,
|
|
|
|
comptime check_tx: bool,
|
|
|
|
) !RawResults {
|
|
|
|
if (check_tx and self.conn.current_tx_level != tx_level) return error.BadTransactionState;
|
|
|
|
|
|
|
|
return switch (self.conn.engine) {
|
|
|
|
.postgres => |pg| RawResults{ .postgres = try pg.exec(sql, args, opt) },
|
|
|
|
.sqlite => |lite| RawResults{ .sqlite = try lite.exec(sql, args, opt) },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execInternal(
|
|
|
|
self: Self,
|
|
|
|
sql: [:0]const u8,
|
|
|
|
args: anytype,
|
|
|
|
alloc: ?std.mem.Allocator,
|
|
|
|
comptime check_tx: bool,
|
|
|
|
) !void {
|
|
|
|
var results = try self.runSql(sql, args, .{ .prep_allocator = alloc }, check_tx);
|
|
|
|
defer results.finish();
|
|
|
|
|
|
|
|
while (try results.row()) |_| {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|