@@ -5,10 +5,21 @@ extension FixedWidthInteger {
5
5
case . binary( let value) :
6
6
let i : Self ?
7
7
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)
12
23
default : throw PostgreSQLError . decode ( self , from: data)
13
24
}
14
25
guard let value = i else {
@@ -36,7 +47,17 @@ extension FixedWidthInteger {
36
47
public func convertToPostgreSQLData( ) throws -> PostgreSQLData {
37
48
let type : PostgreSQLDataFormat
38
49
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
40
61
case 16 : type = . int2
41
62
case 32 : type = . int4
42
63
case 64 : type = . int8
0 commit comments