QueryInterfaceRequest
A request that builds SQL queries with Swift.
struct QueryInterfaceRequest<RowDecoder>
You build a QueryInterfaceRequest
from a TableRecord
type, or a Table
instance. For example:
struct Player: TableRecord, FetchableRecord, DecodableRecord { }
try dbQueue.read { db in
// SELECT * FROM player
// WHERE name = 'O''Reilly'
// ORDER BY score DESC
let request = Player
.filter(Column("name") == "O'Reilly")
.order(Column("score").desc)
let players: [Player] = try request.fetchAll(db)
}
Most features of QueryInterfaceRequest
come from the protocols it conforms to. In particular:
Fetching methods are defined by
FetchRequest
.Request building methods are defined by
DerivableRequest
.
Instance Methods
func isEmpty(Database
) throws -> Bool Returns whether the requests does not match any row in the database.
func limit(Int, offset: Int?
) -> QueryInterfaceRequest<RowDecoder> Returns a limited request.
Changing The Type of Fetched Results
func asRequest<T>(of: T.Type
) -> QueryInterfaceRequest<T> Returns a request that performs an identical database query, but decodes database rows with
type
.func select<T>(any SQLSelectable..., as: T.Type
) -> QueryInterfaceRequest<T> Defines the result columns, and defines the type of decoded rows.
func select<T>([any SQLSelectable], as: T.Type
) -> QueryInterfaceRequest<T> Defines the result columns, and defines the type of decoded rows.
func select<T>(literal: SQL, as: T.Type
) -> QueryInterfaceRequest<T> Defines the result columns with an
SQL
literal, and defines the type of decoded rows.func select<T>(sql: String, arguments: StatementArguments, as: T.Type
) -> QueryInterfaceRequest<T> Defines the result columns with an SQL string, and defines the type of decoded rows.
func selectID(
) -> QueryInterfaceRequest<RowDecoder.ID> Returns a request that selects the primary key.
func selectPrimaryKey<PrimaryKey>(as: PrimaryKey.Type
) -> QueryInterfaceRequest<PrimaryKey> Returns a request that selects the primary key.
Batch Delete
func deleteAll(Database
) throws -> Int Deletes matching rows, and returns the number of deleted rows.
func deleteAndFetchIds(Database
) throws -> Set<RowDecoder.ID> Executes a
DELETE RETURNING
statement and returns the set of deleted ids.func deleteAndFetchCursor(Database
) throws -> RecordCursor<RowDecoder> Returns a cursor over the records deleted by a
DELETE RETURNING
statement.func deleteAndFetchAll(Database
) throws -> [RowDecoder] Executes a
DELETE RETURNING
statement and returns the array of deleted records.func deleteAndFetchSet(Database
) throws -> Set<RowDecoder> Executes a
DELETE RETURNING
statement and returns the set of deleted records.func deleteAndFetchStatement(Database, selection: [any SQLSelectable]
) throws -> Statement Returns a
DELETE RETURNING
prepared statement.
Batch Update
func updateAll(Database, onConflict: Database.ConflictResolution?, ColumnAssignment...
) throws -> Int Updates matching rows, and returns the number of updated rows.
func updateAll(Database, onConflict: Database.ConflictResolution?, [ColumnAssignment]
) throws -> Int Updates matching rows, and returns the number of updated rows.
func updateAndFetchCursor(Database, onConflict: Database.ConflictResolution?, [ColumnAssignment]
) throws -> RecordCursor<RowDecoder> Returns a cursor over the records updated by an
UPDATE RETURNING
statement.func updateAndFetchAll(Database, onConflict: Database.ConflictResolution?, [ColumnAssignment]
) throws -> [RowDecoder] Execute an
UPDATE RETURNING
statement and returns the array of updated records.func updateAndFetchSet(Database, onConflict: Database.ConflictResolution?, [ColumnAssignment]
) throws -> Set<RowDecoder> Execute an
UPDATE RETURNING
statement and returns the set of updated records.func updateAndFetchStatement(Database, onConflict: Database.ConflictResolution?, [ColumnAssignment], selection: [any SQLSelectable]
) throws -> Statement Returns an
UPDATE RETURNING
prepared statement.struct ColumnAssignment
A
ColumnAssignment
assigns a value to a column.