summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2022-04-21 23:37:16 +0200
committerGravatar Vincent Rischmann2022-04-21 23:37:16 +0200
commitea17d8dac169ea820e561baa7f751af4e9e529db (patch)
tree75387645804d059858e24620f91b9adac26d1c28
parentremove comment (diff)
downloadzig-sqlite-ea17d8dac169ea820e561baa7f751af4e9e529db.tar.gz
zig-sqlite-ea17d8dac169ea820e561baa7f751af4e9e529db.tar.xz
zig-sqlite-ea17d8dac169ea820e561baa7f751af4e9e529db.zip
add the Db.execAlloc method
-rw-r--r--sqlite.zig19
1 files changed, 19 insertions, 0 deletions
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 {
479 try stmt.exec(options, values); 479 try stmt.exec(options, values);
480 } 480 }
481 481
482 /// execAlloc is like `exec` but can allocate memory.
483 pub fn execAlloc(self: *Self, allocator: mem.Allocator, comptime query: []const u8, options: QueryOptions, values: anytype) !void {
484 var stmt = try self.prepareWithDiags(query, options);
485 defer stmt.deinit();
486 try stmt.execAlloc(allocator, options, values);
487 }
488
482 /// one is a convenience function which prepares a statement and reads a single row from the result set. 489 /// one is a convenience function which prepares a statement and reads a single row from the result set.
483 pub fn one(self: *Self, comptime Type: type, comptime query: []const u8, options: QueryOptions, values: anytype) !?Type { 490 pub fn one(self: *Self, comptime Type: type, comptime query: []const u8, options: QueryOptions, values: anytype) !?Type {
484 var stmt = try self.prepareWithDiags(query, options); 491 var stmt = try self.prepareWithDiags(query, options);
@@ -2351,6 +2358,18 @@ test "sqlite: statement execDynamic" {
2351 } 2358 }
2352} 2359}
2353 2360
2361test "sqlite: db execAlloc" {
2362 var db = try getTestDb();
2363 defer db.deinit();
2364 try addTestData(&db);
2365
2366 try db.execAlloc(testing.allocator, "INSERT INTO user(id, name, age) VALUES(@id, @name, @age)", .{}, .{
2367 .id = @as(usize, 502),
2368 .name = Blob{ .data = "hello" },
2369 .age = @as(u32, 20),
2370 });
2371}
2372
2354test "sqlite: read a single user into a struct" { 2373test "sqlite: read a single user into a struct" {
2355 var arena = std.heap.ArenaAllocator.init(testing.allocator); 2374 var arena = std.heap.ArenaAllocator.init(testing.allocator);
2356 defer arena.deinit(); 2375 defer arena.deinit();