|
| 1 | +#if compiler(>=5.9) |
| 2 | +extension PostgresRow { |
| 3 | + // --- snip TODO: Remove once bug is fixed, that disallows tuples of one |
| 4 | + @inlinable |
| 5 | + public func decode<Column: PostgresDecodable>( |
| 6 | + _: Column.Type, |
| 7 | + file: String = #fileID, |
| 8 | + line: Int = #line |
| 9 | + ) throws -> (Column) { |
| 10 | + try self.decode(Column.self, context: .default, file: file, line: line) |
| 11 | + } |
| 12 | + |
| 13 | + @inlinable |
| 14 | + public func decode<Column: PostgresDecodable>( |
| 15 | + _: Column.Type, |
| 16 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 17 | + file: String = #fileID, |
| 18 | + line: Int = #line |
| 19 | + ) throws -> (Column) { |
| 20 | + precondition(self.columns.count >= 1) |
| 21 | + let columnIndex = 0 |
| 22 | + var cellIterator = self.data.makeIterator() |
| 23 | + var cellData = cellIterator.next().unsafelyUnwrapped |
| 24 | + var columnIterator = self.columns.makeIterator() |
| 25 | + let column = columnIterator.next().unsafelyUnwrapped |
| 26 | + let swiftTargetType: Any.Type = Column.self |
| 27 | + |
| 28 | + do { |
| 29 | + let r0 = try Column._decodeRaw(from: &cellData, type: column.dataType, format: column.format, context: context) |
| 30 | + |
| 31 | + return (r0) |
| 32 | + } catch let code as PostgresDecodingError.Code { |
| 33 | + throw PostgresDecodingError( |
| 34 | + code: code, |
| 35 | + columnName: column.name, |
| 36 | + columnIndex: columnIndex, |
| 37 | + targetType: swiftTargetType, |
| 38 | + postgresType: column.dataType, |
| 39 | + postgresFormat: column.format, |
| 40 | + postgresData: cellData, |
| 41 | + file: file, |
| 42 | + line: line |
| 43 | + ) |
| 44 | + } |
| 45 | + } |
| 46 | + // --- snap TODO: Remove once bug is fixed, that disallows tuples of one |
| 47 | + |
| 48 | + @inlinable |
| 49 | + public func decode<each Column: PostgresDecodable>( |
| 50 | + _ columnType: (repeat each Column).Type, |
| 51 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 52 | + file: String = #fileID, |
| 53 | + line: Int = #line |
| 54 | + ) throws -> (repeat each Column) { |
| 55 | + let packCount = ComputeParameterPackLength.count(ofPack: repeat (each Column).self) |
| 56 | + precondition(self.columns.count >= packCount) |
| 57 | + |
| 58 | + var columnIndex = 0 |
| 59 | + var cellIterator = self.data.makeIterator() |
| 60 | + var columnIterator = self.columns.makeIterator() |
| 61 | + |
| 62 | + return ( |
| 63 | + repeat try Self.decodeNextColumn( |
| 64 | + (each Column).self, |
| 65 | + cellIterator: &cellIterator, |
| 66 | + columnIterator: &columnIterator, |
| 67 | + columnIndex: &columnIndex, |
| 68 | + context: context, |
| 69 | + file: file, |
| 70 | + line: line |
| 71 | + ) |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + @inlinable |
| 76 | + static func decodeNextColumn<Column: PostgresDecodable>( |
| 77 | + _ columnType: Column.Type, |
| 78 | + cellIterator: inout IndexingIterator<DataRow>, |
| 79 | + columnIterator: inout IndexingIterator<[RowDescription.Column]>, |
| 80 | + columnIndex: inout Int, |
| 81 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 82 | + file: String, |
| 83 | + line: Int |
| 84 | + ) throws -> Column { |
| 85 | + defer { columnIndex += 1 } |
| 86 | + |
| 87 | + let column = columnIterator.next().unsafelyUnwrapped |
| 88 | + var cellData = cellIterator.next().unsafelyUnwrapped |
| 89 | + do { |
| 90 | + return try Column._decodeRaw(from: &cellData, type: column.dataType, format: column.format, context: context) |
| 91 | + } catch let code as PostgresDecodingError.Code { |
| 92 | + throw PostgresDecodingError( |
| 93 | + code: code, |
| 94 | + columnName: column.name, |
| 95 | + columnIndex: columnIndex, |
| 96 | + targetType: Column.self, |
| 97 | + postgresType: column.dataType, |
| 98 | + postgresFormat: column.format, |
| 99 | + postgresData: cellData, |
| 100 | + file: file, |
| 101 | + line: line |
| 102 | + ) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @inlinable |
| 107 | + public func decode<each Column: PostgresDecodable>( |
| 108 | + _ columnType: (repeat each Column).Type, |
| 109 | + file: String = #fileID, |
| 110 | + line: Int = #line |
| 111 | + ) throws -> (repeat each Column) { |
| 112 | + try self.decode(columnType, context: .default, file: file, line: line) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +extension AsyncSequence where Element == PostgresRow { |
| 117 | + // --- snip TODO: Remove once bug is fixed, that disallows tuples of one |
| 118 | + @inlinable |
| 119 | + public func decode<Column: PostgresDecodable>( |
| 120 | + _: Column.Type, |
| 121 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 122 | + file: String = #fileID, |
| 123 | + line: Int = #line |
| 124 | + ) -> AsyncThrowingMapSequence<Self, (Column)> { |
| 125 | + self.map { row in |
| 126 | + try row.decode(Column.self, context: context, file: file, line: line) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @inlinable |
| 131 | + public func decode<Column: PostgresDecodable>( |
| 132 | + _: Column.Type, |
| 133 | + file: String = #fileID, |
| 134 | + line: Int = #line |
| 135 | + ) -> AsyncThrowingMapSequence<Self, (Column)> { |
| 136 | + self.decode(Column.self, context: .default, file: file, line: line) |
| 137 | + } |
| 138 | + // --- snap TODO: Remove once bug is fixed, that disallows tuples of one |
| 139 | + |
| 140 | + public func decode<each Column: PostgresDecodable>( |
| 141 | + _ columnType: (repeat each Column).Type, |
| 142 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 143 | + file: String = #fileID, |
| 144 | + line: Int = #line |
| 145 | + ) -> AsyncThrowingMapSequence<Self, (repeat each Column)> { |
| 146 | + self.map { row in |
| 147 | + try row.decode(columnType, context: context, file: file, line: line) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public func decode<each Column: PostgresDecodable>( |
| 152 | + _ columnType: (repeat each Column).Type, |
| 153 | + file: String = #fileID, |
| 154 | + line: Int = #line |
| 155 | + ) -> AsyncThrowingMapSequence<Self, (repeat each Column)> { |
| 156 | + self.decode(columnType, context: .default, file: file, line: line) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +@usableFromInline |
| 161 | +enum ComputeParameterPackLength { |
| 162 | + @usableFromInline |
| 163 | + enum BoolConverter<T> { |
| 164 | + @usableFromInline |
| 165 | + typealias Bool = Swift.Bool |
| 166 | + } |
| 167 | + |
| 168 | + @inlinable |
| 169 | + static func count<each T>(ofPack t: repeat each T) -> Int { |
| 170 | + MemoryLayout<(repeat BoolConverter<each T>.Bool)>.size / MemoryLayout<Bool>.stride |
| 171 | + } |
| 172 | +} |
| 173 | +#endif // compiler(>=5.9) |
| 174 | + |
0 commit comments