Skip to content

Commit f44de16

Browse files
committed
fix U/Int8 encoding + remove char, bpchar U/Int8 decoding
1 parent 58f02e8 commit f44de16

File tree

3 files changed

+31
-34
lines changed

3 files changed

+31
-34
lines changed

Sources/PostgreSQL/Data/PostgreSQLData+FixedWidthInteger.swift

+26-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ extension FixedWidthInteger {
55
case .binary(let value):
66
let i: Self?
77
switch data.type {
8-
case .char, .bpchar: i = value.as(Int8.self, default: 0).bigEndian.cast(to: Self.self)
9-
case .int2: i = value.as(Int16.self, default: 0).bigEndian.cast(to: Self.self)
10-
case .int4, .oid, .regproc: i = value.as(Int32.self, default: 0).bigEndian.cast(to: Self.self)
11-
case .int8: i = value.as(Int64.self, default: 0).bigEndian.cast(to: Self.self)
8+
case .int2:
9+
guard value.count == 2 else {
10+
throw PostgreSQLError.decode(self, from: data)
11+
}
12+
i = value.as(Int16.self, default: 0).bigEndian.cast(to: Self.self)
13+
case .int4, .oid, .regproc:
14+
guard value.count == 4 else {
15+
throw PostgreSQLError.decode(self, from: data)
16+
}
17+
i = value.as(Int32.self, default: 0).bigEndian.cast(to: Self.self)
18+
case .int8:
19+
guard value.count == 8 else {
20+
throw PostgreSQLError.decode(self, from: data)
21+
}
22+
i = value.as(Int64.self, default: 0).bigEndian.cast(to: Self.self)
1223
default: throw PostgreSQLError.decode(self, from: data)
1324
}
1425
guard let value = i else {
@@ -36,7 +47,17 @@ extension FixedWidthInteger {
3647
public func convertToPostgreSQLData() throws -> PostgreSQLData {
3748
let type: PostgreSQLDataFormat
3849
switch Self.bitWidth {
39-
case 8: type = .char
50+
case 8:
51+
let data: PostgreSQLData?
52+
if Self.isSigned {
53+
data = try cast(to: Int16.self)?.convertToPostgreSQLData()
54+
} else {
55+
data = try cast(to: UInt16.self)?.convertToPostgreSQLData()
56+
}
57+
guard let d = data else {
58+
throw PostgreSQLError(identifier: "singleByteInt", reason: "Could not convert \(Self.self) to PostgreSQLData: \(self)")
59+
}
60+
return d
4061
case 16: type = .int2
4162
case 32: type = .int4
4263
case 64: type = .int8

Sources/PostgreSQL/Utilities/Utilities.swift

-24
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,6 @@ extension ByteBuffer {
6767
}
6868
}
6969

70-
public struct Char: Codable, CustomStringConvertible {
71-
public let value: UInt8
72-
public var description: String {
73-
if let string = String(data: Data([value]), encoding: .utf8) {
74-
return "\"" + string + "\""
75-
} else {
76-
return "\\" + value.description
77-
}
78-
}
79-
public init(_ value: UInt8) {
80-
self.value = value
81-
}
82-
83-
public init(from decoder: Decoder) throws {
84-
let single = try decoder.singleValueContainer()
85-
self.value = try single.decode(UInt8.self)
86-
}
87-
88-
public func encode(to encoder: Encoder) throws {
89-
var single = encoder.singleValueContainer()
90-
try single.encode(value)
91-
}
92-
}
93-
9470
public enum Regproc: PostgreSQLDataConvertible, Codable {
9571
public func convertToPostgreSQLData() throws -> PostgreSQLData {
9672
switch self {

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ class PostgreSQLConnectionTests: XCTestCase {
6363
var typowner: UInt32
6464
var typlen: Int16
6565
var typbyval: Bool
66-
var typtype: Char
67-
var typcategory: Char
66+
var typtype: String
67+
var typcategory: String
6868
var typispreferred: Bool
6969
var typisdefined: Bool
70-
var typdelim: Char
70+
var typdelim: String
7171
var typrelid: UInt32
7272
var typelem: UInt32
7373
var typarray: UInt32
@@ -78,8 +78,8 @@ class PostgreSQLConnectionTests: XCTestCase {
7878
var typmodin: Regproc
7979
var typmodout: Regproc
8080
var typanalyze: Regproc
81-
var typalign: Char
82-
var typstorage: Char
81+
var typalign: String
82+
var typstorage: String
8383
var typnotnull: Bool
8484
var typbasetype: UInt32
8585
var typtypmod: Int

0 commit comments

Comments
 (0)