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
  • selectsRowID Default implementation

    This 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 method

    Returns a QueryInterfaceRequest with a matching predicate.

    // SELECT * FROM books WHERE books MATCH '...'
    var request = Book.matching(pattern)
    

    If the selectsRowID type property is true, then the selection includes the hidden rowid 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 method

    Creates a QueryInterfaceRequest which fetches all records.

    // SELECT * FROM persons
    var request = Person.all()
    

    If the selectsRowID type property is true, then the selection includes the hidden rowid column:

    // SELECT *, rowid FROM persons
    var request = Person.all()
    

    Declaration

    Swift

    public static func all() -> QueryInterfaceRequest<Self>
  • none() Extension method

    Creates a QueryInterfaceRequest which fetches no record.

    Declaration

    Swift

    public static func none() -> QueryInterfaceRequest<Self>
  • select(_:) Extension method

    Creates 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 method

    Creates 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 method

    Creates 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 method

    Creates 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 selectsRowID type property is true, then the selection includes the hidden rowid 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 method

    Creates 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 selectsRowID type property is true, then the selection includes the hidden rowid 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 method

    Creates a QueryInterfaceRequest sorted according to the provided orderings.

    // SELECT * FROM persons ORDER BY name
    var request = Person.order(Column("name"))
    

    If the selectsRowID type property is true, then the selection includes the hidden rowid 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 method

    Creates a QueryInterfaceRequest sorted according to the provided orderings.

    // SELECT * FROM persons ORDER BY name
    var request = Person.order([Column("name")])
    

    If the selectsRowID type property is true, then the selection includes the hidden rowid 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 method

    Creates a QueryInterfaceRequest sorted according to sql.

    // SELECT * FROM persons ORDER BY name
    var request = Person.order(sql: "name")
    

    If the selectsRowID type property is true, then the selection includes the hidden rowid 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 method

    Creates a QueryInterfaceRequest which fetches limit rows, starting at offset.

    // SELECT * FROM persons LIMIT 1
    var request = Person.limit(1)
    

    If the selectsRowID type property is true, then the selection includes the hidden rowid 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 method

    The number of records.

    Declaration

    Swift

    public static func fetchCount(_ db: Database) throws -> Int

    Parameters

    db

    A database connection.

  • deleteAll(_:) Extension method

    Deletes 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 -> Int

    Parameters

    db

    A database connection.

    Return Value

    The number of deleted rows

  • deleteAll(_:keys:) Extension method

    Delete 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: DatabaseValueConvertible

    Parameters

    db

    A database connection.

    keys

    A sequence of primary keys.

    Return Value

    The number of deleted rows

  • deleteOne(_:key:) Extension method

    Delete 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 -> Bool

    Parameters

    db

    A database connection.

    key

    A primary key value.

    Return Value

    Whether a database row was deleted.

  • deleteAll(_:keys:) Extension method

    Delete 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 -> Int

    Parameters

    db

    A database connection.

    keys

    An array of key dictionaries.

    Return Value

    The number of deleted rows

  • deleteOne(_:key:) Extension method

    Delete 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 -> Bool

    Parameters

    db

    A database connection.

    key

    A dictionary of values.

    Return Value

    Whether a database row was deleted.

  • primaryKeyRowComparator(_:) Extension method

    Returns 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.

    Declaration

    Swift

    public static func primaryKeyRowComparator(_ db: Database) throws -> (Row, Row) -> Bool