Request
public protocol Request
The protocol for all types that define a way to fetch values from a database.
Requests can feed the fetching methods of any fetchable type (Row, value, record):
let request: Request = ...
try Row.fetchCursor(db, request) // DatabaseCursor<Row>
try String.fetchAll(db, request) // [String]
try Person.fetchOne(db, request) // Person?
-
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 implementationThe 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.
-
asRequest(of:)
Extension methodReturns a TypedRequest bound to type T.
The returned request can fetch if the type T is fetchable (Row, value, record).
// minHeight in Double? let minHeight = Person .select(min(heightColumn)) .asRequest(of: Double.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 methodReturns an adapted request.
Declaration
Swift
public func adapted(_ makeAdapter: @escaping (Database) throws -> RowAdapter) -> AnyRequest