Skip to content

Commit

Permalink
const refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Dec 28, 2023
1 parent 5c4889a commit 686c76d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
32 changes: 2 additions & 30 deletions src/conn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,7 @@ pub const Conn = struct {
conn.sequence_id = 0;
const query_request: QueryRequest = .{ .query = query_string };
try conn.sendPacketUsingSmallPacketWriter(query_request);
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) },
constants.LOCAL_INFILE_REQUEST => _ = @panic("not implemented"),
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);
} },
},
};
return QueryResult(TextResultRow).init(conn, allocator);
}

// TODO: add options
Expand Down Expand Up @@ -96,21 +82,7 @@ pub const Conn = struct {
.prep_stmt = prep_stmt,
};
try conn.sendPacketUsingSmallPacketWriterWithParams(execute_request, params);

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();
std.debug.assert(packet_reader.finished());
break :blk try ResultSet(BinaryResultRow).init(allocator, conn, column_count);
} },
},
};
return QueryResult(BinaryResultRow).init(conn, allocator);
}

pub fn close(conn: *Conn) void {
Expand Down
20 changes: 19 additions & 1 deletion src/result.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Conn = @import("./conn.zig").Conn;
const EofPacket = protocol.generic_response.EofPacket;
const helper = @import("./helper.zig");
const ResultSetIter = helper.ResultSetIter;
const PacketReader = @import("./protocol/packet_reader.zig").PacketReader;

pub fn QueryResult(comptime ResultRowType: type) type {
return struct {
Expand All @@ -19,10 +20,27 @@ pub fn QueryResult(comptime ResultRowType: type) type {
err: ErrorPacket,
rows: ResultSet(ResultRowType),
};

packet: Packet,
value: Value,

pub fn init(conn: *Conn, allocator: std.mem.Allocator) !QueryResult(ResultRowType) {
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) },
constants.LOCAL_INFILE_REQUEST => _ = @panic("not implemented"),
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(ResultRowType).init(allocator, conn, column_count);
} },
},
};
}

pub fn deinit(q: *const QueryResult(ResultRowType), allocator: std.mem.Allocator) void {
q.packet.deinit(allocator);
switch (q.value) {
Expand Down

0 comments on commit 686c76d

Please sign in to comment.