-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathPostgresRow+SQL.swift
45 lines (36 loc) · 1.29 KB
/
PostgresRow+SQL.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Foundation
import PostgresNIO
import SQLKit
extension PostgresRow {
@inlinable
public func sql() -> some SQLRow {
self.sql(decodingContext: .default)
}
public func sql(decodingContext: PostgresDecodingContext<some PostgresJSONDecoder>) -> some SQLRow {
_PostgresSQLRow(randomAccessView: self.makeRandomAccess(), decodingContext: decodingContext)
}
}
private struct _PostgresSQLRow<D: PostgresJSONDecoder> {
let randomAccessView: PostgresRandomAccessRow
let decodingContext: PostgresDecodingContext<D>
enum _Error: Error {
case missingColumn(String)
}
}
extension _PostgresSQLRow: SQLRow {
var allColumns: [String] {
self.randomAccessView.map { $0.columnName }
}
func contains(column: String) -> Bool {
self.randomAccessView.contains(column)
}
func decodeNil(column: String) throws -> Bool {
!self.randomAccessView.contains(column) || self.randomAccessView[column].bytes == nil
}
func decode<T: Decodable>(column: String, as type: T.Type) throws -> T {
guard self.randomAccessView.contains(column) else {
throw _Error.missingColumn(column)
}
return try PostgresDataTranslation.decode(T.self, from: self.randomAccessView[column], in: self.decodingContext)
}
}