Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/jetquery/adapters.zig
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,10 @@ pub fn Adapter(comptime adapter_name: Name, AdaptedRepo: type) type {
pub fn referenceSql(
self: Self,
comptime reference: jetquery.schema.Column.Reference,
comptime reference_options: ?jetquery.schema.Column.ReferenceOptions,
) []const u8 {
return switch (self) {
inline else => |adapter| @TypeOf(adapter).referenceSql(reference),
inline else => |adapter| @TypeOf(adapter).referenceSql(reference, reference_options),
};
}

Expand Down Expand Up @@ -254,7 +255,7 @@ pub fn Adapter(comptime adapter_name: Name, AdaptedRepo: type) type {
if (column.primary_key) self.primaryKeySql(column) else "",
if (column.options.unique) self.uniqueColumnSql() else "",
if (column.options.reference) |reference|
self.referenceSql(reference)
self.referenceSql(reference, column.options.reference_options)
else
"",
});
Expand Down
6 changes: 5 additions & 1 deletion src/jetquery/adapters/NullAdapter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,12 @@ pub fn uniqueColumnSql() []const u8 {
return "";
}

pub fn referenceSql(comptime reference: jetquery.schema.Column.Reference) []const u8 {
pub fn referenceSql(
comptime reference: jetquery.schema.Column.Reference,
comptime reference_options: ?jetquery.schema.Column.ReferenceOptions,
) []const u8 {
_ = reference;
_ = reference_options;
return "";
}

Expand Down
22 changes: 20 additions & 2 deletions src/jetquery/adapters/PostgresqlAdapter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,18 @@ pub fn uniqueColumnSql() []const u8 {
return " UNIQUE";
}

pub fn referenceSql(comptime reference: jetquery.schema.Column.Reference) []const u8 {
return std.fmt.comptimePrint(
pub fn referenceSql(
comptime reference: jetquery.schema.Column.Reference,
comptime reference_options: ?jetquery.schema.Column.ReferenceOptions,
) []const u8 {
const reference_str = std.fmt.comptimePrint(
" REFERENCES {s}({s})",
.{ comptime identifier(reference[0]), comptime identifier(reference[1]) },
);
if (reference_options) |reference_opts| {
return std.fmt.comptimePrint("{s} {s}", .{ reference_str, comptime deleteOption(reference_opts.on_delete) });
}
return reference_str;
}

pub fn reflect(
Expand Down Expand Up @@ -830,3 +837,14 @@ fn BindCoerce(T: type) type {
else => T,
};
}

fn deleteOption(comptime delete_option: ?jetquery.schema.Column.OnDeleteOption) []const u8 {
if (delete_option) |option| {
switch (option) {
.DELETE => return "ON DELETE CASCADE",
.NULL => return "ON DELETE SET NULL",
}
} else {
return "";
}
}
12 changes: 12 additions & 0 deletions src/jetquery/schema/Column.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ pub const Type = enum {
bigint,
double_precision,
};

pub const OnDeleteOption = enum {
CASCADE,
NULL,
};

pub const Reference = [2][]const u8;

pub const ReferenceOptions = struct {
on_delete: ?OnDeleteOption = null,
};

pub const Options = struct {
optional: bool = false,
index: bool = false,
index_name: ?[]const u8 = null,
unique: bool = false,
reference: ?Reference = null,
reference_options: ?ReferenceOptions = null,
length: ?u16 = null,
primary_key: bool = false,
};
Expand Down