Request

public protocol Request

The protocol for all types that define a way to fetch database rows.

Requests can feed the fetching methods of any fetchable type (Row, value, record):

let request: Request = ...
try Row.fetchCursor(db, request) // RowCursor
try String.fetchAll(db, request) // [String]
try Player.fetchOne(db, request) // Player?
  • A tuple that contains a prepared statement that is ready to be executed, and an eventual row adapter.

    Declaration

    Swift

    func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?)
  • fetchCount(_:) Default implementation

    The number of rows fetched by the request.

    Default implementation builds a naive SQL query based on the statement returned by the prepare method: SELECT COUNT(*) FROM (...).

    Adopting types can refine this countRequest method and return more efficient SQL.

    Default Implementation

    The number of rows fetched by the request.

    This default implementation builds a naive SQL query based on the statement returned by the prepare method: SELECT COUNT(*) FROM (...).

    Declaration

    Swift

    func fetchCount(_ db: Database) throws -> Int

    Parameters

    db

    A database connection.

  • fetchedRegion(_:) Default implementation

    The database region that the request looks into.

    This method has a default implementation.

    Default Implementation

    The database region that the request looks into.

    Declaration

    Swift

    func fetchedRegion(_ db: Database) throws -> DatabaseRegion

    Parameters

    db

    A database connection.

  • asSQLRequest(_:cached:) Extension method

    Returns an equivalent SQLRequest.

    Declaration

    Swift

    public func asSQLRequest(_ db: Database, cached: Bool = false) throws -> SQLRequest

    Parameters

    db

    A database connection.

    cached

    Defaults to false. If true, the request reuses a cached prepared statement.

    Return Value

    An SQLRequest

  • asRequest(of:) Extension method

    Returns a request bound to type T.

    The returned request can fetch if the type T is fetchable (Row, value, record).

    // Int?
    let maxScore = Player
        .select(max(scoreColumn))
        .asRequest(of: Int.self)    // <--
        .fetchOne(db)
    

    Declaration

    Swift

    public func asRequest<T>(of type: T.Type) -> AnyTypedRequest<T>

    Parameters

    type

    The fetched type T

    Return Value

    A typed request bound to type T.

  • adapted(_:) Extension method

    Experimental

    Returns an adapted request.

    Declaration

    Swift

    public func adapted(_ adapter: @escaping (Database) throws -> RowAdapter) -> AdaptedRequest<Self>