From 27d10c1c35eefcf118441018212a13c1d4b52bf8 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Thu, 19 Dec 2024 00:37:36 +0100 Subject: fix for latest zig --- vtab.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vtab.zig b/vtab.zig index 77a9839..53e46c1 100644 --- a/vtab.zig +++ b/vtab.zig @@ -32,7 +32,7 @@ pub const ModuleContext = struct { allocator: mem.Allocator, }; -fn dupeToSQLiteString(s: []const u8) [*c]const u8 { +fn dupeToSQLiteString(s: []const u8) [*c]u8 { var buffer: [*c]u8 = @ptrCast(c.sqlite3_malloc(@intCast(s.len + 1))); mem.copyForwards(u8, buffer[0..s.len], s); @@ -730,7 +730,7 @@ pub fn VirtualTable( return c.SQLITE_ERROR; } - fn xConnect(db: ?*c.sqlite3, module_context_ptr: ?*anyopaque, argc: c_int, argv: [*c]const [*c]const u8, vtab: [*c][*c]c.sqlite3_vtab, err_str: [*c][*c]const u8) callconv(.C) c_int { + fn xConnect(db: ?*c.sqlite3, module_context_ptr: ?*anyopaque, argc: c_int, argv: [*c]const [*c]const u8, vtab: [*c][*c]c.sqlite3_vtab, err_str: [*c][*c]u8) callconv(.C) c_int { const module_context = getModuleContext(module_context_ptr); var arena = heap.ArenaAllocator.init(module_context.allocator); -- cgit v1.2.3 From 39237dc9a467a83814eac6fba7863f243fadae8c Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Thu, 19 Dec 2024 01:02:17 +0100 Subject: dynamic statement: add prepareWithTail --- sqlite.zig | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sqlite.zig b/sqlite.zig index 6d22d1f..b1f9f81 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -1568,6 +1568,10 @@ pub const DynamicStatement = struct { pub const PrepareError = error{EmptyQuery} || Error; fn prepare(db: *Db, query: []const u8, options: QueryOptions, flags: c_uint) PrepareError!Self { + return prepareWithTail(db, query, options, flags, null); + } + + fn prepareWithTail(db: *Db, query: []const u8, options: QueryOptions, flags: c_uint, tail: ?*[*c]const u8) PrepareError!Self { var dummy_diags = Diagnostics{}; var diags = options.diags orelse &dummy_diags; const stmt = blk: { @@ -1578,7 +1582,7 @@ pub const DynamicStatement = struct { @intCast(query.len), flags, &tmp, - options.sql_tail_ptr, + tail, ); if (result != c.SQLITE_OK) { diags.err = getLastDetailedErrorFromDb(db.db); -- cgit v1.2.3 From 5fb342c99cf80cd0be05a410bb251956aa0decab Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Thu, 19 Dec 2024 01:02:33 +0100 Subject: rework execMulti We don't have to expose the tail pointer to get execMulti to work: the tail pointer can stay local to execMulti instead of polluting the QueryOptions. This is a breaking change but as far as I can tell people only use execMulti, never the tail pointer for anything, so I guess it's fine. --- sqlite.zig | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/sqlite.zig b/sqlite.zig index b1f9f81..97ef60f 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -810,30 +810,16 @@ pub const Db = struct { /// /// Exmaple: 'create table a(); create table b();' pub fn execMulti(self: *Self, query: []const u8, options: QueryOptions) !void { - var new_options = options; - var sql_tail_ptr: ?[*:0]const u8 = null; - new_options.sql_tail_ptr = &sql_tail_ptr; + var sql_tail: [*c]const u8 = query.ptr; while (true) { - // continuously prepare and execute (dynamically as there's no - // values to bind in this case) - var stmt: DynamicStatement = undefined; - if (sql_tail_ptr != null) { - const new_query = std.mem.span(sql_tail_ptr.?); - if (new_query.len == 0) break; - stmt = self.prepareDynamicWithDiags(new_query, new_options) catch |err| switch (err) { - error.EmptyQuery => break, - else => return err, - }; - } else { - stmt = self.prepareDynamicWithDiags(query, new_options) catch |err| switch (err) { - error.EmptyQuery => break, - else => return err, - }; - } + const new_query = std.mem.span(sql_tail); + if (new_query.len == 0) return; + var stmt = try DynamicStatement.prepareWithTail(self, new_query, options, 0, &sql_tail); defer stmt.deinit(); - try stmt.exec(new_options, .{}); + + try stmt.exec(options, .{}); } } @@ -1030,11 +1016,6 @@ pub const Savepoint = struct { pub const QueryOptions = struct { /// if provided, diags will be populated in case of failures. diags: ?*Diagnostics = null, - - /// if provided, sql_tail_ptr will point to the last uncompiled statement - /// in the prepare() call. this is useful for multiple-statements being - /// processed. - sql_tail_ptr: ?*?[*:0]const u8 = null, }; /// Iterator allows iterating over a result set. -- cgit v1.2.3