aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/proto/copy_in_response.zig83
-rw-r--r--src/proto/copy_x_response.zig87
-rw-r--r--src/proto/proto.zig7
3 files changed, 92 insertions, 85 deletions
diff --git a/src/proto/copy_in_response.zig b/src/proto/copy_in_response.zig
deleted file mode 100644
index 65d9ad7..0000000
--- a/src/proto/copy_in_response.zig
+++ /dev/null
@@ -1,83 +0,0 @@
-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;
-
-pub const Tag: u8 = 'G';
-// TODO generics it's the same as CopyOutResponse and CopyBothResponse.
-const CopyInResponse = @This();
-
-overall_format_code: u8,
-format_codes: []const FormatCode, // owned
-
-pub fn read(a: std.mem.Allocator, b: []const u8) !CopyInResponse {
- var fbs = std.io.fixedBufferStream(b);
- var reader = fbs.reader();
- const overall_format_code = try reader.readIntBig(u8);
- const n_columns = try reader.readIntBig(u16);
- var format_codes = try a.alloc(FormatCode, n_columns);
- errdefer a.free(format_codes);
- for (0..n_columns) |i| {
- const int_format_code = try reader.readIntBig(u16);
- format_codes[i] = enum_from_int(FormatCode, int_format_code) orelse return ProtocolError.InvalidFormatCode;
- }
- return .{
- .overall_format_code = overall_format_code,
- .format_codes = format_codes,
- };
-}
-
-pub fn write(self: CopyInResponse, a: std.mem.Allocator, stream_writer: anytype) !void {
- try stream_writer.writeIntBig(u8, Tag);
- var al = ByteArrayList.init(a);
- defer al.deinit();
- var cw = std.io.countingWriter(al.writer());
- var writer = cw.writer();
- try writer.writeIntBig(u32, 0); // length placeholder
- try writer.writeIntBig(u8, self.overall_format_code);
- try writer.writeIntBig(u16, @intCast(self.format_codes.len));
- for (self.format_codes) |format_code| {
- try writer.writeIntBig(u16, @intFromEnum(format_code));
- }
- std.mem.writeIntBig(u32, al.items[0..4], @as(u32, @intCast(cw.bytes_written))); // Update length
- try stream_writer.writeAll(al.items);
-}
-
-pub fn deinit(self: *CopyInResponse, a: std.mem.Allocator) void {
- a.free(self.format_codes);
-}
-
-test "round trip" {
- const allocator = std.testing.allocator;
- var format_codes = try allocator.alloc(FormatCode, 3);
- format_codes[0] = .Binary;
- format_codes[1] = .Binary;
- format_codes[2] = .Text;
- var sm = CopyInResponse{
- .overall_format_code = 1,
- .format_codes = format_codes,
- };
- defer sm.deinit(allocator);
-
- var bal = ByteArrayList.init(allocator);
- defer bal.deinit();
- try sm.write(allocator, bal.writer());
-
- var fbs = std.io.fixedBufferStream(bal.items);
- var reader = fbs.reader();
- const tag = try reader.readByte();
- try std.testing.expectEqual(Tag, tag);
- const len = try reader.readIntBig(u32);
- const buf = try allocator.alloc(u8, len - 4);
- defer allocator.free(buf);
- try reader.readNoEof(buf);
- var sm2 = try CopyInResponse.read(allocator, buf);
- defer sm2.deinit(allocator);
-
- try std.testing.expectEqual(FormatCode.Binary, sm2.format_codes[0]);
- try std.testing.expectEqual(FormatCode.Binary, sm2.format_codes[1]);
- try std.testing.expectEqual(FormatCode.Text, sm2.format_codes[2]);
-}
diff --git a/src/proto/copy_x_response.zig b/src/proto/copy_x_response.zig
new file mode 100644
index 0000000..9d1b26c
--- /dev/null
+++ b/src/proto/copy_x_response.zig
@@ -0,0 +1,87 @@
+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;
+
+pub fn CopyXResponse(comptime tag: u8) type {
+ return struct {
+ pub const Tag: u8 = tag;
+
+ overall_format_code: u8,
+ format_codes: []const FormatCode, // owned
+
+ pub fn read(a: std.mem.Allocator, b: []const u8) !@This() {
+ var fbs = std.io.fixedBufferStream(b);
+ var reader = fbs.reader();
+ const overall_format_code = try reader.readIntBig(u8);
+ const n_columns = try reader.readIntBig(u16);
+ var format_codes = try a.alloc(FormatCode, n_columns);
+ errdefer a.free(format_codes);
+ for (0..n_columns) |i| {
+ const int_format_code = try reader.readIntBig(u16);
+ format_codes[i] = enum_from_int(FormatCode, int_format_code) orelse return ProtocolError.InvalidFormatCode;
+ }
+ return .{
+ .overall_format_code = overall_format_code,
+ .format_codes = format_codes,
+ };
+ }
+
+ pub fn write(self: @This(), a: std.mem.Allocator, stream_writer: anytype) !void {
+ try stream_writer.writeIntBig(u8, Tag);
+ var al = ByteArrayList.init(a);
+ defer al.deinit();
+ var cw = std.io.countingWriter(al.writer());
+ var writer = cw.writer();
+ try writer.writeIntBig(u32, 0); // length placeholder
+ try writer.writeIntBig(u8, self.overall_format_code);
+ try writer.writeIntBig(u16, @intCast(self.format_codes.len));
+ for (self.format_codes) |format_code| {
+ try writer.writeIntBig(u16, @intFromEnum(format_code));
+ }
+ std.mem.writeIntBig(u32, al.items[0..4], @as(u32, @intCast(cw.bytes_written))); // Update length
+ try stream_writer.writeAll(al.items);
+ }
+
+ pub fn deinit(self: *@This(), a: std.mem.Allocator) void {
+ a.free(self.format_codes);
+ }
+ };
+}
+
+test "round trip" {
+ const Tag: u8 = 'G';
+ const CopyInResponse = CopyXResponse(Tag);
+ const allocator = std.testing.allocator;
+ var format_codes = try allocator.alloc(FormatCode, 3);
+ format_codes[0] = .Binary;
+ format_codes[1] = .Binary;
+ format_codes[2] = .Text;
+ var sm = CopyInResponse{
+ .overall_format_code = 1,
+ .format_codes = format_codes,
+ };
+ defer sm.deinit(allocator);
+
+ var bal = ByteArrayList.init(allocator);
+ defer bal.deinit();
+ try sm.write(allocator, bal.writer());
+
+ var fbs = std.io.fixedBufferStream(bal.items);
+ var reader = fbs.reader();
+ const tag = try reader.readByte();
+ try std.testing.expectEqual(Tag, tag);
+ const len = try reader.readIntBig(u32);
+ const buf = try allocator.alloc(u8, len - 4);
+ defer allocator.free(buf);
+ try reader.readNoEof(buf);
+ var sm2 = try CopyInResponse.read(allocator, buf);
+ defer sm2.deinit(allocator);
+
+ try std.testing.expectEqual(FormatCode.Binary, sm2.format_codes[0]);
+ try std.testing.expectEqual(FormatCode.Binary, sm2.format_codes[1]);
+ try std.testing.expectEqual(FormatCode.Text, sm2.format_codes[2]);
+}
diff --git a/src/proto/proto.zig b/src/proto/proto.zig
index 4465e4b..df1b717 100644
--- a/src/proto/proto.zig
+++ b/src/proto/proto.zig
@@ -9,7 +9,10 @@ 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");
+const CopyXResponse = @import("copy_x_response.zig").CopyXResponse;
+pub const CopyInResponse = CopyXResponse('G');
+pub const CopyOutResponse = CopyXResponse('H');
+pub const CopyBothResponse = CopyXResponse('W');
test {
_ = AuthenticationRequest;
@@ -22,5 +25,5 @@ test {
_ = DataRow;
_ = RowDescription;
_ = CommandComplete;
- _ = CopyInResponse;
+ _ = CopyXResponse;
}