diff options
author | Martin Ashby <martin@ashbysoft.com> | 2023-09-29 09:44:54 +0100 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2023-09-29 09:44:54 +0100 |
commit | fada72cd26ad31e1fc834788c1224ed05a78143b (patch) | |
tree | db71f2cbc6cbf9c47148271682641c940eb75650 /src/conn/conn.zig | |
parent | 6de632a41bdd127e92de68d61a18dfee91b8b188 (diff) | |
download | pgz-main.tar.gz pgz-main.tar.bz2 pgz-main.tar.xz pgz-main.zip |
structure.
Add a very basic test for running an actual query
Diffstat (limited to 'src/conn/conn.zig')
-rw-r--r-- | src/conn/conn.zig | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/conn/conn.zig b/src/conn/conn.zig index 9d5a8e1..378a8d1 100644 --- a/src/conn/conn.zig +++ b/src/conn/conn.zig @@ -8,6 +8,7 @@ const PasswordMessage = proto.PasswordMessage; const BackendMessage = proto.BackendMessage; const RowDescription = proto.RowDescription; const read_message = proto.read_message; +const clone_message = proto.clone_message; const ProtocolError = @import("../main.zig").ProtocolError; const ServerError = @import("../main.zig").ServerError; const ClientError = @import("../main.zig").ClientError; @@ -99,9 +100,10 @@ fn receive_message(self: *Conn) !BackendMessage { return ServerError.ErrorResponse; } }, - // .NoticeResponse => { - // // TODO handle notice response - // }, + .NoticeResponse => |nr| { + // log it? + log.warn("NOTICE {}", .{nr}); + }, // .NotificationResponse => { // // TODO handle notificationResponse // }, @@ -154,7 +156,8 @@ pub const ResultIterator = struct { pub fn skip_to_end(self: *ResultIterator) !void { while (self.command_complete == null) { - _ = try self.receive_message(); + var msg = try self.receive_message(); + msg.deinit(self.conn.allocator); } } @@ -163,16 +166,17 @@ pub const ResultIterator = struct { 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); + self.current_datarow = try clone_message(dr, self.conn.allocator); }, .RowDescription => |rd| { if (self.row_description != null) return ProtocolError.UnexpectedMessage; - self.row_description = try rd.clone(self.conn.allocator); + self.row_description = try clone_message(rd, self.conn.allocator); }, .CommandComplete => |cc| { if (self.command_complete != null) return ProtocolError.UnexpectedMessage; - self.command_complete = try cc.clone(self.conn.allocator); + self.command_complete = try clone_message(cc, self.conn.allocator); }, + else => {}, } return msg; } @@ -252,3 +256,21 @@ test "connect tcp with wrong password" { // }; // try std.testing.expectError(ServerError.ErrorResponse, Conn.connect(cfg)); } + +test "exec" { + // must have a local postgres runnning + // TODO maybe use docker to start one? + const allocator = std.testing.allocator; + const cfg = Config{ + .allocator = allocator, + .address = .{ .unix = "/run/postgresql/.s.PGSQL.5432" }, + .database = "martin", + .user = "martin", + }; + var conn = try Conn.connect(cfg); + defer conn.deinit(); + var ri = try conn.exec("create table if not exists foo (col1 int not null)"); + defer ri.deinit(); + try ri.skip_to_end(); + +}
\ No newline at end of file |