From 183d60a6e87230cc767c56900b94c9c694596de1 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Tue, 26 Sep 2023 06:51:06 +0100 Subject: Move protocol definitions into a subfolder --- src/conn.zig | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) (limited to 'src/conn.zig') diff --git a/src/conn.zig b/src/conn.zig index 99018da..f9f4fb5 100644 --- a/src/conn.zig +++ b/src/conn.zig @@ -2,12 +2,7 @@ const std = @import("std"); const log = std.log.scoped(.pgz); const SSHashMap = std.StringHashMap([]const u8); const Config = @import("config.zig"); -const StartupMessage = @import("startup_message.zig"); -const ErrorResponse = @import("error_response.zig"); -const AuthenticationRequest = @import("authentication_request.zig"); -const ReadyForQuery = @import("ready_for_query.zig"); -const ParameterStatus = @import("parameter_status.zig"); -const BackendKeyData = @import("backend_key_data.zig"); +const Proto = @import("proto/proto.zig"); const read_message = @import("main.zig").read_message; const ProtocolError = @import("main.zig").ProtocolError; const ServerError = @import("main.zig").ServerError; @@ -44,7 +39,7 @@ pub fn connect(config: Config) !Conn { var params = SSHashMap.init(allocator); try params.put("user", config.user); if (config.database) |database| try params.put("database", database); - var sm = StartupMessage{ + var sm = Proto.StartupMessage{ .parameters = params, }; defer sm.deinit(allocator); @@ -52,36 +47,36 @@ pub fn connect(config: Config) !Conn { lp: while (true) { const response_type = try reader.readByte(); switch (response_type) { - ErrorResponse.Tag => { - var err = try read_message(ErrorResponse, allocator, reader); + Proto.ErrorResponse.Tag => { + var err = try read_message(Proto.ErrorResponse, allocator, reader); defer err.deinit(allocator); log.err("Error connecting to server {any}", .{err}); return ServerError.ErrorResponse; }, - AuthenticationRequest.Tag => { - var ar = try read_message(AuthenticationRequest, allocator, reader); + Proto.AuthenticationRequest.Tag => { + var ar = try read_message(Proto.AuthenticationRequest, allocator, reader); defer ar.deinit(allocator); // TODO handle the authentication request log.info("authentication request", .{}); }, - ReadyForQuery.Tag => { - var rfq = try read_message(ReadyForQuery, allocator, reader); + Proto.ReadyForQuery.Tag => { + var rfq = try read_message(Proto.ReadyForQuery, allocator, reader); defer rfq.deinit(allocator); // TODO do something about transaction state? res.status = .connStatusIdle; log.info("ready for query", .{}); break :lp; }, - ParameterStatus.Tag => { - var ps = try read_message(ParameterStatus, allocator, reader); + Proto.ParameterStatus.Tag => { + var ps = try read_message(Proto.ParameterStatus, allocator, reader); defer ps.deinit(allocator); // TODO Handle this somehow? - log.info("ParameterStatus: {s}:{s}", .{ps.name, ps.value}); + log.info("ParameterStatus: {s}:{s}", .{ ps.name, ps.value }); }, - BackendKeyData.Tag =>{ - var bkd = try read_message(BackendKeyData, allocator, reader); + Proto.BackendKeyData.Tag => { + var bkd = try read_message(Proto.BackendKeyData, allocator, reader); defer bkd.deinit(allocator); - log.info("BackendKeyData process_id {} secret_key {}" , .{bkd.process_id, bkd.secret_key}); + log.info("BackendKeyData process_id {} secret_key {}", .{ bkd.process_id, bkd.secret_key }); }, else => { log.err("unhandled message type [{c}]", .{response_type}); @@ -99,7 +94,7 @@ fn deinit(self: *Conn) void { self.stream.close(); } -//pub fn exec(self: *Conn) +//pub fn exec(self: *Conn) test "connect" { // must have a local postgres runnning @@ -107,11 +102,10 @@ test "connect" { const allocator = std.testing.allocator; const cfg = Config{ .allocator = allocator, - .address = .{.unix = "/run/postgresql/.s.PGSQL.5432"}, + .address = .{ .unix = "/run/postgresql/.s.PGSQL.5432" }, .database = "martin", .user = "martin", }; var conn = try Conn.connect(cfg); defer conn.deinit(); } - -- cgit v1.2.3-ZIG