forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresRow+SQL.swift
36 lines (29 loc) · 950 Bytes
/
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
extension PostgresRow {
public func sql(decoder: PostgresDataDecoder = .init()) -> SQLRow {
return _PostgreSQLRow(row: self, decoder: decoder)
}
}
// MARK: Private
private struct _PostgreSQLRow: SQLRow {
let row: PostgresRow
let decoder: PostgresDataDecoder
enum _Error: Error {
case missingColumn(String)
}
var allColumns: [String] {
self.row.rowDescription.fields.map { $0.name }
}
func contains(column: String) -> Bool {
self.row.rowDescription.fields
.contains { $0.name == column }
}
func decodeNil(column: String) throws -> Bool {
self.row.column(column)?.value == nil
}
func decode<D>(column: String, as type: D.Type) throws -> D where D : Decodable {
guard let data = self.row.column(column) else {
throw _Error.missingColumn(column)
}
return try self.decoder.decode(D.self, from: data)
}
}