diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index e3415ff5a..dc9924dc9 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -1355,7 +1355,7 @@ private func prefetch( // useless, and we only need to select pivot columns: let originRelation = originRelation .unorderedUnlessLimited() // only preserve ordering in the CTE if limited - .removingChildrenForPrefetchedAssociations() + .removingPrefetchedAssociations() .selectOnly(leftColumns.map { SQLExpression.column($0).sqlSelection }) let originCTE = CommonTableExpression( named: "grdb_base", diff --git a/GRDB/QueryInterface/SQL/DatabasePromise.swift b/GRDB/QueryInterface/SQL/DatabasePromise.swift index 815e15f77..23d7a8b7a 100644 --- a/GRDB/QueryInterface/SQL/DatabasePromise.swift +++ b/GRDB/QueryInterface/SQL/DatabasePromise.swift @@ -42,3 +42,9 @@ struct DatabasePromise { } } } + +extension DatabasePromise: CustomStringConvertible { + var description: String { + "DatabasePromise<\(T.self)>" + } +} diff --git a/GRDB/QueryInterface/SQL/SQLAssociation.swift b/GRDB/QueryInterface/SQL/SQLAssociation.swift index aa106a319..4d9a8d30c 100644 --- a/GRDB/QueryInterface/SQL/SQLAssociation.swift +++ b/GRDB/QueryInterface/SQL/SQLAssociation.swift @@ -120,10 +120,10 @@ public struct _SQLAssociation { let reversedSteps = zip(steps, steps.dropFirst()) .map { (step, nextStep) in // Intermediate steps are not selected, and including(all:) - // children are useless: + // children can't impact the destination relation: let relation = step.relation .selectOnly([]) - .removingChildrenForPrefetchedAssociations() + .removingPrefetchedAssociations() // Don't interfere with user-defined keys that could be added later let key = step.key.with { diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index e4c687bf5..4bd764e29 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -530,11 +530,18 @@ extension SQLRelation { } } - func removingChildrenForPrefetchedAssociations() -> Self { - filteringChildren { - switch $0.kind { - case .all, .bridge: return false - case .oneRequired, .oneOptional: return true + /// Return a relation without any `.all` and `.bridge` children, recursively. + func removingPrefetchedAssociations() -> Self { + with { + $0.children = $0.children.compactMapValues { child in + switch child.kind { + case .all, .bridge: + return nil + case .oneRequired, .oneOptional: + return child.with { + $0.relation = $0.relation.removingPrefetchedAssociations() + } + } } } } diff --git a/GRDB/Utils/OrderedDictionary.swift b/GRDB/Utils/OrderedDictionary.swift index 3bd60eaf2..4f7d5242b 100644 --- a/GRDB/Utils/OrderedDictionary.swift +++ b/GRDB/Utils/OrderedDictionary.swift @@ -194,6 +194,19 @@ extension OrderedDictionary: Equatable where Value: Equatable { } } +extension OrderedDictionary: CustomStringConvertible { + var description: String { + let chunks = map { (key, value) in + "\(String(reflecting: key)): \(String(reflecting: value))" + } + if chunks.isEmpty { + return "[:]" + } else { + return "[\(chunks.joined(separator: ", "))]" + } + } +} + extension Dictionary { init(_ orderedDictionary: OrderedDictionary) { self = orderedDictionary.dictionary diff --git a/Tests/GRDBTests/AssociationPrefetchingRelationTests.swift b/Tests/GRDBTests/AssociationPrefetchingRelationTests.swift index cd12610f2..f67811c9f 100644 --- a/Tests/GRDBTests/AssociationPrefetchingRelationTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingRelationTests.swift @@ -690,4 +690,47 @@ class AssociationPrefetchingRelationTests: GRDBTestCase { } } } + + // Regression test for + func testIssue1315() throws { + struct A: TableRecord { + static let b = belongsTo(B.self) + } + struct B: TableRecord { + static let c = hasOne(C.self) + static let e = hasOne(E.self) + } + struct C: TableRecord { + static let ds = hasMany(D.self) + } + struct D: TableRecord { } + struct E: TableRecord { + static let fs = hasMany(F.self) + } + struct F: TableRecord { } + + try DatabaseQueue().write { db in + try db.execute(sql: """ + CREATE TABLE b (id INTEGER PRIMARY KEY); + CREATE TABLE a (id INTEGER PRIMARY KEY, bId INTEGER REFERENCES b(id)); + CREATE TABLE c (id INTEGER PRIMARY KEY, bId INTEGER REFERENCES b(id)); + CREATE TABLE d (id INTEGER PRIMARY KEY, cId INTEGER REFERENCES c(id)); + CREATE TABLE e (id INTEGER PRIMARY KEY, bId INTEGER REFERENCES b(id)); + CREATE TABLE f (id INTEGER PRIMARY KEY, eId INTEGER REFERENCES e(id)); + """) + + let region = try A + .including(required: A.b + .including(optional: B.c.including(all: C.ds)) + .including(optional: B.e.including(all: E.fs))) + .databaseRegion(db) + + XCTAssertTrue(region.isModified(byEventsOfKind: .insert(tableName: "a"))) + XCTAssertTrue(region.isModified(byEventsOfKind: .insert(tableName: "b"))) + XCTAssertTrue(region.isModified(byEventsOfKind: .insert(tableName: "c"))) + XCTAssertTrue(region.isModified(byEventsOfKind: .insert(tableName: "d"))) + XCTAssertTrue(region.isModified(byEventsOfKind: .insert(tableName: "e"))) + XCTAssertTrue(region.isModified(byEventsOfKind: .insert(tableName: "f"))) + } + } } diff --git a/Tests/GRDBTests/OrderedDictionaryTests.swift b/Tests/GRDBTests/OrderedDictionaryTests.swift index cc5823012..3e6b278d1 100644 --- a/Tests/GRDBTests/OrderedDictionaryTests.swift +++ b/Tests/GRDBTests/OrderedDictionaryTests.swift @@ -57,4 +57,33 @@ class OrderedDictionaryTests: GRDBTestCase { } } } + + func testDescription() { + do { + // One key/value pair + let dict: OrderedDictionary = ["toto": "foo"] + XCTAssertEqual(dict.description, #"["toto": "foo"]"#) + } + do { + // Two key/value pairs + let dict: OrderedDictionary = ["toto": "foo", "titi": "bar"] + XCTAssertEqual(dict.description, #"["toto": "foo", "titi": "bar"]"#) + } + } + + func testOrderedDictionaryDescriptionEqualsDictionaryDescription() { + struct Key: Hashable, CustomStringConvertible, CustomDebugStringConvertible { + var description: String { "Key description" } + var debugDescription: String { "Key debugDescription" } + } + struct Value: CustomStringConvertible, CustomDebugStringConvertible { + var description: String { "Value description" } + var debugDescription: String { "Value debugDescription" } + } + do { + let dict = [Key(): Value()] + let orderedDict: OrderedDictionary = [Key(): Value()] + XCTAssertEqual(String(describing: orderedDict), String(describing: dict)) + } + } }