Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120const std = @import("std");
const jetquery = @import("../root.zig");
/// Abstraction of a database table. Define a schema with:/// ```zig/// const Schema = struct {/// pub const Cat = Table("cats", struct { name: []const u8, paws: usize }, .{});/// };/// ```pub fn Model(Schema: type, comptime table_name: []const u8, T: type, options: anytype) type { return struct { // TODO: Implement `format()`
pub const Definition = T; const Self = @This(); pub const name = table_name; pub const info = .{ .schema = Schema };
pub const relations = if (@hasField( @TypeOf(options), "relations", )) options.relations else .{};
pub const primary_key = if (@hasField( @TypeOf(options), "primary_key", )) jetquery.util.stringMaybeEnum(options.primary_key) else "id";
pub fn init(args: anytype) RecordType(@TypeOf(args)) { var record: RecordType(@TypeOf(args)) = undefined;
record.__jetquery = .{ .args = args };
inline for (@typeInfo(@TypeOf(args)).@"struct".field_names) |field_name| { @field(record, field_name) = @field(args, field_name); } return record; }
pub fn columns() [jetquery.fields.structFields(Definition).len]jetquery.columns.Column { comptime { const fields = jetquery.fields.structFields(Definition); var buf: [fields.len]jetquery.columns.Column = undefined; for (fields, 0..) |field, index| { buf[index] = .{ .name = field.name, .table = @This(), .type = field.type, }; } return buf; } }
pub fn column(comptime column_name: []const u8) jetquery.columns.Column { comptime { return for (columns()) |col| { if (std.mem.eql(u8, column_name, col.name)) break col; } else @compileError(std.fmt.comptimePrint( "No column named `{s}` defined in Schema for `{s}'", .{ column_name, table_name }, )); } }
fn RecordType(Args: type) type { comptime { const fields = jetquery.fields.structFields(T); var struct_fields: [fields.len + 3]jetquery.fields.StructField = undefined;
for (fields, 0..) |field, index| { struct_fields[index] = jetquery.fields.structField(field.name, field.type); }
struct_fields[fields.len] = jetquery.fields.structFieldComptime( "__jetquery_model", @This(), );
struct_fields[fields.len + 1] = jetquery.fields.structFieldComptime( "__jetquery_schema", Schema, );
const args_field = jetquery.fields.structField("args", Args); const JetQuery = jetquery.fields.structType(&.{args_field});
struct_fields[fields.len + 2] = jetquery.fields.structField( "__jetquery", JetQuery, );
return jetquery.fields.structType(&struct_fields); } }
pub fn defaultForeignKey() []const u8 { comptime { for (@typeInfo(Schema).@"struct".decl_names) |decl| { const table = @field(Schema, decl);
if (std.mem.eql(u8, table.name, @This().name)) { var buf: [decl.len]u8 = undefined; return std.ascii.lowerString(&buf, decl) ++ "_id"; } }
@compileError("Failed matching `" ++ @typeName(@This()) ++ "` in schema."); } }
pub fn defaultOrderBy() []const jetquery.sql.OrderClause { if (!@hasField(Definition, primary_key)) return &.{};
return &.{.{ .column = column(primary_key), .direction = .ascending }}; } };}