diff --git a/GRDB/QueryInterface/SQL/SQLQuery.swift b/GRDB/QueryInterface/SQL/SQLQuery.swift index 262c61416..454f4f523 100644 --- a/GRDB/QueryInterface/SQL/SQLQuery.swift +++ b/GRDB/QueryInterface/SQL/SQLQuery.swift @@ -7,7 +7,7 @@ struct SQLQuery { var expectsSingleResult: Bool var groupPromise: DatabasePromise<[SQLExpression]>? // Having clause is an array of expressions that we'll join with - // SQLExpressionAnd. This gives nicer output in generated SQL: + // the AND operator. This gives nicer output in generated SQL: // `(a AND b AND c)` instead of `((a AND b) AND c)`. var havingExpressions: [SQLExpression] var limit: SQLLimit? diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index d20a4e075..f29b0ec03 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -153,7 +153,7 @@ struct SQLRelation { var source: SQLSource var selection: [SQLSelectable] - // Filter is an array of expressions that we'll join with SQLExpressionAnd. + // Filter is an array of expressions that we'll join with the AND operator. // This gives nicer output in generated SQL: `(a AND b AND c)` instead of // `((a AND b) AND c)`. var filtersPromise: DatabasePromise<[SQLExpression]> @@ -567,7 +567,7 @@ struct SQLAssociationCondition: Equatable { } } - /// Resolves the condition into an SQL expression which involves both left + /// Resolves the condition into SQL expressions which involve both left /// and right tables. /// /// SELECT * FROM left JOIN right ON (right.a = left.b) @@ -578,11 +578,12 @@ struct SQLAssociationCondition: Equatable { /// JOIN operator. /// - parameter rightAlias: A TableAlias for the table on the right of the /// JOIN operator. - /// - Returns: An SQL expression. - func joinExpression(_ db: Database, leftAlias: TableAlias, rightAlias: TableAlias) throws -> SQLExpression { - return try columnMappings(db) - .map { QualifiedColumn($0.right, alias: rightAlias) == QualifiedColumn($0.left, alias: leftAlias) } - .joined(operator: .and) + /// - Returns: An array of SQL expression that should be joined with + /// the AND operator. + func expressions(_ db: Database, leftAlias: TableAlias, rightAlias: TableAlias) throws -> [SQLExpression] { + return try columnMappings(db).map { + QualifiedColumn($0.right, alias: rightAlias) == QualifiedColumn($0.left, alias: leftAlias) + } } /// Resolves the condition into an SQL expression which involves only the diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 7f0ea667f..1ab3bf6c2 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -458,18 +458,17 @@ private struct SQLQualifiedJoin { case .leftJoin: allowsInnerJoin = false } - sql += kind.rawValue - sql += try " " + relation.source.sql(db, &context) + sql += try "\(kind.rawValue) \(relation.source.sql(db, &context))" let rightAlias = relation.alias - var filters = try [condition.joinExpression(db, leftAlias: leftAlias, rightAlias: rightAlias)] - try filters.append(contentsOf: relation.filtersPromise.resolve(db)) + let filters = try condition.expressions(db, leftAlias: leftAlias, rightAlias: rightAlias) + + relation.filtersPromise.resolve(db) if filters.isEmpty == false { - sql += " ON " + SQLExpressionAnd(filters).expressionSQL(&context, wrappedInParenthesis: false) + sql += " ON \(SQLExpressionAnd(filters).expressionSQL(&context, wrappedInParenthesis: false))" } for (_, join) in relation.joins { - sql += try " " + join.sql(db, &context, leftAlias: rightAlias, allowingInnerJoin: allowsInnerJoin) + sql += try " \(join.sql(db, &context, leftAlias: rightAlias, allowingInnerJoin: allowsInnerJoin))" } return sql diff --git a/Tests/GRDBTests/AssociationBelongsToSQLTests.swift b/Tests/GRDBTests/AssociationBelongsToSQLTests.swift index 3eb06d273..d9ab2e93f 100644 --- a/Tests/GRDBTests/AssociationBelongsToSQLTests.swift +++ b/Tests/GRDBTests/AssociationBelongsToSQLTests.swift @@ -24,6 +24,7 @@ class AssociationBelongsToSQLTests: GRDBTestCase { try dbQueue.inDatabase { db in try db.create(table: "parents") { t in t.column("id", .integer) + t.column("name", .text) } try db.create(table: "children") { t in t.column("parentId", .integer) @@ -53,6 +54,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."rowid" = "children"."parentId" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."rowid" = "children"."parentId") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "rowid" = 1 """) @@ -82,7 +88,12 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parentId" """) - try assertEqualSQL(db, Child().request(for: association), """ + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parentId") AND ("parents"."name" = 'foo') + """) + try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 1 """) try assertEqualSQL(db, Child().request(for: association).aliased(TableAlias(name: "custom")), """ @@ -108,6 +119,7 @@ class AssociationBelongsToSQLTests: GRDBTestCase { try dbQueue.inDatabase { db in try db.create(table: "parents") { t in t.column("id", .integer).primaryKey() + t.column("name", .text) } try db.create(table: "children") { t in t.column("parentId", .integer) @@ -137,6 +149,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parentId" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parentId") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 1 """) @@ -166,6 +183,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parentId" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parentId") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 1 """) @@ -192,6 +214,7 @@ class AssociationBelongsToSQLTests: GRDBTestCase { try dbQueue.inDatabase { db in try db.create(table: "parents") { t in t.column("id", .integer).primaryKey() + t.column("name", .text) } try db.create(table: "children") { t in t.column("parentId", .integer).references("parents") @@ -221,6 +244,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parentId" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parentId") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 1 """) @@ -250,6 +278,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parentId" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parentId") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 1 """) @@ -279,6 +312,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parentId" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parentId") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 1 """) @@ -306,6 +344,7 @@ class AssociationBelongsToSQLTests: GRDBTestCase { try dbQueue.inDatabase { db in try db.create(table: "parents") { t in t.column("id", .integer).primaryKey() + t.column("name", .text) } try db.create(table: "children") { t in t.column("parent1Id", .integer).references("parents") @@ -336,6 +375,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parent1Id" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parent1Id") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 1 """) @@ -365,6 +409,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parent1Id" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parent1Id") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 1 """) @@ -394,6 +443,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parent2Id" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parent2Id") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 2 """) @@ -423,6 +477,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON "parents"."id" = "children"."parent2Id" """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."id" = "children"."parent2Id") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE "id" = 2 """) @@ -451,6 +510,7 @@ class AssociationBelongsToSQLTests: GRDBTestCase { try db.create(table: "parents") { t in t.column("a", .integer) t.column("b", .integer) + t.column("name", .text) } try db.create(table: "children") { t in t.column("parentA", .integer) @@ -481,6 +541,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 1) AND ("b" = 2) """) @@ -509,6 +574,7 @@ class AssociationBelongsToSQLTests: GRDBTestCase { try db.create(table: "parents") { t in t.column("a", .integer) t.column("b", .integer) + t.column("name", .text) t.primaryKey(["a", "b"]) } try db.create(table: "children") { t in @@ -540,6 +606,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 1) AND ("b" = 2) """) @@ -569,6 +640,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 1) AND ("b" = 2) """) @@ -597,6 +673,7 @@ class AssociationBelongsToSQLTests: GRDBTestCase { try db.create(table: "parents") { t in t.column("a", .integer) t.column("b", .integer) + t.column("name", .text) t.primaryKey(["a", "b"]) } try db.create(table: "children") { t in @@ -629,6 +706,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 1) AND ("b" = 2) """) @@ -658,6 +740,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 1) AND ("b" = 2) """) @@ -687,6 +774,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parentA") AND ("parents"."b" = "children"."parentB") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 1) AND ("b" = 2) """) @@ -717,6 +809,7 @@ class AssociationBelongsToSQLTests: GRDBTestCase { try db.create(table: "parents") { t in t.column("a", .integer) t.column("b", .integer) + t.column("name", .text) t.primaryKey(["a", "b"]) } try db.create(table: "children") { t in @@ -752,6 +845,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parent1A") AND ("parents"."b" = "children"."parent1B") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parent1A") AND ("parents"."b" = "children"."parent1B") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 1) AND ("b" = 2) """) @@ -781,6 +879,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parent1A") AND ("parents"."b" = "children"."parent1B") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parent1A") AND ("parents"."b" = "children"."parent1B") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 1) AND ("b" = 2) """) @@ -810,6 +913,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parent2A") AND ("parents"."b" = "children"."parent2B") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parent2A") AND ("parents"."b" = "children"."parent2B") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 3) AND ("b" = 4) """) @@ -839,6 +947,11 @@ class AssociationBelongsToSQLTests: GRDBTestCase { FROM "children" \ LEFT JOIN "parents" ON ("parents"."a" = "children"."parent2A") AND ("parents"."b" = "children"."parent2B") """) + try assertEqualSQL(db, Child.joining(optional: association.filter(Column("name") == "foo")), """ + SELECT "children".* \ + FROM "children" \ + LEFT JOIN "parents" ON ("parents"."a" = "children"."parent2A") AND ("parents"."b" = "children"."parent2B") AND ("parents"."name" = 'foo') + """) try assertEqualSQL(db, Child().request(for: association), """ SELECT * FROM "parents" WHERE ("a" = 3) AND ("b" = 4) """) diff --git a/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift b/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift index 76e85a592..9238115a7 100644 --- a/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift +++ b/Tests/GRDBTests/AssociationPrefetchingSQLTests.swift @@ -219,6 +219,29 @@ class AssociationPrefetchingSQLTests: GRDBTestCase { SELECT * FROM "parent" WHERE 0 ORDER BY "parentA", "parentB" """]) } + + // Request with filters + do { + let request = Parent + .including(all: Parent + .hasMany(Child.self) + .filter(Column("name") == "foo")) + .filter(Column("parentA") == "foo") + .orderByPrimaryKey() + + sqlQueries.removeAll() + _ = try Row.fetchAll(db, request) + + let selectQueries = sqlQueries.filter { $0.contains("SELECT") } + XCTAssertEqual(selectQueries, [ + """ + SELECT * FROM "parent" WHERE "parentA" = 'foo' ORDER BY "parentA", "parentB" + """, + """ + SELECT *, "parentA" AS "grdb_parentA", "parentB" AS "grdb_parentB" \ + FROM "child" WHERE ("name" = 'foo') AND (("parentA" = 'foo') AND ("parentB" = 'bar')) + """]) + } } }