authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-21 17:26:48 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-21 17:26:48 -07:00
log80e34e73bbaed04800ff5e7ee44416606c3039f8
tree67853c2fc40fb652b33ea625537f303fcff3c644
parent2eb196d362acac424242f55d806adfdf33123d88
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add createTable and addColumn semantic methods


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

src/lib.zig+12
...@@ -90,4 +90,16 @@ pub const Engine = union(DriverType) {...@@ -90,4 +90,16 @@ pub const Engine = union(DriverType) {
90 inline else => |*e| e.hasColumnWithName(alloc, table, column),90 inline else => |*e| e.hasColumnWithName(alloc, table, column),
91 };91 };
92 }92 }
93
94 pub fn createTable(engine: *Engine, alloc: std.mem.Allocator, comptime name: []const u8, comptime pk_name: []const u8, pk_type: type) !void {
95 return switch (engine.*) {
96 inline else => |*e| e.createTable(alloc, name, pk_name, pk_type),
97 };
98 }
99
100 pub fn addColumn(engine: *Engine, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void {
101 return switch (engine.*) {
102 inline else => |*e| e.addColumn(alloc, table_name, col_name, T),
103 };
104 }
93};105};
src/postgresql.zig+30
...@@ -281,6 +281,36 @@ pub fn hasColumnWithName(driver: *Driver, alloc: std.mem.Allocator, comptime tab...@@ -281,6 +281,36 @@ pub fn hasColumnWithName(driver: *Driver, alloc: std.mem.Allocator, comptime tab
281 @panic("TODO");281 @panic("TODO");
282}282}
283283
284pub fn createTable(driver: *Driver, alloc: std.mem.Allocator, comptime name: []const u8, comptime pk_name: []const u8, pk_type: type) !void {
285 _ = driver;
286 _ = alloc;
287 _ = name;
288 _ = pk_name;
289 _ = pk_type;
290 @panic("TODO");
291}
292
293pub fn addColumn(driver: *Driver, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void {
294 _ = driver;
295 _ = alloc;
296 _ = table_name;
297 _ = col_name;
298 _ = T;
299 @panic("TODO");
300}
301
302pub fn nameForType(T: type) []const u8 {
303 if (@typeInfo(T) == .optional) {
304 return nameForType2(T);
305 }
306 return nameForType2(T) ++ " not null";
307}
308
309pub fn nameForType2(T: type) []const u8 {
310 _ = T;
311 @panic("TODO");
312}
313
284//314//
285// CancelRequest315// CancelRequest
286// GSSENCRequest316// GSSENCRequest
src/sqlite3.zig+43
...@@ -2,6 +2,7 @@ const std = @import("std");...@@ -2,6 +2,7 @@ const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const sqlite = @import("sqlite");3const sqlite = @import("sqlite");
4const tracer = @import("tracer");4const tracer = @import("tracer");
5const extras = @import("extras");
56
6const Self = @This();7const Self = @This();
78
...@@ -104,6 +105,48 @@ pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table:...@@ -104,6 +105,48 @@ pub fn hasColumnWithName(self: *Self, alloc: std.mem.Allocator, comptime table:
104 return false;105 return false;
105}106}
106107
108pub fn createTable(self: *Self, alloc: std.mem.Allocator, comptime name: []const u8, comptime pk_name: []const u8, pk_type: type) !void {
109 const t = tracer.trace(@src(), " {s} ({s})", .{ name, pk_name });
110 defer t.end();
111 try self.exec(alloc, comptime std.fmt.comptimePrint("create table {s}({s} {s} primary key not null)", .{ name, pk_name, nameForType2(pk_type) }), .{});
112}
113
114pub fn addColumn(self: *Self, alloc: std.mem.Allocator, comptime table_name: []const u8, comptime col_name: []const u8, T: type) !void {
115 const t = tracer.trace(@src(), " {s}.{s}", .{ table_name, col_name });
116 defer t.end();
117 try self.exec(alloc, comptime std.fmt.comptimePrint("alter table {s} add \"{s}\" {s}", .{ table_name, col_name, nameForType(T) }), .{});
118}
119
120pub fn nameForType(T: type) []const u8 {
121 if (@typeInfo(T) == .optional) {
122 return nameForType2(@typeInfo(T).optional.child);
123 }
124 return nameForType2(T) ++ " not null";
125}
126
127pub fn nameForType2(T: type) []const u8 {
128 const tinfo = @typeInfo(T);
129
130 if (comptime extras.isZigString(T)) {
131 return "text";
132 }
133 if (tinfo == .@"struct") {
134 const info = tinfo.@"struct";
135 if (@hasDecl(T, "BaseType")) return nameForType2(T.BaseType);
136 if (info.layout == .@"packed") return nameForType2(info.backing_integer.?);
137 }
138 if (tinfo == .@"enum") {
139 return nameForType2(T.BaseType);
140 }
141 if (tinfo == .int or tinfo == .bool) {
142 return "integer";
143 }
144 if (comptime extras.isArrayOf(u8)(T)) {
145 return "blob";
146 }
147 @compileError(@typeName(T)); // TODO
148}
149
107pub const Pragma = struct {150pub const Pragma = struct {
108 pub const TableInfo = struct {151 pub const TableInfo = struct {
109 cid: u16,152 cid: u16,