Skip to content

Commit

Permalink
feat: refactor and safe deinit
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Nov 4, 2023
1 parent 659d2e8 commit 73e4168
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
46 changes: 23 additions & 23 deletions integration_tests/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,15 @@ test "prepare execute - 1" {
const prep_res = try c.prepare(allocator, "CREATE DATABASE testdb2");
defer prep_res.deinit(allocator);
const prep_ok = try expectOk(prep_res.value);
const query_res = try c.execute(allocator, prep_ok);
const query_res = try c.execute(allocator, &prep_ok);
defer query_res.deinit(allocator);
_ = try expectOk(query_res.value);
}
{
const prep_res = try c.prepare(allocator, "DROP DATABASE testdb2");
defer prep_res.deinit(allocator);
const prep_ok = try expectOk(prep_res.value);
const query_res = try c.execute(allocator, prep_ok);
const query_res = try c.execute(allocator, &prep_ok);
defer query_res.deinit(allocator);
_ = try expectOk(query_res.value);
}
Expand All @@ -229,35 +229,35 @@ test "prepare execute - 2" {
const prep_ok_2 = try expectOk(prep_res_2.value);

{
const query_res = try c.execute(allocator, prep_ok_1);
const query_res = try c.execute(allocator, &prep_ok_1);
defer query_res.deinit(allocator);
_ = try expectOk(query_res.value);
}
{
const query_res = try c.execute(allocator, prep_ok_2);
const query_res = try c.execute(allocator, &prep_ok_2);
defer query_res.deinit(allocator);
_ = try expectOk(query_res.value);
}
}

test "prepare execute with result" {
var c = Client.init(test_config);
defer c.deinit();

{
const query =
\\SELECT 1,3,5,7
;
const prep_res = try c.prepare(allocator, query);
defer prep_res.deinit(allocator);
const prep_ok = try expectOk(prep_res.value);
const query_res = try c.execute(allocator, prep_ok);
defer query_res.deinit(allocator);
const rows = (try expectRows(query_res.value)).iter();
while (try rows.next(allocator)) |row| {
defer row.deinit(allocator);
}
}
}
// test "prepare execute with result" {
// var c = Client.init(test_config);
// defer c.deinit();
//
// {
// const query =
// \\SELECT 1,3,5,7
// ;
// const prep_res = try c.prepare(allocator, query);
// defer prep_res.deinit(allocator);
// const prep_ok = try expectOk(prep_res.value);
// const query_res = try c.execute(allocator, &prep_ok);
// defer query_res.deinit(allocator);
// const rows = (try expectRows(query_res.value)).iter();
// while (try rows.next(allocator)) |row| {
// defer row.deinit(allocator);
// }
// }
// }

// SELECT CONCAT(?, ?) AS col1
2 changes: 1 addition & 1 deletion src/client.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub const Client = struct {
return client.conn.prepare(allocator, query_string);
}

pub fn execute(client: *Client, allocator: std.mem.Allocator, prep_ok: PrepareOk) !QueryResult(BinaryResultRow) {
pub fn execute(client: *Client, allocator: std.mem.Allocator, prep_ok: *const PrepareOk) !QueryResult(BinaryResultRow) {
try client.connectIfNotConnected(allocator);
return client.conn.execute(allocator, prep_ok);
}
Expand Down
20 changes: 13 additions & 7 deletions src/conn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub const Conn = struct {
else => .{ .rows = blk: {
var packet_reader = PacketReader.initFromPacket(&response_packet);
const column_count = packet_reader.readLengthEncodedInteger();
std.debug.assert(packet_reader.finished());
break :blk try ResultSet(TextResultRow).init(allocator, conn, column_count);
} },
},
Expand All @@ -86,23 +87,28 @@ pub const Conn = struct {
}

// TODO: add options
pub fn execute(conn: *Conn, allocator: std.mem.Allocator, prep_ok: PrepareOk) !QueryResult(BinaryResultRow) {
pub fn execute(conn: *Conn, allocator: std.mem.Allocator, prep_ok: *const PrepareOk) !QueryResult(BinaryResultRow) {
std.debug.assert(conn.state == .connected);
conn.sequence_id = 0;
const execute_request: ExecuteRequest = .{ .prep_ok = &prep_ok, .capabilities = conn.client_capabilities };
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 = &.{} },
.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 => .{ .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);
} },
else => return response_packet.asError(conn.client_capabilities),
},
};
}
Expand Down
7 changes: 6 additions & 1 deletion src/protocol/packet.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub const Packet = struct {
sequence_id: u8,
payload: []const u8,

// generate a packet safe to deinit with allocator
pub fn safe_deinit() Packet {
return .{ .payload_length = undefined, .sequence_id = undefined, .payload = &.{} };
}

pub fn initFromReader(allocator: std.mem.Allocator, sbr: *buffered_stream.Reader) !Packet {
var packet: Packet = undefined;

Expand All @@ -27,7 +32,7 @@ pub const Packet = struct {
if (packet.payload[0] == constants.ERR) {
return ErrorPacket.initFromPacket(false, packet, capabilities).asError();
}
std.log.warn("unexpected packet: {any}", .{packet.payload[0]});
std.log.warn("unexpected packet: {any}", .{packet});
return error.UnexpectedPacket;
}

Expand Down
13 changes: 7 additions & 6 deletions src/result.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ pub fn ResultSet(comptime ResultRowType: type) type {
column_definitions: []ColumnDefinition41,

pub fn init(allocator: std.mem.Allocator, conn: *Conn, column_count: u64) !ResultSet(ResultRowType) {
var t: ResultSet(ResultRowType) = undefined;
var t: ResultSet(ResultRowType) = .{ .conn = conn, .column_packets = &.{}, .column_definitions = &.{} };
errdefer t.deinit(allocator);

t.column_packets = try allocator.alloc(Packet, column_count);
errdefer allocator.free(t.column_packets);
@memset(t.column_packets, Packet.safe_deinit());
t.column_definitions = try allocator.alloc(ColumnDefinition41, column_count);
errdefer allocator.free(t.column_definitions);
for (0..column_count) |i| {
t.column_packets[i] = try conn.readPacket(allocator);
t.column_definitions[i] = ColumnDefinition41.initFromPacket(&t.column_packets[i]);

for (t.column_packets, t.column_definitions) |*pac, *def| {
pac.* = try conn.readPacket(allocator);
def.* = ColumnDefinition41.initFromPacket(pac);
}

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

0 comments on commit 73e4168

Please sign in to comment.