aboutsummaryrefslogtreecommitdiff
path: root/src/conn.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-23 15:18:38 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-23 15:18:38 +0100
commit5a91b37ee7dd36db52dfde1727b780ec3fa4c67d (patch)
treed17a1a8449ecd48fcf67f93edf7ba16c99974e6a /src/conn.zig
parentc3c45287396569a96107c9b1413ac181f7197a6e (diff)
downloadpgz-5a91b37ee7dd36db52dfde1727b780ec3fa4c67d.tar.gz
pgz-5a91b37ee7dd36db52dfde1727b780ec3fa4c67d.tar.bz2
pgz-5a91b37ee7dd36db52dfde1727b780ec3fa4c67d.tar.xz
pgz-5a91b37ee7dd36db52dfde1727b780ec3fa4c67d.zip
Add error_response
Start adding connection abstraction
Diffstat (limited to 'src/conn.zig')
-rw-r--r--src/conn.zig49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/conn.zig b/src/conn.zig
new file mode 100644
index 0000000..870ab01
--- /dev/null
+++ b/src/conn.zig
@@ -0,0 +1,49 @@
+const std = @import("std");
+const SSHashMap = std.StringHashMap([]const u8);
+const Config = @import("config.zig");
+const StartupMessage = @import("startup_message.zig");
+const AuthenticationOk = @import("authentication_ok.zig");
+const AuthenticationCleartextPassword = @import("authentication_cleartext_password.zig");
+const ErrorResponse = @import("error_response.zig");
+
+const Conn = @This();
+
+const ConnStatus = enum {
+ connStatusUninitialized,
+ connStatusConnecting,
+ connStatusClosed,
+ connStatusIdle,
+ connStatusBusy,
+};
+
+stream: std.net.Stream,
+config: Config,
+status: ConnStatus,
+
+pub fn connect(config: Config) !Conn {
+ const allocator = config.allocator;
+ var stream = switch (config.address) {
+ .net => |addr| try std.net.tcpConnectToAddress(addr),
+ .unix => |path| try std.net.connectUnixSocket(path),
+ };
+ var writer = stream.writer();
+
+ errdefer stream.close();
+ var params = SSHashMap.init(allocator);
+ errdefer params.deinit();
+ try params.put("user", config.user);
+ if (config.database) |database| try params.put(database);
+ var sm = StartupMessage{
+ .parameters = params,
+ };
+ defer sm.deinit(allocator);
+ try sm.write(allocator, writer);
+}
+
+const StartupMessageResponseType = enum(u8) {
+ ErrorResponse = 'E',
+ AuthenticationResponse = AuthenticationOk.Tag, // All the authentication responses share a message type and must be decoded by the next field
+};
+const StartupMessageResponse = union(StartupMessageResponseType) {
+ error: ErrorResponse,
+}; \ No newline at end of file