diff options
Diffstat (limited to 'src/db/sqlite.zig')
| -rw-r--r-- | src/db/sqlite.zig | 82 |
1 files changed, 40 insertions, 42 deletions
diff --git a/src/db/sqlite.zig b/src/db/sqlite.zig index 55cb60a..0ef1b20 100644 --- a/src/db/sqlite.zig +++ b/src/db/sqlite.zig @@ -8,7 +8,7 @@ const Writer = std.Io.Writer; pub fn Database(comptime T: type) type { const tables = switch (@typeInfo(T)) { .@"struct" => |s| s.fields, - else => @compileError("Database schema must be of type `struct`"), + else => @compileError("Database schema must be of type `struct`"), }; // NOTE: `std.math.log2_int_ceil(@TypeOf(tables.len), tables.len)` panics @@ -16,14 +16,14 @@ pub fn Database(comptime T: type) type { // a (useless) schema that does not define any tables. const TagIntTables = @Int(.unsigned, @bitSizeOf(@TypeOf(tables.len)) - @clz(tables.len)); - var table_names : [tables.len][]const u8 = undefined; - var table_types : [tables.len]type = undefined; + var table_names: [tables.len][]const u8 = undefined; + var table_types: [tables.len]type = undefined; var table_values: [tables.len]TagIntTables = undefined; inline for (&table_names, &table_types, &table_values, tables, 0..) |*table_name, *table_type, *table_value, table, table_index| { const columns = switch (@typeInfo(table.type)) { .@"struct" => |s| s.fields, - else => @compileError("Table `" ++ table.name ++ "` must be of type `struct`"), + else => @compileError("Table `" ++ table.name ++ "` must be of type `struct`"), }; // NOTE: A table must have at least one column; see @@ -32,8 +32,8 @@ pub fn Database(comptime T: type) type { const TagIntColumns = @Int(.unsigned, std.math.log2_int_ceil(@TypeOf(columns.len), columns.len)); - var column_names : [columns.len][]const u8 = undefined; - var column_types : [columns.len]type = undefined; + var column_names: [columns.len][]const u8 = undefined; + var column_types: [columns.len]type = undefined; var column_values: [columns.len]TagIntColumns = undefined; inline for (&column_names, &column_types, &column_values, columns, 0..) |*column_name, *column_type, *column_value, column, column_index| { @@ -49,15 +49,15 @@ pub fn Database(comptime T: type) type { } const TagColumns = @Enum(TagIntColumns, .exhaustive, &column_names, &column_values); - const Columns = @Union(.auto, TagColumns, &column_names, &column_types, &@splat(.{})); + const Columns = @Union(.auto, TagColumns, &column_names, &column_types, &@splat(.{})); - table_name.* = table.name; - table_type.* = Columns; + table_name.* = table.name; + table_type.* = Columns; table_value.* = table_index; } const TagTables = @Enum(TagIntTables, .exhaustive, &table_names, &table_values); - const Tables = @Union(.auto, TagTables, &table_names, &table_types, &@splat(.{})); + const Tables = @Union(.auto, TagTables, &table_names, &table_types, &@splat(.{})); return struct { handle: *sqlite.sqlite3, @@ -87,14 +87,14 @@ pub fn Database(comptime T: type) type { }; const Schema = Tables; - const Self = @This(); + const Self = @This(); // Source: https://sqlite.org/c3ref/open.html pub fn init(kind: Kind) Error!Self { const name = switch (kind) { .temp_in_memory => ":memory:", - .temp_on_disk => "", - .path => |path| path, + .temp_on_disk => "", + .path => |path| path, }; var handle: ?*sqlite.sqlite3 = null; @@ -179,7 +179,6 @@ fn Typename(comptime affinity: Affinity) type { return struct { type : type = T, affinity: Affinity = affinity, - default_value: ?T = null, is_not_null : bool = false, is_unique : bool = false, @@ -610,7 +609,6 @@ fn Select(comptime Schema: type, comptime from: std.meta.Tag(Schema), comptime c } }; } - /// SQLite result codes. /// /// Primary result codes define a broad category of results, whereas extended @@ -829,7 +827,7 @@ test { }; }; - var db: Database(Schema) = try .init(.temp_in_memory); + var db: Database(Schema) = try .init(.temp_in_memory); defer assert(db.deinit() == .ok); const query = try db.select(.contacts, &.{ .first_name , .last_name, .age }) @@ -847,30 +845,30 @@ test { } // test { - // NOTE: we don't use `union(enum)` here as if a column has a default value, - // an integer tag type is needed explicitely, which, from a usability - // standpoint, is not great; we can calulate this value in `Schema.from`. - // - // CREATE TABLE contacts ( - // contact_id INTEGER PRIMARY KEY, - // first_name TEXT NOT NULL DEFAULT "Hans", - // last_name TEXT NOT NULL DEFAULT "Mustermann", - // nickname TEXT, - // email TEXT NOT NULL UNIQUE, - // phone TEXT NOT NULL UNIQUE - // ); - // const S = struct { - // contacts: struct { - // contact_id: PrimaryKey(Integer, .{}), - // first_name: VarChar(255, .{}) = "Hans", - // last_name: VarChar(255, .{}) = "Mustermann", - // nickname: ?VarChar(255, .{}), - // email: VarChar(255, .{.unique}), - // phone: VarChar(255, .{.unique}), - // }, - // }; - // - // // NOTE: init connects to DB and creates tables if not already present - // var db: Database(Schema(S)) = try .init(.temp_in_memory); - // db.deinit(); +// NOTE: we don't use `union(enum)` here as if a column has a default value, +// an integer tag type is needed explicitely, which, from a usability +// standpoint, is not great; we can calulate this value in `Schema.from`. +// +// CREATE TABLE contacts ( +// contact_id INTEGER PRIMARY KEY, +// first_name TEXT NOT NULL DEFAULT "Hans", +// last_name TEXT NOT NULL DEFAULT "Mustermann", +// nickname TEXT, +// email TEXT NOT NULL UNIQUE, +// phone TEXT NOT NULL UNIQUE +// ); +// const S = struct { +// contacts: struct { +// contact_id: PrimaryKey(Integer, .{}), +// first_name: VarChar(255, .{}) = "Hans", +// last_name: VarChar(255, .{}) = "Mustermann", +// nickname: ?VarChar(255, .{}), +// email: VarChar(255, .{.unique}), +// phone: VarChar(255, .{.unique}), +// }, +// }; +// +// // NOTE: init connects to DB and creates tables if not already present +// var db: Database(Schema(S)) = try .init(.temp_in_memory); +// db.deinit(); // } |