|
1 |
| -/// Encodes `Encodable` objects to PostgreSQL row data. |
2 |
| -public struct PostgreSQLQueryEncoder { |
3 |
| - /// Creates a new `PostgreSQLRowEncoder`. |
4 |
| - public init() { } |
5 |
| - |
6 |
| - /// Encodes an `Encodable` object to `[String: PostgreSQLQuery.DML.Value]`. |
7 |
| - /// |
8 |
| - /// - parameters: |
9 |
| - /// - encodable: Item to encode. |
10 |
| - public func encode<E>(_ encodable: E) throws -> [String: PostgreSQLQuery.Value] |
11 |
| - where E: Encodable |
12 |
| - { |
13 |
| - let encoder = _Encoder() |
14 |
| - try encodable.encode(to: encoder) |
15 |
| - return encoder.row |
16 |
| - } |
17 |
| - |
18 |
| - // MARK: Private |
19 |
| - |
20 |
| - private final class _Encoder: Encoder { |
21 |
| - let codingPath: [CodingKey] = [] |
22 |
| - var userInfo: [CodingUserInfoKey: Any] = [:] |
23 |
| - var row: [String: PostgreSQLQuery.Value] |
24 |
| - |
25 |
| - init() { |
26 |
| - self.row = [:] |
27 |
| - } |
28 |
| - |
29 |
| - func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey { |
30 |
| - return .init(_KeyedEncodingContainer(encoder: self)) |
31 |
| - } |
32 |
| - |
33 |
| - func unkeyedContainer() -> UnkeyedEncodingContainer { |
34 |
| - fatalError() |
35 |
| - } |
36 |
| - |
37 |
| - func singleValueContainer() -> SingleValueEncodingContainer { |
38 |
| - fatalError() |
39 |
| - } |
40 |
| - } |
41 |
| - |
42 |
| - private struct _KeyedEncodingContainer<Key>: KeyedEncodingContainerProtocol where Key: CodingKey { |
43 |
| - let codingPath: [CodingKey] = [] |
44 |
| - let encoder: _Encoder |
45 |
| - init(encoder: _Encoder) { |
46 |
| - self.encoder = encoder |
47 |
| - } |
48 |
| - |
49 |
| - mutating func encodeNil(forKey key: Key) throws { |
50 |
| - encoder.row[key.stringValue] = .null |
51 |
| - } |
52 |
| - |
53 |
| - mutating func encode<T>(_ encodable: T, forKey key: Key) throws where T : Encodable { |
54 |
| - encoder.row[key.stringValue] = try PostgreSQLValueEncoder().encode(encodable) |
55 |
| - } |
56 |
| - |
57 |
| - mutating func _encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T : Encodable { |
58 |
| - if let value = value { |
59 |
| - try encode(value, forKey: key) |
60 |
| - } else { |
61 |
| - try encodeNil(forKey: key) |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - mutating func encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T : Encodable { try _encodeIfPresent(value, forKey: key)} |
66 |
| - mutating func encodeIfPresent(_ value: Int?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
67 |
| - mutating func encodeIfPresent(_ value: Int8?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
68 |
| - mutating func encodeIfPresent(_ value: Int16?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
69 |
| - mutating func encodeIfPresent(_ value: Int32?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
70 |
| - mutating func encodeIfPresent(_ value: Int64?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
71 |
| - mutating func encodeIfPresent(_ value: UInt?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
72 |
| - mutating func encodeIfPresent(_ value: UInt16?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
73 |
| - mutating func encodeIfPresent(_ value: UInt32?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
74 |
| - mutating func encodeIfPresent(_ value: UInt64?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
75 |
| - mutating func encodeIfPresent(_ value: Double?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
76 |
| - mutating func encodeIfPresent(_ value: Float?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
77 |
| - mutating func encodeIfPresent(_ value: String?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
78 |
| - mutating func encodeIfPresent(_ value: Bool?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
79 |
| - |
80 |
| - mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey { |
81 |
| - fatalError() |
82 |
| - } |
83 |
| - |
84 |
| - mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer { |
85 |
| - fatalError() |
86 |
| - } |
87 |
| - |
88 |
| - mutating func superEncoder() -> Encoder { |
89 |
| - fatalError() |
90 |
| - } |
91 |
| - |
92 |
| - mutating func superEncoder(forKey key: Key) -> Encoder { |
93 |
| - fatalError() |
94 |
| - } |
95 |
| - } |
96 |
| -} |
| 1 | +///// Encodes `Encodable` objects to PostgreSQL row data. |
| 2 | +//public struct PostgreSQLQueryEncoder { |
| 3 | +// /// Creates a new `PostgreSQLRowEncoder`. |
| 4 | +// public init() { } |
| 5 | +// |
| 6 | +// /// Encodes an `Encodable` object to `[String: PostgreSQLQuery.DML.Value]`. |
| 7 | +// /// |
| 8 | +// /// - parameters: |
| 9 | +// /// - encodable: Item to encode. |
| 10 | +// public func encode<E>(_ encodable: E) throws -> [String: PostgreSQLQuery.Value] |
| 11 | +// where E: Encodable |
| 12 | +// { |
| 13 | +// let encoder = _Encoder() |
| 14 | +// try encodable.encode(to: encoder) |
| 15 | +// return encoder.row |
| 16 | +// } |
| 17 | +// |
| 18 | +// // MARK: Private |
| 19 | +// |
| 20 | +// private final class _Encoder: Encoder { |
| 21 | +// let codingPath: [CodingKey] = [] |
| 22 | +// var userInfo: [CodingUserInfoKey: Any] = [:] |
| 23 | +// var row: [String: PostgreSQLQuery.Value] |
| 24 | +// |
| 25 | +// init() { |
| 26 | +// self.row = [:] |
| 27 | +// } |
| 28 | +// |
| 29 | +// func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey { |
| 30 | +// return .init(_KeyedEncodingContainer(encoder: self)) |
| 31 | +// } |
| 32 | +// |
| 33 | +// func unkeyedContainer() -> UnkeyedEncodingContainer { |
| 34 | +// fatalError() |
| 35 | +// } |
| 36 | +// |
| 37 | +// func singleValueContainer() -> SingleValueEncodingContainer { |
| 38 | +// fatalError() |
| 39 | +// } |
| 40 | +// } |
| 41 | +// |
| 42 | +// private struct _KeyedEncodingContainer<Key>: KeyedEncodingContainerProtocol where Key: CodingKey { |
| 43 | +// let codingPath: [CodingKey] = [] |
| 44 | +// let encoder: _Encoder |
| 45 | +// init(encoder: _Encoder) { |
| 46 | +// self.encoder = encoder |
| 47 | +// } |
| 48 | +// |
| 49 | +// mutating func encodeNil(forKey key: Key) throws { |
| 50 | +// encoder.row[key.stringValue] = .null |
| 51 | +// } |
| 52 | +// |
| 53 | +// mutating func encode<T>(_ encodable: T, forKey key: Key) throws where T : Encodable { |
| 54 | +// encoder.row[key.stringValue] = try PostgreSQLValueEncoder().encode(encodable) |
| 55 | +// } |
| 56 | +// |
| 57 | +// mutating func _encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T : Encodable { |
| 58 | +// if let value = value { |
| 59 | +// try encode(value, forKey: key) |
| 60 | +// } else { |
| 61 | +// try encodeNil(forKey: key) |
| 62 | +// } |
| 63 | +// } |
| 64 | +// |
| 65 | +// mutating func encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T : Encodable { try _encodeIfPresent(value, forKey: key)} |
| 66 | +// mutating func encodeIfPresent(_ value: Int?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 67 | +// mutating func encodeIfPresent(_ value: Int8?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 68 | +// mutating func encodeIfPresent(_ value: Int16?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 69 | +// mutating func encodeIfPresent(_ value: Int32?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 70 | +// mutating func encodeIfPresent(_ value: Int64?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 71 | +// mutating func encodeIfPresent(_ value: UInt?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 72 | +// mutating func encodeIfPresent(_ value: UInt16?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 73 | +// mutating func encodeIfPresent(_ value: UInt32?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 74 | +// mutating func encodeIfPresent(_ value: UInt64?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 75 | +// mutating func encodeIfPresent(_ value: Double?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 76 | +// mutating func encodeIfPresent(_ value: Float?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 77 | +// mutating func encodeIfPresent(_ value: String?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 78 | +// mutating func encodeIfPresent(_ value: Bool?, forKey key: Key) throws { try _encodeIfPresent(value, forKey: key) } |
| 79 | +// |
| 80 | +// mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey { |
| 81 | +// fatalError() |
| 82 | +// } |
| 83 | +// |
| 84 | +// mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer { |
| 85 | +// fatalError() |
| 86 | +// } |
| 87 | +// |
| 88 | +// mutating func superEncoder() -> Encoder { |
| 89 | +// fatalError() |
| 90 | +// } |
| 91 | +// |
| 92 | +// mutating func superEncoder(forKey key: Key) -> Encoder { |
| 93 | +// fatalError() |
| 94 | +// } |
| 95 | +// } |
| 96 | +//} |
0 commit comments