Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458const std = @import("std");
const jetquery = @import("../root.zig");
const coercion = @import("coercion.zig");const sql = @import("sql.zig");
// Number of rows expected to be returned by a query.const ResultContext = enum { one, many, none };
/// Create a new query by passing a table definition./// ```zig/// const query = Query(.postgresql, Schema, .Cat);/// ```////// For convenience, use `repo.Query(.Cat)`.pub fn Query(adapter: jetquery.adapters.Name, Schema: type, comptime model: anytype) type { const Adapter = jetquery.adapters.Type(adapter); const Model = switch (@typeInfo(@TypeOf(model))) { .enum_literal => @field(Schema, @tagName(model)), else => switch (@TypeOf(model)) { type => model, []const u8 => @field(Schema, model), else => @compileError("Expected enum literal, []const u8, or type, found `" ++ @typeName(@TypeOf(model)) ++ "`"), }, };
@setEvalBranchQuota(10000); return struct { table: Model,
pub const Definition = Model.Definition; pub const info = .{ .Model = Model, .Schema = Schema, };
/// Create a `SELECT` query with the specified `columns`, e.g.: /// ```zig /// Query(.postgresql, Schema, .MyTable).select(.{ .foo, .bar }).where(.{ .foo = "qux" }); /// ``` /// Pass an empty `columns` array to select all columns: /// ```zig /// Query(.postgresql, Schema, .MyTable).select(.{}).where(.{ .foo = "qux" }); /// ``` pub fn select( comptime columns: anytype, ) @TypeOf(InitialStatement(Adapter, Schema, Model).select(columns)) { return InitialStatement(Adapter, Schema, Model).select(columns); }
/// Create a `SELECT` query defaulting to all columns selected: /// ```zig /// Query(.postgresql, Schema, .MyTable).where(.{ .foo = "bar" }) /// ``` /// Short-hand for: /// ```zig /// Query(.postgresql, Schema, .MyTable).select(.{}).where(.{ .foo = "bar" }) /// ``` pub fn where(args: anytype) @TypeOf(InitialStatement(Adapter, Schema, Model).where(args)) { return InitialStatement(Adapter, Schema, Model).where(args); }
/// Create an `UPDATE` query with the specified `args`. As a safety measure, an /// `update()` query **must** have a `.where()` clause attached or it will not be /// executed. Use `deleteAll()` if you wish to delete all records. /// ```zig /// Query(.postgresql, Schema, .MyTable).update(.{ .foo = "bar", .baz = "qux" }).where(.{ .quux = "corge" }); /// ``` pub fn update(args: anytype) @TypeOf(InitialStatement(Adapter, Schema, Model).update(args)) { return InitialStatement(Adapter, Schema, Model).update(args); }
/// Create an `UPDATE` query that does not require a `WHERE` clause to update all records /// in a table. /// ```zig /// Query(.postgresql, Schema, .MyTable).updateAll(.{ .foo = "bar", .baz = "qux" }); /// ``` pub fn updateAll(args: anytype) @TypeOf(InitialStatement(Adapter, Schema, Model).updateAll(args)) { return InitialStatement(Adapter, Schema, Model).updateAll(args); } /// Create an `INSERT` query with the specified `args`, e.g.: /// ```zig /// Query(.postgresql, Schema, .MyTable).insert(.{ .foo = "bar", .baz = "qux" }); /// ``` pub fn insert(args: anytype) @TypeOf(InitialStatement(Adapter, Schema, Model).insert(args)) { return InitialStatement(Adapter, Schema, Model).insert(args); }
pub fn returning(comptime columns: anytype) @TypeOf(InitialStatement(Adapter, Schema, Model).returning(columns)) { return InitialStatement(Adapter, Schema, Model).returning(columns); }
/// Create a `DELETE` query. As a safety measure, a `delete()` query **must** have a /// `.where()` clause attached or it will not be executed. Use `deleteAll()` if you wish /// to delete all records. /// ```zig /// Query(.postgresql, Schema, .MyTable).delete().where(.{ .foo = "bar" }); /// ``` pub fn delete() @TypeOf(InitialStatement(Adapter, Schema, Model).delete()) { return InitialStatement(Adapter, Schema, Model).delete(); }
/// Create a `DELETE` query that does not require a `WHERE` clause to delete all records /// from a table. /// ```zig /// Query(.postgresql, Schema, .MyTable).deleteAll(); /// ``` pub fn deleteAll() @TypeOf(InitialStatement(Adapter, Schema, Model).deleteAll()) { return InitialStatement(Adapter, Schema, Model).deleteAll(); }
/// Create a `SELECT` query to return a single row matching the given ID. /// ```zig /// Query(.postgresql, Schema, .MyTable).find(1000); /// ``` /// Short-hand for: /// ```zig /// Query(.postgresql, Schema, .MyTable).select(.{}).where(.{ .id = id }).limit(1); /// ``` pub fn find(id: anytype) @TypeOf(InitialStatement(Adapter, Schema, Model).find(id)) { return InitialStatement(Adapter, Schema, Model).find(id); }
/// Create a `SELECT` query to return a single row matching the given args. /// ```zig /// Query(.postgresql, Schema, .MyTable).findBy(.{ .foo = "bar", .baz = "qux" }); /// ``` /// Short-hand for: /// ```zig /// Query(.postgresql, Schema, .MyTable).select(.{}).where(args).limit(1); /// ``` pub fn findBy(args: anytype) @TypeOf(InitialStatement(Adapter, Schema, Model).findBy(args)) { return InitialStatement(Adapter, Schema, Model).findBy(args); }
/// Return all records from the current table. /// ```zig /// try Query(Schema, .MyTable).all(repo); /// ``` pub fn all( repo: anytype, ) @TypeOf(InitialStatement(Adapter, Schema, Model).select(.{}).all(repo)) { return InitialStatement(Adapter, Schema, Model).select(.{}).all(repo); }
/// Return the first record from the current table. /// ```zig /// try Query(Schema, .MyTable).first(repo); /// ``` pub fn first( repo: anytype, ) @TypeOf(InitialStatement(Adapter, Schema, Model).select(.{}).first(repo)) { return InitialStatement(Adapter, Schema, Model).select(.{}).first(repo); }
/// Indicate that a relation should be fetched with this query. Pass options to control /// the behaviour of the generated query. /// /// Select all columns and all rows of the association: /// ```zig /// Query(.postgresql, Schema, .MyTable).include(.my_relation, .{}); /// ``` /// Select specific columns: /// ```zig /// Query(.postgresql, Schema, .MyTable).include(.my_relation, .{ .select = .{ .foo, .bar } }); /// ``` /// Pass `limit` to limit the number of rows fetched for `hasMany` relations. This option /// is not supported for `belongsTo` relations. Note that the limit applies to the sum of /// all related records from the base result set so this option is only recommended when /// using `find` and `findBy`, e.g. fetch 1 blog post and 10 associated comments. /// ```zig /// Query(.postgresql, Schema, .MyTable).include(.my_relation, .{ .limit = 10 }); /// ``` pub fn include( comptime name: jetquery.relation.RelationsEnum(Model), comptime include_options: anytype, ) @TypeOf(InitialStatement(Adapter, Schema, Model).include(name, include_options)) { return InitialStatement(Adapter, Schema, Model).include(name, include_options); }
/// Join to another table by association name. Columns on the joined table are not /// included in the result set. Use `include` to fetch associations, use `join` to filter /// results. /// ```zig /// Query(.postgresql, Schema, .MyTable).join(.inner, .my_relation); /// ``` /// If required, call `.select` after a `.join` to specify association columns to select: /// ```zig /// Query(.postgresql, Schema, .MyTable).join(.inner, .my_relation) /// .select(.{ .foo, .my_relation = .{ .bar, .baz } }) /// ``` pub fn join( comptime join_context: jetquery.relation.JoinContext, comptime name: jetquery.relation.RelationsEnum(Model), ) @TypeOf(InitialStatement(Adapter, Schema, Model).join(join_context, name)) { return InitialStatement(Adapter, Schema, Model).join(join_context, name); }
/// Create a `SELECT DISTINCT` query with the specified `columns`, e.g.: /// ```zig /// Query(.postgresql, Schema, .MyTable).distinct(.{ .foo, .bar }).where(.{ .foo = "qux" }); /// ``` /// /// ```zig /// Query(.postgresql, Schema, .MyTable) /// .include(.my_relation) /// .distinct(.{ .foo, .{ .my_relation = .{.bar} }); /// ``` pub fn distinct( comptime columns: anytype, ) @TypeOf(InitialStatement(Adapter, Schema, Model).distinct(columns)) { return InitialStatement(Adapter, Schema, Model).distinct(columns); }
/// Apply a `GROUP BY` clause to the query, e.g.: /// ```zig /// Query(.postgresql, Schema, .MyTable).groupBy(.{ .foo, .{ .my_relation = .{.bar} }) /// ``` pub fn groupBy( comptime args: anytype, ) @TypeOf(InitialStatement(Adapter, Schema, Model).groupBy(args)) { return InitialStatement(Adapter, Schema, Model).groupBy(args); }
/// Apply an `ORDER BY` clause to the query, e.g.: /// ```zig /// Query(.postgresql, Schema, .MyTable).orderBy(.{.foo}); /// Query(.postgresql, Schema, .MyTable).orderBy(.{ .foo = .descending }); /// Query(.postgresql, Schema, .MyTable).orderBy(.{ .foo, .{ .my_relattion = .{.bar} } }); /// pub fn orderBy( comptime args: anytype, ) @TypeOf(InitialStatement(Adapter, Schema, Model).orderBy(args)) { return InitialStatement(Adapter, Schema, Model).orderBy(args); }
/// Select a `COUNT` from the result set. pub fn count() @TypeOf(InitialStatement(Adapter, Schema, Model).count()) { return InitialStatement(Adapter, Schema, Model).count(); } };}
const MissingField = struct { missing: void,};
fn InitialStatement( Adapter: type, Schema: type, Model: type,) Statement(Adapter, .none, Schema, Model, .{ .result_context = .none, .default_select = true,}) { return Statement(Adapter, .none, Schema, Model, .{ .result_context = .none, .default_select = true, }){ .values = .{}, .errors = .{} };}
fn SchemaTable(Schema: type, comptime name: std.meta.DeclEnum(Schema)) type { return @field(Schema, @tagName(name));}
fn StatementOptions(comptime query_context: sql.QueryContext) type { return struct { relations: []const type = &.{}, field_infos: []const jetquery.fields.FieldInfo = &.{}, columns: []const jetquery.columns.Column = &.{}, order_clauses: []const sql.OrderClause = &.{}, where_clauses: []const sql.Where.Tree = &.{}, result_context: ResultContext = defaultResultContext(query_context), default_select: bool = false, distinct: ?[]const jetquery.columns.Column = null, group_by: ?[]const jetquery.columns.Column = null, having_clauses: []const sql.Where.Tree = &.{},
pub fn orderClauses(self: @This(), Model: type) []const sql.OrderClause { if (query_context != .select) return self.order_clauses; if (self.order_clauses.len > 0) return self.order_clauses; // We could force ordering by one of the grouped columns but this seems like // overreaching - we should only apply a default order on ungrouped select queries. if (self.group_by != null) return self.order_clauses;
return Model.defaultOrderBy(); } };}
fn defaultResultContext(query_context: sql.QueryContext) ResultContext { return switch (query_context) { .select => .many, .update, .update_all, .insert, .delete, .delete_all, .none => .none, .count, .returning => .one, };}
pub const AuxiliaryQuery = struct { query: type, relation: type,
pub fn baseQuery(comptime self: AuxiliaryQuery) self.BaseQuery() { const q = self.query.select(.{}); const q_order = if (self.relation.order_by) |order_by| q._orderBy(order_by) else q;
const q_limit = if (self.relation.limit) |limit| q_order.limit(limit) else q_order;
return q_limit; }
fn BaseQuery(comptime self: AuxiliaryQuery) type { const q = self.query.select(.{}); const q_order = if (self.relation.order_by) |order_by| q._orderBy(order_by) else q;
const q_limit = if (self.relation.limit) |limit| q_order.limit(limit) else q_order;
return @TypeOf(q_limit); }};
fn Statement( Adapter: type, comptime query_context: sql.QueryContext, Schema: type, Model: type, comptime options: StatementOptions(query_context),) type { return struct { values: jetquery.fields.FieldValues(Model, options.relations, options.field_infos), limit_bound: ?u64 = null, errors: [options.field_infos.len]?anyerror,
comptime query_context: sql.QueryContext = query_context, comptime field_infos: []const jetquery.fields.FieldInfo = options.field_infos, comptime columns: []const jetquery.columns.Column = options.columns, comptime order_clauses: []const sql.OrderClause = options.order_clauses, comptime auxiliary_queries: []const AuxiliaryQuery = &auxiliaryQueries(), comptime sql: []const u8 = render(),
pub const info = .{ .Model = Model, .Schema = Schema, };
pub const Definition = Model.Definition; pub const ResultContext = options.result_context; pub const ResultType = QueryResultType(); pub const ColumnInfos = QueryColumnInfos(); pub const relations = options.relations; pub const where_clauses = options.where_clauses;
const Self = @This();
pub fn render() []const u8 { comptime { return jetquery.sql.render( Adapter, query_context, Model, options.relations, options.field_infos, options.columns, options.orderClauses(Model), options.distinct, options.where_clauses, options.group_by, options.having_clauses, ); } }
pub fn extend( self: Self, S: type, args: anytype, comptime context: jetquery.fields.FieldContext, ) S { validateQueryContext(self.query_context, query_context);
var statement: S = undefined;
inline for (0..self.field_infos.len) |index| { const value = self.values[index]; @field(statement.values, std.fmt.comptimePrint("{}", .{index})) = value; statement.errors[index] = self.errors[index]; }
@setEvalBranchQuota(10000); const tree = sql.Where.tree( Adapter, Model, options.relations, @TypeOf(args), context, self.field_infos.len, ); const clause_values = tree.values(Adapter, args);
const arg_values = clause_values.values; const arg_errors = clause_values.errors;
inline for (options.field_infos.len.., arg_values, arg_errors) |field_index, value, err| { @field(statement.values, std.fmt.comptimePrint("{d}", .{field_index})) = value;
statement.errors[field_index] = err; }
updateTimestamps(S, &statement, statement.query_context);
return statement; }
pub fn select( self: Self, comptime select_columns: anytype, ) Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = &jetquery.columns.translate(Model, options.relations, select_columns, .{}), .order_clauses = options.order_clauses, .where_clauses = options.where_clauses, .group_by = options.group_by, .distinct = options.distinct, .result_context = if (options.default_select) .many else options.result_context, }) { const S = Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = &jetquery.columns.translate(Model, options.relations, select_columns, .{}), .order_clauses = options.order_clauses, .where_clauses = options.where_clauses, .group_by = options.group_by, .distinct = options.distinct, .result_context = if (options.default_select) .many else options.result_context, }); return self.extend(S, .{}, .none); }
/// Apply a `WHERE` clause to the current statement. pub fn where(self: Self, args: anytype) Statement( Adapter, switch (query_context) { .none => .select, else => |tag| tag, }, Schema, Model, .{ .relations = options.relations, .where_clauses = options.where_clauses ++ .{ sql.Where.tree( Adapter, Model, options.relations, @TypeOf(args), .where, options.field_infos.len, ), }, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos( Adapter, Model, options.relations, @TypeOf(args), .where, ), .columns = if (options.default_select) &Model.columns() else options.columns, .order_clauses = options.order_clauses, .group_by = options.group_by, .distinct = options.distinct, .result_context = switch (options.result_context) { .none => switch (query_context) { .none => defaultResultContext(.select), else => |tag| defaultResultContext(tag), }, else => |tag| tag, }, .default_select = options.default_select, }, ) { const S = Statement(Adapter, switch (query_context) { .none => .select, else => |tag| tag, }, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos( Adapter, Model, options.relations, @TypeOf(args), .where, ), .where_clauses = options.where_clauses ++ .{sql.Where.tree( Adapter, Model, options.relations, @TypeOf(args), .where, options.field_infos.len, )}, .columns = if (options.default_select) &Model.columns() else options.columns, .order_clauses = options.order_clauses, .group_by = options.group_by, .distinct = options.distinct, .result_context = switch (options.result_context) { .none => switch (query_context) { .none => defaultResultContext(.select), else => |tag| defaultResultContext(tag), }, else => |tag| tag, }, .default_select = options.default_select, }); return self.extend(S, args, .where); }
pub fn find(self: Self, id: anytype) @TypeOf(self.findBy(.{ .id = id })) { // TODO: Use configured primary key instead of `id` // No need to verify `id` presence as `jetquery.fields.fieldInfos` will reject unknown fields. return self.findBy(.{ .id = id }); }
pub fn findBy(self: Self, args: anytype) Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos(Adapter, Model, options.relations, @TypeOf(args), .where) ++ jetquery.fields.fieldInfos(Adapter, Model, options.relations, @TypeOf(.{1}), .limit), .columns = if (options.columns.len == 0) &Model.columns() else options.columns, .where_clauses = &.{sql.Where.tree( Adapter, Model, &.{}, @TypeOf(args), .where, options.field_infos.len, )}, .group_by = options.group_by, .result_context = .one, }) { const S = Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos(Adapter, Model, options.relations, @TypeOf(args), .where) ++ jetquery.fields.fieldInfos(Adapter, Model, options.relations, @TypeOf(.{1}), .limit), .columns = if (options.columns.len == 0) &Model.columns() else options.columns, .where_clauses = &.{sql.Where.tree(Adapter, Model, &.{}, @TypeOf(args), .where, options.field_infos.len)}, .group_by = options.group_by, .result_context = .one, }); var statement = self.extend(S, args, .where); const arg_fields = jetquery.fields.structFields(@TypeOf(args)); statement.values[options.field_infos.len + arg_fields.len] = 1; statement.errors[options.field_infos.len + arg_fields.len] = null; return statement; }
pub fn count(self: Self) Statement(Adapter, .count, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .distinct = options.distinct, .where_clauses = options.where_clauses, .group_by = options.group_by, }) { const S = Statement(Adapter, .count, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .distinct = options.distinct, .where_clauses = options.where_clauses, .group_by = options.group_by, }); return self.extend(S, .{}, .none); }
pub fn distinct(self: Self, comptime args: anytype) Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = &.{}, .order_clauses = options.order_clauses, .result_context = .many, .distinct = &jetquery.columns.translate(Model, options.relations, args, .{}), .where_clauses = options.where_clauses, .group_by = options.group_by, }) { const S = Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = &.{}, .order_clauses = options.order_clauses, .result_context = .many, .distinct = &jetquery.columns.translate(Model, options.relations, args, .{}), .where_clauses = options.where_clauses, .group_by = options.group_by, }); if (!options.default_select) @compileError( std.fmt.comptimePrint( "Failed attempting to set distinct columns when `select` already invoked. " ++ "Use `Model.distinct(...)` to issue a distinct query.", .{}, ), );
return self.extend(S, .{}, .none); }
pub fn update(self: Self, args: anytype) Statement(Adapter, .update, Schema, Model, .{ .field_infos = &(jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(args), .update) ++ timestampsFields(Adapter, Model, .update)), }) { const S = Statement(Adapter, .update, Schema, Model, .{ .field_infos = &(jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(args), .update) ++ timestampsFields(Adapter, Model, .update)), }); return self.extend(S, args, .update); }
pub fn updateAll(self: Self, args: anytype) Statement(Adapter, .update_all, Schema, Model, .{ .field_infos = &(jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(args), .update) ++ timestampsFields(Adapter, Model, .update)), }) { const S = Statement(Adapter, .update_all, Schema, Model, .{ .field_infos = &(jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(args), .update) ++ timestampsFields(Adapter, Model, .update)), }); return self.extend(S, args, .update); }
pub fn insert(self: Self, args: anytype) Statement(Adapter, .insert, Schema, Model, .{ .field_infos = &(jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(args), .insert) ++ timestampsFields(Adapter, Model, .insert)), }) { const S = Statement(Adapter, .insert, Schema, Model, .{ .field_infos = &(jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(args), .insert) ++ timestampsFields(Adapter, Model, .insert)), }); return self.extend(S, args, .insert); }
/// Return a subset of columns from the inserted record. An empty array will return all columns. pub fn returning( self: Self, comptime return_columns: anytype, ) Statement(Adapter, .returning, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = &jetquery.columns.translate(Model, options.relations, return_columns, .{}), .order_clauses = options.order_clauses, .group_by = options.group_by, .distinct = options.distinct, .result_context = .one, }) { const S = Statement(Adapter, .returning, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = &jetquery.columns.translate(Model, options.relations, return_columns, .{}), .order_clauses = options.order_clauses, .group_by = options.group_by, .distinct = options.distinct, .result_context = .one, }); return self.extend(S, .{}, .returning); }
pub fn delete(self: Self) Statement(Adapter, .delete, Schema, Model, .{ .field_infos = &jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(.{}), .none), .where_clauses = options.where_clauses, }) { // TODO: Add support for `DELETE ... USING ...` if (comptime options.relations.len != 0) @compileError( "Failed attempting to generate `DELETE` query with relations. " ++ "This error occurred to prevent accidential deletion of potentially unexpected behaviour.", ); const S = Statement(Adapter, .delete, Schema, Model, .{ .field_infos = &jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(.{}), .none), .where_clauses = options.where_clauses, }); return self.extend(S, .{}, .none); }
pub fn deleteAll(self: Self) Statement(Adapter, .delete_all, Schema, Model, .{}) { // TODO: Add support for `DELETE ... USING ...` if (comptime options.relations.len != 0) @compileError( "Failed attempting to generate `DELETE` query with relations. " ++ "This error occurred to prevent accidential deletion or potentially unexpected behaviour.", ); const S = Statement(Adapter, .delete_all, Schema, Model, .{}); return self.extend(S, .{}, .none); }
pub fn limit(self: Self, bound: u64) Statement(Adapter, query_context, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(.{bound}), .limit), .columns = options.columns, .order_clauses = options.order_clauses, .result_context = options.result_context, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses, }) { const S = Statement(Adapter, query_context, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(.{bound}), .limit), .columns = options.columns, .order_clauses = options.order_clauses, .result_context = options.result_context, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses, }); return self.extend(S, .{bound}, .limit); }
pub fn offset(self: Self, bound: u64) Statement(Adapter, query_context, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(.{bound}), .offset), .columns = options.columns, .order_clauses = options.order_clauses, .result_context = options.result_context, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses, }) { const S = Statement(Adapter, query_context, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos(Adapter, Model, &.{}, @TypeOf(.{bound}), .offset), .columns = options.columns, .order_clauses = options.order_clauses, .result_context = options.result_context, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses, }); return self.extend(S, .{bound}, .offset); }
pub fn orderBy(self: Self, comptime args: anytype) Statement( Adapter, switch (query_context) { .none => .select, else => query_context, }, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = switch (query_context) { .none => &Model.columns(), else => options.columns, }, .order_clauses = &sql.translateOrderBy(Model, options.relations, args), .result_context = switch (options.result_context) { .none => .many, else => options.result_context, }, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses, }, ) { const S = Statement( Adapter, switch (query_context) { .none => .select, else => query_context, }, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = switch (query_context) { .none => &Model.columns(), else => options.columns, }, .order_clauses = &sql.translateOrderBy(Model, options.relations, args), .result_context = switch (options.result_context) { .none => .many, else => options.result_context, }, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses, }, ); return self.extend(S, .{}, .order); }
pub fn groupBy(self: Self, comptime columns: anytype) Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = options.columns, .order_clauses = options.order_clauses, .result_context = .many, .where_clauses = options.where_clauses, .group_by = &jetquery.columns.translate(Model, options.relations, columns, .{}), .having_clauses = options.having_clauses, }) { const S = Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = options.columns, .order_clauses = options.order_clauses, .group_by = &jetquery.columns.translate(Model, options.relations, columns, .{}), .result_context = .many, .where_clauses = options.where_clauses, .having_clauses = options.having_clauses, }); return self.extend(S, .{}, .none); }
pub fn having(self: Self, args: anytype) Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos( Adapter, Model, options.relations, @TypeOf(args), .where, ), .columns = options.columns, .order_clauses = options.order_clauses, .result_context = .many, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses ++ .{sql.Where.tree( Adapter, Model, options.relations, @TypeOf(args), .where, options.field_infos.len, )}, }) { const S = Statement(Adapter, .select, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos ++ jetquery.fields.fieldInfos( Adapter, Model, options.relations, @TypeOf(args), .where, ), .columns = options.columns, .order_clauses = options.order_clauses, .result_context = .many, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses ++ .{sql.Where.tree( Adapter, Model, options.relations, @TypeOf(args), .where, options.field_infos.len, )}, }); return self.extend(S, args, .none); }
pub fn include( self: Self, comptime name: jetquery.relation.RelationsEnum(Model), comptime include_options: anytype, ) Statement(Adapter, switch (query_context) { .none => .select, else => |tag| tag, }, Schema, Model, .{ .relations = jetquery.relation.concatRelations( options.relations, Schema, Model, name, jetquery.relation.translateRelationOptions(Schema, Model, name, include_options), .include, ), .field_infos = options.field_infos, .columns = switch (query_context) { .none => &Model.columns(), else => options.columns, }, .order_clauses = options.order_clauses, .result_context = switch (options.result_context) { .none => .many, else => |tag| tag, }, .default_select = options.default_select, .where_clauses = options.where_clauses, .having_clauses = options.having_clauses, }) { const S = Statement(Adapter, switch (query_context) { .none => .select, else => |tag| tag, }, Schema, Model, .{ .relations = jetquery.relation.concatRelations( options.relations, Schema, Model, name, jetquery.relation.translateRelationOptions( Schema, Model, name, include_options, ), .include, ), .field_infos = options.field_infos, .columns = switch (query_context) { .none => &Model.columns(), else => options.columns, }, .order_clauses = options.order_clauses, .result_context = switch (options.result_context) { .none => .many, else => |tag| tag, }, .default_select = options.default_select, .where_clauses = options.where_clauses, .having_clauses = options.having_clauses, }); return self.extend(S, .{}, .none); }
pub fn join( self: Self, comptime join_context: jetquery.relation.JoinContext, comptime name: jetquery.relation.RelationsEnum(Model), ) Statement( Adapter, switch (query_context) { .none => .select, else => |tag| tag, }, Schema, Model, .{ .relations = jetquery.relation.concatRelations( options.relations, Schema, Model, name, jetquery.relation.translateRelationOptions( Schema, Model, name, .{ .select = null }, ), join_context, ), .field_infos = options.field_infos, .columns = switch (query_context) { .none => &Model.columns(), else => options.columns, }, .order_clauses = options.order_clauses, .result_context = switch (options.result_context) { .none => .many, else => |tag| tag, }, .default_select = options.default_select, .where_clauses = options.where_clauses, .having_clauses = options.having_clauses, }, ) { const S = Statement( Adapter, switch (query_context) { .none => .select, else => |tag| tag, }, Schema, Model, .{ .relations = jetquery.relation.concatRelations( options.relations, Schema, Model, name, jetquery.relation.translateRelationOptions( Schema, Model, name, .{ .select = null }, ), join_context, ), .field_infos = options.field_infos, .columns = switch (query_context) { .none => &Model.columns(), else => options.columns, }, .order_clauses = options.order_clauses, .result_context = switch (options.result_context) { .none => .many, else => |tag| tag, }, .default_select = options.default_select, .where_clauses = options.where_clauses, .having_clauses = options.having_clauses, }, ); return self.extend(S, .{}, .none); }
pub fn execute(self: Self, repo: anytype) !switch (options.result_context) { .one => ?ResultType, .many => jetquery.Result(@TypeOf(repo.*)), .none => void, } { const caller_info = try jetquery.debug.getCallerInfo(@returnAddress()); return try repo.executeInternal(self, caller_info); }
pub fn all(self: Self, repo: anytype) ![]ResultType { var result = try repo.executeInternal( self, try jetquery.debug.getCallerInfo(@returnAddress()), ); return try result.all(self); }
pub fn first(self: Self, repo: anytype) !?ResultType { const query = self.limit(1); var result = try repo.executeInternal( query, try jetquery.debug.getCallerInfo(@returnAddress()), ); const rows = try result.all(query); defer repo.allocator.free(rows); return if (rows.len == 0) return null else rows[0]; }
pub fn validate(self: Self) !void { try self.validateDelete(); try self.validateUpdate(); try self.validateValues(); }
pub fn validateValues(self: Self) !void { for (self.errors) |maybe_error| { if (maybe_error) |err| return err; } }
pub fn isValid(self: Self) bool { if (!self.isValidDelete()) return false; if (!self.isValidUpdate()) return false;
return for (self.errors) |maybe_error| { if (maybe_error) |_| break false; } else true; }
pub fn validateDelete(self: Self) !void { if (!self.isValidDelete()) return error.JetQueryUnsafeDelete; }
fn isValidDelete(self: Self) bool { return query_context != .delete or self.hasWhereClause(); }
pub fn validateUpdate(self: Self) !void { if (!self.isValidUpdate()) return error.JetQueryUnsafeUpdate; }
fn isValidUpdate(self: Self) bool { return query_context != .update or self.hasWhereClause(); }
pub fn _orderBy( self: Self, comptime order_clauses: []const jetquery.sql.OrderClause, ) Statement( Adapter, switch (query_context) { .none => .select, else => query_context, }, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = switch (query_context) { .none => &Model.columns(), else => options.columns, }, .order_clauses = order_clauses, .result_context = switch (options.result_context) { .none => .many, else => options.result_context, }, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses, }, ) { const S = Statement( Adapter, switch (query_context) { .none => .select, else => query_context, }, Schema, Model, .{ .relations = options.relations, .field_infos = options.field_infos, .columns = switch (query_context) { .none => &Model.columns(), else => options.columns, }, .order_clauses = order_clauses, .result_context = switch (options.result_context) { .none => .many, else => options.result_context, }, .where_clauses = options.where_clauses, .group_by = options.group_by, .having_clauses = options.having_clauses, }, ); return self.extend(S, .{}, .order); }
fn auxiliaryQueries() [auxiliaryQueriesSize()]AuxiliaryQuery { comptime { var queries: [auxiliaryQueriesSize()]AuxiliaryQuery = undefined; var index: usize = 0;
for (options.relations) |Relation| { if (Relation.relation_type != .has_many) continue;
queries[index] = .{ .query = Query(Adapter.name, Schema, Relation.Source), .relation = Relation, }; index += 1; }
return queries; } }
pub fn auxiliaryQueriesSize() usize { comptime { var size: usize = 0; for (options.relations) |Relation| { if (Relation.relation_type != .has_many) continue;
size += 1; } return size; } }
fn hasWhereClause(self: Self) bool { inline for (self.field_infos) |field| { if (field.context == .where) return true; } return false; }
pub fn QueryColumnInfos() [totalColumnLen()]sql.ColumnInfo { comptime { var column_infos: [totalColumnLen()]sql.ColumnInfo = undefined; for (options.columns, 0..) |column, index| { column_infos[index] = .{ .name = column.alias orelse column.name, .type = column.ResultType(Adapter), .index = index, .relation = null, }; } var start: usize = options.columns.len; for (options.relations) |Relation| { if (Relation.relation_type != .belongs_to) continue;
for (Relation.select_columns, start..) |column, index| { column_infos[index] = .{ .name = column.alias orelse column.name, .type = column.ResultType(Adapter), .index = index, .relation = Relation, }; } start += Relation.select_columns.len; }
return column_infos; } }
pub fn QueryResultType() type { comptime { switch (query_context) { .count => return Adapter.Aggregate(.count), else => {}, }
var base_fields: [options.columns.len]jetquery.fields.StructField = undefined;
for (options.columns, 0..) |column, index| { base_fields[index] = jetquery.fields.structField( column.alias orelse column.name, column.ResultType(Adapter), ); }
var relations_fields: [options.relations.len]jetquery.fields.StructField = undefined; for (options.relations, 0..) |Relation, relation_index| { var relation_fields: [Relation.select_columns.len]jetquery.fields.StructField = undefined; for (Relation.select_columns, 0..) |column, index| { relation_fields[index] = jetquery.fields.structField( column.alias orelse column.name, column.ResultType(Adapter), ); }
const RelationBaseType = jetquery.fields.structType(&(relation_fields ++ internalFields(&relation_fields, false)));
const RelationType = switch (Relation.relation_type) { .belongs_to => RelationBaseType, .has_many => []const RelationBaseType, };
const relation_field = jetquery.fields.structField( Relation.relation_name, RelationType, );
relations_fields[relation_index] = relation_field; }
const all_fields = base_fields ++ relations_fields ++ internalFields(&base_fields, true);
return jetquery.fields.structType(&all_fields); } }
fn totalColumnLen() usize { comptime { var len: usize = options.columns.len; for (options.relations) |Relation| { if (Relation.relation_type != .belongs_to) continue;
len += Relation.select_columns.len; } return len; } }
fn internalFields( comptime base_fields: []const jetquery.fields.StructField, comptime with_relations: bool, ) [4]jetquery.fields.StructField { comptime { const Originals = jetquery.fields.structType(base_fields);
const jetquery_fields: [1]jetquery.fields.StructField = .{ jetquery.fields.structField("original_values", Originals), };
const JQ = jetquery.fields.structType(&jetquery_fields);
const len = if (with_relations) options.relations.len else 0; var relation_names: [len][]const u8 = undefined; if (with_relations) { for (options.relations, 0..) |relation, index| { relation_names[index] = relation.relation_name; } }
const relation_field = jetquery.fields.StructField{ .name = "__jetquery_relation_names", .type = [len][]const u8, .default_value_ptr = @ptrCast(&relation_names), .is_comptime = true, .alignment = @alignOf([options.relations.len][]const u8), };
return .{ jetquery.fields.structField("__jetquery", JQ), // Sadly we can't store these inside the nested value as Zig doesn't figure // out that the values are available at comptime. jetquery.fields.structFieldComptime("__jetquery_model", Model), jetquery.fields.structFieldComptime("__jetquery_schema", Schema), relation_field, }; } } };}
fn timestampsFields( Adapter: type, Model: type, comptime field_context: jetquery.fields.FieldContext,) [timestampsSize(Model, field_context)]jetquery.fields.FieldInfo { const timestamps = detectTimestamps(Model); const has_created_at = std.mem.containsAtLeast(TimestampType, timestamps, 1, &.{.created_at}); const has_updated_at = std.mem.containsAtLeast(TimestampType, timestamps, 1, &.{.updated_at}); const updated_at = jetquery.fields.fieldInfo( jetquery.fields.structField(jetquery.default_column_names.updated_at, i64), Model, jetquery.default_column_names.updated_at, field_context, ); const created_at = jetquery.fields.fieldInfo( jetquery.fields.structField(jetquery.default_column_names.created_at, Adapter.DateTimePrimitive), Model, jetquery.default_column_names.created_at, field_context, );
return switch (field_context) { .update => if (has_updated_at) .{updated_at} else .{}, .insert => if (has_created_at and has_updated_at) .{ created_at, updated_at } else if (has_created_at) .{created_at} else if (has_updated_at) .{updated_at} else .{}, else => @compileError( "Timestamps detection not relevant for `" ++ @tagName(field_context) ++ "` query. (This is a bug).", ), };}
const TimestampType = enum { created_at, updated_at };
fn detectTimestamps(Model: type) []const TimestampType { const has_created_at = @hasField(Model.Definition, jetquery.default_column_names.created_at); const has_updated_at = @hasField(Model.Definition, jetquery.default_column_names.updated_at);
return if (has_created_at and has_updated_at) &.{ .created_at, .updated_at } else if (has_created_at) &.{.created_at} else if (has_updated_at) &.{.updated_at} else &.{};}
fn timestampsSize(Model: type, comptime field_context: jetquery.fields.FieldContext) u2 { const timestamps = detectTimestamps(Model);
const has_updated_at = std.mem.containsAtLeast(TimestampType, timestamps, 1, &.{.updated_at});
return switch (field_context) { .update => if (has_updated_at) 1 else 0, .insert => timestamps.len, else => @compileError( "Timestamps detection not relevant for `" ++ @tagName(field_context) ++ "` query. (This is a bug).", ), };}
fn now() i64 { const datetime = @import("jetzig_datetime"); const io = std.Io.Threaded.global_single_threaded.io(); return datetime.DateTime.now(io).unix(.microseconds);}
fn validateQueryContext(comptime initial: sql.QueryContext, comptime attempted: sql.QueryContext) void { comptime { switch (attempted) { .count => switch (initial) { .none, .select => {}, else => if (attempted != initial) @compileError(std.fmt.comptimePrint( "Failed attempting to initialize `{s}` query already-initialized on `{s}` query.", .{ @tagName(attempted), @tagName(initial) }, )), }, else => switch (initial) { .none => {}, else => if (attempted != initial) @compileError(std.fmt.comptimePrint( "Failed attempting to initialize `{s}` query already-initialized on `{s}` query.", .{ @tagName(attempted), @tagName(initial) }, )), }, } }}
fn updateTimestamps( T: type, statement: *T, comptime context: sql.QueryContext,) void { switch (comptime context) { .update, .update_all => { const timestamp = now(); inline for (statement.field_infos, 0..) |field_info, index| { if (comptime std.mem.eql( u8, field_info.name, jetquery.default_column_names.updated_at, )) { @field( statement.values, std.fmt.comptimePrint("{d}", .{index}), ) = timestamp; statement.errors[index] = null; } } }, .insert => { const timestamp = now(); inline for (statement.field_infos, 0..) |field_info, index| { if (comptime std.mem.eql( u8, field_info.name, jetquery.default_column_names.updated_at, )) { @field( statement.values, std.fmt.comptimePrint("{d}", .{index}), ) = timestamp; statement.errors[index] = null; } else if (comptime std.mem.eql( u8, field_info.name, jetquery.default_column_names.created_at, )) { @field( statement.values, std.fmt.comptimePrint("{d}", .{index}), ) = timestamp; statement.errors[index] = null; } } }, else => {}, }}