Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669const std = @import("std");const datetime = @import("datetime");
const DateTime = datetime.DateTime;const ArrayListManaged = std.array_list.Managed;const Random = std.Random;
pub const config = @import("jetquery.config");
pub const Repo = @import("src/Repo.zig").Repo;
pub const Connection = @import("src/Connection.zig").Connection;pub const adapters = @import("src/adapters.zig");pub const sql = @import("src/sql.zig");pub const relation = @import("src/relation.zig");pub const Migration = @import("src/Migration.zig");pub const Seeder = @import("src/Seeder.zig");pub const schema = @import("src/schema.zig");pub const fields = @import("src/fields.zig");pub const columns = @import("src/columns.zig");pub const default_column_names = @import("src/default_column_names.zig");pub const Row = @import("src/Row.zig");pub const Result = @import("src/Result.zig").Result;pub const events = @import("src/events.zig");pub const Query = @import("src/Query.zig").Query;pub const Model = @import("src/Model.zig").Model;pub const Value = @import("src/Value.zig").Value;pub const debug = @import("src/debug.zig");pub const Reflection = @import("src/reflection/Reflection.zig");pub const util = @import("src/util.zig");pub const Environment = std.meta.FieldEnum(@TypeOf(config.database));pub const hasMany = relation.hasMany;pub const belongsTo = relation.belongsTo;pub const CallbackFn = *const fn (event: events.Event) anyerror!void;
pub const CreateTableOptions = struct { if_not_exists: bool = false };pub const DropTableOptions = struct { if_exists: bool = false };pub const AlterTableOptions = struct { columns: AlterTableColumnOptions = .{}, rename: ?[]const u8 = null,
pub const AlterTableColumnOptions = struct { add: []const schema.Column = &.{}, drop: []const []const u8 = &.{}, rename: ?RenameColumn = null,
pub const RenameColumn = struct { from: []const u8, to: []const u8 }; };};pub const DropDatabaseOptions = struct { if_exists: bool = false };pub const CreateIndexOptions = struct { unique: bool = false, name: ?[]const u8 = null, if_not_exists: bool = false,};pub const Context = enum { migration, seed, query, cli };
pub const original_prefix = "__original_";
const TestAdapter: adapters.Name = .postgresql;
test { std.testing.refAllDecls(@This());}
test "select" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).select(.{ .name, .paws });
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE (1 = 1) , query.sql); try std.testing.expect(query.isValid());}
test "select (all)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).select(.{});
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE (1 = 1) , query.sql); try std.testing.expect(query.isValid());}
test "select (with `where`)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); };
const paws = 4; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws }) .where(.{ .name = "bar", .paws = paws });
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE ("cats"."name" = $1 AND "cats"."paws" = $2) , query.sql); try std.testing.expect(query.isValid());}
test "where" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32, color: []const u8 }, .{}); };
const paws = 4; const query = Query(TestAdapter, Schema, .Cat) .where(.{ .name = "bar", .paws = paws });
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws", "cats"."color" FROM "cats" WHERE ("cats"."name" = $1 AND "cats"."paws" = $2) , query.sql); try std.testing.expect(query.isValid());}
test "where (multiple)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); };
const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws }) .where(.{ .name = "bar" }) .where(.{ .paws = 4 });
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE "cats"."name" = $1 AND "cats"."paws" = $2 , query.sql); try std.testing.expect(query.isValid());}
test "limit" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws }) .limit(100);
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE (1 = 1) LIMIT $1 , query.sql); try std.testing.expect(query.isValid());}
test "offset" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws }) .limit(100) .offset(50);
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE (1 = 1) LIMIT $1 OFFSET $2 , query.sql); try std.testing.expect(query.isValid());}
test "order by" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws }) .orderBy(.{ .name = .ascending });
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE (1 = 1) ORDER BY "cats"."name" ASC , query.sql); try std.testing.expect(query.isValid());}
test "order by (aliased desc = descending)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws }) .orderBy(.{ .name = .desc });
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE (1 = 1) ORDER BY "cats"."name" DESC , query.sql); try std.testing.expect(query.isValid());}
test "order by (aliased asc = ascending)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws }) .orderBy(.{ .name = .asc });
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE (1 = 1) ORDER BY "cats"."name" ASC , query.sql); try std.testing.expect(query.isValid());}
test "order by (short-hand)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws }) .orderBy(.name);
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE (1 = 1) ORDER BY "cats"."name" ASC , query.sql); try std.testing.expect(query.isValid());}
test "order by (synthetic column short-hand)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws, sql.max(.paws) }) .orderBy(sql.max(.paws));
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws", MAX("cats"."paws") FROM "cats" WHERE (1 = 1) ORDER BY MAX("cats"."paws") ASC , query.sql); try std.testing.expect(query.isValid());}
test "order by (mixed regular and synthetic columns)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws, sql.max(.paws) }) .orderBy(.{ .name, sql.max(.paws) });
try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws", MAX("cats"."paws") FROM "cats" WHERE (1 = 1) ORDER BY "cats"."name" ASC, MAX("cats"."paws") ASC , query.sql); try std.testing.expect(query.isValid());}
test "order by with relations (short-hand)" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}) } }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}, ); };
const query = Query(TestAdapter, Schema, .Human) .join(.inner, .cat) .orderBy(.{ .cat = .name });
try std.testing.expectEqualStrings( \\SELECT "humans"."id", "humans"."cat_id", "humans"."name" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" WHERE (1 = 1) ORDER BY "cat"."name" ASC , query.sql, ); try std.testing.expect(query.isValid());}
test "order by with relations (explicit form)" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}) } }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}, ); };
const query = Query(TestAdapter, Schema, .Human) .join(.inner, .cat) .orderBy(.{ .cat = .{ .name = .descending } });
try std.testing.expectEqualStrings( \\SELECT "humans"."id", "humans"."cat_id", "humans"."name" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" WHERE (1 = 1) ORDER BY "cat"."name" DESC , query.sql, ); try std.testing.expect(query.isValid());}
test "order by with relations and base table, short + explicit forms and synthetic columns" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, cat_id: i32, family_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}), .family = relation.belongsTo(.Family, .{}), }, }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, age: i32 }, .{}, );
pub const Family = Model( @This(), "families", struct { id: i32, name: []const u8, wealth: i32 }, .{}, ); };
const query = Query(TestAdapter, Schema, .Human) .join(.inner, .cat) .join(.inner, .family) .orderBy(.{ .id = .descending, .cat = .{ .name = .descending, .max_paws = sql.max(.paws), .min_age = .{ sql.min(.age), .desc }, }, .family = .{ .id, .name, sql.max(.wealth) }, });
try std.testing.expectEqualStrings( \\SELECT "humans"."id", "humans"."cat_id", "humans"."family_id", "humans"."name" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" INNER JOIN "families" AS "family" ON "humans"."family_id" = "family"."id" WHERE (1 = 1) ORDER BY "humans"."id" DESC, "cat"."name" DESC, MAX("cat"."paws") ASC, MIN("cat"."age") DESC, "family"."id" ASC, "family"."name" ASC, MAX("family"."wealth") ASC , query.sql, ); try std.testing.expect(query.isValid());}
test "insert" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .insert(.{ .name = "Hercules", .paws = 4 });
try std.testing.expectEqualStrings( \\INSERT INTO "cats" ("name", "paws") VALUES ($1, $2) , query.sql); try std.testing.expect(query.isValid());}
test "insert with returning" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .insert(.{ .name = "Hercules", .paws = 4 }) .returning(.{.name}); try std.testing.expectEqualStrings( \\INSERT INTO "cats" ("name", "paws") VALUES ($1, $2) RETURNING "cats"."name" , query.sql); try std.testing.expect(query.isValid());}
test "update" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .update(.{ .name = "Heracles", .paws = 2 }) .where(.{ .name = "Hercules" });
try std.testing.expectEqualStrings( \\UPDATE "cats" SET "name" = $1, "paws" = $2 WHERE "cats"."name" = $3 , query.sql, ); try std.testing.expect(query.isValid());}
test "update (without whereclause)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .update(.{ .name = "Heracles", .paws = 2 });
try std.testing.expectEqualStrings( \\UPDATE "cats" SET "name" = $1, "paws" = $2 WHERE (1 = 1) , query.sql, ); try std.testing.expectEqual(false, query.isValid()); try std.testing.expectError(error.JetQueryUnsafeUpdate, query.validate());}
test "updateAll" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .updateAll(.{ .name = "Heracles", .paws = 2 });
try std.testing.expectEqualStrings( \\UPDATE "cats" SET "name" = $1, "paws" = $2 WHERE (1 = 1) , query.sql, ); try std.testing.expect(query.isValid());}
test "delete" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .delete() .where(.{ .name = "Hercules" });
try std.testing.expectEqualStrings( \\DELETE FROM "cats" WHERE "cats"."name" = $1 , query.sql); try std.testing.expect(query.isValid());}
test "delete (without where clause)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).delete();
try std.testing.expectEqual(false, query.isValid()); try std.testing.expectError(error.JetQueryUnsafeDelete, query.validate());}
test "deleteAll" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .deleteAll();
try std.testing.expectEqualStrings( \\DELETE FROM "cats" WHERE (1 = 1) , query.sql); try std.testing.expect(query.isValid());}
test "find" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).find(1000);
try std.testing.expectEqualStrings( \\SELECT "cats"."id", "cats"."name", "cats"."paws" FROM "cats" WHERE "cats"."id" = $1 ORDER BY "cats"."id" ASC LIMIT $2 , query.sql); try std.testing.expect(query.isValid());}
test "find (with coerced id)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).find("1000");
try std.testing.expectEqualStrings( \\SELECT "cats"."id", "cats"."name", "cats"."paws" FROM "cats" WHERE "cats"."id" = $1 ORDER BY "cats"."id" ASC LIMIT $2 , query.sql); try std.testing.expect(query.isValid());}
test "find (and select without id)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).find("1000").select(.{.name});
try std.testing.expectEqualStrings( \\SELECT "cats"."name" FROM "cats" WHERE "cats"."id" = $1 ORDER BY "cats"."id" ASC LIMIT $2 , query.sql); try std.testing.expect(query.isValid());}
test "findBy" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).findBy(.{ .name = "Hercules", .paws = 4 });
try std.testing.expectEqualStrings( \\SELECT "cats"."id", "cats"."name", "cats"."paws" FROM "cats" WHERE ("cats"."name" = $1 AND "cats"."paws" = $2) ORDER BY "cats"."id" ASC LIMIT $3 , query.sql); try std.testing.expect(query.isValid());}
test "count()" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).where(.{ .name = "Hercules", .paws = 4 }).count();
try std.testing.expectEqualStrings( \\SELECT COUNT(*) FROM "cats" WHERE ("cats"."name" = $1 AND "cats"."paws" = $2) , query.sql); try std.testing.expect(query.isValid());}
test "count() without whereclause" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).count();
try std.testing.expectEqualStrings( \\SELECT COUNT(*) FROM "cats" WHERE (1 = 1) , query.sql); try std.testing.expect(query.isValid());}
test "distinct().count()" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).where(.{ .name = "Hercules", .paws = 4 }).distinct(.{.name}).count();
try std.testing.expectEqualStrings( \\SELECT COUNT(DISTINCT("cats"."name")) FROM "cats" WHERE ("cats"."name" = $1 AND "cats"."paws" = $2) , query.sql); try std.testing.expect(query.isValid());}
test "nested distinct().count()" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}); pub const Human = Model( @This(), "humans", struct { cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}) }, }, ); }; const query = Query(TestAdapter, Schema, .Human).include(.cat, .{}).where(.{ .name = "Bob" }).distinct(.{ .name, .{ .cat = .{.name} } }).count();
try std.testing.expectEqualStrings( \\SELECT COUNT(DISTINCT("humans"."name", "cat"."name")) FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" WHERE "humans"."name" = $1 , query.sql); try std.testing.expect(query.isValid());}
test "combined" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { id: i32, name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, .paws }) .where(.{ .name = "Hercules" }) .limit(10) .orderBy(.{ .name = .ascending }); try std.testing.expectEqualStrings( \\SELECT "cats"."name", "cats"."paws" FROM "cats" WHERE "cats"."name" = $1 ORDER BY "cats"."name" ASC LIMIT $2 , query.sql); try std.testing.expect(query.isValid());}
test "runtime field values" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; var hercules_buf: [8]u8 = undefined; const hercules = try std.fmt.bufPrint(&hercules_buf, "{s}", .{"Hercules"}); var random: Random.IoSource = .{ .io = std.testing.io }; const generator = random.interface(); const paws: u16 = generator.int(u8); var heracles_buf: [8]u8 = undefined; const heracles = try std.fmt.bufPrint(&heracles_buf, "{s}", .{"Heracles"}); const query = Query(TestAdapter, Schema, .Cat) .update(.{ .name = heracles, .paws = paws + 2 }) .where(.{ .name = hercules, .paws = paws }); try std.testing.expectEqualStrings("Heracles", query.values.@"0"); try std.testing.expectEqual(paws + 2, query.values.@"1"); try std.testing.expectEqualStrings("Hercules", query.values.@"2"); try std.testing.expect(query.isValid());}
test "boolean coercion" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, intelligent: bool }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{.name}) .where(.{ .intelligent = "1" });
try std.testing.expectEqual(query.values.@"0", true); try std.testing.expect(query.isValid());}
test "integer coercion" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{.name}) .where(.{ .paws = "4" });
try std.testing.expectEqual(query.values.@"0", 4); try std.testing.expect(query.isValid());}
test "float coercion" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, intelligence: f64 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{.name}) .where(.{ .intelligence = "10.2" });
try std.testing.expectEqual(query.values.@"0", 10.2); try std.testing.expect(query.isValid());}
test "toJetQuery()" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32, color: []const u8 }, .{}); };
const Name = struct { pub fn toJetQuery(self: @This(), T: type) !T { _ = self; return switch (T) { []const u8 => "Hercules", else => @compileError("Cannot coerce to " ++ @typeName(T)), }; } };
const Paws = struct { pub fn toJetQuery(self: @This(), T: type) !T { _ = self; return switch (T) { i32 => 4, else => @compileError("Cannot coerce to " ++ @typeName(T)), }; } };
const Color = struct { pub fn toJetQuery(self: @This(), T: type) !T { _ = self; return switch (T) { []const u8 => "Black and white", else => @compileError("Cannot coerce to " ++ @typeName(T)), }; } };
const name = Name{}; const paws = Paws{}; const color = Color{};
const query = Query(TestAdapter, Schema, .Cat) .insert(.{ .name = name, .paws = &paws, .color = color }); try std.testing.expectEqualStrings( \\INSERT INTO "cats" ("name", "paws", "color") VALUES ($1, $2, $3) , query.sql); try std.testing.expectEqualStrings(query.values.@"0", "Hercules"); try std.testing.expectEqual(query.values.@"1", 4); try std.testing.expect(query.isValid());}
test "failed coercion (bool)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, intelligent: bool }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{.name}) .where(.{ .intelligent = "not a bool" });
try std.testing.expectError(error.JetQueryInvalidBooleanString, query.validateValues()); try std.testing.expectEqual(false, query.isValid());}
test "failed coercion (int)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, paws: i32 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{.name}) .where(.{ .paws = "not an int" });
try std.testing.expectError(error.JetQueryInvalidIntegerString, query.validateValues()); try std.testing.expectEqual(false, query.isValid());}
test "failed coercion (float)" { const Schema = struct { pub const Cat = Model(@This(), "cats", struct { name: []const u8, intelligence: f64 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat) .select(.{.name}) .where(.{ .intelligence = "not a float" });
try std.testing.expectError(error.JetQueryInvalidFloatString, query.validateValues()); try std.testing.expectEqual(false, query.isValid());}
test "timestamps (create)" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, ); }; const query = Query(TestAdapter, Schema, .Cat) .insert(.{ .name = "Hercules", .paws = 4 });
try std.testing.expectEqualStrings( \\INSERT INTO "cats" ("name", "paws", "created_at", "updated_at") VALUES ($1, $2, $3, $4) , query.sql); try std.testing.expect(query.isValid());}
test "timestamps (update)" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, ); }; const query = Query(TestAdapter, Schema, .Cat) .update(.{ .name = "Heracles", .paws = 2 }) .where(.{ .name = "Hercules" });
try std.testing.expectEqualStrings( \\UPDATE "cats" SET "name" = $1, "paws" = $2, "updated_at" = $3 WHERE "cats"."name" = $4 , query.sql, ); try std.testing.expect(query.isValid());}
test "belongsTo" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}) } }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{ .relations = .{} }, ); }; const query = Query(TestAdapter, Schema, .Human) .include(.cat, .{}) .findBy(.{ .name = "Bob" });
try std.testing.expectEqualStrings( \\SELECT "humans"."id", "humans"."cat_id", "humans"."name", "cat"."id", "cat"."name", "cat"."paws", "cat"."created_at", "cat"."updated_at" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" WHERE "humans"."name" = $1 ORDER BY "humans"."id" ASC LIMIT $2 , query.sql, ); try std.testing.expect(query.isValid());}
test "belongsTo (multiple)" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, family_id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}), .family = relation.belongsTo(.Family, .{}), }, }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, );
pub const Family = Model( @This(), "families", struct { id: i32, name: []const u8 }, .{}, ); }; const query = Query(TestAdapter, Schema, .Human) .include(.cat, .{}) .include(.family, .{}) .findBy(.{ .name = "Bob" });
try std.testing.expectEqualStrings( \\SELECT "humans"."id", "humans"."family_id", "humans"."cat_id", "humans"."name", "cat"."id", "cat"."name", "cat"."paws", "cat"."created_at", "cat"."updated_at", "family"."id", "family"."name" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" INNER JOIN "families" AS "family" ON "humans"."family_id" = "family"."id" WHERE "humans"."name" = $1 ORDER BY "humans"."id" ASC LIMIT $2 , query.sql, ); try std.testing.expect(query.isValid());}
test "belongsTo (with specified columns)" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, family_id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}), .family = relation.belongsTo(.Family, .{}), }, }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, );
pub const Family = Model( @This(), "families", struct { id: i32, name: []const u8 }, .{}, ); }; const query = Query(TestAdapter, Schema, .Human) .include(.cat, .{ .select = .{ .name, .paws } }) .include(.family, .{ .select = .{.name} }) .select(.{.name}) .findBy(.{ .name = "Bob" });
try std.testing.expectEqualStrings( \\SELECT "humans"."name", "cat"."name", "cat"."paws", "family"."name" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" INNER JOIN "families" AS "family" ON "humans"."family_id" = "family"."id" WHERE "humans"."name" = $1 ORDER BY "humans"."id" ASC LIMIT $2 , query.sql, ); try std.testing.expect(query.isValid());}
test "hasMany" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}) } }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{ .relations = .{ .humans = relation.hasMany(.Human, .{}) } }, ); }; const query = Query(TestAdapter, Schema, .Cat) .include(.humans, .{}) .findBy(.{ .name = "Hercules" });
try std.testing.expectEqualStrings( \\SELECT "cats"."id", "cats"."name", "cats"."paws", "cats"."created_at", "cats"."updated_at" FROM "cats" WHERE "cats"."name" = $1 ORDER BY "cats"."id" ASC LIMIT $2 , query.sql, );
try std.testing.expectEqualStrings( \\SELECT "humans"."id", "humans"."cat_id", "humans"."name" FROM "humans" WHERE "humans"."cat_id" = $1 ORDER BY "humans"."id" ASC , // Only the base query is generated at this point, the repo appends the where clause // after fetching results of the first query. This is tested more thoroughly in `Repo.zig` query.auxiliary_queries[0].query.where(.{ .cat_id = 1 }).sql, ); try std.testing.expect(query.isValid());}
test "nested where" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, family_id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}), .family = relation.belongsTo(.Family, .{}), }, }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, );
pub const Family = Model( @This(), "families", struct { id: i32, name: []const u8 }, .{}, ); }; const query = Query(TestAdapter, Schema, .Human) .include(.cat, .{}) .include(.family, .{}) .where(.{ .name = "Bob", .cat = .{ .name = "Hercules" }, .family = .{ .name = "Farrell" }, });
try std.testing.expectEqualStrings( \\SELECT "humans"."id", "humans"."family_id", "humans"."cat_id", "humans"."name", "cat"."id", "cat"."name", "cat"."paws", "cat"."created_at", "cat"."updated_at", "family"."id", "family"."name" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" INNER JOIN "families" AS "family" ON "humans"."family_id" = "family"."id" WHERE ("humans"."name" = $1 AND "cat"."name" = $2 AND "family"."name" = $3) ORDER BY "humans"."id" ASC , query.sql, ); try std.testing.expect(query.isValid());}
test "operator logic" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, family_id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}), .family = relation.belongsTo(.Family, .{}), }, }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, );
pub const Family = Model( @This(), "families", struct { id: i32, name: []const u8 }, .{}, ); }; const query = Query(TestAdapter, Schema, .Human) .include(.cat, .{}) .include(.family, .{}) .where(.{ .{ .name = "Bob" }, .OR, .{ .name = "T-Rex" }, .OR, .{ .cat = .{ .name = "Hercules" } }, .NOT, .{ .family = .{ .name = "Farrell" } }, });
try std.testing.expectEqualStrings( \\SELECT "humans"."id", "humans"."family_id", "humans"."cat_id", "humans"."name", "cat"."id", "cat"."name", "cat"."paws", "cat"."created_at", "cat"."updated_at", "family"."id", "family"."name" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" INNER JOIN "families" AS "family" ON "humans"."family_id" = "family"."id" WHERE ("humans"."name" = $1 OR "humans"."name" = $2 OR "cat"."name" = $3 AND NOT "family"."name" = $4) ORDER BY "humans"."id" ASC , query.sql, ); try std.testing.expect(query.isValid());}
test "slice of []const u8 in whereclause" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { name: []const u8 }, .{}, ); }; var array = ArrayListManaged([]const u8).init(std.testing.allocator); defer array.deinit();
try array.append("Bob"); try array.append("Jane");
const query = Query(TestAdapter, Schema, .Human).where(.{ .name = array.items }); try std.testing.expectEqualStrings( \\SELECT "humans"."name" FROM "humans" WHERE "humans"."name" = ANY ($1) , query.sql); try std.testing.expect(query.isValid());}
test "slice of int in whereclause" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { cats: u128 }, .{}, ); }; var array = ArrayListManaged(u128).init(std.testing.allocator); defer array.deinit();
try array.append(2); try array.append(1231231238128381283);
const query = Query(TestAdapter, Schema, .Human).where(.{ .cats = array.items }); try std.testing.expectEqualStrings( \\SELECT "humans"."cats" FROM "humans" WHERE "humans"."cats" = ANY ($1) , query.sql); try std.testing.expect(query.isValid());}
test "slice of float in whereclause" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { favorite_number: f64 }, .{}, ); }; var array = ArrayListManaged(f64).init(std.testing.allocator); defer array.deinit();
try array.append(3.1415926535897932); try array.append(2.7182818284590452);
const query = Query(TestAdapter, Schema, .Human).where(.{ .favorite_number = array.items }); try std.testing.expectEqualStrings( \\SELECT "humans"."favorite_number" FROM "humans" WHERE "humans"."favorite_number" = ANY ($1) , query.sql); try std.testing.expect(query.isValid());}
test "slice of bool in whereclause" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { has_cats: bool }, .{}, ); }; var array = ArrayListManaged(bool).init(std.testing.allocator); defer array.deinit();
try array.append(true); try array.append(false);
const query = Query(TestAdapter, Schema, .Human).where(.{ .has_cats = array.items }); try std.testing.expectEqualStrings( \\SELECT "humans"."has_cats" FROM "humans" WHERE "humans"."has_cats" = ANY ($1) , query.sql); try std.testing.expect(query.isValid());}
test "null in whereclause" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { name: []const u8 }, .{}, ); };
const query = Query(TestAdapter, Schema, .Human).where(.{ .{ .name = null }, .OR, .{ .name = "baz" } }); try std.testing.expectEqualStrings( \\SELECT "humans"."name" FROM "humans" WHERE ("humans"."name" IS NULL OR "humans"."name" = $1) , query.sql); try std.testing.expect(query.isValid());}
test "groupBy" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { name: []const u8 }, .{}, ); };
const query = Query(TestAdapter, Schema, .Cat).groupBy(.{.name}); try std.testing.expectEqualStrings( \\SELECT FROM "cats" WHERE (1 = 1) GROUP BY "cats"."name" , query.sql); try std.testing.expect(query.isValid());}
test "aggregate max()" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { name: []const u8, paws: usize }, .{}, ); };
const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, sql.max(.paws) }) .groupBy(.{.name}); try std.testing.expectEqualStrings( \\SELECT "cats"."name", MAX("cats"."paws") FROM "cats" WHERE (1 = 1) GROUP BY "cats"."name" , query.sql); try std.testing.expect(query.isValid());}
test "aggregate min()" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { name: []const u8, paws: usize }, .{}, ); };
const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, sql.min(.paws) }) .groupBy(.{.name}); try std.testing.expectEqualStrings( \\SELECT "cats"."name", MIN("cats"."paws") FROM "cats" WHERE (1 = 1) GROUP BY "cats"."name" , query.sql); try std.testing.expect(query.isValid());}
test "aggregate count()" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { name: []const u8, paws: usize }, .{}, ); };
const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, sql.count(.paws) }) .groupBy(.{.name}); try std.testing.expectEqualStrings( \\SELECT "cats"."name", COUNT("cats"."paws") FROM "cats" WHERE (1 = 1) GROUP BY "cats"."name" , query.sql); try std.testing.expect(query.isValid());}
test "aggregate avg()" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { name: []const u8, paws: usize }, .{}, ); };
const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, sql.avg(.paws) }) .groupBy(.{.name}); try std.testing.expectEqualStrings( \\SELECT "cats"."name", AVG("cats"."paws") FROM "cats" WHERE (1 = 1) GROUP BY "cats"."name" , query.sql); try std.testing.expect(query.isValid());}
test "aggregate sum()" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { name: []const u8, paws: usize }, .{}, ); };
const query = Query(TestAdapter, Schema, .Cat) .select(.{ .name, sql.sum(.paws) }) .groupBy(.{.name}); try std.testing.expectEqualStrings( \\SELECT "cats"."name", SUM("cats"."paws") FROM "cats" WHERE (1 = 1) GROUP BY "cats"."name" , query.sql); try std.testing.expect(query.isValid());}
test "like/ilike" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { name: []const u8, paws: usize }, .{}, ); };
const query = Query(TestAdapter, Schema, .Cat) .select(.{.name}) .where(.{ .{ .name, .like, "Herc%" }, .OR, .{ .name, .ilike, "princ%" } }); try std.testing.expectEqualStrings( \\SELECT "cats"."name" FROM "cats" WHERE ("cats"."name" LIKE $1 OR "cats"."name" ILIKE $2) , query.sql); try std.testing.expect(query.isValid());}
test "inner join" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, family_id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}), .computers = relation.hasMany(.Computer, .{}), }, }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, );
pub const Computer = Model( @This(), "computers", struct { id: i32, human_id: i32 }, .{}, ); };
const query = Query(TestAdapter, Schema, .Human) .join(.inner, .cat) .join(.inner, .computers) .select(.{.name}); try std.testing.expectEqualStrings( \\SELECT "humans"."name" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" INNER JOIN "computers" AS "computers" ON "humans"."id" = "computers"."human_id" WHERE (1 = 1) ORDER BY "humans"."id" ASC , query.sql); try std.testing.expect(query.isValid());}
test "outer join" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, family_id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}), .computers = relation.hasMany(.Computer, .{}), }, }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, );
pub const Computer = Model( @This(), "computers", struct { id: i32, human_id: i32 }, .{}, ); };
const query = Query(TestAdapter, Schema, .Human) .join(.outer, .cat) .join(.outer, .computers) .select(.{.name}); try std.testing.expectEqualStrings( \\SELECT "humans"."name" FROM "humans" LEFT OUTER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" LEFT OUTER JOIN "computers" AS "computers" ON "humans"."id" = "computers"."human_id" WHERE (1 = 1) ORDER BY "humans"."id" ASC , query.sql); try std.testing.expect(query.isValid());}
test "inner and outer join" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, family_id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}), .family = relation.belongsTo(.Family, .{}), }, }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, );
pub const Family = Model( @This(), "families", struct { id: i32, name: []const u8 }, .{}, ); };
const query = Query(TestAdapter, Schema, .Human) .join(.inner, .cat) .join(.outer, .family) .select(.{.name}); try std.testing.expectEqualStrings( \\SELECT "humans"."name" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" LEFT OUTER JOIN "families" AS "family" ON "humans"."family_id" = "family"."id" WHERE (1 = 1) ORDER BY "humans"."id" ASC , query.sql); try std.testing.expect(query.isValid());}
test "inner and outer join with select on relation columns" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32, family_id: i32, cat_id: i32, name: []const u8 }, .{ .relations = .{ .cat = relation.belongsTo(.Cat, .{}), .family = relation.belongsTo(.Family, .{}), }, }, );
pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, paws: i32, created_at: i64, updated_at: i64 }, .{}, );
pub const Family = Model( @This(), "families", struct { id: i32, name: []const u8 }, .{}, ); };
const query = Query(TestAdapter, Schema, .Human) .join(.inner, .cat) .join(.outer, .family) .select(.{ .name, .{ .family = .{.id}, .cat = .{.paws} } }); try std.testing.expectEqualStrings( \\SELECT "humans"."name", "family"."id", "cat"."paws" FROM "humans" INNER JOIN "cats" AS "cat" ON "humans"."cat_id" = "cat"."id" LEFT OUTER JOIN "families" AS "family" ON "humans"."family_id" = "family"."id" WHERE (1 = 1) ORDER BY "humans"."id" ASC , query.sql); try std.testing.expect(query.isValid());}
test "default order by (no order clauses, default primary key present)" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32 }, .{}, ); }; const query = Query(TestAdapter, Schema, .Human).select(.{}); try std.testing.expectEqualStrings( \\SELECT "humans"."id" FROM "humans" WHERE (1 = 1) ORDER BY "humans"."id" ASC , query.sql); try std.testing.expect(query.isValid());}
test "default order by (no order clauses, default primary key not present)" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { name: []const u8 }, .{}, ); }; const query = Query(TestAdapter, Schema, .Human).select(.{}); try std.testing.expectEqualStrings( \\SELECT "humans"."name" FROM "humans" WHERE (1 = 1) , query.sql); try std.testing.expect(query.isValid());}
test "default order by (no order clauses, custom primary key present)" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { name: []const u8 }, .{ .primary_key = "name" }, ); }; const query = Query(TestAdapter, Schema, .Human).select(.{}); try std.testing.expectEqualStrings( \\SELECT "humans"."name" FROM "humans" WHERE (1 = 1) ORDER BY "humans"."name" ASC , query.sql); try std.testing.expect(query.isValid());}
test "default order by (no order clauses, custom primary key not present)" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32 }, .{ .primary_key = "name" }, ); }; const query = Query(TestAdapter, Schema, .Human).select(.{}); try std.testing.expectEqualStrings( \\SELECT "humans"."id" FROM "humans" WHERE (1 = 1) , query.sql); try std.testing.expect(query.isValid());}
test "raw whereclause" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32 }, .{}, ); }; var random_source = std.Random.IoSource{ .io = std.testing.io }; const qux = random_source.interface().int(u8); var buf: [4]u8 = undefined; const quux = try std.fmt.bufPrint(&buf, "{s}", .{"quux"}); const query = Query(TestAdapter, Schema, .Human).where(.{ "foo = ? and bar = ? or baz = ? and qux = ? and quux = ? and a = ? and b = ? and c = ? and d = ? and e = ? and f = ? and g = ? and h = ? and i = ? and j = ? and k = ?", .{ "qux", 100, false, qux, quux, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, }); try std.testing.expectEqualStrings( \\SELECT "humans"."id" FROM "humans" WHERE foo = $1 and bar = $2 or baz = $3 and qux = $4 and quux = $5 and a = $6 and b = $7 and c = $8 and d = $9 and e = $10 and f = $11 and g = $12 and h = $13 and i = $14 and j = $15 and k = $16 ORDER BY "humans"."id" ASC , query.sql); try std.testing.expect(query.isValid());}
test "raw select column" { const Schema = struct { pub const Human = Model( @This(), "humans", struct { id: i32 }, .{}, ); }; const query = Query(TestAdapter, Schema, .Human).select(.{ sql.column(u32, "foo(bar + baz)").as(.foo), sql.column([]const u8, "qux(quux, corge)").as(.bar), }); try std.testing.expectEqualStrings( \\SELECT foo(bar + baz), qux(quux, corge) FROM "humans" WHERE (1 = 1) ORDER BY "humans"."id" ASC , query.sql); try std.testing.expect(query.isValid());}
test "complex whereclause" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { id: i32, name: []const u8, age: i32, favorite_sport: []const u8, status: []const u8, }, .{ .relations = .{ .homes = hasMany(.Home, .{}) } }, );
pub const Home = Model(@This(), "homes", struct { id: i32, cat_id: i32, zip_code: []const u8 }, .{}); }; const query = Query(TestAdapter, Schema, .Cat).join(.inner, .homes).where(.{ .{ .name = "Hercules" }, .OR, .{ .name = "Heracles" }, .{ .{ .age, .gt, 4 }, .{ .age, .lt, 10 } }, .{ .favorite_sport, .like, "%ball" }, .{ .favorite_sport, .not_eql, "basketball" }, .{ "my_sql_function(age)", .eql, 100 }, .{ .NOT, .{ .{ .age = 1 }, .OR, .{ .age = 2 } } }, .{ "age / paws = ? or age * paws < ?", .{ 2, 10 } }, .{ .{ .status = null }, .OR, .{ .status = [_][]const u8{ "sleeping", "eating" } } }, .{ .homes = .{ .zip_code = "10304" } }, }); try std.testing.expect(query.isValid()); try std.testing.expectEqualStrings( \\SELECT "cats"."id", "cats"."name", "cats"."age", "cats"."favorite_sport", "cats"."status" FROM "cats" INNER JOIN "homes" AS "homes" ON "cats"."id" = "homes"."cat_id" WHERE ("cats"."name" = $1 OR "cats"."name" = $2 AND ("cats"."age" > $3 AND "cats"."age" < $4) AND "cats"."favorite_sport" LIKE $5 AND "cats"."favorite_sport" <> $6 AND my_sql_function(age) = $7 AND ( NOT ("cats"."age" = $8 OR "cats"."age" = $9)) AND age / paws = $10 or age * paws < $11 AND ("cats"."status" IS NULL OR "cats"."status" = ANY ($12)) AND "homes"."zip_code" = $13) ORDER BY "cats"."id" ASC , query.sql);}
test "boolean coercion https://github.com/jetzig-framework/jetquery/issues/1" { const Schema = struct { pub const Cat = Model( @This(), "cats", struct { a: bool, b: bool, c: bool, d: bool, }, .{}, ); }; const query = Query(TestAdapter, Schema, .Cat).insert(.{ .a = false, .b = 1, .c = true, .d = "1" }); try std.testing.expect(query.isValid()); try std.testing.expectEqualStrings( \\INSERT INTO "cats" ("a", "b", "c", "d") VALUES ($1, $2, $3, $4) , query.sql); try std.testing.expectEqual(query.values.@"0", false); try std.testing.expectEqual(query.values.@"1", true); try std.testing.expectEqual(query.values.@"2", true); try std.testing.expectEqual(query.values.@"3", true);}
test "optionals" { const Schema = struct { pub const Thing = Model( @This(), "things", struct { a: ?bool, b: ?[]const u8, c: ?i32, d: ?i64, e: ?f32, f: ?f64, g: ?DateTime, }, .{}, ); }; const query1 = Query(TestAdapter, Schema, .Thing).insert(.{ .a = @as(?bool, false), .b = @as(?[]const u8, "foo"), .c = @as(?i32, 100), .d = @as(?i64, 10000000000000), .e = @as(?f32, 100.1), .f = @as(?f64, 10000000000000.1), .g = @as(?DateTime, DateTime.now(std.testing.io)), }); try std.testing.expect(query1.isValid()); try std.testing.expectEqualStrings( \\INSERT INTO "things" ("a", "b", "c", "d", "e", "f", "g") VALUES ($1, $2, $3, $4, $5, $6, $7) , query1.sql);
const query2 = Query(TestAdapter, Schema, .Thing).where(.{ .a = @as(?bool, false), .b = @as(?[]const u8, "foo"), .c = @as(?i32, 100), .d = @as(?i64, 10000000000000), .e = @as(?f32, 100.1), .f = @as(?f64, 10000000000000.1), .g = @as(?DateTime, DateTime.now()), }); try std.testing.expect(query2.isValid()); try std.testing.expectEqualStrings( \\SELECT "things"."a", "things"."b", "things"."c", "things"."d", "things"."e", "things"."f", "things"."g" FROM "things" WHERE ("things"."a" IS NOT DISTINCT FROM $1 AND "things"."b" IS NOT DISTINCT FROM $2 AND "things"."c" IS NOT DISTINCT FROM $3 AND "things"."d" IS NOT DISTINCT FROM $4 AND "things"."e" IS NOT DISTINCT FROM $5 AND "things"."f" IS NOT DISTINCT FROM $6 AND "things"."g" IS NOT DISTINCT FROM $7) , query2.sql);
const query3 = Query(TestAdapter, Schema, .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(?DateTime, null), }); try std.testing.expect(query3.isValid()); try std.testing.expectEqualStrings( \\SELECT "things"."a", "things"."b", "things"."c", "things"."d", "things"."e", "things"."f", "things"."g" FROM "things" WHERE ("things"."a" IS NOT DISTINCT FROM $1 AND "things"."b" IS NOT DISTINCT FROM $2 AND "things"."c" IS NOT DISTINCT FROM $3 AND "things"."d" IS NOT DISTINCT FROM $4 AND "things"."e" IS NOT DISTINCT FROM $5 AND "things"."f" IS NOT DISTINCT FROM $6 AND "things"."g" IS NOT DISTINCT FROM $7) , query3.sql);}
test "handle multiple relations back to same table - https://github.com/jetzig-framework/jetquery/issues/15" { const Schema = struct { pub const Address = Model( @This(), "addresses", struct { id: i32, address: []const u8, }, .{ .relations = .{ .messages_from_address = hasMany(.Message, .{ .foreign_key = "from_address_id", }), .messages_to_address = hasMany(.Message, .{ .foreign_key = "to_address_id", }), }, }, );
pub const Message = Model( @This(), "messages", struct { id: i32, from_address_id: i32, to_address_id: i32, content: []const u8, }, .{ .relations = .{ .to_address = belongsTo(.Address, .{ .foreign_key = "to_address_id", }), .from_address = belongsTo(.Address, .{ .foreign_key = "from_address_id", }), }, }, ); }; const query = Query(TestAdapter, Schema, .Message).include(.to_address, .{}).include(.from_address, .{}); // This should create a query where the relations are used as table aliases try std.testing.expectEqualStrings( \\SELECT "messages"."id", "messages"."from_address_id", "messages"."to_address_id", "messages"."content", "to_address"."id", "to_address"."address", "from_address"."id", "from_address"."address" FROM "messages" INNER JOIN "addresses" AS "to_address" ON "messages"."to_address_id" = "to_address"."id" INNER JOIN "addresses" AS "from_address" ON "messages"."from_address_id" = "from_address"."id" WHERE (1 = 1) ORDER BY "messages"."id" ASC , query.sql); try std.testing.expect(query.isValid());}