2 Commits

Author SHA1 Message Date
acbda090d7 feat: list cmd. 2026-05-27 19:30:19 -04:00
fc8474d721 feat: Restore db from file. 2026-05-27 18:27:21 -04:00
8 changed files with 366 additions and 113 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -7,7 +7,7 @@ const age = @import("age.zig");
const Config = @import("Config.zig");
/// controls the keys and filepaths used for saving
config: Config,
opts: OpenOptions,
/// The underlying data store.
sql_db: sqlite.Db,
@@ -30,20 +30,20 @@ pub fn open(
});
defer gpa.free(db_path);
var db = try new(opts.config);
if (db_exists(io, db_path)) {
// const tmp_dir = try std.Io.Dir.cwd().openDir(io, tmp, .{});
// defer tmp_dir.deleteFile(io, "envr.db");
const tmp_db_path = try std.fs.path.join(gpa, &.{ opts.tmp, "envr.db" });
const tmp_db_path = try std.fs.path.joinZ(gpa, &.{ opts.tmp, "envr.db" });
defer gpa.free(tmp_db_path);
if (db_exists(io, db_path)) {
// TODO: Use std.MultiArrayList? Had json issues
{
var private_keys: std.ArrayList([]const u8) = try .initCapacity(
gpa,
opts.config.keys.len,
);
defer private_keys.deinit(gpa);
for (opts.config.keys) |key| {
private_keys.appendAssumeCapacity(key.private);
@@ -51,14 +51,10 @@ pub fn open(
// TODO: Pass key(s) from Config
try age.decrypt(io, gpa, private_keys.items, db_path, tmp_db_path);
try db.restore(tmp_db_path);
try std.Io.Dir.cwd().deleteFile(io, tmp_db_path);
return db;
} else {
return db;
}
}
return open_decrypted(opts, tmp_db_path);
}
const OpenOptions = struct {
@@ -71,16 +67,19 @@ const OpenOptions = struct {
tmp: []const u8 = "/tmp",
};
/// Create a new instance of the database in-memory
fn new(config: Config) !@This() {
/// Create a new instance of the database
fn open_decrypted(opts: OpenOptions, tmp_db_path: [:0]const u8) !@This() {
var db = try sqlite.Db.init(.{
.mode = .Memory,
.open_flags = .{ .write = true, .create = true },
.mode = .{ .File = tmp_db_path },
.open_flags = .{
.write = true,
.create = true,
},
.threading_mode = .MultiThread,
});
try db.exec(
\\create table envr_env_files (
\\create table if not exists envr_env_files (
\\ path text primary key not null
\\, remotes text -- JSON
\\, sha256 text not null
@@ -90,7 +89,7 @@ fn new(config: Config) !@This() {
return .{
.sql_db = db,
.config = config,
.opts = opts,
};
}
@@ -103,26 +102,6 @@ fn db_exists(io: std.Io, path: []const u8) bool {
}
}
/// Loads the unencrypted sqlite db at filepath path into the datbase
/// FIXME: Test me
fn restore(
self: *@This(),
path: []const u8,
) !void {
try self.sql_db.exec(
"ATTACH DATABASE ? AS source",
.{},
.{path},
);
defer self.sql_db.exec("DETACH DATABASE source", .{}, .{}) catch unreachable;
try self.sql_db.exec(
"INSERT INTO main.envr_env_files SELECT * FROM source.envr_env_files",
.{},
.{},
);
}
// TODO: Finish
// pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
// var random_bytes: [TmpDir.random_bytes_count]u8 = undefined;
@@ -140,30 +119,32 @@ pub fn close(
self: *@This(),
io: std.Io,
gpa: std.mem.Allocator,
opts: OpenOptions,
) !void {
defer self.sql_db.deinit();
if (self.changed) {
const tmp_db_path = try std.fs.path.join(gpa, &.{ opts.tmp, "envr.db" });
const tmp_db_path = try std.fs.path.join(gpa, &.{ self.opts.tmp, "envr.db" });
defer gpa.free(tmp_db_path);
try self.sql_db.exec("VACUUM INTO ?", .{}, .{tmp_db_path});
const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" });
const db_path = try std.fs.path.join(gpa, &.{ self.opts.home, ".envr", "data.age" });
defer gpa.free(db_path);
{
// TODO: Use std.MultiArrayList? Had json issues
var public_keys: std.ArrayList([]const u8) = try .initCapacity(
gpa,
opts.config.keys.len,
self.opts.config.keys.len,
);
defer public_keys.deinit(gpa);
for (opts.config.keys) |key| {
for (self.opts.config.keys) |key| {
public_keys.appendAssumeCapacity(key.private);
}
try age.encrypt(io, gpa, public_keys.items, tmp_db_path, db_path);
}
self.changed = false;
}
@@ -171,23 +152,33 @@ pub fn close(
/// Returns a list of all the .env files present in the database.
/// The caller is responsible for freeing memory
fn list(self: @This(), gpa: std.mem.Allocator) ![]EnvFile {
var stmt = self.sql_db.prepare(
pub fn list(self: *@This(), gpa: std.mem.Allocator) ![]EnvFile {
var stmt = try self.sql_db.prepare(
"select path, remotes, sha256, contents from envr_env_files",
);
defer stmt.deinit();
return stmt.all([]const EnvFile, gpa, .{}, .{});
return stmt.all(EnvFile, gpa, .{}, .{});
}
const EnvFile = struct {
// TODO: Should use file_name in the struct and derive from the path.
path: []const u8,
/// dir is derived from Path, and is not stored in the database.
dir: []const u8,
remotes: [][]const u8,
// /// dir is derived from Path, and is not stored in the database.
// dir: []const u8,
/// JSON encoded list of strings
remotes: []const u8,
sha256: []const u8,
contents: []const u8,
fn deinit(self: *EnvFile, alloc: std.mem.Allocator) void {
alloc.free(self.path);
alloc.free(self.remotes);
alloc.free(self.sha256);
alloc.free(self.contents);
}
};
test {
@@ -269,7 +260,6 @@ test "Closing a fresh database does not create a file" {
var tmp_dir = std.testing.tmpDir(.{});
defer tmp_dir.cleanup();
// @compileLog(@typeInfo(std.Io.File.Permissions));
try tmp_dir.dir.createDir(io, "home", .default_dir);
try tmp_dir.dir.createDir(io, "tmp", .default_dir);
@@ -293,7 +283,7 @@ test "Closing a fresh database does not create a file" {
tmp_dir.dir.access(io, db_path, .{ .read = true }),
);
try db.close(io, gpa, .{ .home = home, .tmp = tmp });
try db.close(io, gpa);
try std.testing.expectError(
error.FileNotFound,
@@ -301,6 +291,182 @@ test "Closing a fresh database does not create a file" {
);
}
// test "Closing an unmodified database does not update the file" {}
test "single-file.db has envr_env_files table" {
const io = std.testing.io;
const gpa = std.testing.allocator;
const dir_path = try std.Io.Dir.cwd().realPathFileAlloc(io, ".", gpa);
defer gpa.free(dir_path);
const path = try std.fs.path.joinZ(
gpa,
&.{ dir_path, "fixtures", "single-file.db" },
);
defer gpa.free(path);
var db = try sqlite.Db.init(.{
.mode = .{ .File = path },
.open_flags = .{
.write = false,
.create = false,
},
.threading_mode = .MultiThread,
});
var diags: sqlite.Diagnostics = .{};
var stmt = db.prepareDynamicWithDiags(
"select name from sqlite_master where type='table'",
.{ .diags = &diags },
) catch |err| {
std.log.err(
"unable to prepare statement, got error {}. diagnostics: {f}",
.{ err, diags },
);
return err;
};
defer stmt.deinit();
const tables = (try stmt.oneAlloc(
[]const u8,
gpa,
.{ .diags = &diags },
.{},
)).?;
defer gpa.free(tables);
try std.testing.expectEqualSlices(u8, "envr_env_files", tables);
}
// test "raw restore works" {
// const io = std.testing.io;
// const gpa = std.testing.allocator;
// var db = try sqlite.Db.init(.{
// .mode = .Memory,
// .open_flags = .{
// .write = true,
// .create = true,
// },
// .threading_mode = .MultiThread,
// });
// try db.exec(
// \\create table envr_env_files (
// \\ path text primary key not null
// \\, remotes text -- JSON
// \\, sha256 text not null
// \\, contents text not null
// \\)
// , .{}, .{});
// const dir_path = try std.Io.Dir.cwd().realPathFileAlloc(io, ".", gpa);
// defer gpa.free(dir_path);
// const path = try std.fs.path.join(
// gpa,
// &.{ dir_path, "fixtures", "single-file.db" },
// );
// defer gpa.free(path);
// std.debug.print("path: {s}\n", .{path});
// try db.exec(
// "ATTACH DATABASE ? AS source",
// .{},
// .{path},
// );
// defer db.exec("DETACH DATABASE source", .{}, .{}) catch unreachable;
// var diags: sqlite.Diagnostics = .{};
// db.exec(
// "INSERT INTO main.envr_env_files SELECT * FROM source.envr_env_files",
// .{ .diags = &diags },
// .{},
// ) catch |err| {
// std.log.err(
// "unable to prepare statement, got error {}. diagnostics: {f}",
// .{ err, diags },
// );
// return err;
// };
// }
// test "Closing a modified database does create a file" {}
test "list displays the database's keys" {
const io = std.testing.io;
const gpa = std.testing.allocator;
var tmp_dir = std.testing.tmpDir(.{});
defer tmp_dir.cleanup();
try tmp_dir.dir.createDir(io, "home", .default_dir);
try tmp_dir.dir.createDir(io, "home/.envr", .default_dir);
try tmp_dir.dir.createDir(io, "tmp", .default_dir);
const tmp_dir_path = try tmp_dir.dir.realPathFileAlloc(io, ".", gpa);
defer gpa.free(tmp_dir_path);
const home = try std.fs.path.join(gpa, &.{ tmp_dir_path, "home" });
defer gpa.free(home);
const tmp = try std.fs.path.join(gpa, &.{ tmp_dir_path, "tmp" });
defer gpa.free(tmp);
// TODO: Get rid of direct access
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
defer gpa.free(db_path);
try std.Io.Dir.cwd().copyFile(
"fixtures/encrypted-single-file.db.age",
tmp_dir.dir,
"home/.envr/data.age",
io,
.{},
);
// Asserts file existence
try tmp_dir.dir.access(io, db_path, .{ .read = true });
// TODO: Pass testing keys
const config: Config = .{
.keys = &.{.from_pub_path("fixtures/insecure-test-key.pub")},
};
var db: @This() = try .open(io, gpa, .{
.config = config,
.home = home,
.tmp = tmp,
});
const env_files = try db.list(gpa);
defer gpa.free(env_files);
try std.testing.expectEqual(1, env_files.len);
var hasher = std.crypto.hash.sha2.Sha256.init(.{});
try std.testing.expectEqual(1, env_files.len);
for (env_files) |*file| {
defer file.deinit(gpa);
try std.testing.expectEqualSlices(
u8,
"~/project/.env.example",
file.path,
);
try std.testing.expectEqualSlices(
u8,
"API_KEY=\\\"sk_my_api_key\\\"\\nAPP_ENV=testing",
file.contents,
);
try std.testing.expectEqualSlices(
u8,
"[\"git@github.com:user/project.git\"]",
file.remotes,
);
hasher.update(file.contents);
const hash = hasher.finalResult();
try std.testing.expectEqualStrings(&std.fmt.bytesToHex(&hash, .lower), file.sha256);
}
try db.close(io, gpa);
}

View File

@@ -2,7 +2,6 @@ const std = @import("std");
const Io = std.Io;
const config = @import("config");
const comma = @import("comma");
const envr = @import("envr");
@@ -51,6 +50,20 @@ fn run(
environ_map.get("PATH").?,
);
},
.list => {
var stdout_buffer: [1024]u8 = undefined;
var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
const stdout_writer = &stdout_file_writer.interface;
return envr.list(
io,
arena,
stdout_writer,
environ_map.get("HOME").?,
// TODO: Don't hardcode this?
"/tmp",
);
},
.unknown => {
return fallback_to_go(io, arena, args);
},
@@ -137,10 +150,10 @@ fn fallback_to_go(
test "simple test" {
const gpa = std.testing.allocator;
var list: std.ArrayList(i32) = .empty;
defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
try list.append(gpa, 42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
var alist: std.ArrayList(i32) = .empty;
defer alist.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
try alist.append(gpa, 42);
try std.testing.expectEqual(@as(i32, 42), alist.pop());
}
test "fuzz example" {
@@ -152,23 +165,23 @@ fn testOne(context: void, smith: *std.testing.Smith) !void {
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
const gpa = std.testing.allocator;
var list: std.ArrayList(u8) = .empty;
defer list.deinit(gpa);
var alist: std.ArrayList(u8) = .empty;
defer alist.deinit(gpa);
while (!smith.eos()) switch (smith.value(enum { add_data, dup_data })) {
.add_data => {
const slice = try list.addManyAsSlice(gpa, smith.value(u4));
const slice = try alist.addManyAsSlice(gpa, smith.value(u4));
smith.bytes(slice);
},
.dup_data => {
if (list.items.len == 0) continue;
if (list.items.len > std.math.maxInt(u32)) return error.SkipZigTest;
const len = smith.valueRangeAtMost(u32, 1, @min(32, list.items.len));
const off = smith.valueRangeAtMost(u32, 0, @intCast(list.items.len - len));
try list.appendSlice(gpa, list.items[off..][0..len]);
if (alist.items.len == 0) continue;
if (alist.items.len > std.math.maxInt(u32)) return error.SkipZigTest;
const len = smith.valueRangeAtMost(u32, 1, @min(32, alist.items.len));
const off = smith.valueRangeAtMost(u32, 0, @intCast(alist.items.len - len));
try alist.appendSlice(gpa, alist.items[off..][0..len]);
try std.testing.expectEqualSlices(
u8,
list.items[off..][0..len],
list.items[list.items.len - len ..],
alist.items[off..][0..len],
alist.items[alist.items.len - len ..],
);
},
};

View File

@@ -5,6 +5,9 @@ const Io = std.Io;
const comma = @import("comma");
const Command = comma.Command;
const Config = @import("Config.zig");
const Db = @import("Db.zig");
pub const root: Command = .new(.{
.name = "envr",
.short = "Manage your .env files.",
@@ -49,6 +52,10 @@ pub const root: Command = .new(.{
\\ The deps command reports which binaries are available and which are not."
,
},
.{
.name = "list",
.short = "View your tracked files",
},
.{
.name = "version",
.short = "Show envr's version",
@@ -56,17 +63,59 @@ pub const root: Command = .new(.{
},
});
pub fn list(
io: Io,
arena: std.mem.Allocator,
out: *std.Io.Writer,
home: []const u8,
tmp: []const u8,
) !void {
// TODO: Don't hardcode
const cfgPath = try std.fs.path.join(arena, &.{ home, ".envr", "config.json" });
const cfg: Config = (try Config.load(io, arena, cfgPath)).value;
var db: Db = try .open(io, arena, .{
.config = cfg,
.home = home,
.tmp = tmp,
});
_ = try out.write("Path\n");
const files = try db.list(arena);
for (files) |file| {
// TODO: Table printer
try out.print("{s}\n", .{file.path});
}
try out.flush();
return db.close(io, arena); // TODO: Defer this
}
test {
std.testing.refAllDecls(@import("Config.zig"));
std.testing.refAllDecls(@import("Db.zig"));
}
test "enum type" {
const got: root.Type = @enumFromInt(2);
const got: root.Type = @enumFromInt(3);
try std.testing.expectEqual(.version, got);
}
test "parse deps" {
const args = &[_][]const u8{"deps"};
const cmd = root.parse(args);
try std.testing.expectEqual(.deps, cmd);
}
test "parse list" {
const args = &[_][]const u8{"list"};
const cmd = root.parse(args);
try std.testing.expectEqual(.list, cmd);
}
test "parse version" {
const args = &[_][]const u8{"version"};
const cmd = root.parse(args);

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 ..];