Skip to content

Commit 5a18b1c

Browse files
committed
Use Format when decoding
1 parent 290f3c1 commit 5a18b1c

32 files changed

+202
-112
lines changed

Sources/PostgresNIO/Connection/PostgresConnection+Database.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ extension PostgresConnection: PostgresDatabase {
2323
dataType: PostgresDataType(UInt32(column.dataType.rawValue)),
2424
dataTypeSize: column.dataTypeSize,
2525
dataTypeModifier: column.dataTypeModifier,
26-
formatCode: .init(psqlFormatCode: column.formatCode)
26+
formatCode: .init(psqlFormatCode: column.format)
2727
)
2828
}
2929

Sources/PostgresNIO/Connection/PostgresDatabase+PreparedQuery.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public struct PreparedQuery {
4343
dataType: PostgresDataType(UInt32(column.dataType.rawValue)),
4444
dataTypeSize: column.dataTypeSize,
4545
dataTypeModifier: column.dataTypeModifier,
46-
formatCode: .init(psqlFormatCode: column.formatCode)
46+
formatCode: .init(psqlFormatCode: column.format)
4747
)
4848
}
4949

Sources/PostgresNIO/New/Connection State Machine/ExtendedQueryStateMachine.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ struct ExtendedQueryStateMachine {
157157

158158
return self.avoidingStateMachineCoW { state -> Action in
159159
let row = dataRow.columns.enumerated().map { (index, buffer) in
160-
PSQLData(bytes: buffer, dataType: columns[index].dataType)
160+
PSQLData(bytes: buffer, dataType: columns[index].dataType, format: columns[index].format)
161161
}
162162
buffer.append(row)
163163
state = .bufferingRows(columns, buffer, readOnEmpty: readOnEmpty)
@@ -174,7 +174,7 @@ struct ExtendedQueryStateMachine {
174174
return self.avoidingStateMachineCoW { state -> Action in
175175
precondition(buffer.isEmpty, "Expected the buffer to be empty")
176176
let row = dataRow.columns.enumerated().map { (index, buffer) in
177-
PSQLData(bytes: buffer, dataType: columns[index].dataType)
177+
PSQLData(bytes: buffer, dataType: columns[index].dataType, format: columns[index].format)
178178
}
179179

180180
state = .bufferingRows(columns, buffer, readOnEmpty: false)

Sources/PostgresNIO/New/Data/Array+PSQLCodable.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ extension Array: PSQLEncodable where Element: PSQLArrayElement {
7272
Element.psqlArrayType
7373
}
7474

75+
var psqlFormat: PSQLFormat {
76+
.binary
77+
}
78+
7579
func encode(into buffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
7680
// 0 if empty, 1 if not
7781
buffer.writeInteger(self.isEmpty ? 0 : 1, as: UInt32.self)
@@ -98,7 +102,7 @@ extension Array: PSQLEncodable where Element: PSQLArrayElement {
98102

99103
extension Array: PSQLDecodable where Element: PSQLArrayElement {
100104

101-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Array<Element> {
105+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Array<Element> {
102106
guard let isNotEmpty = buffer.readInteger(as: Int32.self), (0...1).contains(isNotEmpty) else {
103107
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
104108
}
@@ -135,7 +139,7 @@ extension Array: PSQLDecodable where Element: PSQLArrayElement {
135139
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
136140
}
137141

138-
let element = try Element.decode(from: &elementBuffer, type: elementType, context: context)
142+
let element = try Element.decode(from: &elementBuffer, type: elementType, format: format, context: context)
139143

140144
result.append(element)
141145
}

Sources/PostgresNIO/New/Data/Bool+PSQLCodable.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ extension Bool: PSQLCodable {
33
.bool
44
}
55

6-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Bool {
6+
var psqlFormat: PSQLFormat {
7+
.binary
8+
}
9+
10+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Bool {
711
guard type == .bool, buffer.readableBytes == 1 else {
812
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
913
}

Sources/PostgresNIO/New/Data/Bytes+PSQLCodable.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ extension PSQLEncodable where Self: Sequence, Self.Element == UInt8 {
66
.bytea
77
}
88

9+
var psqlFormat: PSQLFormat {
10+
.binary
11+
}
12+
913
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
1014
byteBuffer.writeBytes(self)
1115
}
@@ -16,12 +20,16 @@ extension ByteBuffer: PSQLCodable {
1620
.bytea
1721
}
1822

23+
var psqlFormat: PSQLFormat {
24+
.binary
25+
}
26+
1927
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
2028
var copyOfSelf = self // dirty hack
2129
byteBuffer.writeBuffer(&copyOfSelf)
2230
}
2331

24-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
32+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
2533
return buffer
2634
}
2735
}
@@ -30,12 +38,16 @@ extension Data: PSQLCodable {
3038
var psqlType: PSQLDataType {
3139
.bytea
3240
}
33-
41+
42+
var psqlFormat: PSQLFormat {
43+
.binary
44+
}
45+
3446
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
3547
byteBuffer.writeBytes(self)
3648
}
37-
38-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
49+
50+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
3951
return buffer.readData(length: buffer.readableBytes, byteTransferStrategy: .automatic)!
4052
}
4153
}

Sources/PostgresNIO/New/Data/Date+PSQLCodable.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ extension Date: PSQLCodable {
55
.timestamptz
66
}
77

8-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
8+
var psqlFormat: PSQLFormat {
9+
.binary
10+
}
11+
12+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
913
switch type {
1014
case .timestamp, .timestamptz:
1115
guard buffer.readableBytes == 8, let microseconds = buffer.readInteger(as: Int64.self) else {

Sources/PostgresNIO/New/Data/Float+PSQLCodable.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ extension Float: PSQLCodable {
33
.float4
44
}
55

6-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Float {
6+
var psqlFormat: PSQLFormat {
7+
.binary
8+
}
9+
10+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Float {
711
switch type {
812
case .float4:
913
guard buffer.readableBytes == 4, let float = buffer.readFloat() else {
@@ -30,7 +34,11 @@ extension Double: PSQLCodable {
3034
.float8
3135
}
3236

33-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Double {
37+
var psqlFormat: PSQLFormat {
38+
.binary
39+
}
40+
41+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Double {
3442
switch type {
3543
case .float4:
3644
guard buffer.readableBytes == 4, let float = buffer.readFloat() else {

Sources/PostgresNIO/New/Data/Int+PSQLCodable.swift

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ extension UInt8: PSQLCodable {
33
.char
44
}
55

6+
var psqlFormat: PSQLFormat {
7+
.binary
8+
}
9+
610
// decoding
7-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
11+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
812
switch type {
913
case .bpchar, .char:
1014
guard buffer.readableBytes == 1, let value = buffer.readInteger(as: UInt8.self) else {
@@ -29,8 +33,12 @@ extension Int16: PSQLCodable {
2933
.int2
3034
}
3135

36+
var psqlFormat: PSQLFormat {
37+
.binary
38+
}
39+
3240
// decoding
33-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
41+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
3442
switch type {
3543
case .int2:
3644
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
@@ -53,8 +61,12 @@ extension Int32: PSQLCodable {
5361
.int4
5462
}
5563

64+
var psqlFormat: PSQLFormat {
65+
.binary
66+
}
67+
5668
// decoding
57-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
69+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
5870
switch type {
5971
case .int2:
6072
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
@@ -84,8 +96,12 @@ extension Int64: PSQLCodable {
8496
.int8
8597
}
8698

99+
var psqlFormat: PSQLFormat {
100+
.binary
101+
}
102+
87103
// decoding
88-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
104+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
89105
switch type {
90106
case .int2:
91107
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {
@@ -128,8 +144,12 @@ extension Int: PSQLCodable {
128144
}
129145
}
130146

147+
var psqlFormat: PSQLFormat {
148+
.binary
149+
}
150+
131151
// decoding
132-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
152+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
133153
switch type {
134154
case .int2:
135155
guard buffer.readableBytes == 2, let value = buffer.readInteger(as: Int16.self) else {

Sources/PostgresNIO/New/Data/JSON+PSQLCodable.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ extension PSQLCodable where Self: Codable {
99
.jsonb
1010
}
1111

12-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
12+
var psqlFormat: PSQLFormat {
13+
.binary
14+
}
15+
16+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
1317
switch type {
1418
case .jsonb:
1519
guard JSONBVersionByte == buffer.readInteger(as: UInt8.self) else {

Sources/PostgresNIO/New/Data/Optional+PSQLCodable.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extension Optional: PSQLDecodable where Wrapped: PSQLDecodable {
2-
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Optional<Wrapped> {
2+
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Optional<Wrapped> {
33
preconditionFailure("This code path should never be hit.")
44
// The code path for decoding an optional should be:
55
// -> PSQLData.decode(as: String?.self)
@@ -18,6 +18,15 @@ extension Optional: PSQLEncodable where Wrapped: PSQLEncodable {
1818
}
1919
}
2020

21+
var psqlFormat: PSQLFormat {
22+
switch self {
23+
case .some(let value):
24+
return value.psqlFormat
25+
case .none:
26+
return .binary
27+
}
28+
}
29+
2130
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws {
2231
preconditionFailure("Should never be hit, since `encodeRaw` is implemented.")
2332
}

Sources/PostgresNIO/New/Data/RawRepresentable+PSQLCodable.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ extension PSQLCodable where Self: RawRepresentable, RawValue: PSQLCodable {
33
self.rawValue.psqlType
44
}
55

6-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self {
7-
guard let rawValue = try? RawValue.decode(from: &buffer, type: type, context: context),
6+
var psqlFormat: PSQLFormat {
7+
self.rawValue.psqlFormat
8+
}
9+
10+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self {
11+
guard let rawValue = try? RawValue.decode(from: &buffer, type: type, format: format, context: context),
812
let selfValue = Self.init(rawValue: rawValue) else {
913
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
1014
}

Sources/PostgresNIO/New/Data/String+PSQLCodable.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ extension String: PSQLCodable {
55
.text
66
}
77

8+
var psqlFormat: PSQLFormat {
9+
.binary
10+
}
11+
812
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
913
byteBuffer.writeString(self)
1014
}
1115

12-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> String {
16+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> String {
1317
switch type {
1418
case .varchar, .text, .name:
1519
// we can force unwrap here, since this method only fails if there are not enough
1620
// bytes available.
1721
return buffer.readString(length: buffer.readableBytes)!
1822
case .uuid:
19-
guard let uuid = try? UUID.decode(from: &buffer, type: .uuid, context: context) else {
23+
guard let uuid = try? UUID.decode(from: &buffer, type: .uuid, format: format, context: context) else {
2024
throw PSQLCastingError.failure(targetType: Self.self, type: type, postgresData: buffer, context: context)
2125
}
2226
return uuid.uuidString

Sources/PostgresNIO/New/Data/UUID+PSQLCodable.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ extension UUID: PSQLCodable {
77
.uuid
88
}
99

10+
var psqlFormat: PSQLFormat {
11+
.binary
12+
}
13+
1014
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) {
1115
let uuid = self.uuid
1216
byteBuffer.writeBytes([
@@ -17,7 +21,7 @@ extension UUID: PSQLCodable {
1721
])
1822
}
1923

20-
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> UUID {
24+
static func decode(from buffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> UUID {
2125
switch type {
2226
case .uuid:
2327
guard let uuid = buffer.readUUID() else {

Sources/PostgresNIO/New/Messages/RowDescription.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ extension PSQLBackendMessage {
2323
/// The type modifier (see pg_attribute.atttypmod). The meaning of the modifier is type-specific.
2424
var dataTypeModifier: Int32
2525

26-
/// The format code being used for the field. Currently will be zero (text) or one (binary). In a RowDescription returned
27-
/// from the statement variant of Describe, the format code is not yet known and will always be zero.
28-
var formatCode: PSQLFormatCode
26+
/// The format being used for the field. Currently will text or binary. In a RowDescription returned
27+
/// from the statement variant of Describe, the format code is not yet known and will always be text.
28+
var format: PSQLFormat
2929
}
3030

3131
static func decode(from buffer: inout ByteBuffer) throws -> Self {
@@ -53,8 +53,8 @@ extension PSQLBackendMessage {
5353
let dataTypeModifier = buffer.readInteger(as: Int32.self)!
5454
let formatCodeInt16 = buffer.readInteger(as: Int16.self)!
5555

56-
guard let formatCode = PSQLFormatCode(rawValue: formatCodeInt16) else {
57-
throw PartialDecodingError.valueNotRawRepresentable(value: formatCodeInt16, asType: PSQLFormatCode.self)
56+
guard let format = PSQLFormat(rawValue: formatCodeInt16) else {
57+
throw PartialDecodingError.valueNotRawRepresentable(value: formatCodeInt16, asType: PSQLFormat.self)
5858
}
5959

6060
let field = Column(
@@ -64,7 +64,7 @@ extension PSQLBackendMessage {
6464
dataType: dataType,
6565
dataTypeSize: dataTypeSize,
6666
dataTypeModifier: dataTypeModifier,
67-
formatCode: formatCode)
67+
format: format)
6868

6969
result.append(field)
7070
}

Sources/PostgresNIO/New/PSQLCodable.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ protocol PSQLEncodable {
33
/// identifies the data type that we will encode into `byteBuffer` in `encode`
44
var psqlType: PSQLDataType { get }
55

6+
/// identifies the postgres format that is used to encode the value into `byteBuffer` in `encode`
7+
var psqlFormat: PSQLFormat { get }
8+
69
/// Encode the entity into the `byteBuffer` in Postgres binary format, without setting
710
/// the byte count. This method is called from the default `encodeRaw` implementation.
811
func encode(into byteBuffer: inout ByteBuffer, context: PSQLEncodingContext) throws
@@ -17,7 +20,7 @@ protocol PSQLEncodable {
1720
protocol PSQLDecodable {
1821

1922
/// decode an entity from the `byteBuffer` in postgres binary format
20-
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, context: PSQLDecodingContext) throws -> Self
23+
static func decode(from byteBuffer: inout ByteBuffer, type: PSQLDataType, format: PSQLFormat, context: PSQLDecodingContext) throws -> Self
2124
}
2225

2326
/// A type that can be encoded into and decoded from a postgres binary format

0 commit comments

Comments
 (0)