Instance Method
query(_:logger:summary:file:line:_:)
body closure’s result.func query<Result>(_ query: PostgresQuery, logger: Logger, summary: String? = nil, file: String = #fileID, line: Int = #line, _ body: (PostgresRowSequence) async throws -> Result) async throws -> (result: Result, metadata: PostgresQueryMetadata)
Parameters
queryThe
PostgresQueryto runloggerThe
Loggerto log into for the querysummaryA low-cardinality summary of the query (e.g.
SELECT users), used as the span’s name and recorded asdb.query.summary. Must not contain values, such as an id.fileThe file the query was started in. Used for better error reporting.
lineThe line the query was started in. Used for better error reporting.
bodyThe closure that’s used to consume the query result. Must consume the sequence to its end.
Return Value
The result of the body closure and the query metadata.
Discussion
The result of the query can only be consumed inside of the body closure. Attempting to consume the stream outside of the closure is a programmer error, and will trigger a precondition failure.
The metadata is derived from the command tag the server sends after the last row, so body must iterate the sequence to its end. If it returns early the query is cancelled and the method throws PSQLError.rowSequenceNotFullyConsumed.
A span is created for the query, lasting until the metadata is received. See PostgresTracingConfiguration.