TableMapping
public protocol TableMapping
Types that adopt TableMapping declare a particular relationship with a database table.
Types that adopt both TableMapping and RowConvertible are granted with built-in methods that allow to fetch instances identified by key:
try Player.fetchOne(db, key: 123) // Player?
try Citizenship.fetchOne(db, key: ["citizenId": 12, "countryId": 45]) // Citizenship?
TableMapping is adopted by Record.
-
The name of the database table used to build requests.
struct Player : TableMapping { static var databaseTableName = "players" } // SELECT * FROM players try Player.fetchAll(db)
Declaration
Swift
static var databaseTableName: String
-
databaseSelection
Default implementationThe default request selection.
Unless said otherwise, requests select all columns:
// SELECT * FROM players try Player.fetchAll(db)
You can provide a custom implementation and provide an explicit list of columns:
struct RestrictedPlayer : TableMapping { static var databaseTableName = "players" static var databaseSelection = [Column("id"), Column("name")] } // SELECT id, name FROM players try RestrictedPlayer.fetchAll(db)
You can also add extra columns such as the
rowid
column:struct ExtendedPlayer : TableMapping { static var databaseTableName = "players" static let databaseSelection: [SQLSelectable] = [AllColumns(), Column.rowID] } // SELECT *, rowid FROM players try ExtendedPlayer.fetchAll(db)
Default Implementation
Default value:
[AllColumns()]
.Declaration
Swift
static var databaseSelection: [SQLSelectable]
-
matching(_:)
Extension methodReturns a QueryInterfaceRequest with a matching predicate.
// SELECT * FROM books WHERE books MATCH '...' var request = Book.matching(pattern)
If the search pattern is nil, the request does not match any database row.
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func matching(_ pattern: FTS3Pattern?) -> QueryInterfaceRequest<Self>
-
all()
Extension methodCreates a QueryInterfaceRequest which fetches all records.
// SELECT * FROM players let request = Player.all()
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func all() -> QueryInterfaceRequest<Self>
-
none()
Extension methodCreates a QueryInterfaceRequest which fetches no record.
Declaration
Swift
public static func none() -> QueryInterfaceRequest<Self>
-
select(_:)
Extension methodCreates a QueryInterfaceRequest which selects selection.
// SELECT id, email FROM players let request = Player.select(Column("id"), Column("email"))
Declaration
Swift
public static func select(_ selection: SQLSelectable...) -> QueryInterfaceRequest<Self>
-
select(_:)
Extension methodCreates a QueryInterfaceRequest which selects selection.
// SELECT id, email FROM players let request = Player.select([Column("id"), Column("email")])
Declaration
Swift
public static func select(_ selection: [SQLSelectable]) -> QueryInterfaceRequest<Self>
-
select(sql:arguments:)
Extension methodCreates a QueryInterfaceRequest which selects sql.
// SELECT id, email FROM players let request = Player.select(sql: "id, email")
Declaration
Swift
public static func select(sql: String, arguments: StatementArguments? = nil) -> QueryInterfaceRequest<Self>
-
filter(_:)
Extension methodCreates a QueryInterfaceRequest with the provided predicate.
// SELECT * FROM players WHERE email = 'arthur@example.com' let request = Player.filter(Column("email") == "arthur@example.com")
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func filter(_ predicate: SQLExpressible) -> QueryInterfaceRequest<Self>
-
filter(key:)
Extension methodCreates a QueryInterfaceRequest with the provided primary key predicate.
// SELECT * FROM players WHERE id = 1 let request = Player.filter(key: 1)
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func filter<PrimaryKeyType: DatabaseValueConvertible>(key: PrimaryKeyType?) -> QueryInterfaceRequest<Self>
-
filter(keys:)
Extension methodCreates a QueryInterfaceRequest with the provided primary key predicate.
// SELECT * FROM players WHERE id IN (1, 2, 3) let request = Player.filter(keys: [1, 2, 3])
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func filter<Sequence: Swift.Sequence>(keys: Sequence) -> QueryInterfaceRequest<Self> where Sequence.Element: DatabaseValueConvertible
-
filter(key:)
Extension methodCreates a QueryInterfaceRequest with the provided primary key predicate.
// SELECT * FROM passports WHERE personId = 1 AND countryCode = 'FR' let request = Passport.filter(key: ["personId": 1, "countryCode": "FR"])
When executed, this request raises a fatal error if there is no unique index on the key columns.
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func filter(key: [String: DatabaseValueConvertible?]?) -> QueryInterfaceRequest<Self>
-
filter(keys:)
Extension methodCreates a QueryInterfaceRequest with the provided primary key predicate.
// SELECT * FROM passports WHERE (personId = 1 AND countryCode = 'FR') OR ... let request = Passport.filter(keys: [["personId": 1, "countryCode": "FR"], ...])
When executed, this request raises a fatal error if there is no unique index on the key columns.
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func filter(keys: [[String: DatabaseValueConvertible?]]) -> QueryInterfaceRequest<Self>
-
filter(sql:arguments:)
Extension methodCreates a QueryInterfaceRequest with the provided predicate.
// SELECT * FROM players WHERE email = 'arthur@example.com' let request = Player.filter(sql: "email = ?", arguments: ["arthur@example.com"])
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func filter(sql: String, arguments: StatementArguments? = nil) -> QueryInterfaceRequest<Self>
-
order(_:)
Extension methodCreates a QueryInterfaceRequest sorted according to the provided orderings.
// SELECT * FROM players ORDER BY name let request = Player.order(Column("name"))
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func order(_ orderings: SQLOrderingTerm...) -> QueryInterfaceRequest<Self>
-
order(_:)
Extension methodCreates a QueryInterfaceRequest sorted according to the provided orderings.
// SELECT * FROM players ORDER BY name let request = Player.order([Column("name")])
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func order(_ orderings: [SQLOrderingTerm]) -> QueryInterfaceRequest<Self>
-
order(sql:arguments:)
Extension methodCreates a QueryInterfaceRequest sorted according to sql.
// SELECT * FROM players ORDER BY name let request = Player.order(sql: "name")
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func order(sql: String, arguments: StatementArguments? = nil) -> QueryInterfaceRequest<Self>
-
limit(_:offset:)
Extension methodCreates a QueryInterfaceRequest which fetches limit rows, starting at offset.
// SELECT * FROM players LIMIT 1 let request = Player.limit(1)
The selection defaults to all columns. This default can be changed for all requests by the
TableMapping.databaseSelection
property, or for individual requests with theTableMapping.select
method.Declaration
Swift
public static func limit(_ limit: Int, offset: Int? = nil) -> QueryInterfaceRequest<Self>
-
fetchCount(_:)
Extension methodThe number of records.
Declaration
Swift
public static func fetchCount(_ db: Database) throws -> Int
Parameters
db
A database connection.