authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-08-09 16:34:53-07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-08-09 16:34:53-07:00
logf2163337d5bbd410eec613f9efa6355fffb7a599
treeed7524a3087a2d0fab5588a4a5959f6633aebe25
parent66ae64bdb507e6fdd066683c331e3aaa8350a8c7
signature Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add support for columns that foreign keys


3 files changed, 29 insertions(+), 0 deletions(-)

src/lib.zig+12
...@@ -86,8 +86,20 @@ pub const Engine = union(DriverType) {...@@ -86,8 +86,20 @@ pub const Engine = union(DriverType) {
86 }86 }
8787
88 pub fn addColumn(engine: *Engine, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void {88 pub fn addColumn(engine: *Engine, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void {
89 if (switch (@typeInfo(T)) {
90 .@"struct", .@"enum", .@"union" => @hasDecl(T, "foreign_table"),
91 else => false,
92 }) {
93 return addColumnForeign(engine, alloc, table_name, col_name, T, T.foreign_table, T.foreign_column);
94 }
89 return switch (engine.*) {95 return switch (engine.*) {
90 inline else => |*e| e.addColumn(alloc, table_name, col_name, T),96 inline else => |*e| e.addColumn(alloc, table_name, col_name, T),
91 };97 };
92 }98 }
99
100 pub fn addColumnForeign(engine: *Engine, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type, comptime table_name2: []const u8, comptime col_name2: []const u8) !void {
101 return switch (engine.*) {
102 inline else => |*e| e.addColumnForeign(alloc, table_name, col_name, T, table_name2, col_name2),
103 };
104 }
93};105};
src/postgresql.zig+11
...@@ -284,6 +284,17 @@ pub fn addColumn(driver: *Driver, alloc: std.mem.Allocator, comptime table_name:...@@ -284,6 +284,17 @@ pub fn addColumn(driver: *Driver, alloc: std.mem.Allocator, comptime table_name:
284 @panic("TODO");284 @panic("TODO");
285}285}
286286
287pub fn addColumnForeign(driver: *Driver, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type, comptime table_name2: []const u8, comptime col_name2: []const u8) !void {
288 _ = driver;
289 _ = alloc;
290 _ = table_name;
291 _ = col_name;
292 _ = T;
293 _ = table_name2;
294 _ = col_name2;
295 @panic("TODO");
296}
297
287pub fn nameForType(T: type) []const u8 {298pub fn nameForType(T: type) []const u8 {
288 if (@typeInfo(T) == .optional) {299 if (@typeInfo(T) == .optional) {
289 return nameForType2(T);300 return nameForType2(T);
src/sqlite3.zig+6
...@@ -107,6 +107,12 @@ pub fn addColumn(self: *Self, alloc: std.mem.Allocator, comptime table_name: []c...@@ -107,6 +107,12 @@ pub fn addColumn(self: *Self, alloc: std.mem.Allocator, comptime table_name: []c
107 try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s}", .{ table_name, col_name, nameForType(T) }), .{});107 try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s}", .{ table_name, col_name, nameForType(T) }), .{});
108}108}
109109
110pub fn addColumnForeign(self: *Self, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type, comptime table_name2: []const u8, comptime col_name2: []const u8) !void {
111 const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name });
112 defer t.end();
113 try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s} references \"{s}\" (\"{s}\")", .{ table_name, col_name, nameForType(T), table_name2, col_name2 }), .{});
114}
115
110pub fn nameForType(T: type) []const u8 {116pub fn nameForType(T: type) []const u8 {
111 if (@typeInfo(T) == .optional) {117 if (@typeInfo(T) == .optional) {
112 return nameForType2(@typeInfo(T).optional.child);118 return nameForType2(@typeInfo(T).optional.child);