summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2020-12-20 22:25:14 +0100
committerGravatar Vincent Rischmann2020-12-21 00:18:06 +0100
commit37028f0a6b0e7c349b3cc4a44184f31cbacfe3cc (patch)
tree477f8b18dc5366a7fac748c33bbfe943a92f7f33
parentadd reading a field into an array (diff)
downloadzig-sqlite-37028f0a6b0e7c349b3cc4a44184f31cbacfe3cc.tar.gz
zig-sqlite-37028f0a6b0e7c349b3cc4a44184f31cbacfe3cc.tar.xz
zig-sqlite-37028f0a6b0e7c349b3cc4a44184f31cbacfe3cc.zip
pass the column in readArray
also test that we can read an array field
-rw-r--r--sqlite.zig19
1 files changed, 12 insertions, 7 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 556188f..82d2313 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -230,7 +230,7 @@ pub fn Iterator(comptime Type: type) type {
230 .Array => { 230 .Array => {
231 debug.assert(columns == 1); 231 debug.assert(columns == 1);
232 var ret: Type = undefined; 232 var ret: Type = undefined;
233 try self.readArray(Type, &ret); 233 try self.readArray(Type, 0, &ret);
234 return ret; 234 return ret;
235 }, 235 },
236 .Struct => { 236 .Struct => {
@@ -241,7 +241,8 @@ pub fn Iterator(comptime Type: type) type {
241 } 241 }
242 } 242 }
243 243
244 fn readArray(self: *Self, comptime ArrayType: type, array: anytype) !void { 244 fn readArray(self: *Self, comptime ArrayType: type, _i: usize, array: anytype) !void {
245 const i = @intCast(c_int, _i);
245 const array_type_info = @typeInfo(ArrayType); 246 const array_type_info = @typeInfo(ArrayType);
246 247
247 switch (array_type_info) { 248 switch (array_type_info) {
@@ -252,12 +253,14 @@ pub fn Iterator(comptime Type: type) type {
252 253
253 switch (arr.child) { 254 switch (arr.child) {
254 u8 => { 255 u8 => {
255 const data = c.sqlite3_column_blob(self.stmt, 0); 256 const data = c.sqlite3_column_blob(self.stmt, i);
256 const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, 0)); 257 const size = @intCast(usize, c.sqlite3_column_bytes(self.stmt, i));
257 258
258 if (size >= @as(usize, arr.len)) return error.ArrayTooSmall; 259 if (size >= @as(usize, arr.len)) return error.ArrayTooSmall;
259 260
260 mem.copy(u8, array[0..], @ptrCast([*c]const u8, data)[0..size]); 261 const ptr = @ptrCast([*c]const u8, data)[0..size];
262
263 mem.copy(u8, array[0..], ptr);
261 array[size] = arr.sentinel.?; 264 array[size] = arr.sentinel.?;
262 }, 265 },
263 else => @compileError("cannot populate field " ++ field.name ++ " of type array of " ++ @typeName(arr.child)), 266 else => @compileError("cannot populate field " ++ field.name ++ " of type array of " ++ @typeName(arr.child)),
@@ -340,7 +343,7 @@ pub fn Iterator(comptime Type: type) type {
340 @field(value, field.name) = {}; 343 @field(value, field.name) = {};
341 }, 344 },
342 .Array => { 345 .Array => {
343 try self.readArray(field.fieldtype, &@field(value, field.name), .{}); 346 try self.readArray(field.field_type, i, &@field(value, field.name));
344 }, 347 },
345 else => @compileError("cannot populate field " ++ field.name ++ " of type " ++ @typeName(field.field_type)), 348 else => @compileError("cannot populate field " ++ field.name ++ " of type " ++ @typeName(field.field_type)),
346 }, 349 },
@@ -785,13 +788,14 @@ test "sqlite: read in an anonymous struct" {
785 try db.init(testing.allocator, .{ .mode = dbMode() }); 788 try db.init(testing.allocator, .{ .mode = dbMode() });
786 try addTestData(&db); 789 try addTestData(&db);
787 790
788 var stmt = try db.prepare("SELECT id, name, age FROM user WHERE id = ?{usize}"); 791 var stmt = try db.prepare("SELECT id, name, name, age FROM user WHERE id = ?{usize}");
789 defer stmt.deinit(); 792 defer stmt.deinit();
790 793
791 var row = try stmt.one( 794 var row = try stmt.one(
792 struct { 795 struct {
793 id: usize, 796 id: usize,
794 name: []const u8, 797 name: []const u8,
798 name_2: [200:0xAD]u8,
795 age: usize, 799 age: usize,
796 }, 800 },
797 .{ .allocator = &arena.allocator }, 801 .{ .allocator = &arena.allocator },
@@ -802,6 +806,7 @@ test "sqlite: read in an anonymous struct" {
802 const exp = test_users[0]; 806 const exp = test_users[0];
803 testing.expectEqual(exp.id, row.?.id); 807 testing.expectEqual(exp.id, row.?.id);
804 testing.expectEqualStrings(exp.name, row.?.name); 808 testing.expectEqualStrings(exp.name, row.?.name);
809 testing.expectEqualStrings(exp.name, mem.spanZ(&row.?.name_2));
805 testing.expectEqual(exp.age, row.?.age); 810 testing.expectEqual(exp.age, row.?.age);
806} 811}
807 812