summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2020-12-21 02:09:18 +0100
committerGravatar Vincent Rischmann2020-12-21 02:10:25 +0100
commitaeacdda36647fe9f59fea92d1a2f7c99f5787b95 (patch)
treebb44d8fa16c2fe8ef2f008a6857f9ea5895dcccc /sqlite.zig
parentMerge branch 'refactor' (diff)
downloadzig-sqlite-aeacdda36647fe9f59fea92d1a2f7c99f5787b95.tar.gz
zig-sqlite-aeacdda36647fe9f59fea92d1a2f7c99f5787b95.tar.xz
zig-sqlite-aeacdda36647fe9f59fea92d1a2f7c99f5787b95.zip
add the readPointer method
Diffstat (limited to 'sqlite.zig')
-rw-r--r--sqlite.zig27
1 files changed, 24 insertions, 3 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 015dd64..10695ba 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -248,10 +248,10 @@ pub fn Iterator(comptime Type: type) type {
248 // If the array is too small for the data an error will be returned. 248 // If the array is too small for the data an error will be returned.
249 fn readArray(self: *Self, comptime ArrayType: type, _i: usize) error{ArrayTooSmall}!ArrayType { 249 fn readArray(self: *Self, comptime ArrayType: type, _i: usize) error{ArrayTooSmall}!ArrayType {
250 const i = @intCast(c_int, _i); 250 const i = @intCast(c_int, _i);
251 const array_type_info = @typeInfo(ArrayType); 251 const type_info = @typeInfo(ArrayType);
252 252
253 var ret: ArrayType = undefined; 253 var ret: ArrayType = undefined;
254 switch (array_type_info) { 254 switch (type_info) {
255 .Array => |arr| { 255 .Array => |arr| {
256 comptime if (arr.sentinel == null) { 256 comptime if (arr.sentinel == null) {
257 @compileError("cannot populate array of " ++ @typeName(arr.child) ++ ", arrays must have a sentinel"); 257 @compileError("cannot populate array of " ++ @typeName(arr.child) ++ ", arrays must have a sentinel");
@@ -356,6 +356,27 @@ pub fn Iterator(comptime Type: type) type {
356 } 356 }
357 } 357 }
358 358
359 fn readPointer(self: *Self, comptime PointerType: type, i: usize, options: anytype) !PointerType {
360 const type_info = @typeInfo(PointerType);
361
362 var ret: PointerType = undefined;
363 switch (type_info) {
364 .Pointer => |ptr| {
365 switch (ptr.size) {
366 .One => unreachable,
367 .Slice => switch (ptr.child) {
368 u8 => ret = try self.readBytes(PointerType, i, .Text, options),
369 else => @compileError("cannot read pointer of type " ++ @typeName(PointerType)),
370 },
371 else => @compileError("cannot read pointer of type " ++ @typeName(PointerType)),
372 }
373 },
374 else => @compileError("cannot read pointer of type " ++ @typeName(PointerType)),
375 }
376
377 return ret;
378 }
379
359 // readStruct reads an entire sqlite row into a struct. 380 // readStruct reads an entire sqlite row into a struct.
360 // 381 //
361 // Each field correspond to a column; its position in the struct determines the column used for it. 382 // Each field correspond to a column; its position in the struct determines the column used for it.
@@ -385,7 +406,6 @@ pub fn Iterator(comptime Type: type) type {
385 const field_type_info = @typeInfo(field.field_type); 406 const field_type_info = @typeInfo(field.field_type);
386 407
387 const ret = switch (field.field_type) { 408 const ret = switch (field.field_type) {
388 []const u8, []u8 => try self.readBytes(field.field_type, i, .Blob, options),
389 Blob => try self.readBytes(Blob, i, .Blob, options), 409 Blob => try self.readBytes(Blob, i, .Blob, options),
390 Text => try self.readBytes(Text, i, .Text, options), 410 Text => try self.readBytes(Text, i, .Text, options),
391 else => switch (field_type_info) { 411 else => switch (field_type_info) {
@@ -394,6 +414,7 @@ pub fn Iterator(comptime Type: type) type {
394 .Bool => try self.readBool(i, options), 414 .Bool => try self.readBool(i, options),
395 .Void => {}, 415 .Void => {},
396 .Array => try self.readArray(field.field_type, i), 416 .Array => try self.readArray(field.field_type, i),
417 .Pointer => try self.readPointer(field.field_type, i, options),
397 else => @compileError("cannot populate field " ++ field.name ++ " of type " ++ @typeName(field.field_type)), 418 else => @compileError("cannot populate field " ++ field.name ++ " of type " ++ @typeName(field.field_type)),
398 }, 419 },
399 }; 420 };