Table
A Table
builds database queries with the Swift language instead of SQL.
struct Table<RowDecoder>
Overview
A Table
instance is similar to a TableRecord
type. You will use one when the other is impractical or impossible to use.
For example:
let table = Table("player")
try dbQueue.read { db in
// SELECT * FROM player WHERE score >= 1000
let rows: [Row] = table.filter(Column("score") >= 1000).fetchAll(db)
}
Creating a Table
init(String
) Creates a
Table
.init(String
) Create a
Table<Row>
.
Instance Properties
var tableName: String
The table name.
Counting Rows
func fetchCount(Database
) throws -> Int Returns the number of rows in the database table.
Testing for Row Existence
func exists(Database, id: RowDecoder.ID
) throws -> Bool Returns whether a row exists for this primary key.
func exists(Database, key: some DatabaseValueConvertible
) throws -> Bool Returns whether a row exists for this primary key.
func exists(Database, key: [String : (any DatabaseValueConvertible)?]
) throws -> Bool Returns whether a row exists for this primary or unique key.
Deleting Rows
func deleteAll(Database
) throws -> Int Deletes all rows, and returns the number of deleted rows.
func deleteAll(Database, ids: some Collection<RowDecoder.ID>
) throws -> Int Deletes rows identified by their primary keys, and returns the number of deleted rows.
func deleteAll(Database, keys: some Collection<some DatabaseValueConvertible>
) throws -> Int Deletes rows identified by their primary keys, and returns the number of deleted rows.
func deleteAll(Database, keys: [[String : (any DatabaseValueConvertible)?]]
) throws -> Int Deletes rows identified by their primary or unique keys, and returns the number of deleted rows.
func deleteOne(Database, id: RowDecoder.ID
) throws -> Bool Deletes the row identified by its primary key, and returns whether a row was deleted.
func deleteOne(Database, key: some DatabaseValueConvertible
) throws -> Bool Deletes the row identified by its primary key, and returns whether a row was deleted.
func deleteOne(Database, key: [String : (any DatabaseValueConvertible)?]
) throws -> Bool Deletes the row identified by its primary or unique keys, and returns whether a row was deleted.
Updating Rows
func updateAll(Database, onConflict: Database.ConflictResolution?, ColumnAssignment...
) throws -> Int Updates all rows, and returns the number of updated rows.
func updateAll(Database, onConflict: Database.ConflictResolution?, [ColumnAssignment]
) throws -> Int Updates all rows, and returns the number of updated rows.
Building Query Interface Requests
Table
provide convenience access to most DerivableRequest
and QueryInterfaceRequest
methods.
func aliased(TableAlias
) -> QueryInterfaceRequest<RowDecoder> Returns a request that can be referred to with the provided alias.
func all(
) -> QueryInterfaceRequest<RowDecoder> Returns a request for all rows of the table.
func annotated(with: any SQLSelectable...
) -> QueryInterfaceRequest<RowDecoder> Returns a request with the provided result columns appended to the table columns.
func annotated(with: [any SQLSelectable]
) -> QueryInterfaceRequest<RowDecoder> Returns a request with the provided result columns appended to the table columns.
func annotated(with: AssociationAggregate<RowDecoder>...
) -> QueryInterfaceRequest<RowDecoder> Returns a request with the given association aggregates appended to the table colums.
func annotated(with: [AssociationAggregate<RowDecoder>]
) -> QueryInterfaceRequest<RowDecoder> Returns a request with the given association aggregates appended to the table colums.
func annotated<A>(withOptional: A
) -> QueryInterfaceRequest<RowDecoder> Returns a request with the columns of the eventual associated row appended to the table columns.
func annotated<A>(withRequired: A
) -> QueryInterfaceRequest<RowDecoder> Returns a request with the columns of the associated row appended to the table columns. Rows that do not have an associated row are discarded.
func filter(some SQLSpecificExpressible
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered with a boolean SQL expression.
func filter(id: RowDecoder.ID
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered by primary key.
func filter(ids: some Collection<RowDecoder.ID>
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered by primary key.
func filter(key: some DatabaseValueConvertible
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered by primary key.
func filter(key: [String : (any DatabaseValueConvertible)?]?
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered by primary or unique key.
func filter(keys: some Collection<some DatabaseValueConvertible>
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered by primary key.
func filter(keys: [[String : (any DatabaseValueConvertible)?]]
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered by primary or unique key.
func filter(literal: SQL
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered with an
SQL
literal.func filter(sql: String, arguments: StatementArguments
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered with an SQL string.
func having(AssociationAggregate<RowDecoder>
) -> QueryInterfaceRequest<RowDecoder> Returns a request filtered according to the provided association aggregate.
func including<A>(all: A
) -> QueryInterfaceRequest<RowDecoder> Returns a request that fetches all rows associated with each row in this request.
func including<A>(optional: A
) -> QueryInterfaceRequest<RowDecoder> Returns a request that fetches the eventual row associated with each row of this request.
func including<A>(required: A
) -> QueryInterfaceRequest<RowDecoder> Returns a request that fetches the row associated with each row in this request. Rows that do not have an associated row are discarded.
func joining<A>(optional: A
) -> QueryInterfaceRequest<RowDecoder> Returns a request that joins each row of this request to its eventual associated row.
func joining<A>(required: A
) -> QueryInterfaceRequest<RowDecoder> Returns a request that joins each row of this request to its associated row. Rows that do not have an associated row are discarded.
func limit(Int, offset: Int?
) -> QueryInterfaceRequest<RowDecoder> Returns a limited request.
func none(
) -> QueryInterfaceRequest<RowDecoder> Returns an empty request that fetches no row.
func order(any SQLOrderingTerm...
) -> QueryInterfaceRequest<RowDecoder> Returns a request sorted according to the given SQL ordering terms.
func order([any SQLOrderingTerm]
) -> QueryInterfaceRequest<RowDecoder> Returns a request sorted according to the given SQL ordering terms.
func order(literal: SQL
) -> QueryInterfaceRequest<RowDecoder> Returns a request sorted according to the given
SQL
literal.func order(sql: String, arguments: StatementArguments
) -> QueryInterfaceRequest<RowDecoder> Returns a request sorted according to the given SQL string.
func orderByPrimaryKey(
) -> QueryInterfaceRequest<RowDecoder> Returns a request sorted by primary key.
func select(any SQLSelectable...
) -> QueryInterfaceRequest<RowDecoder> Returns a request that selects the provided result columns.
func select([any SQLSelectable]
) -> QueryInterfaceRequest<RowDecoder> Returns a request that selects the provided result columns.
func select<T>(any SQLSelectable..., as: T.Type
) -> QueryInterfaceRequest<T> Returns a request that selects the provided result columns, and defines the type of decoded rows.
func select<T>([any SQLSelectable], as: T.Type
) -> QueryInterfaceRequest<T> Returns a request that selects the provided result columns, and defines the type of decoded rows.
func select(literal: SQL
) -> QueryInterfaceRequest<RowDecoder> Returns a request that selects the provided
SQL
literal.func select<T>(literal: SQL, as: T.Type
) -> QueryInterfaceRequest<T> Returns a request that selects the provided
SQL
literal, and defines the type of decoded rows.func select(sql: String, arguments: StatementArguments
) -> QueryInterfaceRequest<RowDecoder> Returns a request that selects the provided SQL string.
func select<T>(sql: String, arguments: StatementArguments, as: T.Type
) -> QueryInterfaceRequest<T> Returns a request that selects the provided 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.
func with<T>(CommonTableExpression<T>
) -> QueryInterfaceRequest<RowDecoder> Returns a request that embeds a common table expression.
Defining Associations
func association<Destination>(to: CommonTableExpression<Destination>
) -> JoinAssociation<RowDecoder, Destination> Creates an association to a common table expression.
func association<Destination>(to: CommonTableExpression<Destination>, on: @escaping (_ left: TableAlias, _ right: TableAlias) -> any SQLExpressible
) -> JoinAssociation<RowDecoder, Destination> Creates an association to a common table expression.
func belongsTo<Destination>(Destination.Type, key: String?, using: ForeignKey?
) -> BelongsToAssociation<RowDecoder, Destination> Creates a
BelongsToAssociation
between this table and the destinationTableRecord
type.func belongsTo<Destination>(Table<Destination>, key: String?, using: ForeignKey?
) -> BelongsToAssociation<RowDecoder, Destination> Creates a
BelongsToAssociation
between this table and the destinationTable
.func hasMany<Destination>(Destination.Type, key: String?, using: ForeignKey?
) -> HasManyAssociation<RowDecoder, Destination> Creates a
HasManyAssociation
between this table and the destinationTableRecord
type.func hasMany<Destination>(Table<Destination>, key: String?, using: ForeignKey?
) -> HasManyAssociation<RowDecoder, Destination> Creates a
HasManyAssociation
between this table and the destinationTable
.func hasMany<Pivot, Target>(Target.RowDecoder.Type, through: Pivot, using: Target, key: String?
) -> HasManyThroughAssociation<RowDecoder, Target.RowDecoder> Creates a
HasManyThroughAssociation
between this table and the destination type.func hasOne<Destination>(Destination.Type, key: String?, using: ForeignKey?
) -> HasOneAssociation<RowDecoder, Destination> Creates a
HasOneAssociation
between this table and the destinationTableRecord
type.func hasOne<Destination>(Table<Destination>, key: String?, using: ForeignKey?
) -> HasOneAssociation<RowDecoder, Destination> Creates a
HasOneAssociation
between this table and the destinationTable
.func hasOne<Pivot, Target>(Target.RowDecoder.Type, through: Pivot, using: Target, key: String?
) -> HasOneThroughAssociation<RowDecoder, Target.RowDecoder> Creates a
HasOneThroughAssociation
between this table and the destination type.
Fetching Database Rows
func fetchCursor(Database
) throws -> RowCursor Returns a cursor over all rows fetched from the database.
func fetchAll(Database
) throws -> [Row] Returns an array of all rows fetched from the database.
func fetchSet(Database
) throws -> Set<Row> Returns a set of all rows fetched from the database.
func fetchOne(Database
) throws -> Row? Returns a single row fetched from the database.
Fetching Database Values
func fetchCursor(Database
) throws -> DatabaseValueCursor<RowDecoder> Returns a cursor over fetched values.
func fetchCursor(Database
) throws -> FastDatabaseValueCursor<RowDecoder> Returns a cursor over fetched values.
func fetchAll(Database
) throws -> [RowDecoder] Returns an array of fetched values.
func fetchAll(Database
) throws -> [RowDecoder] Returns an array of fetched values.
func fetchSet(Database
) throws -> Set<RowDecoder> Returns a set of fetched values.
func fetchSet(Database
) throws -> Set<RowDecoder> Returns a set of fetched values.
func fetchOne(Database
) throws -> RowDecoder? Returns a single fetched value.
func fetchOne(Database
) throws -> RowDecoder? Returns a single fetched value.
Fetching Records
func fetchCursor(Database
) throws -> RecordCursor<RowDecoder> Returns a cursor over all records fetched from the database.
func fetchAll(Database
) throws -> [RowDecoder] Returns an array of all records fetched from the database.
func fetchSet(Database
) throws -> Set<RowDecoder> Returns a set of all records fetched from the database.
func fetchOne(Database
) throws -> RowDecoder? Returns a single record fetched from the database.