diff options
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 30 |
1 files changed, 18 insertions, 12 deletions
| @@ -100,13 +100,9 @@ pub const Db = struct { | |||
| 100 | /// | 100 | /// |
| 101 | /// Example usage: | 101 | /// Example usage: |
| 102 | /// | 102 | /// |
| 103 | /// var stmt = try db.prepare("INSERT INTO foo(id, name) VALUES(?, ?)", .{ | 103 | /// var stmt = try db.prepare("INSERT INTO foo(id, name) VALUES(?, ?)"); |
| 104 | /// .id = 3540, | ||
| 105 | /// .name = "Eminem", | ||
| 106 | /// }); | ||
| 107 | /// defer stmt.deinit(); | 104 | /// defer stmt.deinit(); |
| 108 | /// | 105 | /// |
| 109 | /// Note that the name of the fields in the tuple are irrelevant, only the types are. | ||
| 110 | pub fn prepare(self: *Self, comptime query: []const u8) !Statement(StatementOptions.from(query)) { | 106 | pub fn prepare(self: *Self, comptime query: []const u8) !Statement(StatementOptions.from(query)) { |
| 111 | return Statement(comptime StatementOptions.from(query)).prepare(self, 0, query); | 107 | return Statement(comptime StatementOptions.from(query)).prepare(self, 0, query); |
| 112 | } | 108 | } |
| @@ -145,29 +141,31 @@ pub const StatementOptions = struct { | |||
| 145 | /// | 141 | /// |
| 146 | /// The exec function can be used to execute a query which does not return rows: | 142 | /// The exec function can be used to execute a query which does not return rows: |
| 147 | /// | 143 | /// |
| 148 | /// var stmt = try db.prepare("UPDATE foo SET id = ? WHERE name = ?", .{ | 144 | /// var stmt = try db.prepare("UPDATE foo SET id = ? WHERE name = ?"); |
| 145 | /// defer stmt.deinit(); | ||
| 146 | /// | ||
| 147 | /// try stmt.exec(.{ | ||
| 149 | /// .id = 200, | 148 | /// .id = 200, |
| 150 | /// .name = "José", | 149 | /// .name = "José", |
| 151 | /// }); | 150 | /// }); |
| 152 | /// defer stmt.deinit(); | ||
| 153 | /// | 151 | /// |
| 154 | /// The one function can be used to select a single row: | 152 | /// The one function can be used to select a single row: |
| 155 | /// | 153 | /// |
| 156 | /// var stmt = try db.prepare("SELECT name FROM foo WHERE id = ?", .{ .id = 200 }); | 154 | /// var stmt = try db.prepare("SELECT name FROM foo WHERE id = ?"); |
| 157 | /// defer stmt.deinit(); | 155 | /// defer stmt.deinit(); |
| 158 | /// | 156 | /// |
| 159 | /// const name = try stmt.one([]const u8, .{}); | 157 | /// const name = try stmt.one([]const u8, .{}, .{ .id = 200 }); |
| 160 | /// | 158 | /// |
| 161 | /// The all function can be used to select all rows: | 159 | /// The all function can be used to select all rows: |
| 162 | /// | 160 | /// |
| 163 | /// var stmt = try db.prepare("SELECT id, name FROM foo", .{}); | 161 | /// var stmt = try db.prepare("SELECT id, name FROM foo"); |
| 164 | /// defer stmt.deinit(); | 162 | /// defer stmt.deinit(); |
| 165 | /// | 163 | /// |
| 166 | /// const Row = struct { | 164 | /// const Row = struct { |
| 167 | /// id: usize, | 165 | /// id: usize, |
| 168 | /// name: []const u8, | 166 | /// name: []const u8, |
| 169 | /// }; | 167 | /// }; |
| 170 | /// const rows = try stmt.all(Row, .{ .allocator = allocator }); | 168 | /// const rows = try stmt.all(Row, .{ .allocator = allocator }, .{}); |
| 171 | /// | 169 | /// |
| 172 | /// Look at aach function for more complete documentation. | 170 | /// Look at aach function for more complete documentation. |
| 173 | /// | 171 | /// |
| @@ -183,7 +181,6 @@ pub fn Statement(comptime opts: StatementOptions) type { | |||
| 183 | }; | 181 | }; |
| 184 | 182 | ||
| 185 | fn prepare(db: *Db, flags: c_uint, comptime query: []const u8) !Self { | 183 | fn prepare(db: *Db, flags: c_uint, comptime query: []const u8) !Self { |
| 186 | // prepare | ||
| 187 | var stmt = blk: { | 184 | var stmt = blk: { |
| 188 | var tmp: ?*c.sqlite3_stmt = undefined; | 185 | var tmp: ?*c.sqlite3_stmt = undefined; |
| 189 | const result = c.sqlite3_prepare_v3( | 186 | const result = c.sqlite3_prepare_v3( |
| @@ -281,11 +278,14 @@ pub fn Statement(comptime opts: StatementOptions) type { | |||
| 281 | /// age: usize, | 278 | /// age: usize, |
| 282 | /// }, | 279 | /// }, |
| 283 | /// .{ .allocator = allocator }, | 280 | /// .{ .allocator = allocator }, |
| 281 | /// .{ .foo = "bar", .age = 500 }, | ||
| 284 | /// ); | 282 | /// ); |
| 285 | /// | 283 | /// |
| 286 | /// The `options` tuple is used to provide additional state in some cases, for example | 284 | /// The `options` tuple is used to provide additional state in some cases, for example |
| 287 | /// an allocator used to read text and blobs. | 285 | /// an allocator used to read text and blobs. |
| 288 | /// | 286 | /// |
| 287 | /// The `values` tuple is used for the bind parameters. | ||
| 288 | /// | ||
| 289 | pub fn one(self: *Self, comptime Type: type, options: anytype, values: anytype) !?Type { | 289 | pub fn one(self: *Self, comptime Type: type, options: anytype, values: anytype) !?Type { |
| 290 | if (!comptime std.meta.trait.is(.Struct)(@TypeOf(options))) { | 290 | if (!comptime std.meta.trait.is(.Struct)(@TypeOf(options))) { |
| 291 | @compileError("options passed to all must be a struct"); | 291 | @compileError("options passed to all must be a struct"); |
| @@ -327,11 +327,17 @@ pub fn Statement(comptime opts: StatementOptions) type { | |||
| 327 | /// age: usize, | 327 | /// age: usize, |
| 328 | /// }, | 328 | /// }, |
| 329 | /// .{ .allocator = allocator }, | 329 | /// .{ .allocator = allocator }, |
| 330 | /// .{ .foo = "bar", .age = 500 }, | ||
| 330 | /// ); | 331 | /// ); |
| 331 | /// | 332 | /// |
| 332 | /// The `options` tuple is used to provide additional state in some cases. | 333 | /// The `options` tuple is used to provide additional state in some cases. |
| 333 | /// Note that for this function the allocator is mandatory. | 334 | /// Note that for this function the allocator is mandatory. |
| 334 | /// | 335 | /// |
| 336 | /// The `values` tuple is used for the bind parameters. | ||
| 337 | /// | ||
| 338 | /// Note that this allocates all rows into a single slice: if you read a lot of data this can | ||
| 339 | /// use a lot of memory. | ||
| 340 | /// | ||
| 335 | pub fn all(self: *Self, comptime Type: type, options: anytype, values: anytype) ![]Type { | 341 | pub fn all(self: *Self, comptime Type: type, options: anytype, values: anytype) ![]Type { |
| 336 | if (!comptime std.meta.trait.is(.Struct)(@TypeOf(options))) { | 342 | if (!comptime std.meta.trait.is(.Struct)(@TypeOf(options))) { |
| 337 | @compileError("options passed to all must be a struct"); | 343 | @compileError("options passed to all must be a struct"); |