aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-26 06:51:06 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-26 06:51:06 +0100
commit183d60a6e87230cc767c56900b94c9c694596de1 (patch)
treec08b473a293dc465989a09c5d681191898cf2c2f
parent02f9e99bfccad8837d327880f756ec7bab711783 (diff)
downloadpgz-183d60a6e87230cc767c56900b94c9c694596de1.tar.gz
pgz-183d60a6e87230cc767c56900b94c9c694596de1.tar.bz2
pgz-183d60a6e87230cc767c56900b94c9c694596de1.tar.xz
pgz-183d60a6e87230cc767c56900b94c9c694596de1.zip
Move protocol definitions into a subfolder
-rw-r--r--src/conn.zig38
-rw-r--r--src/main.zig39
-rw-r--r--src/proto/authentication_request.zig (renamed from src/authentication_request.zig)16
-rw-r--r--src/proto/backend_key_data.zig (renamed from src/backend_key_data.zig)4
-rw-r--r--src/proto/command_complete.zig (renamed from src/command_complete.zig)8
-rw-r--r--src/proto/copy_in_response.zig (renamed from src/copy_in_response.zig)8
-rw-r--r--src/proto/data_row.zig (renamed from src/data_row.zig)6
-rw-r--r--src/proto/error_response.zig (renamed from src/error_response.zig)2
-rw-r--r--src/proto/parameter_status.zig (renamed from src/parameter_status.zig)4
-rw-r--r--src/proto/password_message.zig (renamed from src/password_message.zig)0
-rw-r--r--src/proto/proto.zig26
-rw-r--r--src/proto/query.zig (renamed from src/query.zig)6
-rw-r--r--src/proto/ready_for_query.zig (renamed from src/ready_for_query.zig)8
-rw-r--r--src/proto/row_description.zig (renamed from src/row_description.zig)8
-rw-r--r--src/proto/startup_message.zig (renamed from src/startup_message.zig)2
15 files changed, 85 insertions, 90 deletions
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();
}
-
diff --git a/src/main.zig b/src/main.zig
index 8bc4649..f5941b7 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,18 +1,7 @@
const std = @import("std");
const testing = std.testing;
-const StartupMessage = @import("startup_message.zig");
-const AuthenticationRequest = @import("authentication_request.zig");
-const PasswordMessage = @import("password_message.zig");
-const ErrorResponse = @import("error_response.zig");
-const ReadyForQuery = @import("ready_for_query.zig");
-const ParameterStatus = @import("parameter_status.zig");
-const BackendKeyData = @import("backend_key_data.zig");
-const Query = @import("query.zig");
-const DataRow = @import("data_row.zig");
-const RowDescription = @import("row_description.zig");
-const CommandComplete = @import("command_complete.zig");
-const CopyInResponse = @import("copy_in_response.zig");
const Conn = @import("conn.zig");
+const Proto = @import("proto/proto.zig");
pub const ProtocolError = error{
InvalidProtocolVersion,
@@ -58,22 +47,21 @@ pub fn read_message(comptime msg_type: type, allocator: std.mem.Allocator, strea
if (!@hasDecl(msg_type, "Tag")) @compileError("msg_type must have a Tag declaration!");
if (!@hasDecl(msg_type, "read")) @compileError("msg_type must have a read() function!");
const len = try stream_reader.readIntBig(u32);
- const buf = try allocator.alloc(u8, @as(u32, @intCast(len-4)));
+ const buf = try allocator.alloc(u8, @as(u32, @intCast(len - 4)));
defer allocator.free(buf);
try stream_reader.readNoEof(buf);
return try msg_type.read(allocator, buf);
}
-
pub fn diagnosticReader(comptime n: usize, base_reader: anytype) DiagnosticReader(n, @TypeOf(base_reader)) {
- return .{.child_reader = base_reader};
+ return .{ .child_reader = base_reader };
}
// keeps a buffer of the last n bytes read
pub fn DiagnosticReader(comptime n: usize, comptime ReaderType: anytype) type {
return struct {
child_reader: ReaderType,
- ring: [n]u8 = [_]u8{0}**n,
+ ring: [n]u8 = [_]u8{0} ** n,
pos: usize = 0,
pub const Error = ReaderType.Error;
@@ -97,8 +85,8 @@ pub fn DiagnosticReader(comptime n: usize, comptime ReaderType: anytype) type {
pub fn get(self: @This(), allocator: std.mem.Allocator) ![]const u8 {
var buf = try allocator.alloc(u8, n);
errdefer allocator.free(buf);
- @memcpy(buf[0..(n-self.pos)], self.ring[self.pos..n]);
- @memcpy(buf[(n-self.pos)..n], self.ring[0..self.pos]);
+ @memcpy(buf[0..(n - self.pos)], self.ring[self.pos..n]);
+ @memcpy(buf[(n - self.pos)..n], self.ring[0..self.pos]);
return buf;
}
};
@@ -110,7 +98,7 @@ test "diagnostc reader" {
var fbs = std.io.fixedBufferStream(string);
var dr = diagnosticReader(15, fbs.reader());
var reader = dr.reader();
- var buf = [_]u8{0}**20;
+ var buf = [_]u8{0} ** 20;
try reader.readNoEof(&buf);
const diag = try dr.get(a);
defer a.free(diag);
@@ -118,17 +106,6 @@ test "diagnostc reader" {
}
test {
- _ = StartupMessage;
- _ = AuthenticationRequest;
- _ = PasswordMessage;
- _ = ErrorResponse;
+ _ = Proto;
_ = Conn;
- _ = ReadyForQuery;
- _ = ParameterStatus;
- _ = BackendKeyData;
- _ = Query;
- _ = DataRow;
- _ = RowDescription;
- _ = CommandComplete;
- _ = CopyInResponse;
}
diff --git a/src/authentication_request.zig b/src/proto/authentication_request.zig
index 549a26b..9203482 100644
--- a/src/authentication_request.zig
+++ b/src/proto/authentication_request.zig
@@ -1,9 +1,9 @@
const std = @import("std");
const log = std.log.scoped(.pgz);
const ByteArrayList = std.ArrayList(u8);
-const ProtocolError = @import("main.zig").ProtocolError;
-const ClientError = @import("main.zig").ClientError;
-const enum_from_int = @import("main.zig").enum_from_int;
+const ProtocolError = @import("../main.zig").ProtocolError;
+const ClientError = @import("../main.zig").ClientError;
+const enum_from_int = @import("../main.zig").enum_from_int;
pub const Tag: u8 = 'R';
@@ -13,7 +13,7 @@ pub const InnerAuthRequestType = enum(u32) {
AuthRequestTypeOk = 0,
AuthRequestTypeCleartextPassword = 3,
};
-pub const InnerAuthRequest = union{
+pub const InnerAuthRequest = union {
ok: AuthRequestOk,
cleartext_password: AuthRequestCleartextPassword,
};
@@ -27,13 +27,13 @@ inner: InnerAuthRequest,
pub fn read(_: std.mem.Allocator, b: []const u8) !AuthenticationRequest {
if (b.len != 4) {
- log.err("invalid message length, expected 4 got {}",.{b.len});
+ log.err("invalid message length, expected 4 got {}", .{b.len});
return ProtocolError.InvalidMessageLength;
}
const inner_type = enum_from_int(InnerAuthRequestType, std.mem.readIntBig(u32, b[0..4])) orelse return ClientError.UnsupportedAuthType;
var inner: InnerAuthRequest = switch (inner_type) {
- .AuthRequestTypeOk => .{.ok = AuthRequestOk{}},
- .AuthRequestTypeCleartextPassword => .{.cleartext_password = AuthRequestCleartextPassword{}},
+ .AuthRequestTypeOk => .{ .ok = AuthRequestOk{} },
+ .AuthRequestTypeCleartextPassword => .{ .cleartext_password = AuthRequestCleartextPassword{} },
};
return .{
.inner_type = inner_type,
@@ -53,7 +53,7 @@ test "round trip" {
const allocator = std.testing.allocator;
var sm = AuthenticationRequest{
.inner_type = .AuthRequestTypeOk,
- .inner = .{.ok = AuthRequestOk{}},
+ .inner = .{ .ok = AuthRequestOk{} },
};
defer sm.deinit(allocator);
diff --git a/src/backend_key_data.zig b/src/proto/backend_key_data.zig
index 525c309..7c32178 100644
--- a/src/backend_key_data.zig
+++ b/src/proto/backend_key_data.zig
@@ -1,8 +1,8 @@
const std = @import("std");
const log = std.log.scoped(.pgz);
const ByteArrayList = std.ArrayList(u8);
-const ProtocolError = @import("main.zig").ProtocolError;
-const ClientError = @import("main.zig").ClientError;
+const ProtocolError = @import("../main.zig").ProtocolError;
+const ClientError = @import("../main.zig").ClientError;
const BackendKeyData = @This();
pub const Tag: u8 = 'K';
diff --git a/src/command_complete.zig b/src/proto/command_complete.zig
index 5478547..80014e9 100644
--- a/src/command_complete.zig
+++ b/src/proto/command_complete.zig
@@ -1,10 +1,10 @@
const std = @import("std");
const log = std.log.scoped(.pgz);
const ByteArrayList = std.ArrayList(u8);
-const ProtocolError = @import("main.zig").ProtocolError;
-const ClientError = @import("main.zig").ClientError;
-const enum_from_int = @import("main.zig").enum_from_int;
-const FormatCode = @import("main.zig").FormatCode;
+const ProtocolError = @import("../main.zig").ProtocolError;
+const ClientError = @import("../main.zig").ClientError;
+const enum_from_int = @import("../main.zig").enum_from_int;
+const FormatCode = @import("../main.zig").FormatCode;
pub const Tag: u8 = 'C';
diff --git a/src/copy_in_response.zig b/src/proto/copy_in_response.zig
index f6ed84b..65d9ad7 100644
--- a/src/copy_in_response.zig
+++ b/src/proto/copy_in_response.zig
@@ -1,10 +1,10 @@
const std = @import("std");
const log = std.log.scoped(.pgz);
const ByteArrayList = std.ArrayList(u8);
-const ProtocolError = @import("main.zig").ProtocolError;
-const ClientError = @import("main.zig").ClientError;
-const enum_from_int = @import("main.zig").enum_from_int;
-const FormatCode = @import("main.zig").FormatCode;
+const ProtocolError = @import("../main.zig").ProtocolError;
+const ClientError = @import("../main.zig").ClientError;
+const enum_from_int = @import("../main.zig").enum_from_int;
+const FormatCode = @import("../main.zig").FormatCode;
pub const Tag: u8 = 'G';
// TODO generics it's the same as CopyOutResponse and CopyBothResponse.
diff --git a/src/data_row.zig b/src/proto/data_row.zig
index 558ebca..c20b794 100644
--- a/src/data_row.zig
+++ b/src/proto/data_row.zig
@@ -1,9 +1,9 @@
const std = @import("std");
const log = std.log.scoped(.pgz);
const ByteArrayList = std.ArrayList(u8);
-const ProtocolError = @import("main.zig").ProtocolError;
-const ClientError = @import("main.zig").ClientError;
-const enum_from_int = @import("main.zig").enum_from_int;
+const ProtocolError = @import("../main.zig").ProtocolError;
+const ClientError = @import("../main.zig").ClientError;
+const enum_from_int = @import("../main.zig").enum_from_int;
pub const Tag: u8 = 'D';
diff --git a/src/error_response.zig b/src/proto/error_response.zig
index 3b66c15..2aafe8d 100644
--- a/src/error_response.zig
+++ b/src/proto/error_response.zig
@@ -1,7 +1,7 @@
const std = @import("std");
const HMByteString = std.AutoHashMap(u8, []const u8);
const ByteArrayList = std.ArrayList(u8);
-const ProtocolError = @import("main.zig").ProtocolError;
+const ProtocolError = @import("../main.zig").ProtocolError;
const ErrorResponse = @This();
pub const Tag: u8 = 'E';
diff --git a/src/parameter_status.zig b/src/proto/parameter_status.zig
index d938fd2..5f95695 100644
--- a/src/parameter_status.zig
+++ b/src/proto/parameter_status.zig
@@ -1,8 +1,8 @@
const std = @import("std");
const log = std.log.scoped(.pgz);
const ByteArrayList = std.ArrayList(u8);
-const ProtocolError = @import("main.zig").ProtocolError;
-const ClientError = @import("main.zig").ClientError;
+const ProtocolError = @import("../main.zig").ProtocolError;
+const ClientError = @import("../main.zig").ClientError;
const ParameterStatus = @This();
pub const Tag: u8 = 'S';
diff --git a/src/password_message.zig b/src/proto/password_message.zig
index 1a8c17a..1a8c17a 100644
--- a/src/password_message.zig
+++ b/src/proto/password_message.zig
diff --git a/src/proto/proto.zig b/src/proto/proto.zig
new file mode 100644
index 0000000..4465e4b
--- /dev/null
+++ b/src/proto/proto.zig
@@ -0,0 +1,26 @@
+pub const StartupMessage = @import("startup_message.zig");
+pub const AuthenticationRequest = @import("authentication_request.zig");
+pub const PasswordMessage = @import("password_message.zig");
+pub const ErrorResponse = @import("error_response.zig");
+pub const ReadyForQuery = @import("ready_for_query.zig");
+pub const ParameterStatus = @import("parameter_status.zig");
+pub const BackendKeyData = @import("backend_key_data.zig");
+pub const Query = @import("query.zig");
+pub const DataRow = @import("data_row.zig");
+pub const RowDescription = @import("row_description.zig");
+pub const CommandComplete = @import("command_complete.zig");
+pub const CopyInResponse = @import("copy_in_response.zig");
+
+test {
+ _ = AuthenticationRequest;
+ _ = PasswordMessage;
+ _ = ErrorResponse;
+ _ = ReadyForQuery;
+ _ = ParameterStatus;
+ _ = BackendKeyData;
+ _ = Query;
+ _ = DataRow;
+ _ = RowDescription;
+ _ = CommandComplete;
+ _ = CopyInResponse;
+}
diff --git a/src/query.zig b/src/proto/query.zig
index 8f07b5f..9f238fc 100644
--- a/src/query.zig
+++ b/src/proto/query.zig
@@ -1,9 +1,9 @@
const std = @import("std");
const log = std.log.scoped(.pgz);
const ByteArrayList = std.ArrayList(u8);
-const ProtocolError = @import("main.zig").ProtocolError;
-const ClientError = @import("main.zig").ClientError;
-const enum_from_int = @import("main.zig").enum_from_int;
+const ProtocolError = @import("../main.zig").ProtocolError;
+const ClientError = @import("../main.zig").ClientError;
+const enum_from_int = @import("../main.zig").enum_from_int;
pub const Tag: u8 = 'Q';
diff --git a/src/ready_for_query.zig b/src/proto/ready_for_query.zig
index 883d9e1..ef99e60 100644
--- a/src/ready_for_query.zig
+++ b/src/proto/ready_for_query.zig
@@ -1,6 +1,6 @@
const std = @import("std");
-const ProtocolError = @import("main.zig").ProtocolError;
-const enum_from_int = @import("main.zig").enum_from_int;
+const ProtocolError = @import("../main.zig").ProtocolError;
+const enum_from_int = @import("../main.zig").enum_from_int;
const ByteArrayList = std.ArrayList(u8);
const ReadyForQuery = @This();
@@ -17,9 +17,7 @@ transaction_status: TransactionStatus,
pub fn read(allocator: std.mem.Allocator, b: []const u8) !ReadyForQuery {
_ = allocator;
if (b.len != 1) return ProtocolError.InvalidMessageLength;
- return .{
- .transaction_status = enum_from_int(TransactionStatus,b[0]) orelse return ProtocolError.InvalidTransactionStatus
- };
+ return .{ .transaction_status = enum_from_int(TransactionStatus, b[0]) orelse return ProtocolError.InvalidTransactionStatus };
}
pub fn write(self: ReadyForQuery, allocator: std.mem.Allocator, stream_writer: anytype) !void {
_ = allocator;
diff --git a/src/row_description.zig b/src/proto/row_description.zig
index 414c174..a0e8810 100644
--- a/src/row_description.zig
+++ b/src/proto/row_description.zig
@@ -1,10 +1,10 @@
const std = @import("std");
const log = std.log.scoped(.pgz);
const ByteArrayList = std.ArrayList(u8);
-const ProtocolError = @import("main.zig").ProtocolError;
-const ClientError = @import("main.zig").ClientError;
-const enum_from_int = @import("main.zig").enum_from_int;
-const FormatCode = @import("main.zig").FormatCode;
+const ProtocolError = @import("../main.zig").ProtocolError;
+const ClientError = @import("../main.zig").ClientError;
+const enum_from_int = @import("../main.zig").enum_from_int;
+const FormatCode = @import("../main.zig").FormatCode;
pub const Tag: u8 = 'T';
diff --git a/src/startup_message.zig b/src/proto/startup_message.zig
index b5031c1..8224bdb 100644
--- a/src/startup_message.zig
+++ b/src/proto/startup_message.zig
@@ -1,5 +1,5 @@
const std = @import("std");
-const ProtocolError = @import("main.zig").ProtocolError;
+const ProtocolError = @import("../main.zig").ProtocolError;
const SSHashMap = std.StringHashMap([]const u8);
const ByteArrayList = std.ArrayList(u8);