aboutsummaryrefslogtreecommitdiff
path: root/src/proto/proto.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-29 09:44:54 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-29 09:44:54 +0100
commitfada72cd26ad31e1fc834788c1224ed05a78143b (patch)
treedb71f2cbc6cbf9c47148271682641c940eb75650 /src/proto/proto.zig
parent6de632a41bdd127e92de68d61a18dfee91b8b188 (diff)
downloadpgz-fada72cd26ad31e1fc834788c1224ed05a78143b.tar.gz
pgz-fada72cd26ad31e1fc834788c1224ed05a78143b.tar.bz2
pgz-fada72cd26ad31e1fc834788c1224ed05a78143b.tar.xz
pgz-fada72cd26ad31e1fc834788c1224ed05a78143b.zip
Generify ErrorResponse to allow for NoticeResponse which shares it'sHEADmain
structure. Add a very basic test for running an actual query
Diffstat (limited to 'src/proto/proto.zig')
-rw-r--r--src/proto/proto.zig23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/proto/proto.zig b/src/proto/proto.zig
index 9025347..261ed9e 100644
--- a/src/proto/proto.zig
+++ b/src/proto/proto.zig
@@ -1,8 +1,11 @@
const std = @import("std");
+const ByteArrayList = std.ArrayList(u8);
+const log = std.log.scoped(.pgz);
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 ErrorResponse = @import("error_response.zig").ErrorNoticeResponse('E');
+pub const NoticeResponse = @import("error_response.zig").ErrorNoticeResponse('N');
pub const ReadyForQuery = @import("ready_for_query.zig");
pub const ParameterStatus = @import("parameter_status.zig");
pub const BackendKeyData = @import("backend_key_data.zig");
@@ -20,6 +23,7 @@ const ProtocolError = @import("../main.zig").ProtocolError;
pub const BackendMessage = union(enum) {
AuthenticationRequest: AuthenticationRequest,
ErrorResponse: ErrorResponse,
+ NoticeResponse: NoticeResponse,
ReadyForQuery: ReadyForQuery,
ParameterStatus: ParameterStatus,
BackendKeyData: BackendKeyData,
@@ -58,10 +62,27 @@ pub fn read_message(allocator: std.mem.Allocator, stream_reader: anytype) !Backe
}
} else {
allocator.free(buf);
+ log.err("InvalidMessageType {c}", .{tag});
return ProtocolError.InvalidMessageType;
}
}
+// Caller owns the resulting message.
+// 'self' must be one of the message types above.
+pub fn clone_message(self: anytype, a: std.mem.Allocator) !@TypeOf(self) {
+ var ba = ByteArrayList.init(a);
+ defer ba.deinit();
+ try self.write(a, ba.writer());
+ var fbs = std.io.fixedBufferStream(ba.items);
+ var reader = fbs.reader();
+ _ = try reader.readByte();
+ const len = try reader.readIntBig(u32);
+ var buf = try a.alloc(u8, len-4);
+ errdefer a.free(buf);
+ try reader.readNoEof(buf);
+ return try @TypeOf(self).read(a, buf);
+}
+
test {
_ = AuthenticationRequest;
_ = PasswordMessage;