forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLConnection+SimpleQuery.swift
33 lines (30 loc) · 1.59 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
extension PostgreSQLConnection {
/// Performs a non-parameterized (text protocol) query to PostgreSQL.
public func simpleQuery(_ query: String) -> Future<Void> {
return operation { self._simpleQuery(query) { _ in }}
}
/// Performs a non-parameterized (text protocol) query to PostgreSQL.
public func simpleQuery(_ query: String, _ onRow: @escaping ([PostgreSQLColumn: PostgreSQLData]) throws -> ()) -> Future<Void> {
return operation { self._simpleQuery(query, onRow) }
}
// MARK: Private
/// Non-operation bounded simple query.
private func _simpleQuery(_ string: String, _ onRow: @escaping ([PostgreSQLColumn: PostgreSQLData]) throws -> ()) -> Future<Void> {
logger?.record(query: string)
var currentRow: PostgreSQLMessage.RowDescription?
return send([.query(.init(query: string))]) { message in
switch message {
case .rowDescription(let row):
currentRow = row
case .dataRow(let data):
guard let row = currentRow else {
throw PostgreSQLError(identifier: "simpleQuery", reason: "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription.")
}
let parsed = try row.parse(data: data, formatCodes: row.fields.map { $0.formatCode })
try onRow(parsed)
case .close: break // query over, waiting for `readyForQuery`
default: throw PostgreSQLError(identifier: "simpleQuery", reason: "Unexpected message during PostgreSQLQuery: \(message)")
}
}
}
}