diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index a55dc9b27..369c7467f 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -202,6 +202,10 @@ extension SQLRelation: Refinable { } } + func unfiltered() -> Self { + with(\.filtersPromise, DatabasePromise(value: [])) + } + func order(_ orderings: @escaping (Database) throws -> [SQLOrderingTerm]) -> Self { with(\.ordering, SQLRelation.Ordering(orderings: orderings)) } @@ -218,15 +222,6 @@ extension SQLRelation: Refinable { func qualified(with alias: TableAlias) -> Self { map(\.source) { $0.qualified(with: alias) } } - - // Remove everything but selection - var witnessRelation: Self { - var result = self - result.filtersPromise = DatabasePromise(value: []) - result.ordering = SQLRelation.Ordering() - result.children = [:] - return result - } } extension SQLRelation { diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 4559678b0..478ef5c31 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -494,10 +494,16 @@ private struct SQLQualifiedRelation { } if child.firstOnly { + let relation = child.relation + // Filters and order are handled in a subquery + .unfiltered() + .unordered() + // Only keep children with a non-empty selection + .filteringChildren { $0.relation.selection.isEmpty == false } return SQLQualifiedJoin( kind: kind, condition: child.condition, - relation: SQLQualifiedRelation(child.relation.witnessRelation), + relation: SQLQualifiedRelation(relation), target: .firstOnly(child.relation)) } else { return SQLQualifiedJoin( @@ -682,7 +688,7 @@ private struct SQLQualifiedJoin: Refinable { let rightAlias = relation.sourceAlias switch target { - case let .all: + case .all: let filters = try condition.expressions(db, leftAlias: leftAlias, rightAlias: rightAlias) + relation.filtersPromise.resolve(db) if filters.isEmpty == false { diff --git a/Tests/GRDBTests/AssociationHasOneSQLTests.swift b/Tests/GRDBTests/AssociationHasOneSQLTests.swift index 064abc5a7..22fe0ce00 100644 --- a/Tests/GRDBTests/AssociationHasOneSQLTests.swift +++ b/Tests/GRDBTests/AssociationHasOneSQLTests.swift @@ -762,7 +762,7 @@ class AssociationHasOneSQLTests: GRDBTestCase { } func testHasManyFirst() throws { - struct Child : TableRecord { } + struct Child: TableRecord { } struct Parent: TableRecord, EncodableRecord { func encode(to container: inout PersistenceContainer) { container["id"] = 1 @@ -784,7 +784,7 @@ class AssociationHasOneSQLTests: GRDBTestCase { let association = Parent .hasMany(Child.self) .orderByPrimaryKey() - .first() + .first try assertEqualSQL(db, Parent.all().including(required: association), """ SELECT "parent".*, "child".* \ FROM "parent" \ @@ -836,8 +836,172 @@ class AssociationHasOneSQLTests: GRDBTestCase { } } + func testHasManyFirstWithDeeperAssociation() throws { + struct Toy: TableRecord { } + struct Child: TableRecord { } + struct Parent: TableRecord, EncodableRecord { + func encode(to container: inout PersistenceContainer) { + container["id"] = 1 + container["rowid"] = 2 + } + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "parent") { t in + t.autoIncrementedPrimaryKey("id") + } + try db.create(table: "child") { t in + t.autoIncrementedPrimaryKey("id") + t.column("parentId", .integer).references("parent") + } + try db.create(table: "toy") { t in + t.autoIncrementedPrimaryKey("id") + t.column("childId", .integer).references("child") + } + + do { + let association = Parent + .hasMany(Child.self) + .orderByPrimaryKey() + .first + .joining(required: Child.hasOne(Toy.self)) + try assertEqualSQL(db, Parent.all().including(required: association), """ + SELECT "parent".*, "child".* \ + FROM "parent" \ + JOIN "child" ON "child"."rowid" = (\ + SELECT "child"."rowid" \ + FROM "child" \ + JOIN "toy" ON "toy"."childId" = "child"."id" \ + WHERE "child"."parentId" = "parent"."id" \ + ORDER BY "child"."id" \ + LIMIT 1) + """) + try assertEqualSQL(db, Parent.all().including(optional: association), """ + SELECT "parent".*, "child".* \ + FROM "parent" \ + LEFT JOIN "child" ON "child"."rowid" = (\ + SELECT "child"."rowid" \ + FROM "child" \ + JOIN "toy" ON "toy"."childId" = "child"."id" \ + WHERE "child"."parentId" = "parent"."id" \ + ORDER BY "child"."id" \ + LIMIT 1) + """) + try assertEqualSQL(db, Parent.all().joining(required: association), """ + SELECT "parent".* \ + FROM "parent" \ + JOIN "child" ON "child"."rowid" = (\ + SELECT "child"."rowid" \ + FROM "child" \ + JOIN "toy" ON "toy"."childId" = "child"."id" \ + WHERE "child"."parentId" = "parent"."id" \ + ORDER BY "child"."id" \ + LIMIT 1) + """) + try assertEqualSQL(db, Parent.all().joining(optional: association), """ + SELECT "parent".* \ + FROM "parent" \ + LEFT JOIN "child" ON "child"."rowid" = (\ + SELECT "child"."rowid" \ + FROM "child" \ + JOIN "toy" ON "toy"."childId" = "child"."id" \ + WHERE "child"."parentId" = "parent"."id" \ + ORDER BY "child"."id" \ + LIMIT 1) + """) + try assertEqualSQL(db, Parent().request(for: association), """ + SELECT "child".* \ + FROM "child" \ + JOIN "toy" ON "toy"."childId" = "child"."id" \ + WHERE "child"."parentId" = 1 \ + ORDER BY "child"."id" \ + LIMIT 1 + """) + } + + do { + let association = Parent + .hasMany(Child.self) + .orderByPrimaryKey() + .first + .including(required: Child.hasOne(Toy.self)) + try assertEqualSQL(db, Parent.all().including(required: association), """ + SELECT "parent".*, "child".*, "toy".* \ + FROM "parent" \ + JOIN "child" ON "child"."rowid" = (\ + SELECT "child"."rowid" \ + FROM "child" \ + JOIN "toy" ON "toy"."childId" = "child"."id" \ + WHERE "child"."parentId" = "parent"."id" \ + ORDER BY "child"."id" LIMIT 1) \ + JOIN "toy" ON "toy"."childId" = "child"."id" + """) + try assertEqualSQL(db, Parent.all().joining(required: association), """ + SELECT "parent".*, "toy".* \ + FROM "parent" \ + JOIN "child" ON "child"."rowid" = (\ + SELECT "child"."rowid" \ + FROM "child" \ + JOIN "toy" ON "toy"."childId" = "child"."id" \ + WHERE "child"."parentId" = "parent"."id" \ + ORDER BY "child"."id" LIMIT 1) \ + JOIN "toy" ON "toy"."childId" = "child"."id" + """) + // TODO: remove toy from selection, since this is a request for child + try assertEqualSQL(db, Parent().request(for: association), """ + SELECT "child".*, "toy".* \ + FROM "child" \ + JOIN "toy" \ + ON "toy"."childId" = "child"."id" \ + WHERE "child"."parentId" = 1 \ + ORDER BY "child"."id" \ + LIMIT 1 + """) + } + + do { + let alias = TableAlias() + let association = Parent + .hasMany(Child.self) + .orderByPrimaryKey() + .filter(Column("id") == alias[Column("id")] + 1) + .first + .including(required: Child.hasOne(Toy.self).filter(Column("id") == alias[Column("id")] * 2)) + try assertEqualSQL(db, Parent.all().aliased(alias).including(required: association), """ + SELECT "parent".*, "child".*, "toy".* \ + FROM "parent" \ + JOIN "child" \ + ON "child"."rowid" = (\ + SELECT "child"."rowid" \ + FROM "child" \ + JOIN "toy" ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2)) \ + WHERE ("child"."parentId" = "parent"."id") AND ("child"."id" = ("parent"."id" + 1)) \ + ORDER BY "child"."id" \ + LIMIT 1) \ + JOIN "toy" \ + ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2)) + """) + try assertEqualSQL(db, Parent.all().aliased(alias).joining(required: association), """ + SELECT "parent".*, "toy".* \ + FROM "parent" \ + JOIN "child" \ + ON "child"."rowid" = (\ + SELECT "child"."rowid" \ + FROM "child" \ + JOIN "toy" ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2)) \ + WHERE ("child"."parentId" = "parent"."id") AND ("child"."id" = ("parent"."id" + 1)) \ + ORDER BY "child"."id" \ + LIMIT 1) \ + JOIN "toy" \ + ON ("toy"."childId" = "child"."id") AND ("toy"."id" = ("parent"."id" * 2)) + """) + } + } + } + func testHasManyLast() throws { - struct Child : TableRecord { } + struct Child: TableRecord { } struct Parent: TableRecord, EncodableRecord { func encode(to container: inout PersistenceContainer) { container["id"] = 1 @@ -859,7 +1023,7 @@ class AssociationHasOneSQLTests: GRDBTestCase { let association = Parent .hasMany(Child.self) .orderByPrimaryKey() - .last() + .last try assertEqualSQL(db, Parent.all().including(required: association), """ SELECT "parent".*, "child".* \ FROM "parent" \