diff options
| author | 2020-11-12 16:44:23 +0100 | |
|---|---|---|
| committer | 2020-11-12 16:44:23 +0100 | |
| commit | 08919c71c87790a92a209e1c49bd76cbbcb2a1e3 (patch) | |
| tree | 6af845b2e8207be558ecf0bceef8b376f93539b2 | |
| parent | add an iterator (diff) | |
| download | zig-sqlite-08919c71c87790a92a209e1c49bd76cbbcb2a1e3.tar.gz zig-sqlite-08919c71c87790a92a209e1c49bd76cbbcb2a1e3.tar.xz zig-sqlite-08919c71c87790a92a209e1c49bd76cbbcb2a1e3.zip | |
add documentation for the iterator
| -rw-r--r-- | sqlite.zig | 41 |
1 files changed, 41 insertions, 0 deletions
| @@ -120,6 +120,28 @@ pub const Db = struct { | |||
| 120 | } | 120 | } |
| 121 | }; | 121 | }; |
| 122 | 122 | ||
| 123 | /// Iterator allows iterating over a result set. | ||
| 124 | /// | ||
| 125 | /// Each call to `next` returns the next row of the result set, or null if the result set is exhausted. | ||
| 126 | /// Each row will have the type `Type` so the columns returned in the result set must be compatible with this type. | ||
| 127 | /// | ||
| 128 | /// Here is an example of how to use the iterator: | ||
| 129 | /// | ||
| 130 | /// const User = struct { | ||
| 131 | /// name: Text, | ||
| 132 | /// age: u16, | ||
| 133 | /// }; | ||
| 134 | /// | ||
| 135 | /// var stmt = try db.prepare("SELECT name, age FROM user"); | ||
| 136 | /// defer stmt.deinit(); | ||
| 137 | /// | ||
| 138 | /// var iter = try stmt.iterator(User, .{}); | ||
| 139 | /// while (true) { | ||
| 140 | /// const row: User = (try iter.next(.{})) orelse break; | ||
| 141 | /// ... | ||
| 142 | /// } | ||
| 143 | /// | ||
| 144 | /// The iterator _must not_ outlive the statement. | ||
| 123 | pub fn Iterator(comptime Type: type) type { | 145 | pub fn Iterator(comptime Type: type) type { |
| 124 | return struct { | 146 | return struct { |
| 125 | const Self = @This(); | 147 | const Self = @This(); |
| @@ -418,6 +440,25 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 418 | } | 440 | } |
| 419 | } | 441 | } |
| 420 | 442 | ||
| 443 | /// iterator returns an iterator to read data from the result set, one row at a time. | ||
| 444 | /// | ||
| 445 | /// The data in the row is used to populate a value of the type `Type`. | ||
| 446 | /// This means that `Type` must have as many fields as is returned in the query | ||
| 447 | /// executed by this statement. | ||
| 448 | /// This also means that the type of each field must be compatible with the SQLite type. | ||
| 449 | /// | ||
| 450 | /// Here is an example of how to use the iterator: | ||
| 451 | /// | ||
| 452 | /// var iter = try stmt.iterator(usize, .{}); | ||
| 453 | /// while (true) { | ||
| 454 | /// const row = (try iter.next(.{})) orelse break; | ||
| 455 | /// ... | ||
| 456 | /// } | ||
| 457 | /// | ||
| 458 | /// The `values` tuple is used for the bind parameters. It must have as many fields as there are bind markers | ||
| 459 | /// in the input query string. | ||
| 460 | /// | ||
| 461 | /// The iterator _must not_ outlive the statement. | ||
| 421 | pub fn iterator(self: *Self, comptime Type: type, values: anytype) !Iterator(Type) { | 462 | pub fn iterator(self: *Self, comptime Type: type, values: anytype) !Iterator(Type) { |
| 422 | self.bind(values); | 463 | self.bind(values); |
| 423 | 464 | ||