Skip to content

Commit

Permalink
add row.get(T, index) API
Browse files Browse the repository at this point in the history
  • Loading branch information
karlseguin committed Nov 22, 2024
1 parent 4438b83 commit 2a5eba1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,14 @@ if (rows.err) |err| {
```

## Row Getters
A `row` exposes the following functions to fetch data:
There are two APIs for fetching column data. The first is the generic `get`:

```zig
get(T, index) T
```
Where `T` can be: `i64`, 'f64', 'bool', '[]const' or `zqlite.Blob` or their nullable equivalent (i.e. `?i64`). The return type for `zqlite.Blob` is `[]const u8`.

Alternatively, the following can be used:

* `boolean(index) bool`
* `nullableBoolean(index) ?bool`
Expand Down
55 changes: 46 additions & 9 deletions src/conn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,30 @@ pub const Row = struct {
return self.stmt.deinitErr();
}

pub fn get(self: Row, comptime T: type, index: usize) GetReturnType(T) {
switch (T) {
i64 => return self.int(index),
?i64 => return self.nullableInt(index),
bool => return self.boolean(index),
?bool => return self.nullableBoolean(index),
?[]const u8 => return self.nullableText(index),
[]const u8 => return self.text(index),
?Blob => return self.nullableBlob(index),
Blob => return self.blob(index),
f64 => return self.float(index),
?f64 => return self.nullableFloat(index),
else => @compileError("unsupport column type: " ++ @typeName(T)),
}
}

fn GetReturnType(comptime T: type) type {
switch (T) {
Blob => return []const u8,
?Blob => return ?[]const u8,
else => return T,
}
}

pub fn boolean(self: Row, index: usize) bool {
return self.stmt.boolean(index);
}
Expand Down Expand Up @@ -628,6 +652,9 @@ test "boolean" {

try t.expectEqual(true, row.boolean(0));
try t.expectEqual(true, row.nullableBoolean(1).?);

try t.expectEqual(true, row.get(bool, 0));
try t.expectEqual(true, row.get(?bool, 1).?);
}

{
Expand All @@ -637,6 +664,9 @@ test "boolean" {

try t.expectEqual(false, row.boolean(0));
try t.expectEqual(false, row.nullableBoolean(1).?);

try t.expectEqual(false, row.get(bool, 0));
try t.expectEqual(false, row.get(?bool, 1).?);
}

{
Expand All @@ -645,6 +675,7 @@ test "boolean" {
defer row.deinit();

try t.expectEqual(@as(?bool, null), row.nullableBoolean(0));
try t.expectEqual(@as(?bool, null), row.get(?bool, 0));
}
}

Expand All @@ -662,9 +693,15 @@ test "blob/text" {
try t.expectEqualStrings(&d1, row.blob(0));
try t.expectEqualStrings(&d2, row.nullableBlob(1).?);

try t.expectEqualStrings(&d1, row.get(Blob, 0));
try t.expectEqualStrings(&d2, row.get(?Blob, 1).?);

try t.expectEqualStrings(&d1, row.text(2));
try t.expectEqualStrings(&d2, row.nullableText(3).?);

try t.expectEqualStrings(&d1, row.get([]const u8, 2));
try t.expectEqualStrings(&d2, row.get(?[]const u8, 3).?);

try t.expectEqualStrings(&d1, std.mem.span(row.textZ(2)));
try t.expectEqualStrings(&d2, std.mem.span(row.nullableTextZ(3).?));
try t.expectEqual(@as(usize, 4), row.textLen(2));
Expand Down Expand Up @@ -885,14 +922,14 @@ fn queryId(conn: Conn, id: i64) ?TestRow {

return .{
.row = row,
.id = row.int(0),
.int = row.int(1),
.intn = row.nullableInt(2),
.real = row.float(3),
.realn = row.nullableFloat(4),
.text = row.text(5),
.textn = row.nullableText(6),
.blob = row.blob(7),
.blobn = row.nullableBlob(8),
.id = row.get(i64, 0),
.int = row.get(i64, 1),
.intn = row.get(?i64, 2),
.real = row.get(f64, 3),
.realn = row.get(?f64, 4),
.text = row.get([]const u8, 5),
.textn = row.get(?[]const u8, 6),
.blob = row.get(Blob, 7),
.blobn = row.get(?Blob, 8),
};
}

0 comments on commit 2a5eba1

Please sign in to comment.