Native PostgreSQL driver / client for Zig
0

Configure Feed

Select the types of activity you want to include in your feed.

Remove usingnamespace

https://github.com/karlseguin/pg.zig/issues/62

Karl Seguin (Mar 8, 2025, 8:51 AM +0800) d4798724 c4c85119

+684 -688
+684 -688
src/types.zig
··· 25 25 // when decoding an individual array value. Once we know the array type, we don't 26 26 // need to assert the oid of each individual value. 27 27 28 - pub const Types = struct { 29 - // Every supported type is here. This includes the format we want to 30 - // encode/decode (text or binary), and the logic for encoding and decoding. 28 + // Every supported type is here. This includes the format we want to 29 + // encode/decode (text or binary), and the logic for encoding and decoding. 31 30 32 - pub usingnamespace @import("types/cidr.zig"); 33 - pub usingnamespace @import("types/numeric.zig"); 31 + pub const Cidr = @import("types/cidr.zig").Cidr; 32 + pub const Numeric = @import("types/numeric.zig").Numeric; 34 33 35 - pub const Char = struct { 36 - // A blank-padded char 37 - pub const oid = OID.make(1042); 38 - const encoding = &binary_encoding; 34 + pub const Char = struct { 35 + // A blank-padded char 36 + pub const oid = OID.make(1042); 37 + const encoding = &binary_encoding; 39 38 40 - fn encode(value: u8, buf: *buffer.Buffer, format_pos: usize) !void { 41 - buf.writeAt(Char.encoding, format_pos); 42 - try buf.write(&.{ 0, 0, 0, 1 }); // length of our data 43 - return buf.writeByte(value); 44 - } 39 + fn encode(value: u8, buf: *buffer.Buffer, format_pos: usize) !void { 40 + buf.writeAt(Char.encoding, format_pos); 41 + try buf.write(&.{ 0, 0, 0, 1 }); // length of our data 42 + return buf.writeByte(value); 43 + } 45 44 46 - pub fn decode(data: []const u8, data_oid: i32) u8 { 47 - lib.assertDecodeType(u8, &.{Char.oid.decimal}, data_oid); 48 - return data[0]; 49 - } 45 + pub fn decode(data: []const u8, data_oid: i32) u8 { 46 + lib.assertDecodeType(u8, &.{Char.oid.decimal}, data_oid); 47 + return data[0]; 48 + } 50 49 51 - pub fn decodeKnown(data: []const u8) u8 { 52 - return data[0]; 53 - } 54 - }; 50 + pub fn decodeKnown(data: []const u8) u8 { 51 + return data[0]; 52 + } 53 + }; 55 54 56 - pub const Int16 = struct { 57 - pub const oid = OID.make(21); 58 - const encoding = &binary_encoding; 55 + pub const Int16 = struct { 56 + pub const oid = OID.make(21); 57 + const encoding = &binary_encoding; 59 58 60 - fn encode(value: i16, buf: *buffer.Buffer, format_pos: usize) !void { 61 - buf.writeAt(Int16.encoding, format_pos); 62 - try buf.write(&.{ 0, 0, 0, 2 }); // length of our data 63 - return buf.writeIntBig(i16, value); 64 - } 59 + fn encode(value: i16, buf: *buffer.Buffer, format_pos: usize) !void { 60 + buf.writeAt(Int16.encoding, format_pos); 61 + try buf.write(&.{ 0, 0, 0, 2 }); // length of our data 62 + return buf.writeIntBig(i16, value); 63 + } 65 64 66 - fn encodeUnsigned(value: u16, buf: *buffer.Buffer, format_pos: usize) !void { 67 - if (value > 32767) return error.UnsignedIntWouldBeTruncated; 68 - return Int16.encode(@intCast(value), buf, format_pos); 69 - } 65 + fn encodeUnsigned(value: u16, buf: *buffer.Buffer, format_pos: usize) !void { 66 + if (value > 32767) return error.UnsignedIntWouldBeTruncated; 67 + return Int16.encode(@intCast(value), buf, format_pos); 68 + } 70 69 71 - pub fn decode(data: []const u8, data_oid: i32) i16 { 72 - lib.assertDecodeType(i16, &.{Int16.oid.decimal}, data_oid); 73 - return Int16.decodeKnown(data); 74 - } 70 + pub fn decode(data: []const u8, data_oid: i32) i16 { 71 + lib.assertDecodeType(i16, &.{Int16.oid.decimal}, data_oid); 72 + return Int16.decodeKnown(data); 73 + } 75 74 76 - pub fn decodeKnown(data: []const u8) i16 { 77 - return std.mem.readInt(i16, data[0..2], .big); 78 - } 79 - }; 75 + pub fn decodeKnown(data: []const u8) i16 { 76 + return std.mem.readInt(i16, data[0..2], .big); 77 + } 78 + }; 80 79 81 - pub const Int32 = struct { 82 - pub const oid = OID.make(23); 83 - const encoding = &binary_encoding; 80 + pub const Int32 = struct { 81 + pub const oid = OID.make(23); 82 + const encoding = &binary_encoding; 84 83 85 - fn encode(value: i32, buf: *buffer.Buffer, format_pos: usize) !void { 86 - buf.writeAt(Int32.encoding, format_pos); 87 - try buf.write(&.{ 0, 0, 0, 4 }); // length of our data 88 - return buf.writeIntBig(i32, value); 89 - } 84 + fn encode(value: i32, buf: *buffer.Buffer, format_pos: usize) !void { 85 + buf.writeAt(Int32.encoding, format_pos); 86 + try buf.write(&.{ 0, 0, 0, 4 }); // length of our data 87 + return buf.writeIntBig(i32, value); 88 + } 90 89 91 - fn encodeUnsigned(value: u32, buf: *buffer.Buffer, format_pos: usize) !void { 92 - if (value > 2147483647) return error.UnsignedIntWouldBeTruncated; 93 - return Int32.encode(@intCast(value), buf, format_pos); 94 - } 90 + fn encodeUnsigned(value: u32, buf: *buffer.Buffer, format_pos: usize) !void { 91 + if (value > 2147483647) return error.UnsignedIntWouldBeTruncated; 92 + return Int32.encode(@intCast(value), buf, format_pos); 93 + } 95 94 96 - pub fn decode(data: []const u8, data_oid: i32) i32 { 97 - lib.assertDecodeType(i32, &.{Int32.oid.decimal}, data_oid); 98 - return Int32.decodeKnown(data); 99 - } 95 + pub fn decode(data: []const u8, data_oid: i32) i32 { 96 + lib.assertDecodeType(i32, &.{Int32.oid.decimal}, data_oid); 97 + return Int32.decodeKnown(data); 98 + } 100 99 101 - pub fn decodeKnown(data: []const u8) i32 { 102 - return std.mem.readInt(i32, data[0..4], .big); 103 - } 104 - }; 100 + pub fn decodeKnown(data: []const u8) i32 { 101 + return std.mem.readInt(i32, data[0..4], .big); 102 + } 103 + }; 105 104 106 - pub const Int64 = struct { 107 - pub const oid = OID.make(20); 108 - const encoding = &binary_encoding; 105 + pub const Int64 = struct { 106 + pub const oid = OID.make(20); 107 + const encoding = &binary_encoding; 109 108 110 - fn encode(value: i64, buf: *buffer.Buffer, format_pos: usize) !void { 111 - buf.writeAt(Int64.encoding, format_pos); 112 - try buf.write(&.{ 0, 0, 0, 8 }); // length of our data 113 - return buf.writeIntBig(i64, value); 114 - } 109 + fn encode(value: i64, buf: *buffer.Buffer, format_pos: usize) !void { 110 + buf.writeAt(Int64.encoding, format_pos); 111 + try buf.write(&.{ 0, 0, 0, 8 }); // length of our data 112 + return buf.writeIntBig(i64, value); 113 + } 115 114 116 - fn encodeUnsigned(value: u64, buf: *buffer.Buffer, format_pos: usize) !void { 117 - if (value > 9223372036854775807) return error.UnsignedIntWouldBeTruncated; 118 - return Int64.encode(@intCast(value), buf, format_pos); 119 - } 115 + fn encodeUnsigned(value: u64, buf: *buffer.Buffer, format_pos: usize) !void { 116 + if (value > 9223372036854775807) return error.UnsignedIntWouldBeTruncated; 117 + return Int64.encode(@intCast(value), buf, format_pos); 118 + } 120 119 121 - pub fn decode(data: []const u8, data_oid: i32) i64 { 122 - switch (data_oid) { 123 - Timestamp.oid.decimal, TimestampTz.oid.decimal => return Timestamp.decode(data, data_oid), 124 - else => { 125 - lib.assertDecodeType(i64, &.{Int64.oid.decimal}, data_oid); 126 - return Int64.decodeKnown(data); 127 - }, 128 - } 120 + pub fn decode(data: []const u8, data_oid: i32) i64 { 121 + switch (data_oid) { 122 + Timestamp.oid.decimal, TimestampTz.oid.decimal => return Timestamp.decode(data, data_oid), 123 + else => { 124 + lib.assertDecodeType(i64, &.{Int64.oid.decimal}, data_oid); 125 + return Int64.decodeKnown(data); 126 + }, 129 127 } 128 + } 130 129 131 - pub fn decodeKnown(data: []const u8) i64 { 132 - return std.mem.readInt(i64, data[0..8], .big); 133 - } 134 - }; 130 + pub fn decodeKnown(data: []const u8) i64 { 131 + return std.mem.readInt(i64, data[0..8], .big); 132 + } 133 + }; 135 134 136 - pub const Timestamp = struct { 137 - pub const oid = OID.make(1114); 138 - const encoding = &binary_encoding; 139 - const us_from_epoch_to_y2k = 946_684_800_000_000; 135 + pub const Timestamp = struct { 136 + pub const oid = OID.make(1114); 137 + const encoding = &binary_encoding; 138 + const us_from_epoch_to_y2k = 946_684_800_000_000; 140 139 141 - fn encode(value: i64, buf: *buffer.Buffer, format_pos: usize) !void { 142 - buf.writeAt(Timestamp.encoding, format_pos); 143 - try buf.write(&.{ 0, 0, 0, 8 }); // length of our data 144 - return buf.writeIntBig(i64, value - us_from_epoch_to_y2k); 145 - } 140 + fn encode(value: i64, buf: *buffer.Buffer, format_pos: usize) !void { 141 + buf.writeAt(Timestamp.encoding, format_pos); 142 + try buf.write(&.{ 0, 0, 0, 8 }); // length of our data 143 + return buf.writeIntBig(i64, value - us_from_epoch_to_y2k); 144 + } 146 145 147 - pub fn decode(data: []const u8, data_oid: i32) i64 { 148 - lib.assertDecodeType(i64, &.{ Timestamp.oid.decimal, TimestampTz.oid.decimal }, data_oid); 149 - return std.mem.readInt(i64, data[0..8], .big) + us_from_epoch_to_y2k; 150 - } 146 + pub fn decode(data: []const u8, data_oid: i32) i64 { 147 + lib.assertDecodeType(i64, &.{ Timestamp.oid.decimal, TimestampTz.oid.decimal }, data_oid); 148 + return std.mem.readInt(i64, data[0..8], .big) + us_from_epoch_to_y2k; 149 + } 151 150 152 - pub fn decodeKnown(data: []const u8) i64 { 153 - return std.mem.readInt(i64, data[0..8], .big) + us_from_epoch_to_y2k; 154 - } 155 - }; 151 + pub fn decodeKnown(data: []const u8) i64 { 152 + return std.mem.readInt(i64, data[0..8], .big) + us_from_epoch_to_y2k; 153 + } 154 + }; 156 155 157 - pub const TimestampTz = struct { 158 - pub const oid = OID.make(1184); 159 - const encoding = &binary_encoding; 160 - }; 156 + pub const TimestampTz = struct { 157 + pub const oid = OID.make(1184); 158 + const encoding = &binary_encoding; 159 + }; 161 160 162 - pub const Float32 = struct { 163 - pub const oid = OID.make(700); 164 - const encoding = &binary_encoding; 161 + pub const Float32 = struct { 162 + pub const oid = OID.make(700); 163 + const encoding = &binary_encoding; 165 164 166 - fn encode(value: f32, buf: *buffer.Buffer, format_pos: usize) !void { 167 - buf.writeAt(Float32.encoding, format_pos); 168 - try buf.write(&.{ 0, 0, 0, 4 }); // length of our data 169 - const tmp: *i32 = @constCast(@ptrCast(&value)); 170 - return buf.writeIntBig(i32, tmp.*); 171 - } 165 + fn encode(value: f32, buf: *buffer.Buffer, format_pos: usize) !void { 166 + buf.writeAt(Float32.encoding, format_pos); 167 + try buf.write(&.{ 0, 0, 0, 4 }); // length of our data 168 + const tmp: *i32 = @constCast(@ptrCast(&value)); 169 + return buf.writeIntBig(i32, tmp.*); 170 + } 172 171 173 - pub fn decode(data: []const u8, data_oid: i32) f32 { 174 - lib.assertDecodeType(f32, &.{Float32.oid.decimal}, data_oid); 175 - return Float32.decodeKnown(data); 176 - } 172 + pub fn decode(data: []const u8, data_oid: i32) f32 { 173 + lib.assertDecodeType(f32, &.{Float32.oid.decimal}, data_oid); 174 + return Float32.decodeKnown(data); 175 + } 177 176 178 - pub fn decodeKnown(data: []const u8) f32 { 179 - const n = std.mem.readInt(i32, data[0..4], .big); 180 - const tmp: *f32 = @constCast(@ptrCast(&n)); 181 - return tmp.*; 182 - } 183 - }; 177 + pub fn decodeKnown(data: []const u8) f32 { 178 + const n = std.mem.readInt(i32, data[0..4], .big); 179 + const tmp: *f32 = @constCast(@ptrCast(&n)); 180 + return tmp.*; 181 + } 182 + }; 184 183 185 - pub const Float64 = struct { 186 - pub const oid = OID.make(701); 187 - const encoding = &binary_encoding; 184 + pub const Float64 = struct { 185 + pub const oid = OID.make(701); 186 + const encoding = &binary_encoding; 188 187 189 - fn encode(value: f64, buf: *buffer.Buffer, format_pos: usize) !void { 190 - buf.writeAt(Float64.encoding, format_pos); 188 + fn encode(value: f64, buf: *buffer.Buffer, format_pos: usize) !void { 189 + buf.writeAt(Float64.encoding, format_pos); 191 190 192 - try buf.write(&.{ 0, 0, 0, 8 }); // length of our data 193 - // not sure if this is the best option... 194 - const tmp: *i64 = @constCast(@ptrCast(&value)); 195 - return buf.writeIntBig(i64, tmp.*); 196 - } 191 + try buf.write(&.{ 0, 0, 0, 8 }); // length of our data 192 + // not sure if this is the best option... 193 + const tmp: *i64 = @constCast(@ptrCast(&value)); 194 + return buf.writeIntBig(i64, tmp.*); 195 + } 197 196 198 - pub fn decode(data: []const u8, data_oid: i32) f64 { 199 - switch (data_oid) { 200 - Types.Numeric.oid.decimal => return Types.Numeric.decode(data, data_oid).toFloat(), 201 - else => { 202 - lib.assertDecodeType(f64, &.{Float64.oid.decimal}, data_oid); 203 - return Float64.decodeKnown(data); 204 - }, 205 - } 197 + pub fn decode(data: []const u8, data_oid: i32) f64 { 198 + switch (data_oid) { 199 + Numeric.oid.decimal => return Numeric.decode(data, data_oid).toFloat(), 200 + else => { 201 + lib.assertDecodeType(f64, &.{Float64.oid.decimal}, data_oid); 202 + return Float64.decodeKnown(data); 203 + }, 206 204 } 205 + } 207 206 208 - pub fn decodeKnown(data: []const u8) f64 { 209 - const n = std.mem.readInt(i64, data[0..8], .big); 210 - const tmp: *f64 = @constCast(@ptrCast(&n)); 211 - return tmp.*; 212 - } 213 - }; 207 + pub fn decodeKnown(data: []const u8) f64 { 208 + const n = std.mem.readInt(i64, data[0..8], .big); 209 + const tmp: *f64 = @constCast(@ptrCast(&n)); 210 + return tmp.*; 211 + } 212 + }; 214 213 215 - pub const Bool = struct { 216 - pub const oid = OID.make(16); 217 - const encoding = &binary_encoding; 214 + pub const Bool = struct { 215 + pub const oid = OID.make(16); 216 + const encoding = &binary_encoding; 218 217 219 - fn encode(value: bool, buf: *buffer.Buffer, format_pos: usize) !void { 220 - buf.writeAt(Bool.encoding, format_pos); 221 - try buf.write(&.{ 0, 0, 0, 1 }); // length of our data 222 - return buf.writeByte(if (value) 1 else 0); 223 - } 218 + fn encode(value: bool, buf: *buffer.Buffer, format_pos: usize) !void { 219 + buf.writeAt(Bool.encoding, format_pos); 220 + try buf.write(&.{ 0, 0, 0, 1 }); // length of our data 221 + return buf.writeByte(if (value) 1 else 0); 222 + } 224 223 225 - pub fn decode(data: []const u8, data_oid: i32) bool { 226 - lib.assertDecodeType(bool, &.{Bool.oid.decimal}, data_oid); 227 - return decodeKnown(data); 228 - } 224 + pub fn decode(data: []const u8, data_oid: i32) bool { 225 + lib.assertDecodeType(bool, &.{Bool.oid.decimal}, data_oid); 226 + return decodeKnown(data); 227 + } 229 228 230 - pub fn decodeKnown(data: []const u8) bool { 231 - return data[0] == 1; 232 - } 233 - }; 229 + pub fn decodeKnown(data: []const u8) bool { 230 + return data[0] == 1; 231 + } 232 + }; 234 233 235 - pub const String = struct { 236 - pub const oid = OID.make(25); 237 - // https://www.postgresql.org/message-id/CAMovtNoHFod2jMAKQjjxv209PCTJx5Kc66anwWvX0mEiaXwgmA%40mail.gmail.com 238 - // says using the text format for text-like things is faster. There was 239 - // some other threads that discussed solutions, but it isn't clear if it was 240 - // ever fixed. 241 - const encoding = &text_encoding; 234 + pub const String = struct { 235 + pub const oid = OID.make(25); 236 + // https://www.postgresql.org/message-id/CAMovtNoHFod2jMAKQjjxv209PCTJx5Kc66anwWvX0mEiaXwgmA%40mail.gmail.com 237 + // says using the text format for text-like things is faster. There was 238 + // some other threads that discussed solutions, but it isn't clear if it was 239 + // ever fixed. 240 + const encoding = &text_encoding; 242 241 243 - fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 244 - buf.writeAt(String.encoding, format_pos); 245 - var view = try buf.skip(4 + value.len); 246 - view.writeIntBig(i32, @intCast(value.len)); 247 - view.write(value); 248 - } 249 - }; 242 + fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 243 + buf.writeAt(String.encoding, format_pos); 244 + var view = try buf.skip(4 + value.len); 245 + view.writeIntBig(i32, @intCast(value.len)); 246 + view.write(value); 247 + } 248 + }; 250 249 251 - pub const Bytea = struct { 252 - pub const oid = OID.make(17); 253 - const encoding = &binary_encoding; 250 + pub const Bytea = struct { 251 + pub const oid = OID.make(17); 252 + const encoding = &binary_encoding; 254 253 255 - fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 256 - buf.writeAt(Bytea.encoding, format_pos); 257 - var view = try buf.skip(4 + value.len); 258 - view.writeIntBig(i32, @intCast(value.len)); 259 - view.write(value); 260 - } 254 + fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 255 + buf.writeAt(Bytea.encoding, format_pos); 256 + var view = try buf.skip(4 + value.len); 257 + view.writeIntBig(i32, @intCast(value.len)); 258 + view.write(value); 259 + } 261 260 262 - pub fn decode(data: []const u8, data_oid: i32) []const u8 { 263 - switch (data_oid) { 264 - JSONB.oid.decimal => return JSONB.decodeKnown(data), 265 - else => return data, 266 - } 261 + pub fn decode(data: []const u8, data_oid: i32) []const u8 { 262 + switch (data_oid) { 263 + JSONB.oid.decimal => return JSONB.decodeKnown(data), 264 + else => return data, 267 265 } 266 + } 268 267 269 - pub fn decodeKnown(data: []const u8) []const u8 { 270 - return data; 271 - } 268 + pub fn decodeKnown(data: []const u8) []const u8 { 269 + return data; 270 + } 272 271 273 - pub fn decodeKnownMutable(data: []const u8) []u8 { 274 - // we know the underlying []u8 is mutable, it comes from our Reader 275 - return @constCast(data); 276 - } 277 - }; 272 + pub fn decodeKnownMutable(data: []const u8) []u8 { 273 + // we know the underlying []u8 is mutable, it comes from our Reader 274 + return @constCast(data); 275 + } 276 + }; 278 277 279 - pub const UUID = struct { 280 - pub const oid = OID.make(2950); 281 - const encoding = &binary_encoding; 278 + pub const UUID = struct { 279 + pub const oid = OID.make(2950); 280 + const encoding = &binary_encoding; 282 281 283 - fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 284 - buf.writeAt(UUID.encoding, format_pos); 285 - var view = try buf.skip(20); 286 - view.write(&.{ 0, 0, 0, 16 }); 287 - switch (value.len) { 288 - 16 => view.write(value), 289 - 36 => view.write(&(try UUID.toBytes(value))), 290 - else => return error.InvalidUUID, 291 - } 282 + fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 283 + buf.writeAt(UUID.encoding, format_pos); 284 + var view = try buf.skip(20); 285 + view.write(&.{ 0, 0, 0, 16 }); 286 + switch (value.len) { 287 + 16 => view.write(value), 288 + 36 => view.write(&(try UUID.toBytes(value))), 289 + else => return error.InvalidUUID, 292 290 } 291 + } 293 292 294 - pub fn decode(data: []const u8, data_oid: i32) []const u8 { 295 - lib.assertDecodeType([]const u8, &.{UUID.oid.decimal}, data_oid); 296 - return data; 293 + pub fn decode(data: []const u8, data_oid: i32) []const u8 { 294 + lib.assertDecodeType([]const u8, &.{UUID.oid.decimal}, data_oid); 295 + return data; 296 + } 297 + 298 + const hex = "0123456789abcdef"; 299 + const encoded_pos = [16]u8{ 0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34 }; 300 + const hex_to_nibble = [256]u8{ 301 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 302 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 303 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 304 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 305 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 306 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 307 + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 308 + 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 309 + 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff, 310 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 311 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 312 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 313 + 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff, 314 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 315 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 316 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 317 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 318 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 319 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 320 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 321 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 322 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 323 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 324 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 325 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 326 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 327 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 328 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 329 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 330 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 331 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 332 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 333 + }; 334 + 335 + pub fn toString(uuid: []const u8) ![36]u8 { 336 + if (uuid.len != 16) { 337 + return error.InvalidUUID; 297 338 } 298 339 299 - const hex = "0123456789abcdef"; 300 - const encoded_pos = [16]u8{ 0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34 }; 301 - const hex_to_nibble = [256]u8{ 302 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 303 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 304 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 305 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 306 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 307 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 308 - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 309 - 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 310 - 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff, 311 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 312 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 313 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 314 - 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff, 315 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 316 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 317 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 318 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 319 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 320 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 321 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 322 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 323 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 324 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 325 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 326 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 327 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 328 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 329 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 330 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 331 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 332 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 333 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 334 - }; 340 + var out: [36]u8 = undefined; 341 + out[8] = '-'; 342 + out[13] = '-'; 343 + out[18] = '-'; 344 + out[23] = '-'; 335 345 336 - pub fn toString(uuid: []const u8) ![36]u8 { 337 - if (uuid.len != 16) { 338 - return error.InvalidUUID; 339 - } 340 - 341 - var out: [36]u8 = undefined; 342 - out[8] = '-'; 343 - out[13] = '-'; 344 - out[18] = '-'; 345 - out[23] = '-'; 346 + inline for (encoded_pos, 0..) |i, j| { 347 + out[i + 0] = hex[uuid[j] >> 4]; 348 + out[i + 1] = hex[uuid[j] & 0x0f]; 349 + } 350 + return out; 351 + } 346 352 347 - inline for (encoded_pos, 0..) |i, j| { 348 - out[i + 0] = hex[uuid[j] >> 4]; 349 - out[i + 1] = hex[uuid[j] & 0x0f]; 350 - } 351 - return out; 353 + pub fn toBytes(str: []const u8) ![16]u8 { 354 + if (str.len != 36 or str[8] != '-' or str[13] != '-' or str[18] != '-' or str[23] != '-') { 355 + return error.InvalidUUID; 352 356 } 353 357 354 - pub fn toBytes(str: []const u8) ![16]u8 { 355 - if (str.len != 36 or str[8] != '-' or str[13] != '-' or str[18] != '-' or str[23] != '-') { 358 + var out: [16]u8 = undefined; 359 + inline for (encoded_pos, 0..) |i, j| { 360 + const hi = hex_to_nibble[str[i + 0]]; 361 + const lo = hex_to_nibble[str[i + 1]]; 362 + if (hi == 0xff or lo == 0xff) { 356 363 return error.InvalidUUID; 357 364 } 358 - 359 - var out: [16]u8 = undefined; 360 - inline for (encoded_pos, 0..) |i, j| { 361 - const hi = hex_to_nibble[str[i + 0]]; 362 - const lo = hex_to_nibble[str[i + 1]]; 363 - if (hi == 0xff or lo == 0xff) { 364 - return error.InvalidUUID; 365 - } 366 - out[j] = hi << 4 | lo; 367 - } 368 - return out; 365 + out[j] = hi << 4 | lo; 369 366 } 370 - }; 367 + return out; 368 + } 369 + }; 371 370 372 - pub const MacAddr = struct { 373 - pub const oid = OID.make(829); 374 - const encoding = &binary_encoding; 371 + pub const MacAddr = struct { 372 + pub const oid = OID.make(829); 373 + const encoding = &binary_encoding; 375 374 376 - fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 377 - if (value.len != 6) { 378 - // assume this is a text representation 379 - return String.encode(value, buf, format_pos); 380 - } 381 - buf.writeAt(MacAddr.encoding, format_pos); 382 - var view = try buf.skip(4 + value.len); 383 - view.writeIntBig(i32, @intCast(value.len)); 384 - view.write(value); 375 + fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 376 + if (value.len != 6) { 377 + // assume this is a text representation 378 + return String.encode(value, buf, format_pos); 385 379 } 386 - }; 380 + buf.writeAt(MacAddr.encoding, format_pos); 381 + var view = try buf.skip(4 + value.len); 382 + view.writeIntBig(i32, @intCast(value.len)); 383 + view.write(value); 384 + } 385 + }; 387 386 388 - pub const MacAddr8 = struct { 389 - pub const oid = OID.make(774); 390 - const encoding = &binary_encoding; 387 + pub const MacAddr8 = struct { 388 + pub const oid = OID.make(774); 389 + const encoding = &binary_encoding; 391 390 392 - fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 393 - if (value.len != 8) { 394 - // assume this is a text representation 395 - return String.encode(value, buf, format_pos); 396 - } 397 - buf.writeAt(MacAddr8.encoding, format_pos); 398 - var view = try buf.skip(4 + value.len); 399 - view.writeIntBig(i32, @intCast(value.len)); 400 - view.write(value); 391 + fn encode(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 392 + if (value.len != 8) { 393 + // assume this is a text representation 394 + return String.encode(value, buf, format_pos); 401 395 } 402 - }; 396 + buf.writeAt(MacAddr8.encoding, format_pos); 397 + var view = try buf.skip(4 + value.len); 398 + view.writeIntBig(i32, @intCast(value.len)); 399 + view.write(value); 400 + } 401 + }; 403 402 404 - pub const JSON = struct { 405 - pub const oid = OID.make(114); 406 - const encoding = &binary_encoding; 403 + pub const JSON = struct { 404 + pub const oid = OID.make(114); 405 + const encoding = &binary_encoding; 407 406 408 - fn encodeBytes(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 409 - buf.writeAt(JSON.encoding, format_pos); 410 - var view = try buf.skip(4 + value.len); 411 - view.writeIntBig(i32, @intCast(value.len)); 412 - view.write(value); 413 - } 407 + fn encodeBytes(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 408 + buf.writeAt(JSON.encoding, format_pos); 409 + var view = try buf.skip(4 + value.len); 410 + view.writeIntBig(i32, @intCast(value.len)); 411 + view.write(value); 412 + } 414 413 415 - fn encode(value: anytype, buf: *buffer.Buffer, format_pos: usize) !void { 416 - buf.writeAt(JSON.encoding, format_pos); 417 - const state = try Encode.variableLengthStart(buf); 418 - try std.json.stringify(value, .{}, buf.writer()); 419 - Encode.variableLengthFill(buf, state); 420 - } 421 - }; 414 + fn encode(value: anytype, buf: *buffer.Buffer, format_pos: usize) !void { 415 + buf.writeAt(JSON.encoding, format_pos); 416 + const state = try Encode.variableLengthStart(buf); 417 + try std.json.stringify(value, .{}, buf.writer()); 418 + Encode.variableLengthFill(buf, state); 419 + } 420 + }; 422 421 423 - pub const JSONB = struct { 424 - pub const oid = OID.make(3802); 425 - const encoding = &binary_encoding; 422 + pub const JSONB = struct { 423 + pub const oid = OID.make(3802); 424 + const encoding = &binary_encoding; 426 425 427 - fn encodeBytes(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 428 - buf.writeAt(JSONB.encoding, format_pos); 429 - var view = try buf.skip(5 + value.len); 430 - // + 1 for the version 431 - view.writeIntBig(i32, @intCast(value.len + 1)); 432 - view.writeByte(1); // jsonb version 433 - view.write(value); 434 - } 426 + fn encodeBytes(value: []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 427 + buf.writeAt(JSONB.encoding, format_pos); 428 + var view = try buf.skip(5 + value.len); 429 + // + 1 for the version 430 + view.writeIntBig(i32, @intCast(value.len + 1)); 431 + view.writeByte(1); // jsonb version 432 + view.write(value); 433 + } 435 434 436 - fn encode(value: anytype, buf: *buffer.Buffer, format_pos: usize) !void { 437 - buf.writeAt(JSON.encoding, format_pos); 438 - const state = try Encode.variableLengthStart(buf); 439 - try buf.writeByte(1); // jsonb version 440 - try std.json.stringify(value, .{}, buf.writer()); 441 - Encode.variableLengthFill(buf, state); 442 - } 435 + fn encode(value: anytype, buf: *buffer.Buffer, format_pos: usize) !void { 436 + buf.writeAt(JSON.encoding, format_pos); 437 + const state = try Encode.variableLengthStart(buf); 438 + try buf.writeByte(1); // jsonb version 439 + try std.json.stringify(value, .{}, buf.writer()); 440 + Encode.variableLengthFill(buf, state); 441 + } 443 442 444 - fn decode(data: []const u8, data_oid: i32) []const u8 { 445 - lib.assertDecodeType([]const u8, &.{JSONB.oid.decimal}, data_oid); 446 - return JSONB.decodeKnown(data); 447 - } 443 + fn decode(data: []const u8, data_oid: i32) []const u8 { 444 + lib.assertDecodeType([]const u8, &.{JSONB.oid.decimal}, data_oid); 445 + return JSONB.decodeKnown(data); 446 + } 448 447 449 - pub fn decodeKnown(data: []const u8) []const u8 { 450 - return data[1..]; 451 - } 448 + pub fn decodeKnown(data: []const u8) []const u8 { 449 + return data[1..]; 450 + } 452 451 453 - pub fn decodeKnownMutable(data: []const u8) []u8 { 454 - // we know the underlying []u8 is mutable, it comes from our Reader 455 - return @constCast(data[1..]); 456 - } 457 - }; 452 + pub fn decodeKnownMutable(data: []const u8) []u8 { 453 + // we know the underlying []u8 is mutable, it comes from our Reader 454 + return @constCast(data[1..]); 455 + } 456 + }; 458 457 459 - pub const Int16Array = struct { 460 - pub const oid = OID.make(1005); 461 - const encoding = &binary_encoding; 458 + pub const Int16Array = struct { 459 + pub const oid = OID.make(1005); 460 + const encoding = &binary_encoding; 462 461 463 - fn encode(values: []const i16, buf: *buffer.Buffer, oid_pos: usize) !void { 464 - buf.writeAt(&Int16.oid.encoded, oid_pos); 465 - return Encode.writeIntArray(i16, 2, values, buf); 466 - } 462 + fn encode(values: []const i16, buf: *buffer.Buffer, oid_pos: usize) !void { 463 + buf.writeAt(&Int16.oid.encoded, oid_pos); 464 + return Encode.writeIntArray(i16, 2, values, buf); 465 + } 467 466 468 - fn encodeUnsigned(values: []const u16, buf: *buffer.Buffer, oid_pos: usize) !void { 469 - for (values) |v| { 470 - if (v > 32767) return error.UnsignedIntWouldBeTruncated; 471 - } 472 - buf.writeAt(&Int16.oid.encoded, oid_pos); 473 - return Encode.writeIntArray(i16, 2, values, buf); 467 + fn encodeUnsigned(values: []const u16, buf: *buffer.Buffer, oid_pos: usize) !void { 468 + for (values) |v| { 469 + if (v > 32767) return error.UnsignedIntWouldBeTruncated; 474 470 } 475 - }; 471 + buf.writeAt(&Int16.oid.encoded, oid_pos); 472 + return Encode.writeIntArray(i16, 2, values, buf); 473 + } 474 + }; 476 475 477 - pub const Int32Array = struct { 478 - pub const oid = OID.make(1007); 479 - const encoding = &binary_encoding; 476 + pub const Int32Array = struct { 477 + pub const oid = OID.make(1007); 478 + const encoding = &binary_encoding; 480 479 481 - fn encode(values: []const i32, buf: *buffer.Buffer, oid_pos: usize) !void { 482 - buf.writeAt(&Int32.oid.encoded, oid_pos); 483 - return Encode.writeIntArray(i32, 4, values, buf); 484 - } 480 + fn encode(values: []const i32, buf: *buffer.Buffer, oid_pos: usize) !void { 481 + buf.writeAt(&Int32.oid.encoded, oid_pos); 482 + return Encode.writeIntArray(i32, 4, values, buf); 483 + } 485 484 486 - fn encodeUnsigned(values: []const u32, buf: *buffer.Buffer, oid_pos: usize) !void { 487 - for (values) |v| { 488 - if (v > 2147483647) return error.UnsignedIntWouldBeTruncated; 489 - } 490 - buf.writeAt(&Int32.oid.encoded, oid_pos); 491 - return Encode.writeIntArray(i32, 4, values, buf); 485 + fn encodeUnsigned(values: []const u32, buf: *buffer.Buffer, oid_pos: usize) !void { 486 + for (values) |v| { 487 + if (v > 2147483647) return error.UnsignedIntWouldBeTruncated; 492 488 } 493 - }; 489 + buf.writeAt(&Int32.oid.encoded, oid_pos); 490 + return Encode.writeIntArray(i32, 4, values, buf); 491 + } 492 + }; 494 493 495 - pub const Int64Array = struct { 496 - pub const oid = OID.make(1016); 497 - const encoding = &binary_encoding; 494 + pub const Int64Array = struct { 495 + pub const oid = OID.make(1016); 496 + const encoding = &binary_encoding; 498 497 499 - fn encode(values: []const i64, buf: *buffer.Buffer, oid_pos: usize) !void { 500 - buf.writeAt(&Int64.oid.encoded, oid_pos); 501 - return Encode.writeIntArray(i64, 8, values, buf); 502 - } 498 + fn encode(values: []const i64, buf: *buffer.Buffer, oid_pos: usize) !void { 499 + buf.writeAt(&Int64.oid.encoded, oid_pos); 500 + return Encode.writeIntArray(i64, 8, values, buf); 501 + } 503 502 504 - fn encodeUnsigned(values: []const u64, buf: *buffer.Buffer, oid_pos: usize) !void { 505 - for (values) |v| { 506 - if (v > 9223372036854775807) return error.UnsignedIntWouldBeTruncated; 507 - } 508 - buf.writeAt(&Int64.oid.encoded, oid_pos); 509 - return Encode.writeIntArray(i64, 8, values, buf); 503 + fn encodeUnsigned(values: []const u64, buf: *buffer.Buffer, oid_pos: usize) !void { 504 + for (values) |v| { 505 + if (v > 9223372036854775807) return error.UnsignedIntWouldBeTruncated; 510 506 } 511 - }; 507 + buf.writeAt(&Int64.oid.encoded, oid_pos); 508 + return Encode.writeIntArray(i64, 8, values, buf); 509 + } 510 + }; 512 511 513 - pub const TimestampArray = struct { 514 - pub const oid = OID.make(1115); 515 - const encoding = &binary_encoding; 516 - const us_from_epoch_to_y2k = 946_684_800_000_000; 512 + pub const TimestampArray = struct { 513 + pub const oid = OID.make(1115); 514 + const encoding = &binary_encoding; 515 + const us_from_epoch_to_y2k = 946_684_800_000_000; 517 516 518 - fn encode(values: []const i64, buf: *buffer.Buffer, oid_pos: usize) !void { 519 - buf.writeAt(&Timestamp.oid.encoded, oid_pos); 517 + fn encode(values: []const i64, buf: *buffer.Buffer, oid_pos: usize) !void { 518 + buf.writeAt(&Timestamp.oid.encoded, oid_pos); 520 519 521 - // every value is 12 bytes, 4 byte length + 8 byte value 522 - var view = try buf.skip(12 * values.len); 523 - for (values) |value| { 524 - view.write(&.{ 0, 0, 0, 8 }); // length of value 525 - view.writeIntBig(i64, value - us_from_epoch_to_y2k); 526 - } 520 + // every value is 12 bytes, 4 byte length + 8 byte value 521 + var view = try buf.skip(12 * values.len); 522 + for (values) |value| { 523 + view.write(&.{ 0, 0, 0, 8 }); // length of value 524 + view.writeIntBig(i64, value - us_from_epoch_to_y2k); 527 525 } 528 - }; 526 + } 527 + }; 529 528 530 - pub const TimestampTzArray = struct { 531 - pub const oid = OID.make(1185); 532 - const encoding = &binary_encoding; 529 + pub const TimestampTzArray = struct { 530 + pub const oid = OID.make(1185); 531 + const encoding = &binary_encoding; 533 532 534 - const us_from_epoch_to_y2k = 946_684_800_000_000; 533 + const us_from_epoch_to_y2k = 946_684_800_000_000; 535 534 536 - fn encode(values: []const i64, buf: *buffer.Buffer, oid_pos: usize) !void { 537 - buf.writeAt(&TimestampTz.oid.encoded, oid_pos); 535 + fn encode(values: []const i64, buf: *buffer.Buffer, oid_pos: usize) !void { 536 + buf.writeAt(&TimestampTz.oid.encoded, oid_pos); 538 537 539 - // every value is 12 bytes, 4 byte length + 8 byte value 540 - var view = try buf.skip(12 * values.len); 541 - for (values) |value| { 542 - view.write(&.{ 0, 0, 0, 8 }); // length of value 543 - view.writeIntBig(i64, value - us_from_epoch_to_y2k); 544 - } 538 + // every value is 12 bytes, 4 byte length + 8 byte value 539 + var view = try buf.skip(12 * values.len); 540 + for (values) |value| { 541 + view.write(&.{ 0, 0, 0, 8 }); // length of value 542 + view.writeIntBig(i64, value - us_from_epoch_to_y2k); 545 543 } 546 - }; 544 + } 545 + }; 547 546 548 - pub const Float32Array = struct { 549 - pub const oid = OID.make(1021); 550 - const encoding = &binary_encoding; 547 + pub const Float32Array = struct { 548 + pub const oid = OID.make(1021); 549 + const encoding = &binary_encoding; 551 550 552 - fn encode(values: []const f32, buf: *buffer.Buffer, oid_pos: usize) !void { 553 - buf.writeAt(&Float32.oid.encoded, oid_pos); 551 + fn encode(values: []const f32, buf: *buffer.Buffer, oid_pos: usize) !void { 552 + buf.writeAt(&Float32.oid.encoded, oid_pos); 554 553 555 - // every value takes 8 bytes, 4 for the length, 4 for the value 556 - var view = try buf.skip(8 * values.len); 557 - for (values) |value| { 558 - view.write(&.{ 0, 0, 0, 4 }); //length 559 - const tmp: *i32 = @constCast(@ptrCast(&value)); 560 - view.writeIntBig(i32, tmp.*); 561 - } 554 + // every value takes 8 bytes, 4 for the length, 4 for the value 555 + var view = try buf.skip(8 * values.len); 556 + for (values) |value| { 557 + view.write(&.{ 0, 0, 0, 4 }); //length 558 + const tmp: *i32 = @constCast(@ptrCast(&value)); 559 + view.writeIntBig(i32, tmp.*); 562 560 } 563 - }; 561 + } 562 + }; 564 563 565 - pub const Float64Array = struct { 566 - pub const oid = OID.make(1022); 567 - const encoding = &binary_encoding; 564 + pub const Float64Array = struct { 565 + pub const oid = OID.make(1022); 566 + const encoding = &binary_encoding; 568 567 569 - fn encode(values: []const f64, buf: *buffer.Buffer, oid_pos: usize) !void { 570 - buf.writeAt(&Float64.oid.encoded, oid_pos); 568 + fn encode(values: []const f64, buf: *buffer.Buffer, oid_pos: usize) !void { 569 + buf.writeAt(&Float64.oid.encoded, oid_pos); 571 570 572 - // every value takes 12 bytes, 4 for the length, 8 for the value 573 - var view = try buf.skip(12 * values.len); 574 - for (values) |value| { 575 - view.write(&.{ 0, 0, 0, 8 }); //length 576 - const tmp: *i64 = @constCast(@ptrCast(&value)); 577 - view.writeIntBig(i64, tmp.*); 578 - } 571 + // every value takes 12 bytes, 4 for the length, 8 for the value 572 + var view = try buf.skip(12 * values.len); 573 + for (values) |value| { 574 + view.write(&.{ 0, 0, 0, 8 }); //length 575 + const tmp: *i64 = @constCast(@ptrCast(&value)); 576 + view.writeIntBig(i64, tmp.*); 579 577 } 580 - }; 578 + } 579 + }; 581 580 582 - pub const BoolArray = struct { 583 - pub const oid = OID.make(1000); 584 - const encoding = &binary_encoding; 581 + pub const BoolArray = struct { 582 + pub const oid = OID.make(1000); 583 + const encoding = &binary_encoding; 585 584 586 - fn encode(values: []const bool, buf: *buffer.Buffer, oid_pos: usize) !void { 587 - buf.writeAt(&Bool.oid.encoded, oid_pos); 585 + fn encode(values: []const bool, buf: *buffer.Buffer, oid_pos: usize) !void { 586 + buf.writeAt(&Bool.oid.encoded, oid_pos); 588 587 589 - // every value takes 5 bytes, 4 for the length, 1 for the value 590 - var view = try buf.skip(5 * values.len); 591 - for (values) |value| { 592 - // each value is prefixed with a 4 byte length 593 - if (value) { 594 - view.write(&.{ 0, 0, 0, 1, 1 }); 595 - } else { 596 - view.write(&.{ 0, 0, 0, 1, 0 }); 597 - } 588 + // every value takes 5 bytes, 4 for the length, 1 for the value 589 + var view = try buf.skip(5 * values.len); 590 + for (values) |value| { 591 + // each value is prefixed with a 4 byte length 592 + if (value) { 593 + view.write(&.{ 0, 0, 0, 1, 1 }); 594 + } else { 595 + view.write(&.{ 0, 0, 0, 1, 0 }); 598 596 } 599 597 } 600 - }; 598 + } 599 + }; 601 600 602 - pub const NumericArray = struct { 603 - pub const oid = OID.make(1231); 604 - const encoding = &binary_encoding; 605 - fn encode(values: anytype, buf: *buffer.Buffer, oid_pos: usize) !void { 606 - buf.writeAt(&Types.Numeric.oid.encoded, oid_pos); 601 + pub const NumericArray = struct { 602 + pub const oid = OID.make(1231); 603 + const encoding = &binary_encoding; 604 + fn encode(values: anytype, buf: *buffer.Buffer, oid_pos: usize) !void { 605 + buf.writeAt(&Numeric.oid.encoded, oid_pos); 607 606 608 - for (values) |value| { 609 - try Types.Numeric.encodeBuf(value, buf); 610 - } 607 + for (values) |value| { 608 + try Numeric.encodeBuf(value, buf); 611 609 } 612 - }; 610 + } 611 + }; 613 612 614 - pub const CidrArray = struct { 615 - pub const oid = OID.make(651); 616 - pub const inet_oid = OID.make(1041); 617 - const encoding = &binary_encoding; 618 - }; 613 + pub const CidrArray = struct { 614 + pub const oid = OID.make(651); 615 + pub const inet_oid = OID.make(1041); 616 + const encoding = &binary_encoding; 617 + }; 619 618 620 - pub const MacAddrArray = struct { 621 - pub const oid = OID.make(1040); 622 - const encoding = &binary_encoding; 619 + pub const MacAddrArray = struct { 620 + pub const oid = OID.make(1040); 621 + const encoding = &binary_encoding; 623 622 624 - fn encode(values: []const []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 625 - // This has challenges. Do we have a binary representation or a text representation? 626 - // Or maybe we have a mix (maybe we shouldn't support that)? 627 - // We handle this with UUID by converting the text representation to binary 628 - // but it's harder wit MacAddr because it supports 7 different text representations 629 - // and I don't really want this library to become a text parsing library which attempts 630 - // to mimic what PostgreSQL does. 631 - // So we're going to send a text-encoded array with text values, which emans 632 - // we need to convert any binary representation to text (which is a lot easier). 623 + fn encode(values: []const []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 624 + // This has challenges. Do we have a binary representation or a text representation? 625 + // Or maybe we have a mix (maybe we shouldn't support that)? 626 + // We handle this with UUID by converting the text representation to binary 627 + // but it's harder wit MacAddr because it supports 7 different text representations 628 + // and I don't really want this library to become a text parsing library which attempts 629 + // to mimic what PostgreSQL does. 630 + // So we're going to send a text-encoded array with text values, which emans 631 + // we need to convert any binary representation to text (which is a lot easier). 633 632 634 - // The worst-case scenario is that each value takes 17 bytes. This is the 635 - // most verbose text-encoded value. When we encode a binary value as text 636 - // we'll use the most compact (12 bytes), but we might be given a 17-byte 637 - // text-encoded value, which we'll write as-is 638 - var l: usize = 0; 639 - for (values) |v| { 640 - // binary values will be encoded in a 12-byte text representation 641 - l += if (v.len == 6) 12 else v.len; 642 - } 633 + // The worst-case scenario is that each value takes 17 bytes. This is the 634 + // most verbose text-encoded value. When we encode a binary value as text 635 + // we'll use the most compact (12 bytes), but we might be given a 17-byte 636 + // text-encoded value, which we'll write as-is 637 + var l: usize = 0; 638 + for (values) |v| { 639 + // binary values will be encoded in a 12-byte text representation 640 + l += if (v.len == 6) 12 else v.len; 641 + } 643 642 644 - return Encode.writeTextEncodedArray(values, l, buf, format_pos, MacAddrArray.writeOneAsText); 645 - } 643 + return Encode.writeTextEncodedArray(values, l, buf, format_pos, MacAddrArray.writeOneAsText); 644 + } 646 645 647 - fn writeOneAsText(value: []const u8, buf: *buffer.Buffer) void { 648 - if (value.len == 6) { 649 - std.fmt.format(buf.writer(), "{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}", .{ value[0], value[1], value[2], value[3], value[4], value[5] }) catch unreachable; 650 - } else { 651 - buf.writeAssumeCapacity(value); 652 - } 646 + fn writeOneAsText(value: []const u8, buf: *buffer.Buffer) void { 647 + if (value.len == 6) { 648 + std.fmt.format(buf.writer(), "{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}", .{ value[0], value[1], value[2], value[3], value[4], value[5] }) catch unreachable; 649 + } else { 650 + buf.writeAssumeCapacity(value); 653 651 } 654 - }; 652 + } 653 + }; 655 654 656 - pub const MacAddr8Array = struct { 657 - pub const oid = OID.make(775); 658 - const encoding = &binary_encoding; 655 + pub const MacAddr8Array = struct { 656 + pub const oid = OID.make(775); 657 + const encoding = &binary_encoding; 659 658 660 - fn encode(values: []const []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 661 - // See comments in MacAddrArray.encode 662 - var l: usize = 0; 663 - for (values) |v| { 664 - // binary values will be encoded in a 16-byte text representation 665 - l += if (v.len == 8) 16 else v.len; 666 - } 667 - 668 - return Encode.writeTextEncodedArray(values, l, buf, format_pos, MacAddr8Array.writeOneAsText); 659 + fn encode(values: []const []const u8, buf: *buffer.Buffer, format_pos: usize) !void { 660 + // See comments in MacAddrArray.encode 661 + var l: usize = 0; 662 + for (values) |v| { 663 + // binary values will be encoded in a 16-byte text representation 664 + l += if (v.len == 8) 16 else v.len; 669 665 } 670 666 671 - fn writeOneAsText(value: []const u8, buf: *buffer.Buffer) void { 672 - if (value.len == 8) { 673 - std.fmt.format(buf.writer(), "{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}", .{ value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7] }) catch unreachable; 674 - } else { 675 - buf.writeAssumeCapacity(value); 676 - } 667 + return Encode.writeTextEncodedArray(values, l, buf, format_pos, MacAddr8Array.writeOneAsText); 668 + } 669 + 670 + fn writeOneAsText(value: []const u8, buf: *buffer.Buffer) void { 671 + if (value.len == 8) { 672 + std.fmt.format(buf.writer(), "{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}{x:0>2}", .{ value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7] }) catch unreachable; 673 + } else { 674 + buf.writeAssumeCapacity(value); 677 675 } 678 - }; 676 + } 677 + }; 679 678 680 - pub const ByteaArray = struct { 681 - pub const oid = OID.make(1001); 682 - const encoding = &binary_encoding; 679 + pub const ByteaArray = struct { 680 + pub const oid = OID.make(1001); 681 + const encoding = &binary_encoding; 683 682 684 - fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 685 - buf.writeAt(&Bytea.oid.encoded, oid_pos); 686 - return Encode.writeByteArray(values, buf); 687 - } 688 - }; 683 + fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 684 + buf.writeAt(&Bytea.oid.encoded, oid_pos); 685 + return Encode.writeByteArray(values, buf); 686 + } 687 + }; 689 688 690 - pub const StringArray = struct { 691 - pub const oid = OID.make(1009); 692 - const encoding = &binary_encoding; 689 + pub const StringArray = struct { 690 + pub const oid = OID.make(1009); 691 + const encoding = &binary_encoding; 693 692 694 - fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 695 - buf.writeAt(&String.oid.encoded, oid_pos); 696 - return Encode.writeByteArray(values, buf); 697 - } 693 + fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 694 + buf.writeAt(&String.oid.encoded, oid_pos); 695 + return Encode.writeByteArray(values, buf); 696 + } 698 697 699 - fn encodeEnum(values: anytype, buf: *buffer.Buffer, oid_pos: usize) !void { 700 - buf.writeAt(&String.oid.encoded, oid_pos); 701 - for (values.*) |value| { 702 - const str = @tagName(value); 703 - try buf.writeIntBig(i32, @intCast(str.len)); 704 - try buf.write(str); 705 - } 698 + fn encodeEnum(values: anytype, buf: *buffer.Buffer, oid_pos: usize) !void { 699 + buf.writeAt(&String.oid.encoded, oid_pos); 700 + for (values.*) |value| { 701 + const str = @tagName(value); 702 + try buf.writeIntBig(i32, @intCast(str.len)); 703 + try buf.write(str); 706 704 } 707 - }; 705 + } 706 + }; 708 707 709 - pub const UUIDArray = struct { 710 - pub const oid = OID.make(2951); 711 - const encoding = &binary_encoding; 708 + pub const UUIDArray = struct { 709 + pub const oid = OID.make(2951); 710 + const encoding = &binary_encoding; 712 711 713 - fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 714 - buf.writeAt(&UUID.oid.encoded, oid_pos); 712 + fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 713 + buf.writeAt(&UUID.oid.encoded, oid_pos); 715 714 716 - // every value is 20 bytes, 4 byte length + 16 byte value 717 - var view = try buf.skip(20 * values.len); 718 - for (values) |value| { 719 - view.write(&.{ 0, 0, 0, 16 }); // length of value 720 - switch (value.len) { 721 - 16 => view.write(value), 722 - 36 => view.write(&(try UUID.toBytes(value))), 723 - else => return error.InvalidUUID, 724 - } 715 + // every value is 20 bytes, 4 byte length + 16 byte value 716 + var view = try buf.skip(20 * values.len); 717 + for (values) |value| { 718 + view.write(&.{ 0, 0, 0, 16 }); // length of value 719 + switch (value.len) { 720 + 16 => view.write(value), 721 + 36 => view.write(&(try UUID.toBytes(value))), 722 + else => return error.InvalidUUID, 725 723 } 726 724 } 727 - }; 725 + } 726 + }; 728 727 729 - pub const JSONArray = struct { 730 - pub const oid = OID.make(199); 731 - const encoding = &binary_encoding; 728 + pub const JSONArray = struct { 729 + pub const oid = OID.make(199); 730 + const encoding = &binary_encoding; 732 731 733 - fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 734 - buf.writeAt(&JSON.oid.encoded, oid_pos); 735 - return Encode.writeByteArray(values, buf); 736 - } 737 - }; 732 + fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 733 + buf.writeAt(&JSON.oid.encoded, oid_pos); 734 + return Encode.writeByteArray(values, buf); 735 + } 736 + }; 738 737 739 - pub const JSONBArray = struct { 740 - pub const oid = OID.make(3807); 741 - const encoding = &binary_encoding; 738 + pub const JSONBArray = struct { 739 + pub const oid = OID.make(3807); 740 + const encoding = &binary_encoding; 742 741 743 - fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 744 - buf.writeAt(&JSONB.oid.encoded, oid_pos); 742 + fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 743 + buf.writeAt(&JSONB.oid.encoded, oid_pos); 745 744 746 - // every value has a 5 byte prefix, a 4 byte length and a 1 byte version 747 - var len = values.len * 5; 748 - for (values) |value| { 749 - len += value.len; 750 - } 745 + // every value has a 5 byte prefix, a 4 byte length and a 1 byte version 746 + var len = values.len * 5; 747 + for (values) |value| { 748 + len += value.len; 749 + } 751 750 752 - var view = try buf.skip(len); 753 - for (values) |value| { 754 - // + 1 for the version 755 - view.writeIntBig(i32, @intCast(value.len + 1)); 756 - view.writeByte(1); // version 757 - view.write(value); 758 - } 751 + var view = try buf.skip(len); 752 + for (values) |value| { 753 + // + 1 for the version 754 + view.writeIntBig(i32, @intCast(value.len + 1)); 755 + view.writeByte(1); // version 756 + view.write(value); 759 757 } 760 - }; 758 + } 759 + }; 761 760 762 - pub const CharArray = struct { 763 - pub const oid = OID.make(1014); 764 - const encoding = &binary_encoding; 761 + pub const CharArray = struct { 762 + pub const oid = OID.make(1014); 763 + const encoding = &binary_encoding; 765 764 766 - // This is for a char[] bound to a []u8 767 - fn encodeOne(values: []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 768 - buf.writeAt(&Char.oid.encoded, oid_pos); 765 + // This is for a char[] bound to a []u8 766 + fn encodeOne(values: []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 767 + buf.writeAt(&Char.oid.encoded, oid_pos); 769 768 770 - // every value has a 5 byte prefix, a 4 byte length and a 1 byte char 771 - const len = values.len * 5; 772 - var view = try buf.skip(len); 773 - for (values) |value| { 774 - view.write(&.{ 0, 0, 0, 1 }); 775 - view.writeByte(value); 776 - } 769 + // every value has a 5 byte prefix, a 4 byte length and a 1 byte char 770 + const len = values.len * 5; 771 + var view = try buf.skip(len); 772 + for (values) |value| { 773 + view.write(&.{ 0, 0, 0, 1 }); 774 + view.writeByte(value); 777 775 } 776 + } 778 777 779 - // This is for a char[] bound to a [][]u8 780 - fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 781 - buf.writeAt(&Char.oid.encoded, oid_pos); 782 - return Encode.writeByteArray(values, buf); 783 - } 784 - }; 778 + // This is for a char[] bound to a [][]u8 779 + fn encode(values: []const []const u8, buf: *buffer.Buffer, oid_pos: usize) !void { 780 + buf.writeAt(&Char.oid.encoded, oid_pos); 781 + return Encode.writeByteArray(values, buf); 782 + } 783 + }; 785 784 786 - // Return the encoding we want PG to use for a particular OID 787 - fn resultEncodingFor(oid: i32) *const [2]u8 { 788 - inline for (@typeInfo(@This()).@"struct".decls) |decl| { 789 - const S = @field(@This(), decl.name); 785 + // Return the encoding we want PG to use for a particular OID 786 + fn resultEncodingFor(oid: i32) *const [2]u8 { 787 + inline for (@typeInfo(@This()).@"struct".decls) |decl| { 788 + const S = @field(@This(), decl.name); 789 + if (@typeInfo(@TypeOf(S)) == .type and @hasField(S, "oid")) { 790 790 if (oid == S.oid.decimal) { 791 791 return S.encoding; 792 792 } 793 793 } 794 - // default to text encoding 795 - return &binary_encoding; 796 794 } 797 - }; 798 - 799 - // expose our Types directly so callers can do types.Int32 rather than 800 - // types.Types.Int32 801 - pub usingnamespace Types; 795 + // default to text encoding 796 + return &binary_encoding; 797 + } 802 798 803 799 pub const Encode = struct { 804 800 // helpers for encoding data (or part of the data) ··· 1127 1123 return buf.write(&.{ 255, 255, 255, 255 }); 1128 1124 }, 1129 1125 .comptime_int => switch (oid) { 1130 - Types.Int16.oid.decimal => { 1126 + Int16.oid.decimal => { 1131 1127 if (value > 32767 or value < -32768) return error.IntWontFit; 1132 - return Types.Int16.encode(@intCast(value), buf, format_pos); 1128 + return Int16.encode(@intCast(value), buf, format_pos); 1133 1129 }, 1134 - Types.Int32.oid.decimal => { 1130 + Int32.oid.decimal => { 1135 1131 if (value > 2147483647 or value < -2147483648) return error.IntWontFit; 1136 - return Types.Int32.encode(@intCast(value), buf, format_pos); 1132 + return Int32.encode(@intCast(value), buf, format_pos); 1137 1133 }, 1138 - Types.Timestamp.oid.decimal, Types.TimestampTz.oid.decimal => return Types.Timestamp.encode(@intCast(value), buf, format_pos), 1139 - Types.Numeric.oid.decimal => return Types.Numeric.encode(@as(f64, @floatFromInt(value)), buf, format_pos), 1140 - Types.Char.oid.decimal => { 1134 + Timestamp.oid.decimal, TimestampTz.oid.decimal => return Timestamp.encode(@intCast(value), buf, format_pos), 1135 + Numeric.oid.decimal => return Numeric.encode(@as(f64, @floatFromInt(value)), buf, format_pos), 1136 + Char.oid.decimal => { 1141 1137 if (value > 255 or value < 0) return error.IntWontFit; 1142 - return Types.Char.encode(@intCast(value), buf, format_pos); 1138 + return Char.encode(@intCast(value), buf, format_pos); 1143 1139 }, 1144 - Types.Int64.oid.decimal => return Types.Int64.encode(@intCast(value), buf, format_pos), 1140 + Int64.oid.decimal => return Int64.encode(@intCast(value), buf, format_pos), 1145 1141 else => return error.BindWrongType, 1146 1142 }, 1147 1143 .int => switch (oid) { 1148 - Types.Int16.oid.decimal => { 1144 + Int16.oid.decimal => { 1149 1145 if (value > 32767 or value < -32768) return error.IntWontFit; 1150 - return Types.Int16.encode(@intCast(value), buf, format_pos); 1146 + return Int16.encode(@intCast(value), buf, format_pos); 1151 1147 }, 1152 - Types.Int32.oid.decimal => { 1148 + Int32.oid.decimal => { 1153 1149 if (value > 2147483647 or value < -2147483648) return error.IntWontFit; 1154 - return Types.Int32.encode(@intCast(value), buf, format_pos); 1150 + return Int32.encode(@intCast(value), buf, format_pos); 1155 1151 }, 1156 - Types.Timestamp.oid.decimal, Types.TimestampTz.oid.decimal => return Types.Timestamp.encode(@intCast(value), buf, format_pos), 1157 - Types.Numeric.oid.decimal => return Types.Numeric.encode(@as(f64, @floatFromInt(value)), buf, format_pos), 1158 - Types.Char.oid.decimal => { 1152 + Timestamp.oid.decimal, TimestampTz.oid.decimal => return Timestamp.encode(@intCast(value), buf, format_pos), 1153 + Numeric.oid.decimal => return Numeric.encode(@as(f64, @floatFromInt(value)), buf, format_pos), 1154 + Char.oid.decimal => { 1159 1155 if (value > 255 or value < 0) return error.IntWontFit; 1160 - return Types.Char.encode(@intCast(value), buf, format_pos); 1156 + return Char.encode(@intCast(value), buf, format_pos); 1161 1157 }, 1162 - Types.Int64.oid.decimal => { 1158 + Int64.oid.decimal => { 1163 1159 if (value > 9223372036854775807 or value < -9223372036854775808) { 1164 1160 return error.IntWontFit; 1165 1161 } 1166 - return Types.Int64.encode(@intCast(value), buf, format_pos); 1162 + return Int64.encode(@intCast(value), buf, format_pos); 1167 1163 }, 1168 1164 else => return error.BindWrongType, 1169 1165 }, 1170 1166 .comptime_float => switch (oid) { 1171 - Types.Float64.oid.decimal => return Types.Float64.encode(@floatCast(value), buf, format_pos), 1172 - Types.Float32.oid.decimal => return Types.Float32.encode(@floatCast(value), buf, format_pos), 1173 - Types.Numeric.oid.decimal => return Types.Numeric.encode(value, buf, format_pos), 1167 + Float64.oid.decimal => return Float64.encode(@floatCast(value), buf, format_pos), 1168 + Float32.oid.decimal => return Float32.encode(@floatCast(value), buf, format_pos), 1169 + Numeric.oid.decimal => return Numeric.encode(value, buf, format_pos), 1174 1170 else => return error.BindWrongType, 1175 1171 }, 1176 1172 .float => switch (oid) { 1177 - Types.Float64.oid.decimal => return Types.Float64.encode(@floatCast(value), buf, format_pos), 1178 - Types.Float32.oid.decimal => return Types.Float32.encode(@floatCast(value), buf, format_pos), 1179 - Types.Numeric.oid.decimal => return Types.Numeric.encode(value, buf, format_pos), 1173 + Float64.oid.decimal => return Float64.encode(@floatCast(value), buf, format_pos), 1174 + Float32.oid.decimal => return Float32.encode(@floatCast(value), buf, format_pos), 1175 + Numeric.oid.decimal => return Numeric.encode(value, buf, format_pos), 1180 1176 else => return error.BindWrongType, 1181 1177 }, 1182 1178 .bool => switch (oid) { 1183 - Types.Bool.oid.decimal => return Types.Bool.encode(value, buf, format_pos), 1179 + Bool.oid.decimal => return Bool.encode(value, buf, format_pos), 1184 1180 else => return error.BindWrongType, 1185 1181 }, 1186 1182 .pointer => |ptr| switch (ptr.size) { ··· 1197 1193 return bindSlice(oid, @as(Slice, value), buf, format_pos); 1198 1194 }, 1199 1195 .@"struct" => switch (oid) { 1200 - Types.JSON.oid.decimal => return Types.JSON.encode(value, buf, format_pos), 1201 - Types.JSONB.oid.decimal => return Types.JSONB.encode(value, buf, format_pos), 1196 + JSON.oid.decimal => return JSON.encode(value, buf, format_pos), 1197 + JSONB.oid.decimal => return JSONB.encode(value, buf, format_pos), 1202 1198 else => return error.CannotBindStruct, 1203 1199 }, 1204 1200 else => compileHaltBindError(T), ··· 1214 1210 // null 1215 1211 return buf.write(&.{ 255, 255, 255, 255 }); 1216 1212 }, 1217 - .@"enum", .enum_literal => return Types.String.encode(@tagName(value), buf, format_pos), 1213 + .@"enum", .enum_literal => return String.encode(@tagName(value), buf, format_pos), 1218 1214 else => compileHaltBindError(T), 1219 1215 } 1220 1216 } ··· 1223 1219 const T = @TypeOf(value); 1224 1220 if (T == []u8 or T == []const u8) { 1225 1221 switch (oid) { 1226 - Types.Bytea.oid.decimal => return Types.Bytea.encode(value, buf, format_pos), 1227 - Types.UUID.oid.decimal => return Types.UUID.encode(value, buf, format_pos), 1228 - Types.JSONB.oid.decimal => return Types.JSONB.encodeBytes(value, buf, format_pos), 1229 - Types.JSON.oid.decimal => return Types.JSON.encodeBytes(value, buf, format_pos), 1230 - Types.MacAddr.oid.decimal => return Types.MacAddr.encode(value, buf, format_pos), 1231 - Types.MacAddr8.oid.decimal => return Types.MacAddr8.encode(value, buf, format_pos), 1232 - Types.CharArray.oid.decimal => { 1222 + Bytea.oid.decimal => return Bytea.encode(value, buf, format_pos), 1223 + UUID.oid.decimal => return UUID.encode(value, buf, format_pos), 1224 + JSONB.oid.decimal => return JSONB.encodeBytes(value, buf, format_pos), 1225 + JSON.oid.decimal => return JSON.encodeBytes(value, buf, format_pos), 1226 + MacAddr.oid.decimal => return MacAddr.encode(value, buf, format_pos), 1227 + MacAddr8.oid.decimal => return MacAddr8.encode(value, buf, format_pos), 1228 + CharArray.oid.decimal => { 1233 1229 // This is actually an array, and in theory we could let it fallthrough 1234 1230 // to the binary-array handling. BUT, if we do that, the code won't compile 1235 1231 // because it would mean T can be []u8 or []const u8, and that makes parts ··· 1237 1233 // is going to be more efficient than encoding it using the binary protocol. 1238 1234 return Encode.writeTextEncodedCharArray(value, buf, format_pos); 1239 1235 }, 1240 - else => return Types.String.encode(value, buf, format_pos), 1236 + else => return String.encode(value, buf, format_pos), 1241 1237 } 1242 1238 } 1243 1239 ··· 1246 1242 // own text->type conversion. 1247 1243 if (comptime isStringArray(T)) { 1248 1244 switch (oid) { 1249 - Types.TimestampArray.oid.decimal, Types.NumericArray.oid.decimal => return Encode.writeTextEncodedEscapedArray(value, buf, format_pos), 1250 - Types.TimestampTzArray.oid.decimal, Types.CidrArray.oid.decimal, Types.CidrArray.inet_oid.decimal => return Encode.writeTextEncodedRawArray(value, buf, format_pos), 1251 - Types.MacAddrArray.oid.decimal => return Types.MacAddrArray.encode(value, buf, format_pos), 1252 - Types.MacAddr8Array.oid.decimal => return Types.MacAddr8Array.encode(value, buf, format_pos), 1245 + TimestampArray.oid.decimal, NumericArray.oid.decimal => return Encode.writeTextEncodedEscapedArray(value, buf, format_pos), 1246 + TimestampTzArray.oid.decimal, CidrArray.oid.decimal, CidrArray.inet_oid.decimal => return Encode.writeTextEncodedRawArray(value, buf, format_pos), 1247 + MacAddrArray.oid.decimal => return MacAddrArray.encode(value, buf, format_pos), 1248 + MacAddr8Array.oid.decimal => return MacAddr8Array.encode(value, buf, format_pos), 1253 1249 else => {}, // fallthrough to binary encoding 1254 1250 } 1255 1251 } ··· 1283 1279 .int => |int| { 1284 1280 if (int.signedness == .signed) { 1285 1281 switch (int.bits) { 1286 - 16 => try Types.Int16Array.encode(value, buf, oid_pos), 1287 - 32 => try Types.Int32Array.encode(value, buf, oid_pos), 1282 + 16 => try Int16Array.encode(value, buf, oid_pos), 1283 + 32 => try Int32Array.encode(value, buf, oid_pos), 1288 1284 64 => { 1289 1285 switch (oid) { 1290 - Types.TimestampArray.oid.decimal => try Types.TimestampArray.encode(value, buf, oid_pos), 1291 - Types.TimestampTzArray.oid.decimal => try Types.TimestampTzArray.encode(value, buf, oid_pos), 1292 - else => try Types.Int64Array.encode(value, buf, oid_pos), 1286 + TimestampArray.oid.decimal => try TimestampArray.encode(value, buf, oid_pos), 1287 + TimestampTzArray.oid.decimal => try TimestampTzArray.encode(value, buf, oid_pos), 1288 + else => try Int64Array.encode(value, buf, oid_pos), 1293 1289 } 1294 1290 }, 1295 1291 else => compileHaltBindError(T), 1296 1292 } 1297 1293 } else { 1298 1294 switch (int.bits) { 1299 - 8 => try Types.CharArray.encodeOne(value, buf, oid_pos), 1300 - 16 => try Types.Int16Array.encodeUnsigned(value, buf, oid_pos), 1301 - 32 => try Types.Int32Array.encodeUnsigned(value, buf, oid_pos), 1302 - 64 => try Types.Int64Array.encodeUnsigned(value, buf, oid_pos), 1295 + 8 => try CharArray.encodeOne(value, buf, oid_pos), 1296 + 16 => try Int16Array.encodeUnsigned(value, buf, oid_pos), 1297 + 32 => try Int32Array.encodeUnsigned(value, buf, oid_pos), 1298 + 64 => try Int64Array.encodeUnsigned(value, buf, oid_pos), 1303 1299 else => compileHaltBindError(T), 1304 1300 } 1305 1301 } 1306 1302 }, 1307 1303 .float => |float| { 1308 - if (oid == Types.NumericArray.oid.decimal) { 1309 - try Types.NumericArray.encode(value, buf, oid_pos); 1304 + if (oid == NumericArray.oid.decimal) { 1305 + try NumericArray.encode(value, buf, oid_pos); 1310 1306 } else switch (float.bits) { 1311 - 32 => try Types.Float32Array.encode(value, buf, oid_pos), 1312 - 64 => try Types.Float64Array.encode(value, buf, oid_pos), 1307 + 32 => try Float32Array.encode(value, buf, oid_pos), 1308 + 64 => try Float64Array.encode(value, buf, oid_pos), 1313 1309 else => compileHaltBindError(T), 1314 1310 } 1315 1311 }, 1316 - .bool => try Types.BoolArray.encode(value, buf, oid_pos), 1312 + .bool => try BoolArray.encode(value, buf, oid_pos), 1317 1313 .pointer => |ptr| switch (ptr.size) { 1318 1314 .slice => switch (ptr.child) { 1319 1315 u8 => switch (oid) { 1320 - Types.StringArray.oid.decimal => try Types.StringArray.encode(value, buf, oid_pos), 1321 - Types.UUIDArray.oid.decimal => try Types.UUIDArray.encode(value, buf, oid_pos), 1322 - Types.JSONBArray.oid.decimal => try Types.JSONBArray.encode(value, buf, oid_pos), 1323 - Types.JSONArray.oid.decimal => try Types.JSONArray.encode(value, buf, oid_pos), 1324 - Types.CharArray.oid.decimal => try Types.CharArray.encode(value, buf, oid_pos), 1316 + StringArray.oid.decimal => try StringArray.encode(value, buf, oid_pos), 1317 + UUIDArray.oid.decimal => try UUIDArray.encode(value, buf, oid_pos), 1318 + JSONBArray.oid.decimal => try JSONBArray.encode(value, buf, oid_pos), 1319 + JSONArray.oid.decimal => try JSONArray.encode(value, buf, oid_pos), 1320 + CharArray.oid.decimal => try CharArray.encode(value, buf, oid_pos), 1325 1321 // we try this as a default to support user defined types with unknown oids 1326 1322 // (like an array of enums) 1327 - else => try Types.ByteaArray.encode(value, buf, oid_pos), 1323 + else => try ByteaArray.encode(value, buf, oid_pos), 1328 1324 }, 1329 1325 else => compileHaltBindError(T), 1330 1326 }, 1331 1327 else => compileHaltBindError(T), 1332 1328 }, 1333 - .@"enum", .enum_literal => try Types.StringArray.encodeEnum(&value, buf, oid_pos), 1329 + .@"enum", .enum_literal => try StringArray.encodeEnum(&value, buf, oid_pos), 1334 1330 .array => try bindSlice(oid, &value, buf, format_pos), 1335 1331 else => compileHaltBindError(T), 1336 1332 } ··· 1368 1364 1369 1365 view.writeIntBig(u16, @intCast(oids.len)); 1370 1366 for (oids) |oid| { 1371 - view.write(Types.resultEncodingFor(oid)); 1367 + view.write(resultEncodingFor(oid)); 1372 1368 } 1373 1369 } 1374 1370 ··· 1378 1374 1379 1375 const t = lib.testing; 1380 1376 test "UUID: toString" { 1381 - try t.expectError(error.InvalidUUID, Types.UUID.toString(&.{ 73, 190, 142, 9, 170, 250, 176, 16, 73, 21 })); 1377 + try t.expectError(error.InvalidUUID, UUID.toString(&.{ 73, 190, 142, 9, 170, 250, 176, 16, 73, 21 })); 1382 1378 1383 - const s = try Types.UUID.toString(&.{ 183, 204, 40, 47, 236, 67, 73, 190, 142, 9, 170, 250, 176, 16, 73, 21 }); 1379 + const s = try UUID.toString(&.{ 183, 204, 40, 47, 236, 67, 73, 190, 142, 9, 170, 250, 176, 16, 73, 21 }); 1384 1380 try t.expectString("b7cc282f-ec43-49be-8e09-aafab0104915", &s); 1385 1381 } 1386 1382 1387 1383 test "UUID: toBytes" { 1388 - try t.expectError(error.InvalidUUID, Types.UUID.toBytes("")); 1384 + try t.expectError(error.InvalidUUID, UUID.toBytes("")); 1389 1385 1390 1386 { 1391 - const s = try Types.UUID.toBytes("166B4751-D702-4FB9-9A2A-CD6B69ED18D6"); 1387 + const s = try UUID.toBytes("166B4751-D702-4FB9-9A2A-CD6B69ED18D6"); 1392 1388 try t.expectSlice(u8, &.{ 22, 107, 71, 81, 215, 2, 79, 185, 154, 42, 205, 107, 105, 237, 24, 214 }, &s); 1393 1389 } 1394 1390 1395 1391 { 1396 - const s = try Types.UUID.toBytes("166b4751-d702-4fb9-9a2a-cd6b69ed18d7"); 1392 + const s = try UUID.toBytes("166b4751-d702-4fb9-9a2a-cd6b69ed18d7"); 1397 1393 try t.expectSlice(u8, &.{ 22, 107, 71, 81, 215, 2, 79, 185, 154, 42, 205, 107, 105, 237, 24, 215 }, &s); 1398 1394 } 1399 1395 }