QueryInterfaceRequest
public struct QueryInterfaceRequest<T>
A QueryInterfaceRequest describes an SQL query.
-
Creates a request with a full-text predicate added to the eventual set of already applied predicates.
// SELECT * FROM book WHERE book MATCH '...' var request = Book.all() request = request.matching(pattern)
If the search pattern is nil, the request does not match any database row.
Declaration
Swift
public func matching(_ pattern: FTS3Pattern?) -> QueryInterfaceRequest<T>
-
Creates a request that includes an association. The columns of the associated record are selected. The returned association does not require that the associated database table contains a matching row.
Declaration
Swift
public func including<A: Association>(optional association: A) -> QueryInterfaceRequest<RowDecoder> where A.OriginRowDecoder == RowDecoder
-
Creates a request that includes an association. The columns of the associated record are selected. The returned association requires that the associated database table contains a matching row.
Declaration
Swift
public func including<A: Association>(required association: A) -> QueryInterfaceRequest<RowDecoder> where A.OriginRowDecoder == RowDecoder
-
Creates a request that includes an association. The columns of the associated record are not selected. The returned association does not require that the associated database table contains a matching row.
Declaration
Swift
public func joining<A: Association>(optional association: A) -> QueryInterfaceRequest<RowDecoder> where A.OriginRowDecoder == RowDecoder
-
Creates a request that includes an association. The columns of the associated record are not selected. The returned association requires that the associated database table contains a matching row.
Declaration
Swift
public func joining<A: Association>(required association: A) -> QueryInterfaceRequest<RowDecoder> where A.OriginRowDecoder == RowDecoder
-
Declaration
Swift
public typealias RowDecoder = T
-
Creates a request with a new set of selected columns.
// SELECT id, email FROM player var request = Player.all() request = request.select([Column("id"), Column("email")])
Any previous selection is replaced:
// SELECT email FROM player request .select([Column("id")]) .select([Column("email")])
Declaration
Swift
public func select(_ selection: [SQLSelectable]) -> QueryInterfaceRequest<T>
-
Creates a request which returns distinct rows.
// SELECT DISTINCT * FROM player var request = Player.all() request = request.distinct() // SELECT DISTINCT name FROM player var request = Player.select(Column("name")) request = request.distinct()
Declaration
Swift
public func distinct() -> QueryInterfaceRequest<T>
-
Creates a request with the provided predicate promise added to the eventual set of already applied predicates.
// SELECT * FROM player WHERE 1 var request = Player.all() request = request.filter { db in true }
Declaration
Swift
public func filter(_ predicate: @escaping (Database) throws -> SQLExpressible) -> QueryInterfaceRequest<T>
-
Creates a request grouped according to expressions.
Declaration
Swift
public func group(_ expressions: [SQLExpressible]) -> QueryInterfaceRequest<T>
-
Creates a request with the provided predicate added to the eventual set of already applied predicates.
Declaration
Swift
public func having(_ predicate: SQLExpressible) -> QueryInterfaceRequest<T>
-
Creates a request with the provided orderings promise.
// SELECT * FROM player ORDER BY name var request = Player.all() request = request.order { _ in [Column("name")] }
Any previous ordering is replaced:
// SELECT * FROM player ORDER BY name request .order{ _ in [Column("email")] } .reversed() .order{ _ in [Column("name")] }
Declaration
Swift
public func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> QueryInterfaceRequest<T>
-
Creates a request that reverses applied orderings.
// SELECT * FROM player ORDER BY name DESC var request = Player.all().order(Column("name")) request = request.reversed()
If no ordering was applied, the returned request is identical.
// SELECT * FROM player var request = Player.all() request = request.reversed()
Declaration
Swift
public func reversed() -> QueryInterfaceRequest<T>
-
Creates a request which fetches limit rows, starting at offset.
// SELECT * FROM player LIMIT 1 var request = Player.all() request = request.limit(1)
Any previous limit is replaced.
Declaration
Swift
public func limit(_ limit: Int, offset: Int? = nil) -> QueryInterfaceRequest<T>
-
Creates a request that allows you to define expressions that target a specific database table.
In the example below, the
team.avgScore < player.score
condition in the ON clause could be not achieved without table aliases.struct Player: TableRecord { static let team = belongsTo(Team.self) } // SELECT player.*, team.* // JOIN team ON ... AND team.avgScore < player.score let playerAlias = TableAlias() let request = Player .all() .aliased(playerAlias) .including(required: Player.team.filter(Column("avgScore") < playerAlias[Column("score")])
Declaration
Swift
public func aliased(_ alias: TableAlias) -> QueryInterfaceRequest
-
Creates a request bound to type Target.
The returned request can fetch if the type Target is fetchable (Row, value, record).
// Int? let maxScore = try Player .select(max(scoreColumn)) .asRequest(of: Int.self) // <-- .fetchOne(db)
Declaration
Swift
public func asRequest<Target>(of type: Target.Type) -> QueryInterfaceRequest<Target>
Parameters
type
The fetched type Target
Return Value
A typed request bound to type Target.
-
Deletes matching rows; returns the number of deleted rows.
Throws
A DatabaseError is thrown whenever an SQLite error occurs.Declaration
Swift
public func deleteAll(_ db: Database) throws -> Int
Parameters
db
A database connection.
Return Value
The number of deleted rows