Row

A database row.

  • The number of columns in the row.

  • A view on the prefetched associated rows.

    For example:

    let request = Author.including(all: Author.books)
    let row = try Row.fetchOne(db, request)!
    
    print(row)
    // Prints [id:1 name:"Herman Melville"]
    
    let bookRows = row.prefetchedRows["books"]
    print(bookRows[0])
    // Prints [id:42 title:"Moby-Dick"]
    

Building rows

  • Creates an empty row.

  • Creates a row from a dictionary of values.

  • Creates a row initialized with elements. Column order is preserved, and duplicated columns names are allowed.

    let row: Row = ["foo": 1, "foo": "bar", "baz": nil]
    print(row)
    // Prints [foo:1 foo:"bar" baz:NULL]
    
  • Returns an immutable copy of the row.

    For performance reasons, rows fetched from a cursor are reused during the iteration of a query: make sure to make a copy of it whenever you want to keep a specific one: row.copy().

Columns

  • The names of columns in the row.

    Columns appear in the same order as they occur as the .0 member of column-value pairs in self.

  • Returns true if and only if the row has that column.

    This method is case-insensitive.

Extracting Values

  • Returns true if and only if one column contains a non-null value, or if the row was fetched with a row adapter that defines a scoped row that contains a non-null value.

    For example:

    let row = try Row.fetchOne(db, sql: "SELECT 'foo', 1")!
    row.containsNonNullValue // true
    
    let row = try Row.fetchOne(db, sql: "SELECT NULL, NULL")!
    row.containsNonNullValue // false
    
  • Returns true if the row contains null at given index.

    Indexes span from 0 for the leftmost column to (row.count - 1) for the righmost column.

    This method is equivalent to row[index] == nil, but may be preferred in performance-critical code because it can avoid decoding database values.

  • Returns Int64, Double, String, Data or nil, depending on the value stored at the given index.

    Indexes span from 0 for the leftmost column to (row.count - 1) for the righmost column.

  • Returns the optional Data at given index.

    Indexes span from 0 for the leftmost column to (row.count - 1) for the righmost column.

    If the SQLite value is NULL, the result is nil. If the SQLite value can not be converted to Data, a fatal error is raised.

    The returned data does not owns its bytes: it must not be used longer than the row’s lifetime.

  • Returns the optional Data at given column.

    Column name lookup is case-insensitive, and when several columns have the same name, the leftmost column is considered.

    If the column is missing or if the SQLite value is NULL, the result is nil. If the SQLite value can not be converted to Data, a fatal error is raised.

    The returned data does not owns its bytes: it must not be used longer than the row’s lifetime.

  • Returns the optional Data at given column.

    Column name lookup is case-insensitive, and when several columns have the same name, the leftmost column is considered.

    If the column is missing or if the SQLite value is NULL, the result is nil. If the SQLite value can not be converted to Data, a fatal error is raised.

    The returned data does not owns its bytes: it must not be used longer than the row’s lifetime.

Extracting DatabaseValue

  • The database values in the row.

    Values appear in the same order as they occur as the .1 member of column-value pairs in self.

Scopes

  • Returns a view on the scopes defined by row adapters.

    For example:

    // Define a tree of nested scopes
    let adapter = ScopeAdapter([
        "foo": RangeRowAdapter(0..<1),
        "bar": RangeRowAdapter(1..<2).addingScopes([
            "baz" : RangeRowAdapter(2..<3)])])
    
    // Fetch
    let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz"
    let row = try Row.fetchOne(db, sql: sql, adapter: adapter)!
    
    row.scopes.count  // 2
    row.scopes.names  // ["foo", "bar"]
    
    row.scopes["foo"] // [foo:1]
    row.scopes["bar"] // [bar:2]
    row.scopes["baz"] // nil
    
  • Returns a view on the scopes tree defined by row adapters.

    For example:

    // Define a tree of nested scopes
    let adapter = ScopeAdapter([
        "foo": RangeRowAdapter(0..<1),
        "bar": RangeRowAdapter(1..<2).addingScopes([
            "baz" : RangeRowAdapter(2..<3)])])
    
    // Fetch
    let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz"
    let row = try Row.fetchOne(db, sql: sql, adapter: adapter)!
    
    row.scopesTree.names  // ["foo", "bar", "baz"]
    
    row.scopesTree["foo"] // [foo:1]
    row.scopesTree["bar"] // [bar:2]
    row.scopesTree["baz"] // [baz:3]
    
  • Returns a copy of the row, without any scopes.

    This property can turn out useful when you want to test the content of adapted rows, such as rows fetched from joined requests.

    let row = ...
    // Failure because row equality tests for row scopes:
    XCTAssertEqual(row, ["id": 1, "name": "foo"])
    // Success:
    XCTAssertEqual(row.unscoped, ["id": 1, "name": "foo"])
    
  • Return the raw row fetched from the database.

    This property can turn out useful when you debug the consumption of adapted rows, such as rows fetched from joined requests.

Fetching From Prepared Statement

  • Returns a cursor over rows fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT ...")
    let rows = try Row.fetchCursor(statement) // RowCursor
    while let row = try rows.next() { // Row
        let id: Int64 = row[0]
        let name: String = row[1]
    }
    

    Fetched rows are reused during the cursor iteration: don’t turn a row cursor into an array with Array(rows) or rows.filter { ... } since you would not get the distinct rows you expect. Use Row.fetchAll(...) instead.

    For the same reason, make sure you make a copy whenever you extract a row for later use: row.copy().

    If the database is modified during the cursor iteration, the remaining elements are undefined.

    The cursor must be iterated in a protected dispatch queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns an array of rows fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT ...")
    let rows = try Row.fetchAll(statement)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns a set of rows fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT ...")
    let rows = try Row.fetchSet(statement)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns a single row fetched from a prepared statement.

    let statement = try db.makeStatement(sql: "SELECT ...")
    let row = try Row.fetchOne(statement)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

Fetching From SQL

  • Returns a cursor over rows fetched from an SQL query.

    let rows = try Row.fetchCursor(db, sql: "SELECT id, name FROM player") // RowCursor
    while let row = try rows.next() { // Row
        let id: Int64 = row[0]
        let name: String = row[1]
    }
    

    Fetched rows are reused during the cursor iteration: don’t turn a row cursor into an array with Array(rows) or rows.filter { ... } since you would not get the distinct rows you expect. Use Row.fetchAll(...) instead.

    For the same reason, make sure you make a copy whenever you extract a row for later use: row.copy().

    If the database is modified during the cursor iteration, the remaining elements are undefined.

    The cursor must be iterated in a protected dispatch queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns an array of rows fetched from an SQL query.

    let rows = try Row.fetchAll(db, sql: "SELECT id, name FROM player") // [Row]
    for row in rows {
        let id: Int64 = row[0]
        let name: String = row[1]
    }
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns a set of rows fetched from an SQL query.

    let rows = try Row.fetchSet(db, sql: "SELECT id, name FROM player") // Set<Row>
    for row in rows {
        let id: Int64 = row[0]
        let name: String = row[1]
    }
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns a single row fetched from an SQL query.

    let row = try Row.fetchOne(db, sql: "SELECT id, name FROM player") // Row?
    if let row = row {
        let id: Int64 = row[0]
        let name: String = row[1]
    }
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

Fetching From FetchRequest

  • Returns a cursor over rows fetched from a fetch request.

    let request = Player.all()
    let rows = try Row.fetchCursor(db, request) // RowCursor
    while let row = try rows.next() { // Row
        let id: Int64 = row["id"]
        let name: String = row["name"]
    }
    

    Fetched rows are reused during the cursor iteration: don’t turn a row cursor into an array with Array(rows) or rows.filter { ... } since you would not get the distinct rows you expect. Use Row.fetchAll(...) instead.

    For the same reason, make sure you make a copy whenever you extract a row for later use: row.copy().

    If the database is modified during the cursor iteration, the remaining elements are undefined.

    The cursor must be iterated in a protected dispatch queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns an array of rows fetched from a fetch request.

    let request = Player.all()
    let rows = try Row.fetchAll(db, request)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns a set of rows fetched from a fetch request.

    let request = Player.all()
    let rows = try Row.fetchSet(db, request)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.
  • Returns a single row fetched from a fetch request.

    let request = Player.filter(key: 1)
    let row = try Row.fetchOne(db, request)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

Row.ScopesView

  • A view of the scopes defined by row adapters. It is a collection of tuples made of a scope name and a scoped row, which behaves like a dictionary.

    For example:

    // Define a tree of nested scopes
    let adapter = ScopeAdapter([
        "foo": RangeRowAdapter(0..<1),
        "bar": RangeRowAdapter(1..<2).addingScopes([
            "baz" : RangeRowAdapter(2..<3)])])
    
    // Fetch
    let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz"
    let row = try Row.fetchOne(db, sql: sql, adapter: adapter)!
    
    row.scopes.count  // 2
    row.scopes.names  // ["foo", "bar"]
    
    row.scopes["foo"] // [foo:1]
    row.scopes["bar"] // [bar:2]
    row.scopes["baz"] // nil
    
    See more

Row.ScopesTreeView

  • A view on the scopes tree defined by row adapters.

    For example:

    // Define a tree of nested scopes
    let adapter = ScopeAdapter([
        "foo": RangeRowAdapter(0..<1),
        "bar": RangeRowAdapter(1..<2).addingScopes([
            "baz" : RangeRowAdapter(2..<3)])])
    
    // Fetch
    let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz"
    let row = try Row.fetchOne(db, sql: sql, adapter: adapter)!
    
    row.scopesTree.names  // ["foo", "bar", "baz"]
    
    row.scopesTree["foo"] // [foo:1]
    row.scopesTree["bar"] // [bar:2]
    row.scopesTree["baz"] // [baz:3]
    
    See more

Row.PrefetchedRowsView

  • A view on the prefetched associated rows.

    For example:

    let request = Author.including(all: Author.books)
    let row = try Row.fetchOne(db, request)!
    
    print(row)
    // Prints [id:1 name:"Herman Melville"]
    
    let bookRows = row.prefetchedRows["books"]
    print(bookRows[0])
    // Prints [id:42 title:"Moby-Dick"]
    
    See more