Skip to content

Commit adfc112

Browse files
committed
extend psql data to single value decoding container
1 parent d59701c commit adfc112

File tree

2 files changed

+31
-41
lines changed

2 files changed

+31
-41
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
extension PostgreSQLData: SingleValueDecodingContainer {
2+
public var codingPath: [CodingKey] { return [] }
3+
public func decodeNil() -> Bool { return data == nil }
4+
public func decode(_ type: Int.Type) throws -> Int { return try decode() }
5+
public func decode(_ type: Int8.Type) throws -> Int8 { return try decode() }
6+
public func decode(_ type: Int16.Type) throws -> Int16 { return try decode() }
7+
public func decode(_ type: Int32.Type) throws -> Int32 { return try decode() }
8+
public func decode(_ type: Int64.Type) throws -> Int64 { return try decode() }
9+
public func decode(_ type: UInt.Type) throws -> UInt { return try decode() }
10+
public func decode(_ type: UInt8.Type) throws -> UInt8 { return try decode() }
11+
public func decode(_ type: UInt16.Type) throws -> UInt16 { return try decode() }
12+
public func decode(_ type: UInt32.Type) throws -> UInt32 { return try decode() }
13+
public func decode(_ type: UInt64.Type) throws -> UInt64 { return try decode() }
14+
public func decode(_ type: Double.Type) throws -> Double { return try decode() }
15+
public func decode(_ type: Float.Type) throws -> Float { return try decode() }
16+
public func decode(_ type: Bool.Type) throws -> Bool { return try decode() }
17+
public func decode(_ type: String.Type) throws -> String { return try decode() }
18+
public func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
19+
guard let convertible = type as? PostgreSQLDataCustomConvertible.Type else {
20+
throw PostgreSQLError(
21+
identifier: "convertible",
22+
reason: "Unsupported decodable type: \(type)",
23+
suggestedFixes: [
24+
"Conform \(type) to PostgreSQLDataCustomConvertible"
25+
]
26+
)
27+
}
28+
return try convertible.convertFromPostgreSQLData(self) as! T
29+
}
30+
}

Sources/FluentPostgreSQL/PostgreSQLDatabase+QuerySupporting.swift

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ extension PostgreSQLDatabase: QuerySupporting {
8282
// Run the query
8383
return try connection.query(sqlString, parameters) { row in
8484
let codableDict = row.mapValues { psqlData -> DecodableData in
85-
return .decoder(PostgreSQLDataDecoder(data: psqlData))
85+
return .decoder(psqlData)
8686
}
8787
do {
8888
let decoded = try CodableDataDecoder().decode(D.self, from: .dictionary(codableDict))
@@ -125,43 +125,3 @@ extension PostgreSQLDatabase: QuerySupporting {
125125
return .done
126126
}
127127
}
128-
129-
130-
internal struct PostgreSQLDataDecoder: SingleValueDecodingContainer {
131-
var codingPath: [CodingKey]
132-
var userInfo: [CodingUserInfoKey: Any]
133-
let data: PostgreSQLData
134-
init(data: PostgreSQLData) {
135-
self.data = data
136-
self.userInfo = [:]
137-
self.codingPath = []
138-
}
139-
140-
func decodeNil() -> Bool { return data.data == nil }
141-
func decode(_ type: Int.Type) throws -> Int { return try data.decode(Int.self) }
142-
func decode(_ type: Int8.Type) throws -> Int8 { return try data.decode(Int8.self) }
143-
func decode(_ type: Int16.Type) throws -> Int16 { return try data.decode(Int16.self) }
144-
func decode(_ type: Int32.Type) throws -> Int32 { return try data.decode(Int32.self) }
145-
func decode(_ type: Int64.Type) throws -> Int64 { return try data.decode(Int64.self) }
146-
func decode(_ type: UInt.Type) throws -> UInt { return try data.decode(UInt.self) }
147-
func decode(_ type: UInt8.Type) throws -> UInt8 { return try data.decode(UInt8.self) }
148-
func decode(_ type: UInt16.Type) throws -> UInt16 { return try data.decode(UInt16.self) }
149-
func decode(_ type: UInt32.Type) throws -> UInt32 { return try data.decode(UInt32.self) }
150-
func decode(_ type: UInt64.Type) throws -> UInt64 { return try data.decode(UInt64.self) }
151-
func decode(_ type: Double.Type) throws -> Double { return try data.decode(Double.self) }
152-
func decode(_ type: Float.Type) throws -> Float { return try data.decode(Float.self) }
153-
func decode(_ type: Bool.Type) throws -> Bool { return try data.decode(Bool.self) }
154-
func decode(_ type: String.Type) throws -> String { return try data.decode(String.self) }
155-
func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
156-
guard let convertible = type as? PostgreSQLDataCustomConvertible.Type else {
157-
throw PostgreSQLError(
158-
identifier: "convertible",
159-
reason: "Unsupported decodable type: \(type)",
160-
suggestedFixes: [
161-
"Conform \(type) to PostgreSQLDataCustomConvertible"
162-
]
163-
)
164-
}
165-
return try convertible.convertFromPostgreSQLData(data) as! T
166-
}
167-
}

0 commit comments

Comments
 (0)