ScopesTreeView
public struct 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]
-
The scopes defined on this row, recursively.
Declaration
Swift
public var names: Set<String> { get }
-
Returns the row associated with the given scope.
For example:
let request = Book.including(required: Book.author) let row = try Row.fetchOne(db, request)! print(row) // Prints [id:42 title:"Moby-Dick"] let authorRow = row.scopesTree["author"] print(authorRow) // Prints [id:1 name:"Herman Melville"]
Associated rows 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(row) // Prints [id:42 title:"Moby-Dick"] let countryRow = row.scopesTree["country"] print(countryRow) // Prints [code:"US" name:"United States"]
Nil is returned if the scope is not available.
Declaration
Swift
public subscript(name: String) -> Row? { get }