From f2163337d5bbd410eec613f9efa6355fffb7a599 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Sun, 9 Aug 2026 16:34:53 -0700 Subject: [PATCH] add support for columns that foreign keys --- src/lib.zig | 12 ++++++++++++ src/postgresql.zig | 11 +++++++++++ src/sqlite3.zig | 6 ++++++ 3 files changed, 29 insertions(+) diff --git a/src/lib.zig b/src/lib.zig index f74f4330c9ae08df23f5d963261bf19c4011ebfa..fca69f388bd400263e4546fadbc56dc204d25545 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -86,8 +86,20 @@ pub const Engine = union(DriverType) { } pub fn addColumn(engine: *Engine, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void { + if (switch (@typeInfo(T)) { + .@"struct", .@"enum", .@"union" => @hasDecl(T, "foreign_table"), + else => false, + }) { + return addColumnForeign(engine, alloc, table_name, col_name, T, T.foreign_table, T.foreign_column); + } return switch (engine.*) { inline else => |*e| e.addColumn(alloc, table_name, col_name, T), }; } + + 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 { + return switch (engine.*) { + inline else => |*e| e.addColumnForeign(alloc, table_name, col_name, T, table_name2, col_name2), + }; + } }; diff --git a/src/postgresql.zig b/src/postgresql.zig index 26506964778e166bf95800b0df88e6c4e8af64f9..3dedd1dc0559acc5f5ad02c15d3d6de925d650a7 100644 --- a/src/postgresql.zig +++ b/src/postgresql.zig @@ -284,6 +284,17 @@ pub fn addColumn(driver: *Driver, alloc: std.mem.Allocator, comptime table_name: @panic("TODO"); } +pub 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 { + _ = driver; + _ = alloc; + _ = table_name; + _ = col_name; + _ = T; + _ = table_name2; + _ = col_name2; + @panic("TODO"); +} + pub fn nameForType(T: type) []const u8 { if (@typeInfo(T) == .optional) { return nameForType2(T); diff --git a/src/sqlite3.zig b/src/sqlite3.zig index 9a4f119b0efec1254064c3bf00cc26d0b8fa45ec..36c255818905970cc85a784729e8c0392b235718 100644 --- a/src/sqlite3.zig +++ b/src/sqlite3.zig @@ -107,6 +107,12 @@ pub fn addColumn(self: *Self, alloc: std.mem.Allocator, comptime table_name: []c try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s}", .{ table_name, col_name, nameForType(T) }), .{}); } +pub 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 { + const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name }); + defer t.end(); + 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 }), .{}); +} + pub fn nameForType(T: type) []const u8 { if (@typeInfo(T) == .optional) { return nameForType2(@typeInfo(T).optional.child); -- 2.54.0