aboutsummaryrefslogtreecommitdiff
path: root/src/conn.zig
blob: 870ab012e94fde01e150f0776481d27832621819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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,
};