From 03a1305726973ced7a70e1a3f85b82e44c38dabb Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Thu, 23 Sep 2021 22:12:56 -0700 Subject: add iteratorAlloc and execAlloc --- sqlite.zig | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'sqlite.zig') diff --git a/sqlite.zig b/sqlite.zig index 0bc3384..2ddb3f7 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -1214,6 +1214,23 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t } } + /// execAlloc is like `exec` but can allocate memory. + pub fn execAlloc(self: *Self, allocator: *std.mem.Allocator, options: QueryOptions, values: anytype) !void { + try self.bind(.{ .allocator = allocator }, values); + + var dummy_diags = Diagnostics{}; + var diags = options.diags orelse &dummy_diags; + + const result = c.sqlite3_step(self.stmt); + switch (result) { + c.SQLITE_DONE => {}, + else => { + diags.err = getLastDetailedErrorFromDb(self.db); + return errors.errorFromResultCode(result); + }, + } + } + /// iterator returns an iterator to read data from the result set, one row at a time. /// /// The data in the row is used to populate a value of the type `Type`. @@ -1242,6 +1259,17 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t return res; } + /// iteratorAlloc is like `iterator` but can allocate memory. + pub fn iteratorAlloc(self: *Self, comptime Type: type, allocator: *std.mem.Allocator, values: anytype) !Iterator(Type) { + try self.bind(.{ .allocator = allocator }, values); + + var res: Iterator(Type) = undefined; + res.db = self.db; + res.stmt = self.stmt; + + return res; + } + /// one reads a single row from the result set of this statement. /// /// The data in the row is used to populate a value of the type `Type`. @@ -1276,7 +1304,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t /// oneAlloc is like `one` but can allocate memory. pub fn oneAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) !?Type { - var iter = try self.iterator(Type, values); + var iter = try self.iteratorAlloc(Type, allocator, values); const row = (try iter.nextAlloc(allocator, options)) orelse return null; return row; @@ -1309,7 +1337,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t /// /// Note that this allocates all rows into a single slice: if you read a lot of data this can use a lot of memory. pub fn all(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) ![]Type { - var iter = try self.iterator(Type, values); + var iter = try self.iteratorAlloc(Type, allocator, values); var rows = std.ArrayList(Type).init(allocator); while (try iter.nextAlloc(allocator, options)) |row| { -- cgit v1.2.3