ScopeAdapter
public struct ScopeAdapter : RowAdapter
ScopeAdapter is a row adapter that lets you define scopes on rows.
// 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: sql, adapter: adapter)!
// Scoped rows:
if let fooRow = row.scopes["foo"] {
fooRow["value"] // "foo"
}
if let barRow = row.scopes["bar"] {
barRow["value"] // "bar"
}
-
Creates an adapter that preserves row contents and add scoped rows.
For example:
let adapter = ScopeAdapter(["suffix": SuffixRowAdapter(fromIndex: 1)]) let row = try Row.fetchOne(db, sql: "SELECT 1, 2, 3", adapter: adapter)! row // [1, 2, 3] row.scopes["suffix"] // [2, 3]
Declaration
Swift
public init(_ scopes: [String : RowAdapter])
Parameters
scopes
A dictionary that maps scope names to row adapters.
-
Creates an adapter based on the base adapter, and add scoped rows.
For example:
let baseAdapter = RangeRowAdapter(0..<1) let adapter = ScopeAdapter(base: baseAdapter, scopes: ["suffix": SuffixRowAdapter(fromIndex: 1)]) let row = try Row.fetchOne(db, sql: "SELECT 1, 2, 3", adapter: adapter)! row // [1] row.scopes["initial"] // [2, 3]
If the base adapter already defines scopes, the given scopes replace eventual existing scopes with the same name.
This initializer is equivalent to
baseAdapter.addingScopes(scopes)
.Declaration
Swift
public init(base: RowAdapter, scopes: [String : RowAdapter])
Parameters
base
A dictionary that maps scope names to row adapters.
scopes
A dictionary that maps scope names to row adapters.