Merge associations: including wins over joining

This commit is contained in:
Gwendal Roué committed 2018-09-29 14:37:53 +02:00
1 parent 33b40637a8
commit 37a9999b35
2 files changed
+147 -1

No files matched your search

@@ -178,9 +178,17 @@ extension AssociationQuery {
mergedJoins.append(value: join, forKey: key)
}
// There is a possible conflict when both selections are not empty.
let mergedSelection: [SQLSelectable]
if selection.isEmpty {
mergedSelection = other.selection
} else {
mergedSelection = selection
}
return AssociationQuery(
source: mergedSource,
selection: other.selection, // possible conflict with self.selection here.
selection: mergedSelection,
filterPromise: mergedFilterPromise,
ordering: other.ordering, // reorder
joins: mergedJoins)
@@ -208,6 +208,52 @@ class AssociationParallelSQLTests: GRDBTestCase {
}
}
func testParallelTwoIncludingJoiningSameAssociation() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try assertEqualSQL(db, A.including(required: A.b).joining(required: A.b), """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, A.including(required: A.b).joining(optional: A.b), """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, A.including(optional: A.b).joining(required: A.b), """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, A.including(optional: A.b).joining(optional: A.b), """
SELECT "a".*, "b".* \
FROM "a" \
LEFT JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, B.including(required: B.a).joining(required: B.a), """
SELECT "b".*, "a".* \
FROM "b" \
JOIN "a" ON ("a"."bid" = "b"."id")
""")
try assertEqualSQL(db, B.including(required: B.a).joining(optional: B.a), """
SELECT "b".*, "a".* \
FROM "b" \
JOIN "a" ON ("a"."bid" = "b"."id")
""")
try assertEqualSQL(db, B.including(optional: B.a).joining(required: B.a), """
SELECT "b".*, "a".* \
FROM "b" \
JOIN "a" ON ("a"."bid" = "b"."id")
""")
try assertEqualSQL(db, B.including(optional: B.a).joining(optional: B.a), """
SELECT "b".*, "a".* \
FROM "b" \
LEFT JOIN "a" ON ("a"."bid" = "b"."id")
""")
}
}
func testParallelTwoJoiningIncluding() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
@@ -262,6 +308,52 @@ class AssociationParallelSQLTests: GRDBTestCase {
}
}
func testParallelTwoJoiningIncludingSameAssociation() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try assertEqualSQL(db, A.joining(required: A.b).including(required: A.b), """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, A.joining(required: A.b).including(optional: A.b), """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, A.joining(optional: A.b).including(required: A.b), """
SELECT "a".*, "b".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, A.joining(optional: A.b).including(optional: A.b), """
SELECT "a".*, "b".* \
FROM "a" \
LEFT JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, B.joining(required: B.a).including(required: B.a), """
SELECT "b".*, "a".* \
FROM "b" \
JOIN "a" ON ("a"."bid" = "b"."id")
""")
try assertEqualSQL(db, B.joining(required: B.a).including(optional: B.a), """
SELECT "b".*, "a".* \
FROM "b" \
JOIN "a" ON ("a"."bid" = "b"."id")
""")
try assertEqualSQL(db, B.joining(optional: B.a).including(required: B.a), """
SELECT "b".*, "a".* \
FROM "b" \
JOIN "a" ON ("a"."bid" = "b"."id")
""")
try assertEqualSQL(db, B.joining(optional: B.a).including(optional: B.a), """
SELECT "b".*, "a".* \
FROM "b" \
LEFT JOIN "a" ON ("a"."bid" = "b"."id")
""")
}
}
func testParallelTwoJoiningJoining() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
@@ -315,4 +407,50 @@ class AssociationParallelSQLTests: GRDBTestCase {
""")
}
}
func testParallelTwoJoiningJoiningSameAssociation() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try assertEqualSQL(db, A.joining(required: A.b).joining(required: A.b), """
SELECT "a".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, A.joining(required: A.b).joining(optional: A.b), """
SELECT "a".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, A.joining(optional: A.b).joining(required: A.b), """
SELECT "a".* \
FROM "a" \
JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, A.joining(optional: A.b).joining(optional: A.b), """
SELECT "a".* \
FROM "a" \
LEFT JOIN "b" ON ("b"."id" = "a"."bid")
""")
try assertEqualSQL(db, B.joining(required: B.a).joining(required: B.a), """
SELECT "b".* \
FROM "b" \
JOIN "a" ON ("a"."bid" = "b"."id")
""")
try assertEqualSQL(db, B.joining(required: B.a).joining(optional: B.a), """
SELECT "b".* \
FROM "b" \
JOIN "a" ON ("a"."bid" = "b"."id")
""")
try assertEqualSQL(db, B.joining(optional: B.a).joining(required: B.a), """
SELECT "b".* \
FROM "b" \
JOIN "a" ON ("a"."bid" = "b"."id")
""")
try assertEqualSQL(db, B.joining(optional: B.a).joining(optional: B.a), """
SELECT "b".* \
FROM "b" \
LEFT JOIN "a" ON ("a"."bid" = "b"."id")
""")
}
}
}