From 66ae64bdb507e6fdd066683c331e3aaa8350a8c7 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Wed, 8 Jul 2026 18:04:01 -0700 Subject: [PATCH] sqlite: handle default values now that nameForType handles 'not null' --- src/sqlite3.zig | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/sqlite3.zig b/src/sqlite3.zig index 3f4c7a92e39771ab7eb7ecb927c3d3b3206290ef..9a4f119b0efec1254064c3bf00cc26d0b8fa45ec 100644 --- a/src/sqlite3.zig +++ b/src/sqlite3.zig @@ -111,7 +111,7 @@ pub fn nameForType(T: type) []const u8 { if (@typeInfo(T) == .optional) { return nameForType2(@typeInfo(T).optional.child); } - return nameForType2(T) ++ " not null"; + return nameForType2(T) ++ " not null default (" ++ defaultForType(T) ++ ")"; } pub fn nameForType2(T: type) []const u8 { @@ -137,6 +137,28 @@ pub fn nameForType2(T: type) []const u8 { @compileError(@typeName(T)); // TODO } +pub fn defaultForType(T: type) []const u8 { + const info = @typeInfo(T); + if (info == .bool) { + return "false"; + } + if (info == .@"struct") { + const sinfo = info.@"struct"; + if (@hasDecl(T, "BaseType")) return defaultForType(T.BaseType); + if (sinfo.layout == .@"packed") return defaultForType(sinfo.backing_integer.?); + } + if (comptime extras.isZigString(T)) { + return "''"; + } + if (info == .int) { + return "0"; + } + if (info == .@"enum") { + return defaultForType(T.BaseType); + } + @compileError(@typeName(T)); // TODO +} + pub const Pragma = struct { pub const TableInfo = struct { cid: u16, -- 2.54.0