forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSQLRow.swift
65 lines (55 loc) · 2.52 KB
/
PSQLRow.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import NIOCore
/// `PSQLRow` represents a single row that was received from the Postgres Server.
struct PSQLRow {
internal let lookupTable: [String: Int]
internal let data: DataRow
internal let columns: [RowDescription.Column]
internal let jsonDecoder: PSQLJSONDecoder
internal init(data: DataRow, lookupTable: [String: Int], columns: [RowDescription.Column], jsonDecoder: PSQLJSONDecoder) {
self.data = data
self.lookupTable = lookupTable
self.columns = columns
self.jsonDecoder = jsonDecoder
}
}
extension PSQLRow: Equatable {
static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.data == rhs.data && lhs.columns == rhs.columns
}
}
extension PSQLRow {
/// Access the data in the provided column and decode it into the target type.
///
/// - Parameters:
/// - column: The column name to read the data from
/// - type: The type to decode the data into
/// - Throws: The error of the decoding implementation. See also `PSQLDecodable` protocol for this.
/// - Returns: The decoded value of Type T.
func decode<T: PSQLDecodable>(column: String, as type: T.Type, file: String = #file, line: Int = #line) throws -> T {
guard let index = self.lookupTable[column] else {
preconditionFailure("A column '\(column)' does not exist.")
}
return try self.decode(column: index, as: type, file: file, line: line)
}
/// Access the data in the provided column and decode it into the target type.
///
/// - Parameters:
/// - column: The column index to read the data from
/// - type: The type to decode the data into
/// - Throws: The error of the decoding implementation. See also `PSQLDecodable` protocol for this.
/// - Returns: The decoded value of Type T.
func decode<T: PSQLDecodable>(column index: Int, as type: T.Type, file: String = #file, line: Int = #line) throws -> T {
precondition(index < self.data.columnCount)
let column = self.columns[index]
let context = PSQLDecodingContext(
jsonDecoder: self.jsonDecoder,
columnName: column.name,
columnIndex: index,
file: file,
line: line)
guard var cellSlice = self.data[column: index] else {
throw PSQLCastingError.missingData(targetType: T.self, type: column.dataType, context: context)
}
return try T.decode(from: &cellSlice, type: column.dataType, format: column.format, context: context)
}
}