mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Merge pull request #1317 from groue/dev/fix-1315
Fix removal of prefetched associations
This commit is contained in:
7 files changed
+106
-8
No files matched your search
@@ -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",
|
||||
|
||||
@@ -42,3 +42,9 @@ struct DatabasePromise<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension DatabasePromise: CustomStringConvertible {
|
||||
var description: String {
|
||||
"DatabasePromise<\(T.self)>"
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Key, Value>) {
|
||||
self = orderedDictionary.dictionary
|
||||
|
||||
@@ -690,4 +690,47 @@ class AssociationPrefetchingRelationTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Regression test for <https://github.com/groue/GRDB.swift/issues/1315>
|
||||
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")))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user