From a45065c34015742e7159ade567380d77c6c00a73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 5 May 2019 19:31:43 +0200 Subject: [PATCH] Fix failing tests for consumption of prefetched rows --- GRDB/Core/Row.swift | 25 +++++++++++-------- GRDB/Core/RowAdapter.swift | 4 +-- .../AssociationPrefetchingTests.swift | 6 ++--- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 2ddef3a68..6809bd78e 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -566,7 +566,7 @@ extension Row { /// row.scopes["bar"] // [bar:2] /// row.scopes["baz"] // nil public var scopes: ScopesView { - return impl.scopes + return impl.scopes(prefetches: prefetches) } /// Returns a view on the scopes tree defined by row adapters. @@ -1154,19 +1154,21 @@ extension Row { public typealias Index = Dictionary.Index private let row: Row private let scopes: [String: LayoutedRowAdapter] - + private let prefetches: [String: Row.Prefetch] + /// The scopes defined on this row. public var names: Dictionary.Keys { return scopes.keys } init() { - self.init(row: Row(), scopes: [:]) + self.init(row: Row(), scopes: [:], prefetches: [:]) } - init(row: Row, scopes: [String: LayoutedRowAdapter]) { + init(row: Row, scopes: [String: LayoutedRowAdapter], prefetches: [String: Row.Prefetch]) { self.row = row self.scopes = scopes + self.prefetches = prefetches } /// :nodoc: @@ -1187,16 +1189,17 @@ extension Row { /// :nodoc: public subscript(position: Index) -> (name: String, row: Row) { let (name, adapter) = scopes[position] - return (name: name, row: Row(base: row, adapter: adapter)) + let adaptedRow = Row(base: row, adapter: adapter) + if let prefetch = prefetches[name] { + adaptedRow.prefetches = prefetch.prefetches + } + return (name: name, row: adaptedRow) } /// Returns the row associated with the given scope, or nil if the /// scope is not defined. public subscript(_ name: String) -> Row? { - guard let adapter = scopes[name] else { - return nil - } - return Row(base: row, adapter: adapter) + return scopes.index(forKey: name).map { self[$0].row } } } } @@ -1296,7 +1299,7 @@ extension Row { protocol RowImpl { var count: Int { get } var isFetched: Bool { get } - var scopes: Row.ScopesView { get } + func scopes(prefetches: [String: Row.Prefetch]) -> Row.ScopesView func columnName(atUncheckedIndex index: Int) -> String func hasNull(atUncheckedIndex index:Int) -> Bool func databaseValue(atUncheckedIndex index: Int) -> DatabaseValue @@ -1329,7 +1332,7 @@ extension RowImpl { return row } - var scopes: Row.ScopesView { + func scopes(prefetches: [String: Row.Prefetch]) -> Row.ScopesView { // unless customized, assume unuscoped row (see AdaptedRowImpl for customization) return Row.ScopesView() } diff --git a/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index fe80eeb2d..3432eb19d 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -489,8 +489,8 @@ struct AdaptedRowImpl : RowImpl { return base.isFetched } - var scopes: Row.ScopesView { - return Row.ScopesView(row: base, scopes: adapter.scopes) + func scopes(prefetches: [String: Row.Prefetch]) -> Row.ScopesView { + return Row.ScopesView(row: base, scopes: adapter.scopes, prefetches: prefetches) } func hasNull(atUncheckedIndex index: Int) -> Bool { diff --git a/Tests/GRDBTests/AssociationPrefetchingTests.swift b/Tests/GRDBTests/AssociationPrefetchingTests.swift index 591163c85..7ae1889f5 100644 --- a/Tests/GRDBTests/AssociationPrefetchingTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingTests.swift @@ -1042,7 +1042,7 @@ class AssociationPrefetchingTests: GRDBTestCase { XCTAssertEqual(rows[0].scopes["a"]!.prefetchTree.keys, ["cs"]) XCTAssertEqual(rows[0].scopes["a"]!.prefetchTree["cs"]!.count, 1) XCTAssertEqual(rows[0].scopes["a"]!.prefetchTree["cs"]![0], ["colc1": 7, "colc2": 1, "grdb_cola1": 1]) // TODO: remove grdb_ column? - + XCTAssertEqual(rows[1].unscoped, ["colb1": 5, "colb2": 1, "colb3": "b2"]) XCTAssertEqual(rows[1].prefetchTree.keys, ["cs"]) XCTAssertEqual(rows[1].prefetchTree["cs"]!.count, 1) @@ -1052,7 +1052,7 @@ class AssociationPrefetchingTests: GRDBTestCase { XCTAssertEqual(rows[1].scopes["a"]!.prefetchTree.keys, ["cs"]) XCTAssertEqual(rows[1].scopes["a"]!.prefetchTree["cs"]!.count, 1) XCTAssertEqual(rows[1].scopes["a"]!.prefetchTree["cs"]![0], ["colc1": 7, "colc2": 1, "grdb_cola1": 1]) // TODO: remove grdb_ column? - + XCTAssertEqual(rows[2].unscoped, ["colb1": 6, "colb2": 2, "colb3": "b3"]) XCTAssertEqual(rows[2].prefetchTree.keys, ["cs"]) XCTAssertEqual(rows[2].prefetchTree["cs"]!.count, 2) @@ -1064,7 +1064,7 @@ class AssociationPrefetchingTests: GRDBTestCase { XCTAssertEqual(rows[2].scopes["a"]!.prefetchTree["cs"]!.count, 2) XCTAssertEqual(rows[2].scopes["a"]!.prefetchTree["cs"]![0], ["colc1": 8, "colc2": 2, "grdb_cola1": 2]) // TODO: remove grdb_ column? XCTAssertEqual(rows[2].scopes["a"]!.prefetchTree["cs"]![1], ["colc1": 9, "colc2": 2, "grdb_cola1": 2]) // TODO: remove grdb_ column? - + XCTAssertEqual(rows[3].unscoped, ["colb1": 14, "colb2": nil, "colb3": "b4"]) XCTAssertEqual(rows[3].prefetchTree.keys, ["cs"]) XCTAssertEqual(rows[3].prefetchTree["cs"]!.count, 0)