diff options
| -rw-r--r-- | sqlite.zig | 39 |
1 files changed, 36 insertions, 3 deletions
| @@ -1452,8 +1452,41 @@ pub const DynamicStatement = struct { | |||
| 1452 | /// | 1452 | /// |
| 1453 | /// Possible errors: | 1453 | /// Possible errors: |
| 1454 | /// - SQLiteError.SQLiteNotFound if some fields not found | 1454 | /// - SQLiteError.SQLiteNotFound if some fields not found |
| 1455 | pub fn iterator(self: *Self, comptime Type: type, options: anytype, values: anytype) !Iterator(Type) { | 1455 | pub fn iterator(self: *Self, comptime Type: type, values: anytype) !Iterator(Type) { |
| 1456 | try self.smartBind(options, values); | 1456 | try self.smartBind(.{}, values); |
| 1457 | |||
| 1458 | var res: Iterator(Type) = undefined; | ||
| 1459 | res.db = self.db; | ||
| 1460 | res.stmt = self.stmt; | ||
| 1461 | |||
| 1462 | return res; | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | /// iterator returns an iterator to read data from the result set, one row at a time. | ||
| 1466 | /// | ||
| 1467 | /// The data in the row is used to populate a value of the type `Type`. | ||
| 1468 | /// This means that `Type` must have as many fields as is returned in the query | ||
| 1469 | /// executed by this statement. | ||
| 1470 | /// This also means that the type of each field must be compatible with the SQLite type. | ||
| 1471 | /// | ||
| 1472 | /// Here is an example of how to use the iterator: | ||
| 1473 | /// | ||
| 1474 | /// var iter = try stmt.iterator(usize, .{}); | ||
| 1475 | /// while (try iter.next(.{})) |row| { | ||
| 1476 | /// ... | ||
| 1477 | /// } | ||
| 1478 | /// | ||
| 1479 | /// The `values` tuple is used for the bind parameters. It must have as many fields as there are bind markers | ||
| 1480 | /// in the input query string. | ||
| 1481 | /// The values will be binded depends on the numberic name when it's a tuple, or the | ||
| 1482 | /// string name when it's a normal structure. | ||
| 1483 | /// | ||
| 1484 | /// The iterator _must not_ outlive the statement. | ||
| 1485 | /// | ||
| 1486 | /// Possible errors: | ||
| 1487 | /// - SQLiteError.SQLiteNotFound if some fields not found | ||
| 1488 | pub fn iteratorAlloc(self: *Self, comptime Type: type, allocator: *std.mem.Allocator, values: anytype) !Iterator(Type) { | ||
| 1489 | try self.smartBind(.{ .allocator = allocator }, values); | ||
| 1457 | 1490 | ||
| 1458 | var res: Iterator(Type) = undefined; | 1491 | var res: Iterator(Type) = undefined; |
| 1459 | res.db = self.db; | 1492 | res.db = self.db; |
| @@ -1496,7 +1529,7 @@ pub const DynamicStatement = struct { | |||
| 1496 | 1529 | ||
| 1497 | /// oneAlloc is like `one` but can allocate memory. | 1530 | /// oneAlloc is like `one` but can allocate memory. |
| 1498 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) !?Type { | 1531 | pub fn oneAlloc(self: *Self, comptime Type: type, allocator: *mem.Allocator, options: QueryOptions, values: anytype) !?Type { |
| 1499 | var iter = try self.iterator(Type, .{ .allocator = allocator }, values); | 1532 | var iter = try self.iteratorAlloc(Type, allocator, values); |
| 1500 | 1533 | ||
| 1501 | const row = (try iter.nextAlloc(allocator, options)) orelse return null; | 1534 | const row = (try iter.nextAlloc(allocator, options)) orelse return null; |
| 1502 | return row; | 1535 | return row; |