diff --git a/Tests/GRDBTests/AssociationHasOneThroughSQLDerivationTests.swift b/Tests/GRDBTests/AssociationHasOneThroughSQLDerivationTests.swift index 860443585..6f20f0155 100644 --- a/Tests/GRDBTests/AssociationHasOneThroughSQLDerivationTests.swift +++ b/Tests/GRDBTests/AssociationHasOneThroughSQLDerivationTests.swift @@ -119,4 +119,146 @@ class AssociationHasOneThroughSQLDerivationTests: GRDBTestCase { } } } + + func testFilteredAssociationImpactsJoinOnClause() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + do { + let request = A.including(required: A.c.filter(Column("name") != nil)) + try assertEqualSQL(db, request, """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON (("c"."id" = "b"."cId") AND ("c"."name" IS NOT NULL)) + """) + } + do { + let request = A.including(required: A.c.filter(key: 1)) + try assertEqualSQL(db, request, """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON (("c"."id" = "b"."cId") AND ("c"."id" = 1)) + """) + } + do { + let request = A.including(required: A.c.filter(keys: [1, 2, 3])) + try assertEqualSQL(db, request, """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON (("c"."id" = "b"."cId") AND ("c"."id" IN (1, 2, 3))) + """) + } + do { + let request = A.including(required: A.c.filter(key: ["id": 1])) + try assertEqualSQL(db, request, """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON (("c"."id" = "b"."cId") AND ("c"."id" = 1)) + """) + } + do { + let request = A.including(required: A.c.filter(keys: [["id": 1], ["id": 2]])) + try assertEqualSQL(db, request, """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON (("c"."id" = "b"."cId") AND (("c"."id" = 1) OR ("c"."id" = 2))) + """) + } + do { + let cAlias = TableAlias(name: "customC") + let request = A.including(required: A.c + .aliased(cAlias) + .filter(sql: "customC.name = ?", arguments: ["foo"])) + try assertEqualSQL(db, request, """ + SELECT "a".*, "customC".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" "customC" ON (("customC"."id" = "b"."cId") AND (customC.name = 'foo')) + """) + } + } + } + + func testFilterAssociationInWhereClause() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + let cAlias = TableAlias() + let request = A + .including(required: A.c.aliased(cAlias)) + .filter(cAlias[Column("name")] != nil) + try assertEqualSQL(db, request, """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + WHERE ("c"."name" IS NOT NULL) + """) + } + } + + func testOrderAssociation() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try assertEqualSQL(db, A.including(required: A.c.order(Column("name"))), """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + ORDER BY "c"."name" + """) + try assertEqualSQL(db, A.including(required: A.c.orderByPrimaryKey()), """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + ORDER BY "c"."id" + """) + try assertEqualSQL(db, A.including(required: A.c.orderByPrimaryKey()).orderByPrimaryKey(), """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + ORDER BY "a"."id", "c"."id" + """) + try assertEqualSQL(db, A.including(required: A.c.orderByPrimaryKey().reversed()).orderByPrimaryKey(), """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + ORDER BY "a"."id", "c"."id" DESC + """) + try assertEqualSQL(db, A.including(required: A.c.order(Column("name"))).reversed(), """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + ORDER BY "c"."name" DESC + """) + try assertEqualSQL(db, A.including(required: A.c.orderByPrimaryKey()).reversed(), """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + ORDER BY "c"."id" DESC + """) + try assertEqualSQL(db, A.including(required: A.c.orderByPrimaryKey()).orderByPrimaryKey().reversed(), """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + ORDER BY "a"."id" DESC, "c"."id" DESC + """) + try assertEqualSQL(db, A.including(required: A.c.orderByPrimaryKey().reversed()).orderByPrimaryKey().reversed(), """ + SELECT "a".*, "c".* \ + FROM "a" \ + JOIN "b" ON ("b"."id" = "a"."bId") \ + JOIN "c" ON ("c"."id" = "b"."cId") \ + ORDER BY "a"."id" DESC, "c"."id" + """) + } + } }