Skip to content

Commit

Permalink
fix: binary result set
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Nov 4, 2023
1 parent 4126320 commit 659d2e8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
3 changes: 1 addition & 2 deletions integration_tests/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ test "prepare execute with result" {

{
const query =
\\SELECT 42,null,'hello'
\\SELECT 1,3,5,7
;
const prep_res = try c.prepare(allocator, query);
defer prep_res.deinit(allocator);
Expand All @@ -256,7 +256,6 @@ test "prepare execute with result" {
const rows = (try expectRows(query_res.value)).iter();
while (try rows.next(allocator)) |row| {
defer row.deinit(allocator);
// std.debug.print("rows.next: {any}\n", .{row});
}
}
}
Expand Down
28 changes: 10 additions & 18 deletions src/conn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,11 @@ pub const Conn = struct {
constants.OK => .{ .ok = OkPacket.initFromPacket(&response_packet, conn.client_capabilities) },
constants.ERR => .{ .err = ErrorPacket.initFromPacket(false, &response_packet, conn.client_capabilities) },
constants.LOCAL_INFILE_REQUEST => _ = @panic("not implemented"),
else => .{
.rows = blk: {
var packet_reader = PacketReader.initFromPacket(&response_packet);
const column_count = packet_reader.readLengthEncodedInteger();
var result_set = try ResultSet(TextResultRow).init(allocator, conn, column_count);
break :blk result_set;
},
},
else => .{ .rows = blk: {
var packet_reader = PacketReader.initFromPacket(&response_packet);
const column_count = packet_reader.readLengthEncodedInteger();
break :blk try ResultSet(TextResultRow).init(allocator, conn, column_count);
} },
},
};
}
Expand Down Expand Up @@ -95,22 +92,17 @@ pub const Conn = struct {
const execute_request: ExecuteRequest = .{ .prep_ok = &prep_ok, .capabilities = conn.client_capabilities };
try conn.sendPacketUsingSmallPacketWriter(execute_request);

if (prep_ok.num_columns > 0) {
return .{
.packet = .{ .payload_length = 0, .sequence_id = 0, .payload = &.{} }, // create phantom packet to make the result type happy
.value = .{
.rows = try ResultSet(BinaryResultRow).init(allocator, conn, prep_ok.num_columns),
},
};
}

const response_packet = try conn.readPacket(allocator);
return .{
.packet = response_packet,
.value = switch (response_packet.payload[0]) {
constants.OK => .{ .ok = OkPacket.initFromPacket(&response_packet, conn.client_capabilities) },
constants.ERR => .{ .err = ErrorPacket.initFromPacket(false, &response_packet, conn.client_capabilities) },
else => return response_packet.asError(conn.client_capabilities),
else => .{ .rows = blk: {
var packet_reader = PacketReader.initFromPacket(&response_packet);
const column_count = packet_reader.readLengthEncodedInteger();
break :blk try ResultSet(BinaryResultRow).init(allocator, conn, column_count);
} },
},
};
}
Expand Down
6 changes: 2 additions & 4 deletions src/result.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ pub fn ResultSet(comptime ResultRowType: type) type {
t.column_definitions = try allocator.alloc(ColumnDefinition41, column_count);
errdefer allocator.free(t.column_definitions);
for (0..column_count) |i| {
const packet = try conn.readPacket(allocator);
errdefer packet.deinit(allocator);
t.column_packets[i] = packet;
t.column_definitions[i] = ColumnDefinition41.initFromPacket(&packet);
t.column_packets[i] = try conn.readPacket(allocator);
t.column_definitions[i] = ColumnDefinition41.initFromPacket(&t.column_packets[i]);
}

const eof_packet = try conn.readPacket(allocator);
Expand Down

0 comments on commit 659d2e8

Please sign in to comment.