-
Notifications
You must be signed in to change notification settings - Fork 0
Table structur
The Table type is provided as a struct that represents a named collection of rows (elements) in the database, with the following fields:
-
rows— dynamic list that holds all elements/rows of this table (std.ArrayList(Types.Element)) -
allocator— memory allocator used for rows list and internal allocations -
tname— name of the table (also used as type tag for validation of appended elements) -
data_buffer— optional buffer for serialized data (used during save/load operations)
-
initPurpose: creates an empty table with given name
Signature:init(tname: []const u8, allocator: std.mem.Allocator) Table
-
appendPurpose: adds new element (row) to the end of the table
Signature:append(self: *Table, item: Types.Element) !void
Checks that
item.tnamematches table name, otherwise returnserror.InvalidType -
appendManyPurpose: adds multiple elements (rows) to the end of the table efficiently
Signature:appendMany(self: *Table, items: []const Types.Element) !void
Pre-allocates required memory and iterates through
items. Validates that eachitem.tnamematches the table name, returningerror.InvalidTypeon the first mismatch. -
removePurpose: removes element at specified index and calls itsdeinit
Signature:remove(self: *@This(), index: usize) !void
Returns error.InvalidIndex if the index does not exist
-
clearPurpose: clears the table completely
Signature:clear(self: *@This()) void
-
getPurpose: returns element at given index (or null if index is out of bounds)
Signature:get(self: Table, idx: usize) ?Types.Element
-
iteratorPurpose: returns iterator that allows walking through all rows
Signature:iterator(self: *@This()) Types.TableIterator
-
lenPurpose: returns rows len
Signature:len(self: @This()) usize
-
deinitPurpose: deinitializes all contained elements and frees internal structures
Signature:deinit(self: *@This()) void
var table = Table.init("users", allocator);
defer table.deinit();
var elem1 = Types.Element{ .tname = "users", .field = std.StringHashMap(Types.FieldType).init(allocator) };
defer elem1.deinit();
try elem1.setInt("id", 1);
try elem1.setStr("name", "Alice");
var elem2 = Types.Element{ .tname = "users", .field = std.StringHashMap(Types.FieldType).init(allocator) };
defer elem2.deinit();
try elem2.setInt("id", 2);
try elem2.setStr("name", "Bob");
try table.appendMany(&.{elem1, elem2})// assuming table already contains data
if (table.get(0)) |row| {
const id = row.getInt("id") orelse 0;
const name = row.getStr("name") orelse "unknown";
std.debug.print("First row: {s} (id {d})\n", .{name, id});
}
if (table.get(1)) |row| {
const name = row.getStr("name") orelse "?";
std.debug.print("Second row name: {s}\n", .{name});
}var iter = table.iterator();
while (iter.next()) |*row| {
const id = row.*.getInt("id") orelse continue;
const name = row.*.getStr("name") orelse continue;
std.debug.print("→ id: {d:>4} | name: {s}\n", .{id, name});
}if (table.rows.items.len > 0) {
table.remove(0); // removes first row
std.debug.print("First row removed, now {d} rows left\n", .{table.rows.items.len});
}