feat: Restore db from file.

This commit is contained in:
2026-05-03 13:03:14 -04:00
parent d00055aa3e
commit 336bd37613
6 changed files with 283 additions and 91 deletions

View File

@@ -226,6 +226,7 @@ fn ParseType(comptime type_info: []const u8) type {
const signedness = switch (type_info[0]) {
'u' => .unsigned,
'i' => .signed,
else => unreachable,
};
return @Int(signedness, std.fmt.parseInt(usize, type_info[1..type_info.len], 10) catch {
@compileError("invalid type info " ++ type_info);

View File

@@ -3,7 +3,7 @@ const builtin = @import("builtin");
const build_options = @import("build_options");
const debug = std.debug;
const heap = std.heap;
const io = std.io;
const io = std.Io;
const mem = std.mem;
const testing = std.testing;
@@ -1967,7 +1967,7 @@ pub const DynamicStatement = struct {
pub fn all(self: *Self, comptime Type: type, allocator: mem.Allocator, options: QueryOptions, values: anytype) ![]Type {
var iter = try self.iteratorAlloc(Type, allocator, values);
var rows: std.ArrayList(Type) = .{};
var rows: std.ArrayList(Type) = .empty;
while (try iter.nextAlloc(allocator, options)) |row| {
try rows.append(allocator, row);
}
@@ -2257,7 +2257,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: anytype) type
pub fn all(self: *Self, comptime Type: type, allocator: mem.Allocator, options: QueryOptions, values: anytype) ![]Type {
var iter = try self.iteratorAlloc(Type, allocator, values);
var rows: std.ArrayList(Type) = .{};
var rows: std.ArrayList(Type) = .empty;
while (try iter.nextAlloc(allocator, options)) |row| {
try rows.append(allocator, row);
}
@@ -3020,7 +3020,7 @@ test "sqlite: statement iterator" {
var stmt = try db.prepare("INSERT INTO user(name, id, age, weight, favorite_color) VALUES(?{[]const u8}, ?{usize}, ?{usize}, ?{f32}, ?{[]const u8})");
defer stmt.deinit();
var expected_rows: std.ArrayList(TestUser) = .{};
var expected_rows: std.ArrayList(TestUser) = .empty;
var i: usize = 0;
while (i < 20) : (i += 1) {
const name = try std.fmt.allocPrint(allocator, "Vincent {d}", .{i});
@@ -3047,7 +3047,7 @@ test "sqlite: statement iterator" {
var iter = try stmt2.iterator(RowType, .{});
var rows: std.ArrayList(RowType) = .{};
var rows: std.ArrayList(RowType) = .empty;
while (try iter.next(.{})) |row| {
try rows.append(allocator, row);
}
@@ -3074,7 +3074,7 @@ test "sqlite: statement iterator" {
var iter = try stmt2.iterator(RowType, .{});
var rows: std.ArrayList(RowType) = .{};
var rows: std.ArrayList(RowType) = .empty;
while (try iter.nextAlloc(allocator, .{})) |row| {
try rows.append(allocator, row);
}
@@ -3459,7 +3459,7 @@ test "sqlite: bind runtime slice" {
const allocator = arena.allocator();
// creating array list on heap so that it's deemed runtime size
var list: std.ArrayList([]const u8) = .{};
var list: std.ArrayList([]const u8) = .empty;
defer list.deinit(allocator);
try list.append(allocator, "this is some data");
const args = try list.toOwnedSlice(allocator);
@@ -3749,7 +3749,11 @@ test "sqlite: create aggregate function with no aggregate context" {
var db = try getTestDb();
defer db.deinit();
var rand = std.Random.DefaultPrng.init(@intCast(std.time.milliTimestamp()));
const test_io = testing.io;
var rand = std.Random.DefaultPrng.init(@intCast(
std.Io.Timestamp.now(test_io, .real).toMilliseconds(),
));
// Create an aggregate function working with a MyContext
@@ -3810,7 +3814,11 @@ test "sqlite: create aggregate function with an aggregate context" {
var db = try getTestDb();
defer db.deinit();
var rand = std.Random.DefaultPrng.init(@intCast(std.time.milliTimestamp()));
const test_io = std.testing.io;
var rand = std.Random.DefaultPrng.init(
@intCast(std.Io.Timestamp.now(test_io, .real).toMilliseconds()),
);
try db.createAggregateFunction(
"mySum",
@@ -3877,7 +3885,7 @@ test "sqlite: empty slice" {
defer db.deinit();
try addTestData(&db);
var list: std.ArrayList(u8) = .{};
var list: std.ArrayList(u8) = .empty;
const ptr = try list.toOwnedSlice(allocator);
try db.exec("INSERT INTO article(author_id, data) VALUES(?, ?)", .{}, .{ 1, ptr });
@@ -4054,7 +4062,7 @@ test "reuse same field twice in query string" {
test "fuzzing" {
const Context = struct {
fn testOne(_: @This(), input: []const u8) anyerror!void {
fn testOne(_: @This(), input: *testing.Smith) anyerror!void {
var db = try Db.init(.{
.mode = .Memory,
.open_flags = .{
@@ -4066,7 +4074,7 @@ test "fuzzing" {
try db.exec("CREATE TABLE test(id integer primary key, name text, data blob)", .{}, .{});
db.execDynamic(input, .{}, .{}) catch |err| switch (err) {
db.execDynamic(input.value([]const u8), .{}, .{}) catch |err| switch (err) {
error.SQLiteError => return,
error.ExecReturnedData => return,
error.EmptyQuery => return,

View File

@@ -766,7 +766,8 @@ pub fn VirtualTable(
//
const nullable_state: ?*State = @fieldParentPtr("vtab", vtab);
const vtab_ptr: *c.sqlite3_vtab = @ptrCast(vtab);
const nullable_state: ?*State = @fieldParentPtr("vtab", vtab_ptr);
const state = nullable_state orelse unreachable;
var arena = heap.ArenaAllocator.init(state.module_context.allocator);
@@ -789,7 +790,8 @@ pub fn VirtualTable(
}
fn xDisconnect(vtab: [*c]c.sqlite3_vtab) callconv(.c) c_int {
const nullable_state: ?*State = @fieldParentPtr("vtab", vtab);
const vtab_ptr: *c.sqlite3_vtab = @ptrCast(vtab);
const nullable_state: ?*State = @fieldParentPtr("vtab", vtab_ptr);
const state = nullable_state orelse unreachable;
state.deinit();
@@ -806,7 +808,8 @@ pub fn VirtualTable(
}
fn xOpen(vtab: [*c]c.sqlite3_vtab, vtab_cursor: [*c][*c]c.sqlite3_vtab_cursor) callconv(.c) c_int {
const nullable_state: ?*State = @fieldParentPtr("vtab", vtab);
const vtab_ptr: *c.sqlite3_vtab = @ptrCast(vtab);
const nullable_state: ?*State = @fieldParentPtr("vtab", vtab_ptr);
const state = nullable_state orelse unreachable;
const cursor_state = CursorState.init(state.module_context, state.table) catch |err| {
@@ -819,7 +822,8 @@ pub fn VirtualTable(
}
fn xClose(vtab_cursor: [*c]c.sqlite3_vtab_cursor) callconv(.c) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const vtab_cursor_ptr: *c.sqlite3_vtab_cursor = @ptrCast(vtab_cursor);
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor_ptr);
const cursor_state = nullable_cursor_state orelse unreachable;
cursor_state.deinit();
@@ -828,7 +832,8 @@ pub fn VirtualTable(
}
fn xEof(vtab_cursor: [*c]c.sqlite3_vtab_cursor) callconv(.c) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const vtab_cursor_ptr: *c.sqlite3_vtab_cursor = @ptrCast(vtab_cursor);
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor_ptr);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
@@ -866,7 +871,8 @@ pub fn VirtualTable(
}
fn xFilter(vtab_cursor: [*c]c.sqlite3_vtab_cursor, idx_num: c_int, idx_str: [*c]const u8, argc: c_int, argv: [*c]?*c.sqlite3_value) callconv(.c) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const vtab_cursor_ptr: *c.sqlite3_vtab_cursor = @ptrCast(vtab_cursor);
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor_ptr);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
@@ -892,7 +898,8 @@ pub fn VirtualTable(
}
fn xNext(vtab_cursor: [*c]c.sqlite3_vtab_cursor) callconv(.c) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const vtab_cursor_ptr: *c.sqlite3_vtab_cursor = @ptrCast(vtab_cursor);
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor_ptr);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
@@ -911,7 +918,8 @@ pub fn VirtualTable(
}
fn xColumn(vtab_cursor: [*c]c.sqlite3_vtab_cursor, ctx: ?*c.sqlite3_context, n: c_int) callconv(.c) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const vtab_cursor_ptr: *c.sqlite3_vtab_cursor = @ptrCast(vtab_cursor);
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor_ptr);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
@@ -955,7 +963,8 @@ pub fn VirtualTable(
}
fn xRowid(vtab_cursor: [*c]c.sqlite3_vtab_cursor, row_id_ptr: [*c]c.sqlite3_int64) callconv(.c) c_int {
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor);
const vtab_cursor_ptr: *c.sqlite3_vtab_cursor = @ptrCast(vtab_cursor);
const nullable_cursor_state: ?*CursorState = @fieldParentPtr("vtab_cursor", vtab_cursor_ptr);
const cursor_state = nullable_cursor_state orelse unreachable;
const cursor = cursor_state.cursor;
@@ -1023,7 +1032,9 @@ const TestVirtualTable = struct {
//
const data = &[_][]const u8{
"Vincent", "José", "Michel",
"Vincent",
"José",
"Michel",
};
var rand = std.Random.DefaultPrng.init(204882485);
@@ -1064,13 +1075,18 @@ const TestVirtualTable = struct {
debug.print("connect\n", .{});
}
pub const BuildBestIndexError = error{} || mem.Allocator.Error;
pub const BuildBestIndexError = error{} || mem.Allocator.Error || error{WriteFailed};
pub fn buildBestIndex(self: *TestVirtualTable, diags: *VTabDiagnostics, builder: *BestIndexBuilder) BuildBestIndexError!void {
pub fn buildBestIndex(
self: *TestVirtualTable,
diags: *VTabDiagnostics,
builder: *BestIndexBuilder,
) BuildBestIndexError!void {
_ = self;
_ = diags;
var id_str_writer = builder.id_str_buffer.writer(builder.allocator);
// var id_str_writer = builder.id_str_buffer.writer(builder.allocator);
var id_str_writer = std.Io.Writer.fromArrayList(&builder.id_str_buffer);
var argv_index: i32 = 0;
for (builder.constraints) |*constraint| {
@@ -1174,7 +1190,7 @@ const TestVirtualTableCursor = struct {
// 3 chars for the '=' marker
// 6 chars because we format all columns in a 6 char wide string
const col_str = id[pos + 1 .. pos + 1 + 6];
const col = try fmt.parseInt(i32, mem.trimRight(u8, col_str, " "), 10);
const col = try fmt.parseInt(i32, mem.trimEnd(u8, col_str, " "), 10);
id = id[pos + 1 + 6 ..];