diff options
author | Martin Ashby <martin@ashbysoft.com> | 2023-09-28 20:46:10 +0100 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2023-09-28 20:46:10 +0100 |
commit | c00a7cd57be154b5a770a397319c8c8ad35c98b6 (patch) | |
tree | 89ef0f7bef322574883260c8952e0f3f71fa40ff /src/conn/conn.zig | |
parent | d1435847fa68a548f8a2b8c61857691c6f5ac924 (diff) | |
download | pgz-c00a7cd57be154b5a770a397319c8c8ad35c98b6.tar.gz pgz-c00a7cd57be154b5a770a397319c8c8ad35c98b6.tar.bz2 pgz-c00a7cd57be154b5a770a397319c8c8ad35c98b6.tar.xz pgz-c00a7cd57be154b5a770a397319c8c8ad35c98b6.zip |
WIP
Diffstat (limited to 'src/conn/conn.zig')
-rw-r--r-- | src/conn/conn.zig | 72 |
1 files changed, 45 insertions, 27 deletions
diff --git a/src/conn/conn.zig b/src/conn/conn.zig index fbde06c..db5084c 100644 --- a/src/conn/conn.zig +++ b/src/conn/conn.zig @@ -125,56 +125,74 @@ pub const ResultIterator = struct { row_description: ?proto.RowDescription = null, current_datarow: ?proto.DataRow = null, command_complete: ?proto.CommandComplete = null, + pub fn init(conn: *Conn) ResultIterator { + return .{ + .conn = conn, + }; + } pub fn deinit(self: *ResultIterator) void { if (self.row_description != null) self.row_description.?.deinit(self.conn.allocator); if (self.current_datarow != null) self.current_datarow.?.deinit(self.conn.allocator); if (self.command_complete != null) self.command_complete.?.deinit(self.conn.allocator); } - // NextRow advances the ResultIterator to the next row and returns true if a row is available. - pub fn next_row(self: *ResultIterator) !bool { + // NextRow advances the ResultIterator to the next row and returns a row if one is available. + // or null if we've reached the end of the reuslt. + pub fn next_row(self: *ResultIterator) !?[][]const u8 { while (self.command_complete == null) { - var msg = try self.conn.receive_message(); + var msg = try self.receive_message(); switch (msg) { - .DataRow => |dr| { - if (self.row_description != null) self.row_description.?.deinit(self.conn.allocator); - self.current_datarow = dr; - return true; - }, - .RowDescription => |rd| { - if (self.row_description != null) return ProtocolError.UnexpectedMessage; - self.row_description = rd; - }, - .CommandComplete => |cc| { - if (self.command_complete != null) return ProtocolError.UnexpectedMessage; - self.command_complete = cc; + .DataRow => { + return self.current_datarow.?.columns; }, else => { msg.deinit(self.conn.allocator); }, } } - return false; + return null; + } + + pub fn skip_to_end(self: *ResultIterator) !void { + while (self.command_complete == null) { + _ = try self.receive_message(); + } } - // row returns the current row data - pub fn row(self: ResultIterator) ?[][]const u8 { - return if (self.current_datarow) |dr| dr.columns else null; + + fn receive_message(self: *ResultIterator) !BackendMessage { + var msg = if (self.multi_iterator == null) try self.conn.receive_message() else try self.multi_iterator.?.receive_message(); + switch (msg) { + .DataRow => |dr| { + if (self.current_datarow != null) self.current_datarow.?.deinit(self.conn.allocator); + self.current_datarow = try dr.clone(self.conn.allocator); + }, + .RowDescription => |rd| { + if (self.row_description != null) return ProtocolError.UnexpectedMessage; + self.row_description = try rd.clone(self.conn.allocator); + }, + .CommandComplete => |cc| { + if (self.command_complete != null) return ProtocolError.UnexpectedMessage; + self.command_complete = try cc.clone(self.conn.allocator); + }, + } + return msg; } }; pub const MultiResultIterator = struct { conn: *Conn, - cri: ?*ResultIterator = null, + cri: ?*ResultIterator, - pub fn next_result(self: *MultiResultIterator) !bool { - if () + // returns the next result iterator, or null if we've reached the end of the results + pub fn next_result(self: *MultiResultIterator) !?*ResultIterator { + if (self.cri != null) { + try self.cri.?.skip_to_end(); + } } - pub fn result(self: MultiResultIterator) ?*ResultIterator { - return self.cri; - } fn receive_message(self: *MultiResultIterator) !BackendMessage { - _ = self; - return error.NotImplemented; + var msg = try self.conn.receive_message(); + switch (msg) {} + return msg; } }; |