Skip to content

Commit

Permalink
feat: add more assertion to conn
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Feb 24, 2024
1 parent 9242c92 commit 5b791c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
37 changes: 31 additions & 6 deletions src/conn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const max_packet_size = 1 << 24 - 1;
const buffer_size: usize = 4096;

pub const Conn = struct {
connected: bool,
stream: std.net.Stream,
reader: PacketReader,
writer: PacketWriter,
Expand All @@ -40,6 +41,7 @@ pub const Conn = struct {
var conn: Conn = blk: {
const stream = try std.net.tcpConnectToAddress(config.address);
break :blk .{
.connected = true,
.stream = stream,
.reader = try PacketReader.init(stream, allocator),
.writer = try PacketWriter.init(stream, allocator),
Expand Down Expand Up @@ -90,7 +92,7 @@ pub const Conn = struct {
}

pub fn ping(c: *Conn) !void {
c.sequence_id = 0;
c.ready();
try c.writeBytesAsPacket(&[_]u8{constants.COM_PING});
try c.writer.flush();
const packet = try c.readPacket();
Expand All @@ -103,25 +105,25 @@ pub const Conn = struct {

// query that doesn't return any rows
pub fn query(c: *Conn, query_string: []const u8) !QueryResult {
c.sequence_id = 0;
c.ready();
const query_req: QueryRequest = .{ .query = query_string };
try c.writePacket(query_req);
try c.writer.flush();
const packet = try c.readPacket();
return QueryResult.init(&packet, c.capabilities);
return c.queryResult(&packet);
}

// query that expect rows, even if it returns 0 rows
pub fn queryRows(c: *Conn, allocator: std.mem.Allocator, query_string: []const u8) !QueryResultRows(TextResultRow) {
c.sequence_id = 0;
c.ready();
const query_req: QueryRequest = .{ .query = query_string };
try c.writePacket(query_req);
try c.writer.flush();
return QueryResultRows(TextResultRow).init(c, allocator);
}

pub fn prepare(c: *Conn, allocator: std.mem.Allocator, query_string: []const u8) !PrepareResult {
c.sequence_id = 0;
c.ready();
const prepare_request: PrepareRequest = .{ .query = query_string };
try c.writePacket(prepare_request);
try c.writer.flush();
Expand All @@ -130,6 +132,7 @@ pub const Conn = struct {

// execute a prepared statement that doesn't return any rows
pub fn execute(c: *Conn, prep_stmt: *const PreparedStatement, params: anytype) !QueryResult {
c.ready();
std.debug.assert(prep_stmt.res_cols.len == 0); // execute expects no rows
c.sequence_id = 0;
const execute_request: ExecuteRequest = .{
Expand All @@ -139,11 +142,12 @@ pub const Conn = struct {
try c.writePacketWithParam(execute_request, params);
try c.writer.flush();
const packet = try c.readPacket();
return QueryResult.init(&packet, c.capabilities);
return c.queryResult(&packet);
}

// execute a prepared statement that expect rows, even if it returns 0 rows
pub fn executeRows(c: *Conn, allocator: std.mem.Allocator, prep_stmt: *const PreparedStatement, params: anytype) !QueryResultRows(BinaryResultRow) {
c.ready();
std.debug.assert(prep_stmt.res_cols.len > 0); // executeRows expects rows
c.sequence_id = 0;
const execute_request: ExecuteRequest = .{
Expand Down Expand Up @@ -260,4 +264,25 @@ pub const Conn = struct {
c.sequence_id += 1;
return sequence_id;
}

inline fn queryResult(c: *Conn, packet: *const Packet) !QueryResult {
const res = QueryResult.init(packet, c.capabilities) catch |err| {
switch (err) {
error.UnrecoverableError => {
c.stream.close();
c.connected = false;
return err;
},
else => return err,
}
};
return res;
}

inline fn ready(c: *Conn) void {
std.debug.assert(c.connected);
std.debug.assert(c.writer.pos == 0);
std.debug.assert(c.reader.pos == c.reader.len);
c.sequence_id = 0;
}
};
3 changes: 2 additions & 1 deletion src/result.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ pub const QueryResult = union(enum) {
std.log.warn(
\\Unexpected packet: {any}\n,
\\Are you expecting a result set? If so, use QueryResultRows instead.
\\This is unrecoverable error.
, .{packet});
return packet.asError(capabilities);
return error.UnrecoverableError;
},
};
}
Expand Down

0 comments on commit 5b791c5

Please sign in to comment.