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,