Row

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

A database row.

  • The number of columns in the row.

    Declaration

    Swift

    public let count: Int
  • 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"]
    

    Declaration

    Swift

    public internal(set) var prefetchedRows: Row.PrefetchedRowsView { get }

Building rows

  • 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])
  • 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]
    

    Declaration

    Swift

    public convenience init(dictionaryLiteral elements: (String, DatabaseValueConvertible?)...)
  • 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

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.

    Declaration

    Swift

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

    This method is case-insensitive.

    Declaration

    Swift

    public func hasColumn(_ columnName: String) -> Bool

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
    

    Declaration

    Swift

    public var containsNonNullValue: Bool { get }
  • 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.

    Declaration

    Swift

    public func hasNull(atIndex index: Int) -> 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? { get }
  • 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

    @inlinable
    public subscript<Value>(index: Int) -> Value? where Value : DatabaseValueConvertible { get }
  • 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

    @inline(__always)
    @inlinable
    public subscript<Value>(index: Int) -> Value? where Value : DatabaseValueConvertible, Value : StatementColumnConvertible { get }
  • 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

    @inlinable
    public subscript<Value>(index: Int) -> Value where Value : DatabaseValueConvertible { get }
  • 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

    @inline(__always)
    @inlinable
    public subscript<Value>(index: Int) -> Value where Value : DatabaseValueConvertible, Value : StatementColumnConvertible { get }
  • 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? { get }
  • 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

    @inlinable
    public subscript<Value>(columnName: String) -> Value? where Value : DatabaseValueConvertible { get }
  • 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

    @inlinable
    public subscript<Value>(columnName: String) -> Value? where Value : DatabaseValueConvertible, Value : StatementColumnConvertible { get }
  • 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

    @inlinable
    public subscript<Value>(columnName: String) -> Value where Value : DatabaseValueConvertible { get }
  • 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

    @inlinable
    public subscript<Value>(columnName: String) -> Value where Value : DatabaseValueConvertible, Value : StatementColumnConvertible { get }
  • 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: Column) -> DatabaseValueConvertible? where Column : ColumnExpression { get }
  • 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

    @inlinable
    public subscript<Value, Column>(column: Column) -> Value? where Value : DatabaseValueConvertible, Column : ColumnExpression { get }
  • 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

    @inlinable
    public subscript<Value, Column>(_ column: Column)
    -> Value?
    where
        Value: DatabaseValueConvertible & StatementColumnConvertible,
        Column: ColumnExpression
  • 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

    @inlinable
    public subscript<Value, Column>(column: Column) -> Value where Value : DatabaseValueConvertible, Column : ColumnExpression { get }
  • 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

    @inlinable
    public subscript<Value, Column>(_ column: Column)
    -> Value
    where
        Value: DatabaseValueConvertible & StatementColumnConvertible,
        Column: ColumnExpression
  • 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: Column) -> Data? where Column : ColumnExpression

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.

    Declaration

    Swift

    public var databaseValues: LazyMapCollection<Row, DatabaseValue> { get }

Extracting Records

  • Returns the record associated with the given scope.

    For example:

    let request = Book.including(required: Book.author)
    let row = try Row.fetchOne(db, request)!
    
    print(Book(row: row).title)
    // Prints "Moby-Dick"
    
    let author: Author = row["author"]
    print(author.name)
    // Prints "Herman Melville"
    

    Associated records stored in nested associations are available, too:

    let request = Book.including(required: Book.author.including(required: Author.country))
    let row = try Row.fetchOne(db, request)!
    
    print(Book(row: row).title)
    // Prints "Moby-Dick"
    
    let country: Country = row["country"]
    print(country.name)
    // Prints "United States"
    

    A fatal error is raised if the scope is not available, or contains only null values.

    See https://github.com/groue/GRDB.swift/blob/master/README.md#joined-queries-support for more information.

    Declaration

    Swift

    public subscript<Record>(scope: String) -> Record where Record : FetchableRecord { get }
  • Returns the eventual record associated with the given scope.

    For example:

    let request = Book.including(optional: Book.author)
    let row = try Row.fetchOne(db, request)!
    
    print(Book(row: row).title)
    // Prints "Moby-Dick"
    
    let author: Author? = row["author"]
    print(author.name)
    // Prints "Herman Melville"
    

    Associated records stored in nested associations are available, too:

    let request = Book.including(optional: Book.author.including(optional: Author.country))
    let row = try Row.fetchOne(db, request)!
    
    print(Book(row: row).title)
    // Prints "Moby-Dick"
    
    let country: Country? = row["country"]
    print(country.name)
    // Prints "United States"
    

    Nil is returned if the scope is not available, or contains only null values.

    See https://github.com/groue/GRDB.swift/blob/master/README.md#joined-queries-support for more information.

    Declaration

    Swift

    public subscript<Record>(scope: String) -> Record? where Record : FetchableRecord { get }
  • Returns the records encoded in the given prefetched rows.

    For example:

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

    Declaration

    Swift

    public subscript<Collection>(_ key: String)
    -> Collection
    where
        Collection: RangeReplaceableCollection,
        Collection.Element: FetchableRecord
  • Returns the set of records encoded in the given prefetched rows.

    For example:

    let request = Author.including(all: Author.books)
    let row = try Row.fetchOne(db, request)!
    
    print(Author(row: row).name)
    // Prints "Herman Melville"
    
    let books: Set<Book> = row["books"]
    print(books.first!.title)
    // Prints "Moby-Dick"
    

    Declaration

    Swift

    public subscript<Record>(key: String) -> Set<Record> where Record : FetchableRecord, Record : Hashable { get }

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
    

    Declaration

    Swift

    public var scopes: ScopesView { get }
  • 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]
    

    Declaration

    Swift

    public var scopesTree: ScopesTreeView { get }
  • 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"])
    

    Declaration

    Swift

    public var unscoped: Row { get }
  • 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.

    Declaration

    Swift

    public var unadapted: Row { get }

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.

    Declaration

    Swift

    public static func fetchCursor(
        _ statement: Statement,
        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.makeStatement(sql: "SELECT ...")
    let rows = try Row.fetchAll(statement)
    

    Throws

    A DatabaseError is thrown whenever an SQLite error occurs.

    Declaration

    Swift

    public static func fetchAll(
        _ statement: Statement,
        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 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.

    Declaration

    Swift

    public static func fetchSet(
        _ statement: Statement,
        arguments: StatementArguments? = nil,
        adapter: RowAdapter? = nil)
    throws -> Set<Row>

    Parameters

    statement

    The statement to run.

    arguments

    Optional statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A set of rows.

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

    Declaration

    Swift

    public static func fetchOne(
        _ statement: Statement,
        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.

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.

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

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

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An array of rows.

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

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    A set of rows.

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

    Declaration

    Swift

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

    Parameters

    db

    A database connection.

    sql

    An SQL query.

    arguments

    Statement arguments.

    adapter

    Optional RowAdapter

    Return Value

    An optional row.

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.

    Declaration

    Swift

    public static func fetchCursor<R>(_ db: Database, _ request: R) throws -> RowCursor where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    A cursor over fetched rows.

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

    Declaration

    Swift

    public static func fetchAll<R>(_ db: Database, _ request: R) throws -> [Row] where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    An array of rows.

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

    Declaration

    Swift

    public static func fetchSet<R>(_ db: Database, _ request: R) throws -> Set<Row> where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    A set of rows.

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

    Declaration

    Swift

    public static func fetchOne<R>(_ db: Database, _ request: R) throws -> Row? where R : FetchRequest

    Parameters

    db

    A database connection.

    request

    A FetchRequest.

    Return Value

    An optional row.

Row as a Collection of (ColumnName, DatabaseValue) Pairs

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

    Declaration

    Swift

    public struct ScopesView : Collection

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

    Declaration

    Swift

    public struct ScopesTreeView

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

    Declaration

    Swift

    public struct PrefetchedRowsView : Equatable