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 Person.fetchOne(db, key: 123) // Person?
try Citizenship.fetchOne(db, key: ["personId": 12, "countryId": 45]) // Citizenship?
TableMapping is adopted by Record.
-
The name of the database table
Declaration
Swift
static var databaseTableName: String -
selectsRowIDDefault implementationThis flag tells whether the hidden
rowid
column should be fetched with other columns.Its default value is false:
// SELECT * FROM persons try Person.fetchAll(db)When true, the rowid column is fetched:
// SELECT *, rowid FROM persons try Person.fetchAll(db)Default Implementation
Default value: false.
Declaration
Swift
static var selectsRowID: Bool
-
matching(_:)Extension methodReturns a QueryInterfaceRequest with a matching predicate.
// SELECT * FROM books WHERE books MATCH '...' var request = Book.matching(pattern)If the
selectsRowIDtype property is true, then the selection includes the hiddenrowid
column:// SELECT *, rowid FROM books WHERE books MATCH '...' var request = Book.matching(pattern)If the search pattern is nil, the request does not match any database row.
Declaration
Swift
public static func matching(_ pattern: FTS3Pattern?) -> QueryInterfaceRequest<Self>
-
all()Extension methodCreates a QueryInterfaceRequest which fetches all records.
// SELECT * FROM persons var request = Person.all()If the
selectsRowIDtype property is true, then the selection includes the hiddenrowid
column:// SELECT *, rowid FROM persons var request = Person.all()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 persons var request = Person.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 persons var request = Person.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 persons var request = Person.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 persons WHERE email = 'arthur@example.com' var request = Person.filter(Column("email") == "arthur@example.com")If the
selectsRowIDtype property is true, then the selection includes the hiddenrowid
column:// SELECT *, rowid FROM persons WHERE email = 'arthur@example.com' var request = Person.filter(Column("email") == "arthur@example.com")Declaration
Swift
public static func filter(_ predicate: SQLExpressible) -> QueryInterfaceRequest<Self> -
filter(sql:arguments:)Extension methodCreates a QueryInterfaceRequest with the provided predicate.
// SELECT * FROM persons WHERE email = 'arthur@example.com' var request = Person.filter(sql: "email = ?", arguments: ["arthur@example.com"])If the
selectsRowIDtype property is true, then the selection includes the hiddenrowid
column:// SELECT *, rowid FROM persons WHERE email = 'arthur@example.com' var request = Person.filter(sql: "email = ?", arguments: ["arthur@example.com"])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 persons ORDER BY name var request = Person.order(Column("name"))If the
selectsRowIDtype property is true, then the selection includes the hiddenrowid
column:// SELECT *, rowid FROM persons ORDER BY name var request = Person.order(Column("name"))Declaration
Swift
public static func order(_ orderings: SQLOrderingTerm...) -> QueryInterfaceRequest<Self> -
order(_:)Extension methodCreates a QueryInterfaceRequest sorted according to the provided orderings.
// SELECT * FROM persons ORDER BY name var request = Person.order([Column("name")])If the
selectsRowIDtype property is true, then the selection includes the hiddenrowid
column:// SELECT *, rowid FROM persons ORDER BY name var request = Person.order([Column("name")])Declaration
Swift
public static func order(_ orderings: [SQLOrderingTerm]) -> QueryInterfaceRequest<Self> -
order(sql:arguments:)Extension methodCreates a QueryInterfaceRequest sorted according to sql.
// SELECT * FROM persons ORDER BY name var request = Person.order(sql: "name")If the
selectsRowIDtype property is true, then the selection includes the hiddenrowid
column:// SELECT *, rowid FROM persons ORDER BY name var request = Person.order(sql: "name")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 persons LIMIT 1 var request = Person.limit(1)If the
selectsRowIDtype property is true, then the selection includes the hiddenrowid
column:// SELECT *, rowid FROM persons LIMIT 1 var request = Person.limit(1)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 -> IntParameters
dbA database connection.
-
deleteAll(_:)Extension methodDeletes all records; returns the number of deleted rows.
Throws
A DatabaseError is thrown whenever an SQLite error occurs.Declaration
Swift
public static func deleteAll(_ db: Database) throws -> IntParameters
dbA database connection.
Return Value
The number of deleted rows
-
deleteAll(_:keys:)Extension methodDelete records identified by their primary keys; returns the number of deleted rows.
try Person.deleteAll(db, keys: [1, 2, 3])Declaration
Swift
public static func deleteAll<Sequence: Swift.Sequence>(_ db: Database, keys: Sequence) throws -> Int where Sequence.Iterator.Element: DatabaseValueConvertibleParameters
dbA database connection.
keysA sequence of primary keys.
Return Value
The number of deleted rows
-
deleteOne(_:key:)Extension methodDelete a record, identified by its primary key; returns whether a database row was deleted.
try Person.deleteOne(db, key: 123)Declaration
Swift
public static func deleteOne<PrimaryKeyType: DatabaseValueConvertible>(_ db: Database, key: PrimaryKeyType?) throws -> BoolParameters
dbA database connection.
keyA primary key value.
Return Value
Whether a database row was deleted.
-
deleteAll(_:keys:)Extension methodDelete records identified by the provided unique keys (primary key or any key with a unique index on it); returns the number of deleted rows.
try Person.deleteAll(db, keys: [["email": "a@example.com"], ["email": "b@example.com"]])Declaration
Swift
public static func deleteAll(_ db: Database, keys: [[String: DatabaseValueConvertible?]]) throws -> IntParameters
dbA database connection.
keysAn array of key dictionaries.
Return Value
The number of deleted rows
-
deleteOne(_:key:)Extension methodDelete a record, identified by a unique key (the primary key or any key with a unique index on it); returns whether a database row was deleted.
Person.deleteOne(db, key: ["name": Arthur"])Declaration
Swift
public static func deleteOne(_ db: Database, key: [String: DatabaseValueConvertible?]) throws -> BoolParameters
dbA database connection.
keyA dictionary of values.
Return Value
Whether a database row was deleted.
-
primaryKeyRowComparator(_:)Extension methodReturns a function that returns true if and only if two rows have the same primary key and both primary keys contain at least one non-null value.
try dbQueue.inDatabase { db in let comparator = try Person.primaryKeyRowComparator(db) let row0 = Row(["id": nil, "name": "Unsaved"]) let row1 = Row(["id": 1, "name": "Arthur"]) let row2 = Row(["id": 1, "name": "Arthur"]) let row3 = Row(["id": 2, "name": "Barbara"]) comparator(row0, row0) // false comparator(row1, row2) // true comparator(row1, row3) // false }Throws
A DatabaseError if table does not exist.Attention
This method is deprecated. It will be removed is the next major version of the library, without any replacement.Declaration
View on GitHub
Install in Dash
TableMapping Protocol Reference