Skip to content

Commit

Permalink
feat: generics result iter
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Nov 3, 2023
1 parent b898e69 commit 1906ab4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 40 deletions.
27 changes: 24 additions & 3 deletions integration_tests/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ test "query text protocol" {
{
const qr = try c.query(allocator, "SELECT 3,4");
defer qr.deinit(allocator);
var rows = (try expectRows(qr.value)).iter();
const rows = (try expectRows(qr.value)).iter();

var dest = [_]?[]const u8{ undefined, undefined };
while (try rows.next(allocator)) |row| {
Expand All @@ -106,7 +106,7 @@ test "query text protocol" {
{
const qr = try c.query(allocator, "SELECT 5,null,7");
defer qr.deinit(allocator);
var rows = (try expectRows(qr.value)).iter();
const rows = (try expectRows(qr.value)).iter();
var dest = [_]?[]const u8{ undefined, undefined, undefined };
while (try rows.next(allocator)) |row| {
defer row.deinit(allocator);
Expand All @@ -119,7 +119,7 @@ test "query text protocol" {
{
const qr = try c.query(allocator, "SELECT 8,9 UNION ALL SELECT 10,11");
defer qr.deinit(allocator);
var rows = try expectRows(qr.value);
const rows = try expectRows(qr.value);

var dest = [_]?[]const u8{ undefined, undefined };
{
Expand Down Expand Up @@ -239,3 +239,24 @@ test "prepare execute - 2" {
_ = try expectOk(qr.value);
}
}

// test "prepare execute with result" {
// var c = Client.init(test_config);
// defer c.deinit();
//
// {
// const query =
// \\SELECT 42,null,'hello'
// ;
// const pr = try c.prepare(allocator, query);
// defer pr.deinit(allocator);
// const prep_ok = try expectOk(pr.value);
// const qr = try c.execute(allocator, prep_ok);
// const rows = (try expectRows(qr.value)).iter();
// while (try rows.next(allocator)) |row| {
// defer row.deinit(allocator);
// }
// }
// }
//
// // SELECT CONCAT(?, ?) AS col1
72 changes: 37 additions & 35 deletions src/helper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,47 @@ pub fn scanBinaryResultRow(comptime T: type, raw: []const u8, dest: *T, options:
@panic("not implemented");
}

pub const TextResultSetIter = struct {
text_result_set: *const ResultSet(TextResultRow),
pub fn ResultSetIter(comptime ResultRowType: type) type {
return struct {
text_result_set: *const ResultSet(ResultRowType),

pub fn next(i: *const TextResultSetIter, allocator: std.mem.Allocator) !?TextResultRow {
const row = try i.text_result_set.readRow(allocator);
return switch (row.value) {
.eof => {
// need to deinit as caller would not know to do so
row.deinit(allocator);
return null;
},
.err => |err| err.asError(),
else => row,
};
}

pub fn collect(iter: *const TextResultSetIter, allocator: std.mem.Allocator) !TableTexts {
var row_acc = std.ArrayList(TextResultRow).init(allocator);
while (try iter.next(allocator)) |row| {
var new_row_ptr = try row_acc.addOne();
new_row_ptr.* = row;
pub fn next(i: *const ResultSetIter(ResultRowType), allocator: std.mem.Allocator) !?TextResultRow {
const row = try i.text_result_set.readRow(allocator);
return switch (row.value) {
.eof => {
// need to deinit as caller would not know to do so
row.deinit(allocator);
return null;
},
.err => |err| err.asError(),
else => row,
};
}

const num_cols = iter.text_result_set.column_definitions.len;
var rows = try allocator.alloc([]?[]const u8, row_acc.items.len);
var elems = try allocator.alloc(?[]const u8, row_acc.items.len * num_cols);
for (row_acc.items, 0..) |row, i| {
const dest_row = elems[i * num_cols .. (i + 1) * num_cols];
try row.scan(dest_row);
rows[i] = dest_row;
}
pub fn collect(iter: *const ResultSetIter(ResultRowType), allocator: std.mem.Allocator) !TableTexts {
var row_acc = std.ArrayList(TextResultRow).init(allocator);
while (try iter.next(allocator)) |row| {
var new_row_ptr = try row_acc.addOne();
new_row_ptr.* = row;
}

return .{
.result_rows = try row_acc.toOwnedSlice(),
.elems = elems,
.rows = rows,
};
}
};
const num_cols = iter.text_result_set.column_definitions.len;
var rows = try allocator.alloc([]?[]const u8, row_acc.items.len);
var elems = try allocator.alloc(?[]const u8, row_acc.items.len * num_cols);
for (row_acc.items, 0..) |row, i| {
const dest_row = elems[i * num_cols .. (i + 1) * num_cols];
try row.scan(dest_row);
rows[i] = dest_row;
}

return .{
.result_rows = try row_acc.toOwnedSlice(),
.elems = elems,
.rows = rows,
};
}
};
}

pub const TableTexts = struct {
result_rows: []TextResultRow,
Expand Down
4 changes: 2 additions & 2 deletions src/result.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ColumnDefinition41 = protocol.column_definition.ColumnDefinition41;
const Conn = @import("./conn.zig").Conn;
const EofPacket = protocol.generic_response.EofPacket;
const helper = @import("./helper.zig");
const TextResultSetIter = helper.TextResultSetIter;
const ResultSetIter = helper.ResultSetIter;

pub fn QueryResult(comptime ResultRowType: type) type {
return struct {
Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn ResultSet(comptime ResultRowType: type) type {
};
}

pub fn iter(t: *const ResultSet(ResultRowType)) TextResultSetIter {
pub fn iter(t: *const ResultSet(ResultRowType)) ResultSetIter(ResultRowType) {
return .{ .text_result_set = t };
}
};
Expand Down

0 comments on commit 1906ab4

Please sign in to comment.