Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721const std = @import("std");const Io = std.Io;const Allocator = std.mem.Allocator;const Writer = Io.Writer;const Mutex = Io.Mutex;const ArrayList = std.ArrayList;const ArrayListManaged = std.array_list.Managed;
const jetquery = @import("../root.zig");
pub fn Repo(adapter_name: jetquery.adapters.Name, Schema: type) type { return struct { const AdaptedRepo = @This(); const Adapter = jetquery.adapters.Type(adapter_name); const Result = jetquery.Result(AdaptedRepo);
pub const RepoAdapter = jetquery.adapters.Adapter(adapter_name, @This()); pub const AdapterOptions = switch (Adapter.name) { .postgresql => jetquery.adapters.PostgresqlAdapter.Options, .null => jetquery.adapters.NullAdapter.Options, };
pub const GlobalOptions = struct { eventCallback: jetquery.CallbackFn = jetquery.events.defaultCallback, lazy_connect: bool = false, admin: bool = false, context: jetquery.Context = .query, env: ?AdapterOptions = null, };
comptime adapter_name: jetquery.adapters.Name = adapter_name, io: Io, allocator: Allocator, adapter: *RepoAdapter, eventCallback: jetquery.CallbackFn = jetquery.events.defaultCallback, result: ?jetquery.Result(AdaptedRepo) = null, context: jetquery.Context = .query, connection: ?jetquery.Connection = null, debug_mutex: Mutex = .init, child_debug_mutex: ?*Mutex = null,
// For convenience, allow users to call `repo.Query(...)` instead of // `@TypeOf(repo).Query(...)` comptime Query: fn (anytype) type = _Query,
pub const InitOptions = switch (adapter_name) { // All adapter options must be a superset of `GlobalOptions` .postgresql => struct { adapter: jetquery.adapters.PostgresqlAdapter.Options, eventCallback: jetquery.CallbackFn = jetquery.events.defaultCallback, lazy_connect: bool = false, admin: bool = false, context: jetquery.Context = .query, env: ?AdapterOptions = null, /// Pass `&init.environ_map` from your main function to enable env var defaults. environ: ?*const std.process.Environ.Map = null, }, .null => struct { eventCallback: jetquery.CallbackFn = jetquery.events.defaultCallback, admin: bool = false, lazy_connect: bool = false, context: jetquery.Context = .query, env: ?AdapterOptions = null, environ: ?*const std.process.Environ.Map = null, }, };
pub fn _Query(table: anytype) type { comptime { return jetquery.Query(adapter_name, Schema, table); } }
pub fn format(_: AdaptedRepo, _: []const u8, _: anytype, writer: *Writer) !void { try writer.print("Repo({s})", .{@tagName(adapter_name)}); }
/// Initialize a new Repo for executing queries. pub fn init(io: Io, allocator: Allocator, options: InitOptions) !AdaptedRepo { const adapter: RepoAdapter = switch (comptime adapter_name) { .postgresql => .{ .postgresql = try jetquery.adapters.PostgresqlAdapter.init( io, allocator, try applyDefaultOptions(@TypeOf(options.adapter), options.adapter, options.environ), options.lazy_connect, ), }, .null => .{ .null = jetquery.adapters.NullAdapter{} }, };
const adapter_ptr = try allocator.create(RepoAdapter); adapter_ptr.* = adapter;
return .{ .io = io, .allocator = allocator, .adapter = adapter_ptr, .eventCallback = options.eventCallback, .context = options.context, }; }
pub fn applyDefaultOptions(T: type, initial: T, environ: ?*const std.process.Environ.Map) !T { var options: T = undefined; const prefix = "JETQUERY_"; inline for (jetquery.fields.structFields(T)) |field| { const env_name = comptime blk: { var buf: [prefix.len + field.name.len]u8 = undefined; @memcpy(buf[0..prefix.len], prefix); for (field.name, prefix.len..) |char, index| buf[index] = std.ascii.toUpper(char); break :blk buf; }; const env_value = if (environ) |e| e.get(&env_name) else null; @field(options, field.name) = @field(initial, field.name) orelse switch (field.type) { ?u16, ?u32 => if (env_value) |value| try std.fmt.parseInt(@typeInfo(field.type).optional.child, value, 10) else comptime T.defaultValue(field.type, field.name), inline else => env_value orelse comptime T.defaultValue(field.type, field.name), }; } return options; }
/// Initialize a new repo using a config file. Config file build path is configured by build /// option `jetquery_config_path`. pub fn loadConfig( io: Io, allocator: Allocator, comptime environment: jetquery.Environment, global_options: GlobalOptions, ) !AdaptedRepo { const config = switch (environment) { inline else => |tag| @field(jetquery.config.database, @tagName(tag)), };
var options: AdapterOptions = undefined; inline for (jetquery.fields.structFields(AdapterOptions)) |field| { if (@hasField(@TypeOf(config), field.name)) { @field(options, field.name) = @field(config, field.name); } else if (field.defaultValue()) |default| { @field(options, field.name) = default; } else { var buf: [field.name.len]u8 = undefined; @compileError(std.fmt.comptimePrint( "Missing database configuration value for: `{s}`. " ++ "Configure in JetQuery config file or `JETQUERY_{s}`.", .{ field.name, std.ascii.upperString(&buf, field.name) }, )); } }
var init_options: InitOptions = switch (Adapter.name) { .postgresql => .{ .adapter = mergeOptions(global_options.admin, global_options.env, options), }, .null => .{}, };
inline for (jetquery.fields.structFields(GlobalOptions)) |field| { @field(init_options, field.name) = @field(global_options, field.name); } return AdaptedRepo.init(io, allocator, init_options); }
// Allow an application to pass an `env` option containing database options. Any values // passed here override anything defined in the config file. Jetzig uses this to read // environment variables from an `.env` file and the process environment. fn mergeOptions(admin: bool, maybe_env: ?AdapterOptions, options: AdapterOptions) AdapterOptions { const env = maybe_env orelse return options;
var merged = AdapterOptions{}; inline for (jetquery.fields.structFields(AdapterOptions)) |field| { const value = if (comptime std.mem.eql(u8, field.name, "database")) // If we are running in "admin" mode (i.e. creating or dropping a database), // assume the database we are connecting to is the same as the admin schema, e.g. // if username is `postgres`, connect to database `postgres`. // If this option does not work then users must manually create/drop a database // through their database's CLI/admin tooling - we take our best guess here. if (admin) env.username orelse options.username else env.database orelse options.database else @field(env, field.name) orelse @field(options, field.name); @field(merged, field.name) = value; }
return merged; }
/// Close connections and free resources. pub fn deinit(self: *AdaptedRepo) void { self.release(); switch (self.adapter.*) { inline else => |*adapter| adapter.deinit(), } self.allocator.destroy(self.adapter); }
/// Execute the given query and return results. pub fn execute( self: *AdaptedRepo, query: anytype, ) !switch (@TypeOf(query).ResultContext) { .one => ?@TypeOf(query).ResultType, .many => Result, .none => void, } { const caller_info = try jetquery.debug.getCallerInfo(@returnAddress()); defer if (caller_info) |info| info.deinit();
return try self.executeInternal(query, caller_info); }
/// Execute SQL with the active adapter and return a result (same as `execute` but accepts an SQL /// string and values instead of a generated query). pub fn executeSql( self: *AdaptedRepo, sql: []const u8, values: anytype, ) !Result { const connection = try self.connectManaged(); const caller_info = try jetquery.debug.getCallerInfo(@returnAddress()); return try connection.executeSql(self.io, sql, values, caller_info, self); }
pub const BindConnectOptions = struct { allocator: ?Allocator = null, };
/// Return a new Repo bound to a connection. e.g. when a database connection is needed /// per request in a web server, use this function to create a new Repo and deinit after /// the request has processed to free the connection back to the pool. pub fn bindConnect(self: *AdaptedRepo, options: BindConnectOptions) !AdaptedRepo { if (self.adapter.* == .null) return self.*;
return .{ .io = self.io, .allocator = options.allocator orelse self.allocator, .adapter = self.adapter, .eventCallback = self.eventCallback, .context = self.context, .connection = try self.connect(), .child_debug_mutex = &self.debug_mutex, }; }
/// Establish a new connection. Caller is responsible for calling `connection.release()` when /// connection can be returned to the pool. Only use this function if a new connection is /// specifically required, otherwise use `repo.execute()` which will automatically assign a /// connection and release it on `repo.deinit()`. /// /// The returned `Connection` implements `execute`, `executeSql`, and `executeVoid` to provide /// parity with the same member functions in `Repo`. Use `execute` to execute a generated query, /// use `executeSql` and `executeVoid` to execute SQL and a values tuple (e.g. when using /// handwritten SQL statements). pub fn connect(self: *AdaptedRepo) !jetquery.Connection { return try self.adapter.connect(.{ .context = self.context }); }
/// Used internally to create a managed connection which is released on `repo.deinit()`. pub fn connectManaged(self: *AdaptedRepo) !jetquery.Connection { return if (self.connection) |connection| connection else blk: { const connection = try self.connect(); self.connection = connection; break :blk connection; }; }
/// Release the repo's connection to the pool. If no connection is currently acquired then this /// is a no-op. pub fn release(self: *AdaptedRepo) void { if (self.connection) |connection| { defer self.connection = null; connection.release(); } }
pub fn executeInternal( self: *AdaptedRepo, query: anytype, caller_info: ?jetquery.debug.CallerInfo, ) !switch (@TypeOf(query).ResultContext) { .one => ?@TypeOf(query).ResultType, .many => Result, .none => void, } { try query.validate();
const connection = try self.connectManaged();
return try connection.execute(self.io, query, caller_info, self); }
pub fn begin(self: *AdaptedRepo) !void { const connection = try self.connectManaged(); try connection.executeVoid( self.io, "BEGIN", .{}, try jetquery.debug.getCallerInfo(@returnAddress()), self, ); }
pub fn commit(self: *AdaptedRepo) !void { const connection = try self.connectManaged(); try connection.executeVoid( self.io, "COMMIT", .{}, try jetquery.debug.getCallerInfo(@returnAddress()), self, ); }
pub fn rollback(self: *AdaptedRepo) !void { const connection = try self.connectManaged(); try connection.executeVoid( self.io, "ROLLBACK", .{}, try jetquery.debug.getCallerInfo(@returnAddress()), self, ); }
/// Execute a query and return all of its results. Call `repo.free(result)` to free /// allocated memory. pub fn all(self: *AdaptedRepo, query: anytype) ![]@TypeOf(query).ResultType { var result = try self.executeInternal( query, try jetquery.debug.getCallerInfo(@returnAddress()), ); return try result.all(query); }
pub fn save(self: *AdaptedRepo, record: anytype) !void { const value = switch (@typeInfo(@TypeOf(record))) { .pointer => record.*, else => record, };
// Unfortunately we need to make an exception to comptime SQL generation here as we // only want to update columns where the value has been modified, so the SQL cannot // be known at comptime. We use `Connection.executeVoidRuntimeBind` to achieve this, // which pretty much exists solely for this use case. if (!isModified(value)) return;
const primary_key = value.__jetquery_model.primary_key;
comptime var size: usize = 0; comptime { for (jetquery.fields.structFields(@TypeOf(value))) |field| { const is_primary_key = std.mem.eql(u8, field.name, primary_key); if (!std.mem.startsWith(u8, field.name, "__") and !is_primary_key) { size += 1; } } } comptime var fields: [size]jetquery.fields.StructField = undefined; comptime { var index: usize = 0; for (jetquery.fields.structFields(@TypeOf(value))) |field| { const is_internal_field = std.mem.startsWith(u8, field.name, "__"); if (!is_internal_field and !std.mem.eql(u8, field.name, primary_key)) { fields[index] = field; index += 1; } } }
// We include ALL fields here, but `Connection.executeVoidRuntimeBind` receives a // slice of `jetquery.sql.FieldState` (provided by `Repo.fieldStates`) and only binds // values that have been modified. const Update = jetquery.fields.structType(&fields);
var update: Update = undefined;
inline for (jetquery.fields.structFields(Update)) |field| { @field(update, field.name) = @field(value, field.name); }
if (@hasField(Update, "updated_at")) { update.updated_at = jetquery.DateTime.now(self.io); }
const primary_key_field = jetquery.fields.structField( primary_key, @TypeOf(@field(value, primary_key)), );
const PrimaryKeyArgs = jetquery.fields.structType(&.{primary_key_field}); var primary_key_args: PrimaryKeyArgs = undefined; @field(primary_key_args, primary_key) = @field(value, primary_key);
// We generate the where clause at comptime. Note that, since we generate this first, // the first bind param is the primary key arg, i.e. `WHERE "id" = $1`. // `Connection.executeRuntimeBind` expects a tuple of values (in our case, the tuple // has only one value) and an args struct, which contains the values to update, using // the update statement we render at runtime. We iterate over the args in the struct // and bind only the modified values. The values tuple is bound first, since its bind // param is indexed first. const query = jetquery.Query( adapter_name, value.__jetquery_schema, value.__jetquery_model, ).where(primary_key_args);
const field_states = fieldStates(value);
const sql = try jetquery.sql.renderUpdateRuntime( self.allocator, Adapter, value.__jetquery_model, @TypeOf(query).where_clauses, Update, &field_states, query.field_infos.len, ); defer self.allocator.free(sql);
const connection = try self.connectManaged(); try connection.executeVoidRuntimeBind( self.io, sql, .{@field(value, primary_key)}, Update, update, &field_states, try jetquery.debug.getCallerInfo(@returnAddress()), self, ); }
/// Insert a record into the database. /// ```zig /// try repo.insert(.Cat, .{ .name = "Hercules", .paws = 4 }); /// ``` pub fn insert( self: *AdaptedRepo, comptime model_name: std.meta.DeclEnum(Schema), args: anytype, ) !void { const query = jetquery.Query( adapter_name, Schema, @field(Schema, @tagName(model_name)), ).insert(args);
try self.executeInternal(query, try jetquery.debug.getCallerInfo(@returnAddress())); }
/// Delete a fetched record from the database. Record must have a primary key. /// ```zig /// const maybe_cat = try Repo.Query(.Cat) /// .findBy(.{ .name = "Heracles" }) /// .execute(repo); /// if (maybe_cat) |cat| try repo.delete(cat); /// ``` pub fn delete(self: *AdaptedRepo, value: anytype) !void { const primary_key_field_name = value.__jetquery_model.primary_key; const Args = jetquery.fields.structType( &.{jetquery.fields.structField( primary_key_field_name, jetquery.fields.fieldType(@TypeOf(value), primary_key_field_name), )}, ); var args: Args = undefined; @field(args, primary_key_field_name) = @field(value, primary_key_field_name);
const query = jetquery.Query( adapter_name, value.__jetquery_schema, value.__jetquery_model, ).delete().where(args); try self.executeInternal(query, try jetquery.debug.getCallerInfo(@returnAddress())); }
pub fn isModified(value: anytype) bool { const field_states = fieldStates(value); for (field_states) |field_state| { if (field_state.modified) return true; } return false; }
pub fn fieldStates( value: anytype, ) [fieldStatesSize(@TypeOf(value))]jetquery.sql.FieldState { var field_state: [fieldStatesSize(@TypeOf(value))]jetquery.sql.FieldState = undefined; var index: usize = 0;
const originals = value.__jetquery.original_values; inline for (jetquery.fields.structFields(@TypeOf(originals))) |field| { const modified = if (field.type == jetquery.DateTime) @field(originals, field.name).eql(@field(value, field.name)) else switch (@typeInfo(field.type)) { .pointer => |info| std.mem.eql( info.child, @field(originals, field.name), @field(value, field.name), ), else => @field(originals, field.name) == @field(value, field.name), };
field_state[index] = .{ .name = field.name, .modified = modified }; index += 1; } return field_state; }
fn fieldStatesSize(T: type) usize { comptime { const t: T = undefined; return jetquery.fields.structFields(@TypeOf(t.__jetquery.original_values)).len; } }
/// Free a result's allocated memory. Supports flexible inputs and can be used in conjunction /// with `all()` and `findBy()` etc.: /// ```zig /// pub fn free(self: *AdaptedRepo, value: anytype) void { switch (@typeInfo(@TypeOf(value))) { .pointer => |info| switch (info.size) { .slice => { for (value) |item| self.free(item); self.allocator.free(value); }, else => {}, }, .optional => { if (value) |capture| return self.free(capture) else return; }, else => {}, }
if (!comptime @hasField(@TypeOf(value), "__jetquery")) return;
self._freeRow(value);
inline for (value.__jetquery_relation_names) |relation_name| { switch (@typeInfo(@TypeOf(@field(value, relation_name)))) { // belongs_to .@"struct" => self.free(@field(value, relation_name)), // has_many .pointer => { for (@field(value, relation_name)) |item| self.free(item); self.allocator.free(@field(value, relation_name)); }, else => {}, } } }
/// Create a database table named `nme`. Pass `.{ .if_not_exists = true }` to use /// `CREATE TABLE IF NOT EXISTS` syntax. pub fn createTable( self: *AdaptedRepo, comptime name: []const u8, comptime columns: []const jetquery.schema.Column, comptime options: jetquery.CreateTableOptions, ) !void { var buf: Writer.Allocating = .init(self.allocator); defer buf.deinit();
const writer = &buf.writer;
try writer.print( \\CREATE TABLE{s} {s} ( , .{ if (options.if_not_exists) " IF NOT EXISTS" else "", self.adapter.identifier(name) });
inline for (columns, 0..) |column, index| { try self.adapter.writeAddColumnSql(column, writer); if (index < columns.len - 1) try writer.print(", ", .{}); }
try writer.print(")", .{}); const connection = try self.connectManaged(); try connection.executeVoid( self.io, buf.writer.buffered(), &.{}, try jetquery.debug.getCallerInfo(@returnAddress()), self, );
try self.createIndexes( name, columns, .{ .if_not_exists = options.if_not_exists }, ); }
/// Drop a database table named `name`. Pass `.{ .if_exists = true }` to use /// `DROP TABLE IF EXISTS` syntax. pub fn dropTable( self: *AdaptedRepo, comptime name: []const u8, comptime options: jetquery.DropTableOptions, ) !void { var buf: Writer.Allocating = .init(self.allocator); defer buf.deinit();
const writer = &buf.writer;
try writer.print( \\DROP TABLE{s} {s} , .{ if (options.if_exists) " IF EXISTS" else "", self.adapter.identifier(name) });
const connection = try self.connectManaged(); try connection.executeVoid( self.io, buf.writer.buffered(), &.{}, try jetquery.debug.getCallerInfo(@returnAddress()), self, ); }
/// Alter a database table. Rename table or add, drop, modify, or rename columns. pub fn alterTable( self: *AdaptedRepo, comptime name: []const u8, comptime options: jetquery.AlterTableOptions, ) !void { var buf: Writer.Allocating = .init(self.allocator); defer buf.deinit(); const writer = &buf.writer;
// XXX: We don't prevent a user from generating an incoherent `ALTER TABLE` // statement, e.g. with multiple column renames or adding columns and renaming the // table. This allows the database to produce an informative error in such cases, // instead of us needing to have knowledge of all database backends' semantics (which // may change over time).
try writer.print( \\ALTER TABLE {s} , .{self.adapter.identifier(name)});
inline for (options.columns.add, 0..) |column, index| { try writer.print(" ADD COLUMN ", .{}); try self.adapter.writeAddColumnSql(column, writer); if (index < options.columns.add.len - 1) try writer.print(",", .{}); }
if (options.columns.rename) |rename_column| { try writer.print(" RENAME {s} TO {s}", .{ self.adapter.identifier(rename_column.from), self.adapter.identifier(rename_column.to), }); }
inline for (options.columns.drop, 0..) |column_name, index| { try writer.print(" DROP COLUMN {s}{s}", .{ self.adapter.identifier(column_name), if (index + 1 < options.columns.drop.len) "," else "", }); }
if (options.rename) |rename_table| { try writer.print(" RENAME TO {s}", .{self.adapter.identifier(rename_table)}); }
const connection = try self.connectManaged(); try connection.executeVoid( self.io, buf.writer.buffered(), &.{}, try jetquery.debug.getCallerInfo(@returnAddress()), self, );
try self.createIndexes(options.rename orelse name, options.columns.add, .{}); }
/// Create a new database in the current repo. Repo must be initialized with the appropriate user /// credentials for creating new databases. pub fn createDatabase( self: *AdaptedRepo, name: []const u8, options: struct {}, ) !void { _ = options; var buf: Writer.Allocating = .init(self.allocator); defer buf.deinit(); const database = try self.adapter.identifierAlloc(self.allocator, name); defer self.allocator.free(database);
const writer = &buf.writer;
try writer.print( \\CREATE DATABASE {s} , .{database});
const connection = try self.connectManaged(); try connection.executeVoid( self.io, buf.writer.buffered(), &.{}, try jetquery.debug.getCallerInfo(@returnAddress()), self, ); }
/// Create a new database in the current repo. Repo must be initialized with the appropriate user /// credentials for creating new databases. pub fn dropDatabase( self: *AdaptedRepo, name: []const u8, options: jetquery.DropDatabaseOptions, ) !void { var buf: Writer.Allocating = .init(self.allocator); defer buf.deinit(); const database = try self.adapter.identifierAlloc(self.allocator, name); defer self.allocator.free(database);
const writer = &buf.writer;
try writer.print( \\DROP DATABASE{s} {s} , .{ if (options.if_exists) " IF EXISTS" else "", database, });
const connection = try self.connectManaged(); try connection.executeVoid( self.io, buf.writer.buffered(), &.{}, try jetquery.debug.getCallerInfo(@returnAddress()), self, ); }
/// Create an index on the specified table name and column names. Optionally pass] /// `.{ .unique = true }` to create a unique constraint on the index. pub fn createIndex( self: *AdaptedRepo, comptime table_name: []const u8, comptime column_names: []const []const u8, comptime options: jetquery.CreateIndexOptions, ) !void { const index_name = comptime options.name orelse Adapter.indexName( table_name, column_names, ); const sql = comptime Adapter.createIndexSql( index_name, table_name, column_names, options, ); const connection = try self.connectManaged(); try connection.executeVoid( self.io, sql, .{}, try jetquery.debug.getCallerInfo(@returnAddress()), self, ); }
// Internally used by `createTable` - use `createIndex` to create a single index. fn createIndexes( self: *AdaptedRepo, comptime name: []const u8, comptime columns: []const jetquery.schema.Column, comptime options: jetquery.CreateIndexOptions, ) !void { inline for (columns) |column| { if (comptime !column.options.index) continue; try self.createIndex( name, &.{column.name}, .{ .name = column.options.index_name, .if_not_exists = options.if_not_exists }, ); }
inline for (columns) |column| { if (comptime column.timestamps) |timestamps| { if (timestamps.created_at) { try self.createIndex( name, &.{jetquery.default_column_names.created_at}, .{ .if_not_exists = options.if_not_exists }, ); } if (timestamps.updated_at) { try self.createIndex( name, &.{jetquery.default_column_names.updated_at}, .{ .if_not_exists = options.if_not_exists }, ); } } } }
pub fn debugMutex(self: *AdaptedRepo) *Mutex { return self.child_debug_mutex orelse &self.debug_mutex; }
/// Use `Repo.free()` to free a row. This function is for internal use only. pub fn _freeRow(self: *AdaptedRepo, value: anytype) void { // Value may be modified after fetching so we only free the original value. If user // messes with internals and modifies original values then they will run into // trouble. inline for (jetquery.fields.structFields(@TypeOf(value.__jetquery.original_values))) |field| { self._freeValue(field.name, field.type, value); } }
fn _freeValue( self: *AdaptedRepo, comptime field_name: []const u8, T: type, value: anytype, ) void { switch (@typeInfo(T)) { .pointer => |info| { switch (info.child) { // TODO: Couple this with `maybeDupe` logic to make sure we stay // consistent in which types need to be freed. u8 => { self.allocator.free(@field( value.__jetquery.original_values, field_name, )); }, else => {}, } }, .optional => |info| { if (@field(value.__jetquery.original_values, field_name)) |capture| { switch (@typeInfo(info.child)) { .pointer => |pointer| switch (pointer.child) { u8 => { self.allocator.free(capture); }, else => {}, }, else => {}, } } }, .@"struct" => self.free(@field(value, field_name)), else => {}, } } };}
test "Repo" { try resetDatabase();
const Schema = struct { pub const Cat = jetquery.Model( @This(), "cats", struct { name: []const u8, paws: i32 }, .{}, ); };
var repo = try Repo(.postgresql, Schema).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer repo.deinit();
try repo.createTable( "cats", &.{ jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column("paws", .integer, .{}), }, .{ .if_not_exists = true }, );
try repo.Query(.Cat).insert(.{ .name = "Hercules", .paws = 4 }).execute(&repo); try repo.Query(.Cat).insert(.{ .name = "Princes", .paws = 4 }).execute(&repo);
const coalesced_paws = jetquery.sql.column(i32, "coalesce(cats.paws, 3)").as(.coalesced_paws);
const query = repo.Query(.Cat) .select(.{ .name, .paws, coalesced_paws }) .where(.{ .paws = 4 });
const cats = try repo.all(query); defer repo.free(cats);
for (cats) |cat| { try std.testing.expectEqualStrings("Hercules", cat.name); try std.testing.expectEqual(4, cat.paws); try std.testing.expectEqual(4, cat.coalesced_paws); break; } else { try std.testing.expect(false); }
const count_all = try query.count().execute(&repo); try std.testing.expectEqual(2, count_all);
const count_distinct = try repo.Query(.Cat) .distinct(.{.paws}) .count() .execute(&repo); try std.testing.expectEqual(1, count_distinct);}
test "Repo.loadConfig" { try resetDatabase();
// Loads default config file: `jetquery.config.zig` var repo = try Repo(.postgresql, void).loadConfig(std.testing.io, std.testing.allocator, .testing, .{}); defer repo.deinit(); try std.testing.expect(repo.adapter.* == .postgresql);}
test "Repo.loadConfig with env" { try resetDatabase();
var repo = try Repo(.postgresql, void).loadConfig( std.testing.io, std.testing.allocator, .testing, .{ .lazy_connect = true, .env = .{ .database = "database_from_env", .password = "password_from_env", }, }, ); defer repo.deinit(); try std.testing.expect(repo.adapter.* == .postgresql); try std.testing.expectEqualStrings( repo.adapter.postgresql.options.database.?, "database_from_env", ); try std.testing.expectEqualStrings( repo.adapter.postgresql.options.password.?, "password_from_env", ); // Testing fallback to config file: try std.testing.expectEqualStrings( repo.adapter.postgresql.options.hostname.?, "127.0.0.1", ); try std.testing.expectEqual( repo.adapter.postgresql.options.port.?, 5432, ); try std.testing.expectEqualStrings( repo.adapter.postgresql.options.username.?, "postgres", );}
test "relations" { try resetDatabase();
const Schema = struct { pub const Cat = jetquery.Model( @This(), "cats", struct { id: i32, human_id: i32, name: []const u8, paws: i32 }, .{ .relations = .{ .human = jetquery.relation.belongsTo(.Human, .{}) } }, );
pub const Human = jetquery.Model( @This(), "humans", struct { id: i32, name: []const u8 }, .{ .relations = .{ .cats = jetquery.relation.hasMany(.Cat, .{}) } }, ); };
var repo = try Repo(.postgresql, Schema).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer repo.deinit();
try repo.createTable( "cats", &.{ jetquery.schema.table.column("id", .integer, .{}), jetquery.schema.table.column("human_id", .integer, .{}), jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column("paws", .integer, .{}), }, .{ .if_not_exists = true }, );
try repo.createTable( "humans", &.{ jetquery.schema.table.column("id", .integer, .{}), jetquery.schema.table.column("name", .string, .{}), }, .{ .if_not_exists = true }, );
try repo.Query(.Human) .insert(.{ .id = 1, .name = "Bob" }) .execute(&repo); try repo.Query(.Cat) .insert(.{ .id = 1, .name = "Hercules", .paws = 4, .human_id = 1 }) .execute(&repo);
const query = repo.Query(.Cat) .include(.human, .{}) .findBy(.{ .name = "Hercules" });
const cat = try query.execute(&repo) orelse return try std.testing.expect(false); defer repo.free(cat);
try std.testing.expectEqualStrings("Hercules", cat.name); try std.testing.expectEqual(4, cat.paws); try std.testing.expectEqualStrings("Bob", cat.human.name);
const bob = try repo.Query(.Human) .include(.cats, .{}) .findBy(.{ .name = "Bob" }) .execute(&repo) orelse return try std.testing.expect(false); defer repo.free(bob);
try std.testing.expectEqualStrings("Hercules", bob.cats[0].name);
try repo.insert(.Cat, .{ .id = 2, .name = "Princes", .paws = blk: { var b: [1]u8 = undefined; std.testing.io.random(&b); break :blk @as(u3, @truncate(b[0])); }, .human_id = bob.id, }); try repo.insert(.Cat, .{ .id = 3, .name = "Heracles", .paws = blk: { var b: [1]u8 = undefined; std.testing.io.random(&b); break :blk @as(u3, @truncate(b[0])); }, .human_id = 1000, });
const bob_with_more_cats = try repo.Query(.Human) .include(.cats, .{}) .findBy(.{ .name = "Bob" }) .execute(&repo) orelse return try std.testing.expect(false); defer repo.free(bob_with_more_cats);
try std.testing.expect(bob_with_more_cats.cats.len == 2); try std.testing.expectEqualStrings("Hercules", bob_with_more_cats.cats[0].name); try std.testing.expectEqualStrings("Princes", bob_with_more_cats.cats[1].name);
try repo.insert(.Human, .{ .id = 2, .name = "Jane", });
const jane = try repo.Query(.Human) .include(.cats, .{}) .findBy(.{ .name = "Jane" }) .execute(&repo) orelse return try std.testing.expect(false); defer repo.free(jane);
try std.testing.expect(jane.cats.len == 0);
try repo.insert(.Cat, .{ .id = 4, .human_id = jane.id, .name = "Cindy", .paws = blk: { var b: [1]u8 = undefined; std.testing.io.random(&b); break :blk @as(u3, @truncate(b[0])); }, });
try repo.insert(.Cat, .{ .id = 5, .human_id = jane.id, .name = "Garfield", .paws = blk: { var b: [1]u8 = undefined; std.testing.io.random(&b); break :blk @as(u3, @truncate(b[0])); }, });
try repo.insert(.Cat, .{ .id = 6, .human_id = jane.id, .name = "Felix", .paws = blk: { var b: [1]u8 = undefined; std.testing.io.random(&b); break :blk @as(u3, @truncate(b[0])); }, });
const humans = try repo.Query(.Human).include(.cats, .{}).orderBy(.name).all(&repo); defer repo.free(humans);
try std.testing.expect(humans.len == 2); try std.testing.expect(humans[0].cats.len == 2); try std.testing.expect(humans[1].cats.len == 3); try std.testing.expectEqualStrings("Bob", humans[0].name); try std.testing.expectEqualStrings("Jane", humans[1].name); try std.testing.expectEqualStrings("Hercules", humans[0].cats[0].name); try std.testing.expectEqualStrings("Princes", humans[0].cats[1].name); try std.testing.expectEqualStrings("Jane", humans[1].name); try std.testing.expectEqualStrings("Cindy", humans[1].cats[0].name); try std.testing.expectEqualStrings("Garfield", humans[1].cats[1].name); try std.testing.expectEqualStrings("Felix", humans[1].cats[2].name);
const jane_two_cats = try repo.Query(.Human).findBy(.{ .name = "Jane" }) .include(.cats, .{ .limit = 2, .order_by = .name }) .execute(&repo); defer repo.free(jane_two_cats); try std.testing.expect(jane_two_cats.?.cats.len == 2); try std.testing.expectEqualStrings("Cindy", jane_two_cats.?.cats[0].name); try std.testing.expectEqualStrings("Felix", jane_two_cats.?.cats[1].name);
const iterating_query = repo.Query(.Human) .include(.cats, .{ .order_by = .name }) .orderBy(.name); var humans_iterated = try iterating_query.execute(&repo); defer humans_iterated.deinit(); var index: usize = 0; while (try humans_iterated.next(iterating_query)) |human| : (index += 1) { defer repo.free(human); switch (index) { 0 => { try std.testing.expectEqualStrings("Bob", human.name); try std.testing.expectEqualStrings("Hercules", human.cats[0].name); try std.testing.expectEqualStrings("Princes", human.cats[1].name); }, 1 => { try std.testing.expectEqualStrings("Jane", human.name); try std.testing.expectEqualStrings("Cindy", human.cats[0].name); try std.testing.expectEqualStrings("Felix", human.cats[1].name); try std.testing.expectEqualStrings("Garfield", human.cats[2].name); }, else => try std.testing.expect(false), } }
const lila = try repo.Query(.Human) .insert(.{ .id = 20, .name = "Lila" }) .returning(.{.id}) .execute(&repo) orelse return try std.testing.expect(false); defer repo.free(lila); try std.testing.expectEqual(20, lila.id); const fluffy = try repo.Query(.Cat) .insert(.{ .id = lila.id, .name = "Fluffy", .paws = 4, .human_id = 1 }) .returning(.{}) .execute(&repo) orelse return try std.testing.expect(false); defer repo.free(fluffy); try std.testing.expectEqual(20, fluffy.id); try std.testing.expectEqual(4, fluffy.paws);}
test "timestamps" { try resetDatabase();
const Schema = struct { pub const Cat = jetquery.Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: jetquery.DateTime, updated_at: jetquery.DateTime, }, .{}, ); };
var repo = try Repo(.postgresql, Schema).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer repo.deinit();
try repo.createTable( "cats", &.{ jetquery.schema.table.primaryKey("id", .{}), jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column("paws", .integer, .{}), jetquery.schema.table.column("created_at", .datetime, .{}), jetquery.schema.table.column("updated_at", .datetime, .{}), }, .{}, );
const before_us = Io.Clock.now(.real, repo.io).toMicroseconds(); try std.Io.sleep(repo.io, Io.Duration.fromMilliseconds(1), .real);
try repo.Query(.Cat).insert(.{ .name = "Hercules", .paws = 4 }).execute(&repo);
const maybe_cat = try repo.Query(.Cat) .findBy(.{ .name = "Hercules" }) .execute(&repo);
if (maybe_cat) |cat| { defer repo.free(cat); try std.testing.expect(cat.created_at.toMicroseconds() > before_us); }}
test "save" { try resetDatabase();
const Schema = struct { pub const Cat = jetquery.Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}, ); };
var repo = try Repo(.postgresql, Schema).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer repo.deinit();
try repo.createTable("cats", &.{ jetquery.schema.table.column("id", .integer, .{}), jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column("paws", .integer, .{}), }, .{ .if_not_exists = true });
try repo.Query(.Cat) .insert(.{ .id = 1000, .name = "Hercules", .paws = 4 }) .execute(&repo);
var maybe_cat = try repo.Query(.Cat) .findBy(.{ .name = "Hercules" }) .execute(&repo); defer repo.free(maybe_cat); if (maybe_cat) |*cat| { cat.name = "Princes"; try repo.save(cat); }
const updated_cat = try repo.Query(.Cat) .find(1000) .execute(&repo) orelse return std.testing.expect(false); defer repo.free(updated_cat); try std.testing.expectEqualStrings("Princes", updated_cat.name);}
test "aggregate max()" { try resetDatabase();
const Schema = struct { pub const Cat = jetquery.Model( @This(), "cats", struct { name: []const u8, paws: usize }, .{}, ); };
var repo = try Repo(.postgresql, Schema).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer repo.deinit();
try repo.createTable("cats", &.{ jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column("paws", .integer, .{}), }, .{ .if_not_exists = true });
const sql = jetquery.sql;
try repo.insert(.Cat, .{ .name = "Hercules", .paws = 2 }); try repo.insert(.Cat, .{ .name = "Hercules", .paws = 8 }); try repo.insert(.Cat, .{ .name = "Hercules", .paws = 4 }); try repo.insert(.Cat, .{ .name = "Princes", .paws = 100 }); try repo.insert(.Cat, .{ .name = "Princes", .paws = 5 }); try repo.insert(.Cat, .{ .name = "Princes", .paws = 2 });
const cats = try repo.Query(.Cat) .select(.{ .name, sql.max(.paws) }) .groupBy(.{.name}) .orderBy(.{.name}) .all(&repo); defer repo.free(cats);
try std.testing.expectEqualStrings(cats[0].name, "Hercules"); try std.testing.expect(cats[0].max__paws == 8); try std.testing.expectEqualStrings(cats[1].name, "Princes"); try std.testing.expect(cats[1].max__paws == 100);}
test "aggregate count() with HAVING" { try resetDatabase();
const Schema = struct { pub const Cat = jetquery.Model( @This(), "cats", struct { name: []const u8, paws: usize }, .{}, ); };
var repo = try Repo(.postgresql, Schema).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer repo.deinit();
try repo.createTable("cats", &.{ jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column("paws", .integer, .{}), }, .{ .if_not_exists = true });
try repo.insert(.Cat, .{ .name = "Hercules", .paws = 2 }); try repo.insert(.Cat, .{ .name = "Hercules", .paws = 8 }); try repo.insert(.Cat, .{ .name = "Hercules", .paws = 4 }); try repo.insert(.Cat, .{ .name = "Princes", .paws = 100 }); try repo.insert(.Cat, .{ .name = "Princes", .paws = 5 }); try repo.insert(.Cat, .{ .name = "Princes", .paws = 2 });
const sql = jetquery.sql;
const cats = try repo.Query(.Cat) .select(.{ .name, sql.max(.paws).as("maximum_paws") }) .where(.{ .{ .name = "Hercules" }, .OR, .{ .name, .like, "Pri%" } }) .groupBy(.{.name}) .having(.{ .{ sql.count(.name), .gt_eql, "3" }, .OR, .{ sql.count(.name), .lt_eql, 3 } }) .orderBy(.{.name}) .all(&repo); defer repo.free(cats);
try std.testing.expectEqualStrings(cats[0].name, "Hercules"); try std.testing.expect(cats[0].maximum_paws == 8); try std.testing.expectEqualStrings(cats[1].name, "Princes"); try std.testing.expect(cats[1].maximum_paws == 100);}
test "missing config options" { try resetDatabase();
const result = Repo(.postgresql, struct {}).init(std.testing.io, std.testing.allocator, .{ .adapter = .{} }); try std.testing.expect(result == error.JetQueryConfigError);}
test "transactions" { try resetDatabase();
const Schema = struct { pub const Cat = jetquery.Model( @This(), "cats", struct { name: []const u8, paws: i32 }, .{}, ); };
var repo = try Repo(.postgresql, Schema).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer repo.deinit();
try repo.createTable("cats", &.{ jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column("paws", .integer, .{}), }, .{ .if_not_exists = true });
try repo.begin(); try repo.insert(.Cat, .{ .name = "Hercules", .paws = 4 }); try repo.rollback();
const no_cat = try repo.Query(.Cat) .findBy(.{ .name = "Hercules" }) .execute(&repo); defer repo.free(no_cat); try std.testing.expect(no_cat == null);
try repo.begin(); const waited = try repo.Query(.Cat) .insert(.{ .name = "Waiting", .paws = 3 }) .returning(.{.name}) .execute(&repo) orelse return try std.testing.expect(false); defer repo.free(waited); try repo.rollback(); try std.testing.expect(std.mem.eql(u8, waited.name, "Waiting"));
try repo.begin(); try repo.insert(.Cat, .{ .name = "Hercules", .paws = 4 }); try repo.commit();
const yes_cat = try repo.Query(.Cat) .findBy(.{ .name = "Hercules" }) .execute(&repo); defer repo.free(yes_cat); try std.testing.expect(yes_cat != null);}
test "alterTable" { try resetDatabase(); const Schema = struct { pub const Cat = jetquery.Model( @This(), "cats", struct { name: []const u8, paws: i32 }, .{}, ); pub const Dog = jetquery.Model( @This(), "dogs", struct { name: []const u8, identifier: []const u8, paws: i32 }, .{}, ); };
var repo = try Repo(.postgresql, Schema).init(std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }); defer repo.deinit();
try repo.createTable("cats", &.{}, .{});
const result1 = repo.Query(.Cat).select(.{ .name, .paws }).all(&repo); try std.testing.expect(result1 == error.PG);
try repo.alterTable("cats", .{ .columns = .{ .add = &.{ jetquery.schema.table.column("name", .string, .{}), jetquery.schema.table.column("paws", .integer, .{ .index = true }), }, }, });
const cats = try repo.Query(.Cat).select(.{ .name, .paws }).all(&repo); try std.testing.expect(cats.len == 0); // Empty table but valid select columns.
const result2 = repo.Query(.Dog).select(.{ .name, .paws }).all(&repo); try std.testing.expect(result2 == error.PG);
try repo.alterTable("cats", .{ .rename = "dogs" });
const result3 = repo.Query(.Cat).select(.{ .name, .paws }).all(&repo); try std.testing.expect(result3 == error.PG);
const dogs = try repo.Query(.Dog).select(.{ .name, .paws }).all(&repo); try std.testing.expect(dogs.len == 0); // Empty table but valid select columns.
try repo.alterTable("dogs", .{ .columns = .{ .drop = &.{"paws"} } }); const result4 = repo.Query(.Dog).select(.{ .name, .paws }).all(&repo); try std.testing.expect(result4 == error.PG);
try repo.alterTable( "dogs", .{ .columns = .{ .rename = .{ .from = "name", .to = "identifier" } } }, ); const dogs2 = try repo.Query(.Dog).select(.{.identifier}).all(&repo); try std.testing.expect(dogs2.len == 0); // Empty table but valid select columns.}
test "optional DateTime" { try resetDatabase();
const Schema = struct { pub const Thing = jetquery.Model( @This(), "things", struct { a: ?jetquery.DateTime, b: ?jetquery.DateTime, }, .{}, ); }; var repo = try Repo(.postgresql, Schema).init(std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }); defer repo.deinit();
try repo.createTable("things", &.{ jetquery.schema.table.column("a", .datetime, .{ .optional = true }), jetquery.schema.table.column("b", .datetime, .{ .optional = true }), }, .{});
const now = jetquery.DateTime.now(repo.io);
try repo.insert(.Thing, .{ .a = now, .b = null, });
if (try repo.Query(.Thing).findBy(.{ .a = now }).execute(&repo)) |thing| { defer repo.free(thing); try std.testing.expect(thing.a != null); try std.testing.expectEqual(thing.a.?.toMicroseconds(), now.toMicroseconds()); try std.testing.expect(thing.b == null); } else try std.testing.expect(false);}
test "optionals" { try resetDatabase();
const Schema = struct { pub const Thing = jetquery.Model( @This(), "things", struct { a: ?bool, b: ?[]const u8, c: ?i32, d: ?i64, e: ?f32, f: ?f64, g: ?jetquery.DateTime, }, .{}, ); }; var repo = try Repo(.postgresql, Schema).init(std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "repo_test", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }); defer repo.deinit();
try repo.createTable("things", &.{ jetquery.schema.table.column("a", .boolean, .{ .optional = true }), jetquery.schema.table.column("b", .string, .{ .optional = true }), jetquery.schema.table.column("c", .integer, .{ .optional = true }), jetquery.schema.table.column("d", .bigint, .{ .optional = true }), jetquery.schema.table.column("e", .float, .{ .optional = true }), jetquery.schema.table.column("f", .double_precision, .{ .optional = true }), jetquery.schema.table.column("g", .datetime, .{ .optional = true }), }, .{});
const now = jetquery.DateTime.now(repo.io);
const insert1 = repo.Query(.Thing).insert(.{ .a = @as(?bool, null), .b = @as(?[]const u8, null), .c = @as(?i32, null), .d = @as(?i64, null), .e = @as(?f32, null), .f = @as(?f64, null), .g = @as(?jetquery.DateTime, null), }); const select1 = repo.Query(.Thing).where(.{ .a = @as(?bool, null), .b = @as(?[]const u8, null), .c = @as(?i32, null), .d = @as(?i64, null), .e = @as(?f32, null), .f = @as(?f64, null), .g = @as(?jetquery.DateTime, null), });
const insert2 = repo.Query(.Thing).insert(.{ .a = @as(?bool, true), .b = @as(?[]const u8, "foo"), .c = @as(?i32, 123), .d = @as(?i64, 456), .e = @as(?f32, 123.4), .f = @as(?f64, 567.8), .g = @as(?jetquery.DateTime, now), }); const select2 = repo.Query(.Thing).where(.{ .a = @as(?bool, true), .b = @as(?[]const u8, "foo"), .c = @as(?i32, 123), .d = @as(?i64, 456), .e = @as(?f32, 123.4), .f = @as(?f64, 567.8), .g = @as(?jetquery.DateTime, now), });
try repo.execute(insert1); try repo.execute(insert2);
const things1 = try repo.all(select1); defer repo.free(things1);
try std.testing.expect(things1[0].a == null); try std.testing.expect(things1[0].b == null); try std.testing.expect(things1[0].c == null); try std.testing.expect(things1[0].d == null); try std.testing.expect(things1[0].e == null); try std.testing.expect(things1[0].f == null); try std.testing.expect(things1[0].g == null);
const things2 = try repo.all(select2); defer repo.free(things2);
try std.testing.expect(things2[0].a.? == true); try std.testing.expectEqualStrings(things2[0].b.?, "foo"); try std.testing.expect(things2[0].c.? == 123); try std.testing.expect(things2[0].d.? == 456); try std.testing.expect(things2[0].e.? == 123.4); try std.testing.expect(things2[0].f.? == 567.8); try std.testing.expectEqual(things2[0].g.?.toMicroseconds(), now.toMicroseconds());}
fn resetDatabase() !void { var repo = try Repo(.postgresql, void).init( std.testing.io, std.testing.allocator, .{ .adapter = .{ .database = "postgres", .username = "postgres", .hostname = "127.0.0.1", .password = "password", .port = 5432, }, }, ); defer repo.deinit(); try repo.dropDatabase("repo_test", .{ .if_exists = true }); try repo.createDatabase("repo_test", .{});}