RowAdapter

public protocol RowAdapter

RowAdapter is a protocol that helps two incompatible row interfaces working together.

GRDB ships with four concrete types that adopt the RowAdapter protocol:

  • ColumnMapping: renames row columns
  • SuffixRowAdapter: hides the first columns of a row
  • RangeRowAdapter: only exposes a range of columns
  • ScopeAdapter: groups several adapters together to define named scopes

To use a row adapter, provide it to any method that fetches:

let adapter = SuffixRowAdapter(fromIndex: 2)
let sql = "SELECT 1 AS foo, 2 AS bar, 3 AS baz"

// [baz:3]
try Row.fetchOne(db, sql: sql, adapter: adapter)
  • Experimental

    You never call this method directly. It is called for you whenever an adapter has to be applied.

    The result is a value that adopts LayoutedRowAdapter, such as LayoutedColumnMapping.

    For example:

    // An adapter that turns any row to a row that contains a single
    // column named "foo" whose value is the leftmost value of the
    // base row.
    struct FirstColumnAdapter : RowAdapter {
        func layoutedAdapter(from layout: RowLayout) throws -> LayoutedRowAdapter {
            return LayoutedColumnMapping(layoutColumns: [(0, "foo")])
        }
    }
    
    // [foo:1]
    try Row.fetchOne(db, sql: "SELECT 1, 2, 3", adapter: FirstColumnAdapter())
    

    Declaration

    Swift

    func layoutedAdapter(from layout: RowLayout) throws -> LayoutedRowAdapter
  • addingScopes(_:) Extension method

    Returns an adapter based on self, with added scopes.

    If self already defines scopes, the added scopes replace eventual existing scopes with the same name.

    Declaration

    Swift

    public func addingScopes(_ scopes: [String : RowAdapter]) -> RowAdapter

    Parameters

    scopes

    A dictionary that maps scope names to row adapters.