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

  • Row
    The type rows returned by the statement will be decoded into

Instance Methods

  • decodeRow(_:)
    Decode a row returned by the database into an instance of Row
  • makeBindings()
    Make the bindings to provide concrete values to use when executing the prepared SQL statement. The order must match bindingDataTypes.

Type Properties

  • bindingDataTypes
    The Postgres data types of the values that are bound when this statement is executed.
  • name
    The prepared statement’s name.
  • sql
    The SQL statement to prepare on the database server.

Relationships

Inherits From

  • Swift.Sendable
  • Swift.SendableMetatype