Skip to content

Instance Method

execute(_:logger:file:line:_:)

Execute a prepared statement, taking care of the preparation when necessary, and return the statement’s metadata alongside the body closure’s result.
func execute<Statement, Row, Result>(_ preparedStatement: Statement, logger: Logger, file: String = #fileID, line: Int = #line, _ body: (AsyncThrowingMapSequence<PostgresRowSequence, Row>) async throws -> Result) async throws -> (result: Result, metadata: PostgresQueryMetadata) where Statement : PostgresPreparedStatement, Row == Statement.Row

Parameters

preparedStatement

The PostgresPreparedStatement to execute

logger

The Logger to log into for the statement

file

The file the statement was executed in. Used for better error reporting.

line

The line the statement was executed in. Used for better error reporting.

body

The closure that’s used to consume the decoded rows. Must consume the sequence to its end.

Return Value

The result of the body closure and the statement metadata.

Discussion

The result of the statement 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 statement is cancelled and the method throws PSQLError.rowSequenceNotFullyConsumed.