From ea17d8dac169ea820e561baa7f751af4e9e529db Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Thu, 21 Apr 2022 23:37:16 +0200 Subject: add the Db.execAlloc method --- sqlite.zig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sqlite.zig b/sqlite.zig index 0213b9f..1d32880 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -479,6 +479,13 @@ pub const Db = struct { try stmt.exec(options, values); } + /// execAlloc is like `exec` but can allocate memory. + pub fn execAlloc(self: *Self, allocator: mem.Allocator, comptime query: []const u8, options: QueryOptions, values: anytype) !void { + var stmt = try self.prepareWithDiags(query, options); + defer stmt.deinit(); + try stmt.execAlloc(allocator, options, values); + } + /// one is a convenience function which prepares a statement and reads a single row from the result set. pub fn one(self: *Self, comptime Type: type, comptime query: []const u8, options: QueryOptions, values: anytype) !?Type { var stmt = try self.prepareWithDiags(query, options); @@ -2351,6 +2358,18 @@ test "sqlite: statement execDynamic" { } } +test "sqlite: db execAlloc" { + var db = try getTestDb(); + defer db.deinit(); + try addTestData(&db); + + try db.execAlloc(testing.allocator, "INSERT INTO user(id, name, age) VALUES(@id, @name, @age)", .{}, .{ + .id = @as(usize, 502), + .name = Blob{ .data = "hello" }, + .age = @as(u32, 20), + }); +} + test "sqlite: read a single user into a struct" { var arena = std.heap.ArenaAllocator.init(testing.allocator); defer arena.deinit(); -- cgit v1.2.3