forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLConnection+SimpleQuery.swift
33 lines (31 loc) · 1.41 KB
/
PostgreSQLConnection+SimpleQuery.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
import Async
extension PostgreSQLConnection {
/// Sends a simple PostgreSQL query command, collecting the parsed results.
public func simpleQuery(_ string: String) -> Future<[[String: PostgreSQLData]]> {
var rows: [[String: PostgreSQLData]] = []
return simpleQuery(string) { row in
rows.append(row)
}.map(to: [[String: PostgreSQLData]].self) {
return rows
}
}
/// Sends a simple PostgreSQL query command, returning the parsed results to
/// the supplied closure.
public func simpleQuery(_ string: String, onRow: @escaping ([String: PostgreSQLData]) -> ()) -> Future<Void> {
logger?.log(query: string, parameters: [])
var currentRow: PostgreSQLRowDescription?
let query = PostgreSQLQuery(query: string)
return send([.query(query)]) { message in
switch message {
case .rowDescription(let row):
currentRow = row
case .dataRow(let data):
let row = currentRow !! "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription."
let parsed = try row.parse(data: data, formatCodes: row.fields.map { $0.formatCode })
onRow(parsed)
case .close: break // query over, waiting for `readyForQuery`
default: fatalError("Unexpected message during PostgreSQLQuery: \(message)")
}
}
}
}