@@ -8,130 +8,144 @@ public struct PostgreSQLRowEncoder {
8
8
/// - parameters:
9
9
/// - encodable: Item to encode.
10
10
/// - tableOID: Optional table OID to use when encoding.
11
- public func encode< E> ( _ encodable: E , tableOID: UInt32 ? = nil ) throws -> [ PostgreSQLColumn : PostgreSQLData ]
11
+ public func encode< E> ( _ encodable: E , tableOID: UInt32 = 0 ) throws -> [ PostgreSQLColumn : PostgreSQLData ]
12
12
where E: Encodable
13
13
{
14
- let encoder = _PostgreSQLRowEncoder ( tableOID: tableOID)
15
- try encodable. encode ( to: encoder)
16
- return encoder. data
17
- }
18
- }
19
-
20
-
21
- // MARK: Private
22
-
23
- private final class _PostgreSQLRowEncoder : Encoder {
24
- let codingPath : [ CodingKey ] = [ ]
25
- let userInfo : [ CodingUserInfoKey : Any ] = [ : ]
26
- var data : [ PostgreSQLColumn : PostgreSQLData ]
27
- let tableOID : UInt32 ?
28
- init ( tableOID: UInt32 ? ) {
29
- self . data = [ : ]
30
- self . tableOID = tableOID
31
- }
32
-
33
- func container< Key> ( keyedBy type: Key . Type ) -> KeyedEncodingContainer < Key > where Key : CodingKey {
34
- let container = _PostgreSQLRowKeyedEncodingContainer < Key > ( encoder: self )
35
- return KeyedEncodingContainer ( container)
36
- }
37
-
38
- func unkeyedContainer( ) -> UnkeyedEncodingContainer {
39
- unsupported ( )
40
- }
41
-
42
- func singleValueContainer( ) -> SingleValueEncodingContainer {
43
- unsupported ( )
44
- }
45
-
46
- }
47
-
48
- private struct _PostgreSQLRowKeyedEncodingContainer < K> : KeyedEncodingContainerProtocol
49
- where K: CodingKey
50
- {
51
- var codingPath : [ CodingKey ]
52
- let encoder : _PostgreSQLRowEncoder
53
- init ( encoder: _PostgreSQLRowEncoder ) {
54
- self . encoder = encoder
55
- self . codingPath = [ ]
56
- }
57
-
58
- func set( _ key: CodingKey , to value: PostgreSQLDataConvertible ) throws {
59
- let col = PostgreSQLColumn ( tableOID: encoder. tableOID ?? 0 , name: key. stringValue)
60
- self . encoder. data [ col] = try value. convertToPostgreSQLData ( )
61
- }
62
-
63
- mutating func encodeNil( forKey key: K ) throws { try set ( key, to: PostgreSQLData ( null: . void) ) }
64
- mutating func encode( _ value: Bool , forKey key: K ) throws { try set ( key, to: value) }
65
- mutating func encode( _ value: Int , forKey key: K ) throws { try set ( key, to: value) }
66
- mutating func encode( _ value: Int16 , forKey key: K ) throws { try set ( key, to: value) }
67
- mutating func encode( _ value: Int32 , forKey key: K ) throws { try set ( key, to: value) }
68
- mutating func encode( _ value: Int64 , forKey key: K ) throws { try set ( key, to: value) }
69
- mutating func encode( _ value: UInt , forKey key: K ) throws { try set ( key, to: value) }
70
- mutating func encode( _ value: UInt8 , forKey key: K ) throws { try set ( key, to: value) }
71
- mutating func encode( _ value: UInt16 , forKey key: K ) throws { try set ( key, to: value) }
72
- mutating func encode( _ value: UInt32 , forKey key: K ) throws { try set ( key, to: value) }
73
- mutating func encode( _ value: UInt64 , forKey key: K ) throws { try set ( key, to: value) }
74
- mutating func encode( _ value: Double , forKey key: K ) throws { try set ( key, to: value) }
75
- mutating func encode( _ value: Float , forKey key: K ) throws { try set ( key, to: value) }
76
- mutating func encode( _ value: String , forKey key: K ) throws { try set ( key, to: value) }
77
- mutating func encodeIfPresent( _ value: Bool ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
78
- mutating func encodeIfPresent( _ value: Int ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
79
- mutating func encodeIfPresent( _ value: Int16 ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
80
- mutating func encodeIfPresent( _ value: Int32 ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
81
- mutating func encodeIfPresent( _ value: Int64 ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
82
- mutating func encodeIfPresent( _ value: UInt ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
83
- mutating func encodeIfPresent( _ value: UInt8 ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
84
- mutating func encodeIfPresent( _ value: UInt16 ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
85
- mutating func encodeIfPresent( _ value: UInt32 ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
86
- mutating func encodeIfPresent( _ value: UInt64 ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
87
- mutating func encodeIfPresent( _ value: Double ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
88
- mutating func encodeIfPresent( _ value: Float ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
89
- mutating func encodeIfPresent( _ value: String ? , forKey key: K ) throws { try _encodeIfPresent ( value, forKey: key) }
90
- mutating func encodeIfPresent< T> ( _ value: T ? , forKey key: K ) throws where T: Encodable { try _encodeIfPresent ( value, forKey: key) }
91
-
92
- mutating func superEncoder( ) -> Encoder { return encoder }
93
- mutating func superEncoder( forKey key: K ) -> Encoder { return encoder }
94
- mutating func _encodeIfPresent< T> ( _ value: T ? , forKey key: K ) throws where T : Encodable {
95
- if let value = value {
96
- try encode ( value, forKey: key)
97
- } else {
98
- if let convertibleType = T . self as? PostgreSQLDataConvertible . Type {
99
- try set ( key, to: PostgreSQLData ( null: convertibleType. postgreSQLDataType) )
100
- } else {
101
- try encodeNil ( forKey: key)
14
+ let all = try CodableDataEncoder ( ) . encode ( encodable)
15
+ print ( all)
16
+ switch all {
17
+ case . keyed( let keyed) :
18
+ var row : [ PostgreSQLColumn : PostgreSQLData ] = [ : ]
19
+ for (key, val) in keyed {
20
+ let col = PostgreSQLColumn ( tableOID: tableOID, name: key)
21
+ if let unwrapped = val. unwrapped {
22
+ print ( type ( of: unwrapped) )
23
+ row [ col] = try PostgreSQLDataEncoder ( ) . encode ( unwrapped)
24
+ } else {
25
+ row [ col] = PostgreSQLData ( null: . null)
26
+ }
102
27
}
28
+ return row
29
+ default : throw PostgreSQLError ( identifier: " rowEncoder " , reason: " Unsupported row encode type: \( all) . " )
103
30
}
104
31
}
105
-
106
- mutating func encode< T> ( _ value: T , forKey key: K ) throws where T: Encodable {
107
- guard let convertible = value as? PostgreSQLDataConvertible else {
108
- let type = Swift . type ( of: value)
109
- throw PostgreSQLError (
110
- identifier: " convertible " ,
111
- reason: " Unsupported encodable type: \( type) " ,
112
- suggestedFixes: [
113
- " Conform \( type) to PostgreSQLDataCustomConvertible "
114
- ]
115
- )
116
- }
117
- try set ( key, to: convertible)
118
- }
119
- mutating func nestedContainer< NestedKey> ( keyedBy keyType: NestedKey . Type , forKey key: K ) -> KeyedEncodingContainer < NestedKey > where NestedKey : CodingKey {
120
- return encoder. container ( keyedBy: NestedKey . self)
121
- }
122
-
123
- mutating func nestedUnkeyedContainer( forKey key: K ) -> UnkeyedEncodingContainer {
124
- return encoder. unkeyedContainer ( )
125
- }
126
32
}
127
33
128
- private func unsupported( ) -> Never {
129
- fatalError ( """
130
- PostgreSQL rows only support a flat, keyed structure `[String: T]`.
131
34
132
- Query data must be an encodable dictionary, struct, or class.
133
-
134
- You can also conform nested types to `PostgreSQLJSONType` or `PostgreSQLArrayType`. (Nested types must be `PostgreSQLDataCustomConvertible`.)
135
- """ )
136
- }
35
+ // MARK: Private
137
36
37
+ //private final class _PostgreSQLRowEncoder: Encoder {
38
+ // let codingPath: [CodingKey] = []
39
+ // let userInfo: [CodingUserInfoKey: Any] = [:]
40
+ // var data: [PostgreSQLColumn: PostgreSQLData]
41
+ // let tableOID: UInt32?
42
+ // init(tableOID: UInt32?) {
43
+ // self.data = [:]
44
+ // self.tableOID = tableOID
45
+ // }
46
+ //
47
+ // func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
48
+ // let container = _PostgreSQLRowKeyedEncodingContainer<Key>(encoder: self)
49
+ // return KeyedEncodingContainer(container)
50
+ // }
51
+ //
52
+ // func unkeyedContainer() -> UnkeyedEncodingContainer {
53
+ // unsupported()
54
+ // }
55
+ //
56
+ // func singleValueContainer() -> SingleValueEncodingContainer {
57
+ // unsupported()
58
+ // }
59
+ //
60
+ //}
61
+ //
62
+ //private struct _PostgreSQLRowKeyedEncodingContainer<K>: KeyedEncodingContainerProtocol
63
+ // where K: CodingKey
64
+ //{
65
+ // var codingPath: [CodingKey]
66
+ // let encoder: _PostgreSQLRowEncoder
67
+ // init(encoder: _PostgreSQLRowEncoder) {
68
+ // self.encoder = encoder
69
+ // self.codingPath = []
70
+ // }
71
+ //
72
+ // func set(_ key: CodingKey, to value: PostgreSQLDataConvertible) throws {
73
+ // let col = PostgreSQLColumn(tableOID: encoder.tableOID ?? 0, name: key.stringValue)
74
+ // self.encoder.data[col] = try value.convertToPostgreSQLData()
75
+ // }
76
+ //
77
+ // mutating func encodeNil(forKey key: K) throws { try set(key, to: PostgreSQLData(null: .void)) }
78
+ // mutating func encode(_ value: Bool, forKey key: K) throws { try set(key, to: value) }
79
+ // mutating func encode(_ value: Int, forKey key: K) throws { try set(key, to: value) }
80
+ // mutating func encode(_ value: Int16, forKey key: K) throws { try set(key, to: value) }
81
+ // mutating func encode(_ value: Int32, forKey key: K) throws { try set(key, to: value) }
82
+ // mutating func encode(_ value: Int64, forKey key: K) throws { try set(key, to: value) }
83
+ // mutating func encode(_ value: UInt, forKey key: K) throws { try set(key, to: value) }
84
+ // mutating func encode(_ value: UInt8, forKey key: K) throws { try set(key, to: value) }
85
+ // mutating func encode(_ value: UInt16, forKey key: K) throws { try set(key, to: value) }
86
+ // mutating func encode(_ value: UInt32, forKey key: K) throws { try set(key, to: value) }
87
+ // mutating func encode(_ value: UInt64, forKey key: K) throws { try set(key, to: value) }
88
+ // mutating func encode(_ value: Double, forKey key: K) throws { try set(key, to: value) }
89
+ // mutating func encode(_ value: Float, forKey key: K) throws { try set(key, to: value) }
90
+ // mutating func encode(_ value: String, forKey key: K) throws { try set(key, to: value) }
91
+ // mutating func encodeIfPresent(_ value: Bool?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
92
+ // mutating func encodeIfPresent(_ value: Int?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
93
+ // mutating func encodeIfPresent(_ value: Int16?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
94
+ // mutating func encodeIfPresent(_ value: Int32?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
95
+ // mutating func encodeIfPresent(_ value: Int64?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
96
+ // mutating func encodeIfPresent(_ value: UInt?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
97
+ // mutating func encodeIfPresent(_ value: UInt8?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
98
+ // mutating func encodeIfPresent(_ value: UInt16?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
99
+ // mutating func encodeIfPresent(_ value: UInt32?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
100
+ // mutating func encodeIfPresent(_ value: UInt64?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
101
+ // mutating func encodeIfPresent(_ value: Double?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
102
+ // mutating func encodeIfPresent(_ value: Float?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
103
+ // mutating func encodeIfPresent(_ value: String?, forKey key: K) throws { try _encodeIfPresent(value, forKey: key) }
104
+ // mutating func encodeIfPresent<T>(_ value: T?, forKey key: K) throws where T: Encodable { try _encodeIfPresent(value, forKey: key) }
105
+ //
106
+ // mutating func superEncoder() -> Encoder { return encoder }
107
+ // mutating func superEncoder(forKey key: K) -> Encoder { return encoder }
108
+ // mutating func _encodeIfPresent<T>(_ value: T?, forKey key: K) throws where T : Encodable {
109
+ // if let value = value {
110
+ // try encode(value, forKey: key)
111
+ // } else {
112
+ // if let convertibleType = T.self as? PostgreSQLDataConvertible.Type {
113
+ // try set(key, to: PostgreSQLData(null: convertibleType.postgreSQLDataType))
114
+ // } else {
115
+ // try encodeNil(forKey: key)
116
+ // }
117
+ // }
118
+ // }
119
+ //
120
+ // mutating func encode<T>(_ value: T, forKey key: K) throws where T: Encodable {
121
+ // guard let convertible = value as? PostgreSQLDataConvertible else {
122
+ // let type = Swift.type(of: value)
123
+ // throw PostgreSQLError(
124
+ // identifier: "convertible",
125
+ // reason: "Unsupported encodable type: \(type)",
126
+ // suggestedFixes: [
127
+ // "Conform \(type) to PostgreSQLDataCustomConvertible"
128
+ // ]
129
+ // )
130
+ // }
131
+ // try set(key, to: convertible)
132
+ // }
133
+ // mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: K) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
134
+ // return encoder.container(keyedBy: NestedKey.self)
135
+ // }
136
+ //
137
+ // mutating func nestedUnkeyedContainer(forKey key: K) -> UnkeyedEncodingContainer {
138
+ // return encoder.unkeyedContainer()
139
+ // }
140
+ //}
141
+ //
142
+ //private func unsupported() -> Never {
143
+ // fatalError("""
144
+ // PostgreSQL rows only support a flat, keyed structure `[String: T]`.
145
+ //
146
+ // Query data must be an encodable dictionary, struct, or class.
147
+ //
148
+ // You can also conform nested types to `PostgreSQLJSONType` or `PostgreSQLArrayType`. (Nested types must be `PostgreSQLDataCustomConvertible`.)
149
+ // """)
150
+ //}
151
+ //
0 commit comments