Skip to content

Protocol

PostgresPreparedStatement

A prepared statement.
protocol PostgresPreparedStatement : Sendable

Overview

Structs conforming to this protocol will need to provide the SQL statement to send to the server and a way of creating bindings and decoding the result.

As an example, consider this struct:

struct Example: PostgresPreparedStatement {
    static let sql = "SELECT pid, datname FROM pg_stat_activity WHERE state = $1"
    typealias Row = (Int, String)

    var state: String

    func makeBindings() -> PostgresBindings {
        var bindings = PostgresBindings()
        bindings.append(self.state)
        return bindings
    }

    func decodeRow(_ row: PostgresNIO.PostgresRow) throws -> Row {
        try row.decode(Row.self)
    }
}

Structs conforming to this protocol can then be used with PostgresConnection.execute(_ preparedStatement:, logger:), which will take care of preparing the statement on the server side and executing it.

Topics

Associated Types

Instance Methods

Type Properties

Relationships

Inherits From

  • Swift.Sendable
  • Swift.SendableMetatype