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 implementation

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

    Returns 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 the TableMapping.select method.

    Declaration

    Swift

    public static func matching(_ pattern: FTS3Pattern?) -> QueryInterfaceRequest<Self>
  • all() Extension method

    Creates 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 the TableMapping.select method.

    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 players
    let request = Player.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 players
    let request = Player.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 players
    let request = Player.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 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 the TableMapping.select method.

    Declaration

    Swift

    public static func filter(_ predicate: SQLExpressible) -> QueryInterfaceRequest<Self>
  • filter(sql:arguments:) Extension method

    Creates 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 the TableMapping.select method.

    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 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 the TableMapping.select method.

    Declaration

    Swift

    public static func order(_ orderings: SQLOrderingTerm...) -> QueryInterfaceRequest<Self>
  • order(_:) Extension method

    Creates 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 the TableMapping.select method.

    Declaration

    Swift

    public static func order(_ orderings: [SQLOrderingTerm]) -> QueryInterfaceRequest<Self>
  • order(sql:arguments:) Extension method

    Creates 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 the TableMapping.select method.

    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 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 the TableMapping.select method.

    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.