From fada72cd26ad31e1fc834788c1224ed05a78143b Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Fri, 29 Sep 2023 09:44:54 +0100 Subject: Generify ErrorResponse to allow for NoticeResponse which shares it's structure. Add a very basic test for running an actual query --- src/conn/conn.zig | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'src/conn') 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 -- cgit v1.2.3-ZIG