Row

public final class Row : Equatable, Hashable, RandomAccessCollection, ExpressibleByDictionaryLiteral, CustomStringConvertible

A database row.

  • The number of columns in the row.

    Declaration

    Swift

    public let count: Int
  • Creates an empty row.

    Declaration

    Swift

    public convenience init()
  • Creates a row from a dictionary of values.

    Declaration

    Swift

    public convenience init(_ dictionary: [String: DatabaseValueConvertible?])
  • Creates a row from [AnyHashable: Any].

    The result is nil unless all dictionary keys are strings, and values adopt DatabaseValueConvertible.

    Declaration

    Swift

    public convenience init?(_ dictionary: [AnyHashable: Any])
  • 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().

    Declaration

    Swift

    public func copy() -> Row
  • 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.

    Declaration

    Swift

    public var columnNames: LazyMapCollection<Row, String>
  • Returns true if and only if the row has that column.

    This method is case-insensitive.

    Declaration

    Swift

    public func hasColumn(_ columnName: String) -> Bool
  • 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.

    Declaration

    Swift

    public subscript(_ index: Int) -> DatabaseValueConvertible?
  • Returns the value at given index, converted to the requested type.

    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. Otherwise the SQLite value is converted to the requested type Value. Should this conversion fail, a fatal error is raised.

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value?
  • Returns the value at given index, converted to the requested type.

    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. Otherwise the SQLite value is converted to the requested type Value. Should this conversion fail, a fatal error is raised.

    This method exists as an optimization opportunity for types that adopt StatementColumnConvertible. It may trigger SQLite built-in conversions (see https://www.sqlite.org/datatype3.html).

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value?
  • Returns the value at given index, converted to the requested type.

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

    This method crashes if the fetched SQLite value is NULL, or if the SQLite value can not be converted to Value.

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible>(_ index: Int) -> Value
  • Returns the value at given index, converted to the requested type.

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

    This method crashes if the fetched SQLite value is NULL, or if the SQLite value can not be converted to Value.

    This method exists as an optimization opportunity for types that adopt StatementColumnConvertible. It may trigger SQLite built-in conversions (see https://www.sqlite.org/datatype3.html).

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ index: Int) -> Value
  • Returns Int64, Double, String, Data or nil, depending on the value stored at the given column.

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

    The result is nil if the row does not contain the column.

    Declaration

    Swift

    public subscript(_ columnName: String) -> DatabaseValueConvertible?
  • Returns the value at given column, converted to the requested type.

    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. Otherwise the SQLite value is converted to the requested type Value. Should this conversion fail, a fatal error is raised.

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible>(_ columnName: String) -> Value?
  • Returns the value at given column, converted to the requested type.

    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. Otherwise the SQLite value is converted to the requested type Value. Should this conversion fail, a fatal error is raised.

    This method exists as an optimization opportunity for types that adopt StatementColumnConvertible. It may trigger SQLite built-in conversions (see https://www.sqlite.org/datatype3.html).

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ columnName: String) -> Value?
  • Returns the value at given column, converted to the requested type.

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

    If the row does not contain the column, a fatal error is raised.

    This method crashes if the fetched SQLite value is NULL, or if the SQLite value can not be converted to Value.

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible>(_ columnName: String) -> Value
  • Returns the value at given column, converted to the requested type.

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

    If the row does not contain the column, a fatal error is raised.

    This method crashes if the fetched SQLite value is NULL, or if the SQLite value can not be converted to Value.

    This method exists as an optimization opportunity for types that adopt StatementColumnConvertible. It may trigger SQLite built-in conversions (see https://www.sqlite.org/datatype3.html).

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ columnName: String) -> Value
  • Returns Int64, Double, String, NSData or nil, depending on the value stored at the given column.

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

    The result is nil if the row does not contain the column.

    Declaration

    Swift

    public subscript(_ column: Column) -> DatabaseValueConvertible?
  • Returns the value at given column, converted to the requested type.

    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. Otherwise the SQLite value is converted to the requested type Value. Should this conversion fail, a fatal error is raised.

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible>(_ column: Column) -> Value?
  • Returns the value at given column, converted to the requested type.

    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. Otherwise the SQLite value is converted to the requested type Value. Should this conversion fail, a fatal error is raised.

    This method exists as an optimization opportunity for types that adopt StatementColumnConvertible. It may trigger SQLite built-in conversions (see https://www.sqlite.org/datatype3.html).

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ column: Column) -> Value?
  • Returns the value at given column, converted to the requested type.

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

    If the row does not contain the column, a fatal error is raised.

    This method crashes if the fetched SQLite value is NULL, or if the SQLite value can not be converted to Value.

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible>(_ column: Column) -> Value
  • Returns the value at given column, converted to the requested type.

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

    If the row does not contain the column, a fatal error is raised.

    This method crashes if the fetched SQLite value is NULL, or if the SQLite value can not be converted to Value.

    This method exists as an optimization opportunity for types that adopt StatementColumnConvertible. It may trigger SQLite built-in conversions (see https://www.sqlite.org/datatype3.html).

    Declaration

    Swift

    public subscript<Value: DatabaseValueConvertible & StatementColumnConvertible>(_ column: Column) -> Value
  • 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.

    Declaration

    Swift

    public func dataNoCopy(atIndex index: Int) -> Data?
  • 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.

    Declaration

    Swift

    public func dataNoCopy(named columnName: String) -> Data?
  • Returns the optional NSData 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 NSData, a fatal error is raised.

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

    Declaration

    Swift

    public func dataNoCopy(_ column: Column) -> Data?
  • 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.

    Declaration

    Swift

    public var databaseValues: LazyMapCollection<Row, DatabaseValue>
  • Returns a scoped row, if the row was fetched along with a row adapter that defines this scope.

    // Two adapters
    let fooAdapter = ColumnMapping(["value": "foo"])
    let barAdapter = ColumnMapping(["value": "bar"])
    
    // Define scopes
    let adapter = ScopeAdapter([
        "foo": fooAdapter,
        "bar": barAdapter])
    
    // Fetch
    let sql = "SELECT 'foo' AS foo, 'bar' AS bar"
    let row = try Row.fetchOne(db, sql, adapter: adapter)!
    
    // Scoped rows:
    if let fooRow = row.scoped(on: "foo") {
        fooRow["value"]    // "foo"
    }
    if let barRow = row.scopeed(on: "bar") {
        barRow["value"]    // "bar"
    }
    

    Declaration

    Swift

    public func scoped(on name: String) -> Row?
  • Returns a cursor over rows fetched from a prepared statement.

    let statement = try db.makeSelectStatement("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 dispath queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> RowCursor

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched rows.

  • Returns an array of rows fetched from a prepared statement.

    let statement = try db.makeSelectStatement("SELECT ...")
    let rows = try Row.fetchAll(statement)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Row]

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of rows.

  • Returns a single row fetched from a prepared statement.

    let statement = try db.makeSelectStatement("SELECT ...")
    let row = try Row.fetchOne(statement)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Row?

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An optional row.

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

    let idColumn = Column("id")
    let nameColumn = Column("name")
    let request = Player.select(idColumn, nameColumn)
    let rows = try Row.fetchCursor(db) // 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 dispath queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(_ db: Database, _ request: Request) throws -> RowCursor

    Parameters

    db

    A database connection.

    request

    A fetch request.

    Return Value

    A cursor over fetched rows.

  • Returns an array of rows fetched from a fetch request.

    let idColumn = Column("id")
    let nameColumn = Column("name")
    let request = Player.select(idColumn, nameColumn)
    let rows = try Row.fetchAll(db, request)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(_ db: Database, _ request: Request) throws -> [Row]

    Parameters

    db

    A database connection.

  • Returns a single row fetched from a fetch request.

    let idColumn = Column("id")
    let nameColumn = Column("name")
    let request = Player.select(idColumn, nameColumn)
    let row = try Row.fetchOne(db, request)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchOne(_ db: Database, _ request: Request) throws -> Row?

    Parameters

    db

    A database connection.

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

    let rows = try Row.fetchCursor(db, "SELECT id, name FROM players") // 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 dispath queue.

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> RowCursor

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A cursor over fetched rows.

  • Returns an array of rows fetched from an SQL query.

    let rows = try Row.fetchAll(db, "SELECT ...")
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> [Row]

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of rows.

  • Returns a single row fetched from an SQL query.

    let row = try Row.fetchOne(db, "SELECT ...")
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchOne(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Row?

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An optional row.

  • 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 <Row foo:1 foo:"bar" baz:NULL>
    

    Declaration

    Swift

    public convenience init(dictionaryLiteral elements: (String, DatabaseValueConvertible?)...)