@@ -44,7 +44,7 @@ struct PostgreSQLRowDescriptionField: Decodable {
44
44
/// Currently will be zero (text) or one (binary).
45
45
/// In a RowDescription returned from the statement variant of Describe,
46
46
/// the format code is not yet known and will always be zero.
47
- var formatCode : Int16
47
+ var formatCode : PostgreSQLFormatCode
48
48
49
49
/// See Decodable.decode
50
50
init ( from decoder: Decoder ) throws {
@@ -55,13 +55,40 @@ struct PostgreSQLRowDescriptionField: Decodable {
55
55
dataType = try single. decode ( PostgreSQLDataType . self)
56
56
dataTypeSize = try single. decode ( Int16 . self)
57
57
dataTypeModifier = try single. decode ( Int32 . self)
58
- formatCode = try single. decode ( Int16 . self)
58
+ formatCode = try single. decode ( PostgreSQLFormatCode . self)
59
+ }
60
+ }
61
+
62
+ /// The format code being used for the field.
63
+ /// Currently will be zero (text) or one (binary).
64
+ /// In a RowDescription returned from the statement variant of Describe,
65
+ /// the format code is not yet known and will always be zero.
66
+ enum PostgreSQLFormatCode : Int16 , Decodable {
67
+ case text = 0
68
+ case binary = 1
69
+
70
+ /// See Decodable.decode
71
+ init ( from decoder: Decoder ) throws {
72
+ let single = try decoder. singleValueContainer ( )
73
+ let code = try single. decode ( Int16 . self)
74
+ guard let type = PostgreSQLFormatCode . make ( code) else {
75
+ throw PostgreSQLError (
76
+ identifier: " formatCode " ,
77
+ reason: " Unsupported format code: \( code) "
78
+ )
79
+ }
80
+ self = type
81
+ }
82
+
83
+ /// Static make (non-failable)
84
+ private static func make( _ code: Int16 ) -> PostgreSQLFormatCode ? {
85
+ return PostgreSQLFormatCode ( rawValue: code)
59
86
}
60
87
}
61
88
62
89
/// The data type's raw object ID.
63
90
/// Use `select * from pg_type where oid in (<idhere>);` to lookup more information.
64
- enum PostgreSQLDataType : Int32 , Decodable , Equatable {
91
+ enum PostgreSQLDataType : Int32 , Decodable {
65
92
case bool = 16
66
93
case char = 18
67
94
case name = 19
@@ -79,13 +106,14 @@ enum PostgreSQLDataType: Int32, Decodable, Equatable {
79
106
let objectID = try single. decode ( Int32 . self)
80
107
guard let type = PostgreSQLDataType . make ( objectID) else {
81
108
throw PostgreSQLError (
82
- identifier: " unsupportedColumnType " ,
109
+ identifier: " dataType " ,
83
110
reason: " Unsupported data type: \( objectID) "
84
111
)
85
112
}
86
113
self = type
87
114
}
88
115
116
+ /// Static make (non-failable)
89
117
private static func make( _ objectID: Int32 ) -> PostgreSQLDataType ? {
90
118
return PostgreSQLDataType ( rawValue: objectID)
91
119
}
0 commit comments