diff options
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 19 |
1 files changed, 19 insertions, 0 deletions
| @@ -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 | ||
| 2361 | test "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 | |||
| 2354 | test "sqlite: read a single user into a struct" { | 2373 | test "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(); |