diff options
| author | 2021-09-23 22:12:56 -0700 | |
|---|---|---|
| committer | 2021-09-23 22:12:56 -0700 | |
| commit | 03a1305726973ced7a70e1a3f85b82e44c38dabb (patch) | |
| tree | bb65f7992fbfa4dd9c87c23aecdfcf659fe939ae /sqlite.zig | |
| parent | add options parameter to bind/bindField (diff) | |
| download | zig-sqlite-03a1305726973ced7a70e1a3f85b82e44c38dabb.tar.gz zig-sqlite-03a1305726973ced7a70e1a3f85b82e44c38dabb.tar.xz zig-sqlite-03a1305726973ced7a70e1a3f85b82e44c38dabb.zip | |
add iteratorAlloc and execAlloc
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 32 |
1 files changed, 30 insertions, 2 deletions
| @@ -1214,6 +1214,23 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1214 | } | 1214 | } |
| 1215 | } | 1215 | } |
| 1216 | 1216 | ||
| 1217 | /// execAlloc is like `exec` but can allocate memory. | ||
| 1218 | pub fn execAlloc(self: *Self, allocator: *std.mem.Allocator, options: QueryOptions, values: anytype) !void { | ||
| 1219 | try self.bind(.{ .allocator = allocator }, values); | ||
| 1220 | |||
| 1221 | var dummy_diags = Diagnostics{}; | ||
| 1222 | var diags = options.diags orelse &dummy_diags; | ||
| 1223 | |||
| 1224 | const result = c.sqlite3_step(self.stmt); | ||
| 1225 | switch (result) { | ||
| 1226 | c.SQLITE_DONE => {}, | ||
| 1227 | else => { | ||
| 1228 | diags.err = getLastDetailedErrorFromDb(self.db); | ||
| 1229 | return errors.errorFromResultCode(result); | ||
| 1230 | }, | ||
| 1231 | } | ||
| 1232 | } | ||
| 1233 | |||
| 1217 | /// iterator returns an iterator to read data from the result set, one row at a time. | 1234 | /// iterator returns an iterator to read data from the result set, one row at a time. |
| 1218 | /// | 1235 | /// |
| 1219 | /// The data in the row is used to populate a value of the type `Type`. | 1236 | /// 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 | |||
| 1242 | return res; | 1259 | return res; |
| 1243 | } | 1260 | } |
| 1244 | 1261 | ||
| 1262 | /// iteratorAlloc is like `iterator` but can allocate memory. | ||
| 1263 | pub fn iteratorAlloc(self: *Self, comptime Type: type, allocator: *std.mem.Allocator, values: anytype) !Iterator(Type) { | ||
| 1264 | try self.bind(.{ .allocator = allocator }, values); | ||
| 1265 | |||
| 1266 | var res: Iterator(Type) = undefined; | ||
| 1267 | res.db = self.db; | ||
| 1268 | res.stmt = self.stmt; | ||
| 1269 | |||
| 1270 | return res; | ||
| 1271 | } | ||
| 1272 | |||
| 1245 | /// one reads a single row from the result set of this statement. | 1273 | /// one reads a single row from the result set of this statement. |
| 1246 | /// | 1274 | /// |
| 1247 | /// The data in the row is used to populate a value of the type `Type`. | 1275 | /// 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 | |||
| 1276 | 1304 | ||
| 1277 | /// oneAlloc is like `one` but can allocate memory. | 1305 | /// oneAlloc is like `one` but can allocate memory. |
| 1278 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) !?Type { | 1306 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) !?Type { |
| 1279 | var iter = try self.iterator(Type, values); | 1307 | var iter = try self.iteratorAlloc(Type, allocator, values); |
| 1280 | 1308 | ||
| 1281 | const row = (try iter.nextAlloc(allocator, options)) orelse return null; | 1309 | const row = (try iter.nextAlloc(allocator, options)) orelse return null; |
| 1282 | return row; | 1310 | return row; |
| @@ -1309,7 +1337,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1309 | /// | 1337 | /// |
| 1310 | /// Note that this allocates all rows into a single slice: if you read a lot of data this can use a lot of memory. | 1338 | /// Note that this allocates all rows into a single slice: if you read a lot of data this can use a lot of memory. |
| 1311 | pub fn all(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) ![]Type { | 1339 | pub fn all(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) ![]Type { |
| 1312 | var iter = try self.iterator(Type, values); | 1340 | var iter = try self.iteratorAlloc(Type, allocator, values); |
| 1313 | 1341 | ||
| 1314 | var rows = std.ArrayList(Type).init(allocator); | 1342 | var rows = std.ArrayList(Type).init(allocator); |
| 1315 | while (try iter.nextAlloc(allocator, options)) |row| { | 1343 | while (try iter.nextAlloc(allocator, options)) |row| { |