summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2020-10-25 00:47:39 +0200
committerGravatar Vincent Rischmann2020-10-25 00:47:39 +0200
commit204050b9f97d8027642bd41a01b985dc89faba4f (patch)
tree3ab6ef6386bd9a2613d263ac00069002f73ea1de /sqlite.zig
parentcheck the type of the options argument (diff)
downloadzig-sqlite-204050b9f97d8027642bd41a01b985dc89faba4f.tar.gz
zig-sqlite-204050b9f97d8027642bd41a01b985dc89faba4f.tar.xz
zig-sqlite-204050b9f97d8027642bd41a01b985dc89faba4f.zip
add the Bytes type
Diffstat (limited to '')
-rw-r--r--sqlite.zig31
1 files changed, 28 insertions, 3 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 46f2e7e..3c9b162 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -117,6 +117,11 @@ pub const Db = struct {
117 } 117 }
118}; 118};
119 119
120pub const Bytes = union(enum) {
121 Blob: []const u8,
122 Text: []const u8,
123};
124
120/// Statement is a wrapper around a SQLite statement, providing high-level functions to execute 125/// Statement is a wrapper around a SQLite statement, providing high-level functions to execute
121/// a statement and retrieve rows for SELECT queries. 126/// a statement and retrieve rows for SELECT queries.
122/// 127///
@@ -153,11 +158,17 @@ pub const Statement = struct {
153 158
154 stmt: *c.sqlite3_stmt, 159 stmt: *c.sqlite3_stmt,
155 160
161 const BytesType = enum {
162 Text,
163 Blob,
164 };
165
156 fn prepare(db: *Db, flags: c_uint, comptime query: []const u8, values: anytype) !Self { 166 fn prepare(db: *Db, flags: c_uint, comptime query: []const u8, values: anytype) !Self {
157 const StructType = @typeInfo(@TypeOf(values)).Struct; 167 const StructType = @TypeOf(values);
168 const StructTypeInfo = @typeInfo(StructType).Struct;
158 comptime { 169 comptime {
159 const bind_parameter_count = std.mem.count(u8, query, "?"); 170 const bind_parameter_count = std.mem.count(u8, query, "?");
160 if (bind_parameter_count != StructType.fields.len) { 171 if (bind_parameter_count != StructTypeInfo.fields.len) {
161 @compileError("bind parameter count != number of fields in tuple/struct"); 172 @compileError("bind parameter count != number of fields in tuple/struct");
162 } 173 }
163 } 174 }
@@ -183,7 +194,7 @@ pub const Statement = struct {
183 194
184 // Bind 195 // Bind
185 196
186 inline for (StructType.fields) |struct_field, _i| { 197 inline for (StructTypeInfo.fields) |struct_field, _i| {
187 const i = @as(usize, _i); 198 const i = @as(usize, _i);
188 const field_type_info = @typeInfo(struct_field.field_type); 199 const field_type_info = @typeInfo(struct_field.field_type);
189 const field_value = @field(values, struct_field.name); 200 const field_value = @field(values, struct_field.name);
@@ -193,6 +204,10 @@ pub const Statement = struct {
193 []const u8, []u8 => { 204 []const u8, []u8 => {
194 _ = c.sqlite3_bind_text(stmt, column, field_value.ptr, @intCast(c_int, field_value.len), null); 205 _ = c.sqlite3_bind_text(stmt, column, field_value.ptr, @intCast(c_int, field_value.len), null);
195 }, 206 },
207 Bytes => switch (field_value) {
208 .Text => |v| _ = c.sqlite3_bind_text(stmt, column, v.ptr, @intCast(c_int, v.len), null),
209 .Blob => |v| _ = c.sqlite3_bind_blob(stmt, column, v.ptr, @intCast(c_int, v.len), null),
210 },
196 else => switch (field_type_info) { 211 else => switch (field_type_info) {
197 .Int, .ComptimeInt => _ = c.sqlite3_bind_int64(stmt, column, @intCast(c_longlong, field_value)), 212 .Int, .ComptimeInt => _ = c.sqlite3_bind_int64(stmt, column, @intCast(c_longlong, field_value)),
198 .Float, .ComptimeFloat => _ = c.sqlite3_bind_double(stmt, column, field_value), 213 .Float, .ComptimeFloat => _ = c.sqlite3_bind_double(stmt, column, field_value),
@@ -512,6 +527,16 @@ test "sqlite: statement exec" {
512 527
513 testing.expectEqual(@as(usize, 33), age.?); 528 testing.expectEqual(@as(usize, 33), age.?);
514 } 529 }
530
531 // Test with a Bytes struct
532
533 {
534 // try db.exec("INSERT INTO user(id, name, age) VALUES(?, ?, ?)", .{
535 // .id = 200,
536 // .name = Bytes{ .Text = "hello" },
537 // .age = 20,
538 // });
539 }
515} 540}
516 541
517fn dbMode() Db.Mode { 542fn dbMode() Db.Mode {