diff --git a/GRDB/QueryInterface/Request/Association/Association.swift b/GRDB/QueryInterface/Request/Association/Association.swift index 26dbd9418..defc319bb 100644 --- a/GRDB/QueryInterface/Request/Association/Association.swift +++ b/GRDB/QueryInterface/Request/Association/Association.swift @@ -350,6 +350,7 @@ extension AssociationToMany { /// /// let teams: [Team] = try Team.having(Team.players.count() > 10).fetchAll(db) public var count: AssociationAggregate { + // TODO: try to replace rowid with actual primary key makeAggregate(SQLExpressionCountDistinct(Column.rowID)) .forKey("\(key.singularizedName)Count") } @@ -370,6 +371,7 @@ extension AssociationToMany { /// let teams: [Team] = try Team.having(!Team.players.isEmpty()) /// let teams: [Team] = try Team.having(Team.players.isEmpty() == false) public var isEmpty: AssociationAggregate { + // TODO: try to replace rowid with actual primary key makeAggregate(SQLExpressionIsEmpty(SQLExpressionCountDistinct(Column.rowID))) .forKey("hasNo\(key.singularizedName.uppercasingFirstCharacter)") } diff --git a/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift b/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift index aafa8de13..f0008313a 100644 --- a/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift +++ b/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift @@ -27,6 +27,7 @@ typealias AssociationAggregatePreparation = /// // Won't compile because Fruit is not Author. /// let request = Fruit.annotated(with: bookCount) public struct AssociationAggregate { + // TODO: replace rowid with actual primary key in the doc when implemented /// Given a request, returns a tuple made of a request extended with the /// associated records used to compute the aggregate, and an expression /// whose value is the aggregated value. diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest+Association.swift index dd33bd864..4c7743a40 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest+Association.swift @@ -11,6 +11,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { } } + // TODO: replace rowid with actual primary key in the doc when implemented /// Creates a request which appends *aggregates* to the current selection. /// /// // SELECT player.*, COUNT(DISTINCT book.rowid) AS bookCount @@ -21,6 +22,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { annotated(with: aggregates) } + // TODO: replace rowid with actual primary key in the doc when implemented /// Creates a request which appends *aggregates* to the current selection. /// /// // SELECT player.*, COUNT(DISTINCT book.rowid) AS bookCount @@ -33,6 +35,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { } } + // TODO: replace rowid with actual primary key in the doc when implemented /// Creates a request which appends the provided aggregate *predicate* to /// the eventual set of already applied predicates. /// diff --git a/GRDB/QueryInterface/SQL/SQLExpression.swift b/GRDB/QueryInterface/SQL/SQLExpression.swift index 19cabbc40..7361d6eb9 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression.swift @@ -141,3 +141,22 @@ struct SQLExpressionNot: SQLExpression { SQLExpressionNot(expression.qualifiedExpression(with: alias)) } } + +// MARK: - + +extension Database { + /// Returns an expression for the primary key of this table. + /// + /// When the table as a primary key made of several columns, we return + /// the rowid column. Eventually we'll support + /// [row values](https://www.sqlite.org/rowvalue.html) and be able to deal + /// with WITHOUT ROWID tables. + func primaryKeyExpression(_ tableName: String) throws -> SQLExpression { + let columns = try primaryKey(tableName).columns + if columns.count == 1 { + return Column(columns[0]) + } else { + return Column.rowID + } + } +} diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index 369c7467f..3df240276 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -177,6 +177,18 @@ struct SQLRelation { } } } + + /// Returns nil if there is no way to express the primary key of the + /// relation as an expression. + func primaryKeyExpression(_ db: Database) throws -> SQLExpression? { + switch source { + case .query: + // No primary key for sub query + return nil + case let .table(tableName: tableName, alias: _): + return try db.primaryKeyExpression(tableName) + } + } } extension SQLRelation: Refinable { diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 8a2e1c22d..a948b3ed1 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -1,5 +1,6 @@ + /// SQLQueryGenerator is able to generate an SQL SELECT query. -struct SQLQueryGenerator { +struct SQLQueryGenerator: Refinable { fileprivate private(set) var relation: SQLQualifiedRelation private let isDistinct: Bool private let groupPromise: DatabasePromise<[SQLExpression]>? @@ -168,7 +169,7 @@ struct SQLQueryGenerator { } } - /// DELETE FROM table WHERE rowid IN (SELECT rowid FROM table ...) + /// DELETE FROM table WHERE id IN (SELECT id FROM table ...) private func makeTrivialDeleteStatement(_ db: Database) throws -> UpdateStatement { guard case let .table(tableName: tableName, alias: _) = relation.source else { // Programmer error @@ -176,14 +177,13 @@ struct SQLQueryGenerator { } var context = SQLGenerationContext.queryContext(aliases: relation.allAliases) + let primaryKey = try db.primaryKeyExpression(tableName) - // SELECT rowid FROM table ... - var generator = self - generator.relation = generator.relation.selectOnly([Column.rowID]) - let selectSQL = try generator.sql(db, &context) - - // DELETE FROM table WHERE rowid IN (SELECT rowid FROM table ...) - let sql = "DELETE FROM \(tableName.quotedDatabaseIdentifier) WHERE rowid IN (\(selectSQL))" + var sql = "DELETE FROM \(tableName.quotedDatabaseIdentifier) WHERE " + sql += primaryKey.expressionSQL(&context, wrappedInParenthesis: false) + sql += " IN (" + sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(db, &context) + sql += ")" let statement = try db.makeUpdateStatement(sql: sql) statement.arguments = context.arguments @@ -258,7 +258,7 @@ struct SQLQueryGenerator { } } - /// UPDATE table SET ... WHERE rowid IN (SELECT rowid FROM table ...) + /// UPDATE table SET ... WHERE id IN (SELECT id FROM table ...) /// Returns nil if assignments is empty private func makeTrivialUpdateStatement( _ db: Database, @@ -278,6 +278,7 @@ struct SQLQueryGenerator { } var context = SQLGenerationContext.queryContext(aliases: relation.allAliases) + let primaryKey = try db.primaryKeyExpression(tableName) // UPDATE table... var sql = "UPDATE " @@ -292,10 +293,12 @@ struct SQLQueryGenerator { .joined(separator: ", ") sql += " SET " + assignmentsSQL - // WHERE rowid IN (SELECT rowid FROM ...) - var generator = self - generator.relation = generator.relation.selectOnly([Column.rowID]) - sql += try " WHERE rowid IN (\(generator.sql(db, &context)))" + // WHERE id IN (SELECT rowid FROM ...) + sql += " WHERE " + sql += primaryKey.expressionSQL(&context, wrappedInParenthesis: false) + sql += " IN (" + sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(db, &context) + sql += ")" let statement = try db.makeUpdateStatement(sql: sql) statement.arguments = context.arguments @@ -694,18 +697,20 @@ private struct SQLQualifiedJoin: Refinable { sql += " ON \(filters.joined(operator: .and).expressionSQL(&context, wrappedInParenthesis: false))" } case let .firstInSubRelation(subRelation): + guard let primaryKey = try subRelation.primaryKeyExpression(db) else { + fatalError("Not implemented") + } let subAlias = TableAlias() let filters = try condition.expressions(db, leftAlias: leftAlias, rightAlias: subAlias) let subRelation = subRelation .qualified(with: subAlias) - .selectOnly([Column.rowID]) + .selectOnly([primaryKey]) .map(\.filtersPromise) { $0.flatMap { DatabasePromise(value: filters + $0) } } let subQuery = SQLQuery(relation: subRelation, limit: SQLLimit(limit: 1, offset: nil)) let subQueryGenerator = SQLQueryGenerator(subQuery) sql += " ON " - sql += rightAlias[Column.rowID].expressionSQL(&context, wrappedInParenthesis: false) - // SELECT rowid FROM child WHERE child.parentId = parent.id ORDER BY id DESC LIMIT 1 + sql += rightAlias[primaryKey].expressionSQL(&context, wrappedInParenthesis: false) sql += try " = (" + subQueryGenerator.sql(db, &context) + ")" } diff --git a/GRDB/QueryInterface/TableRecord+Association.swift b/GRDB/QueryInterface/TableRecord+Association.swift index 4d49c3ba7..052671bee 100644 --- a/GRDB/QueryInterface/TableRecord+Association.swift +++ b/GRDB/QueryInterface/TableRecord+Association.swift @@ -552,6 +552,7 @@ extension TableRecord { // MARK: - Association Aggregates + // TODO: replace rowid with actual primary key in the doc when implemented /// Creates a request with *aggregates* appended to the selection. /// /// // SELECT player.*, COUNT(DISTINCT book.rowid) AS bookCount @@ -561,6 +562,7 @@ extension TableRecord { all().annotated(with: aggregates) } + // TODO: replace rowid with actual primary key in the doc when implemented /// Creates a request with *aggregates* appended to the selection. /// /// // SELECT player.*, COUNT(DISTINCT book.rowid) AS bookCount @@ -570,6 +572,7 @@ extension TableRecord { all().annotated(with: aggregates) } + // TODO: replace rowid with actual primary key in the doc when implemented /// Creates a request with the provided aggregate *predicate*. /// /// // SELECT player.* diff --git a/Tests/GRDBTests/AssociationHasManyFirstSQLTests.swift b/Tests/GRDBTests/AssociationHasManyFirstSQLTests.swift index f3e2fde61..b6c55af6e 100644 --- a/Tests/GRDBTests/AssociationHasManyFirstSQLTests.swift +++ b/Tests/GRDBTests/AssociationHasManyFirstSQLTests.swift @@ -30,8 +30,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().including(required: association), """ SELECT "parent".*, "child".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" \ @@ -40,8 +40,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().including(optional: association), """ SELECT "parent".*, "child".* \ FROM "parent" \ - LEFT JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + LEFT JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" \ @@ -50,8 +50,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().joining(required: association), """ SELECT "parent".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" \ @@ -60,8 +60,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().joining(optional: association), """ SELECT "parent".* \ FROM "parent" \ - LEFT JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + LEFT JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" \ @@ -110,8 +110,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().including(required: association), """ SELECT "parent".*, "child".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ JOIN "toy" ON "toy"."childId" = "child"."id" \ WHERE "child"."parentId" = "parent"."id" \ @@ -121,8 +121,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().including(optional: association), """ SELECT "parent".*, "child".* \ FROM "parent" \ - LEFT JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + LEFT JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ JOIN "toy" ON "toy"."childId" = "child"."id" \ WHERE "child"."parentId" = "parent"."id" \ @@ -132,8 +132,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().joining(required: association), """ SELECT "parent".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ JOIN "toy" ON "toy"."childId" = "child"."id" \ WHERE "child"."parentId" = "parent"."id" \ @@ -143,8 +143,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().joining(optional: association), """ SELECT "parent".* \ FROM "parent" \ - LEFT JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + LEFT JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ JOIN "toy" ON "toy"."childId" = "child"."id" \ WHERE "child"."parentId" = "parent"."id" \ @@ -170,8 +170,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().including(required: association), """ SELECT "parent".*, "child".*, "toy".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ JOIN "toy" ON "toy"."childId" = "child"."id" \ WHERE "child"."parentId" = "parent"."id" \ @@ -181,8 +181,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().joining(required: association), """ SELECT "parent".*, "toy".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ JOIN "toy" ON "toy"."childId" = "child"."id" \ WHERE "child"."parentId" = "parent"."id" \ @@ -212,8 +212,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { SELECT "parent".*, "child".* \ FROM "parent" \ JOIN "child" \ - ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + ON "child"."id" = (\ + SELECT "child"."id" \ 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)) \ @@ -224,8 +224,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { SELECT "parent".* \ FROM "parent" \ JOIN "child" \ - ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + ON "child"."id" = (\ + SELECT "child"."id" \ 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)) \ @@ -246,8 +246,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { SELECT "parent".*, "child".*, "toy".* \ FROM "parent" \ JOIN "child" \ - ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + ON "child"."id" = (\ + SELECT "child"."id" \ 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)) \ @@ -260,8 +260,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { SELECT "parent".*, "toy".* \ FROM "parent" \ JOIN "child" \ - ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + ON "child"."id" = (\ + SELECT "child"."id" \ 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)) \ @@ -301,8 +301,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().including(required: association), """ SELECT "parent".*, "child".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" DESC \ @@ -311,8 +311,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().including(optional: association), """ SELECT "parent".*, "child".* \ FROM "parent" \ - LEFT JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + LEFT JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" DESC \ @@ -321,8 +321,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().joining(required: association), """ SELECT "parent".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" DESC \ @@ -331,8 +331,8 @@ class AssociationHasManyFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, Parent.all().joining(optional: association), """ SELECT "parent".* \ FROM "parent" \ - LEFT JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + LEFT JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" DESC \ diff --git a/Tests/GRDBTests/AssociationHasManyThroughFirstSQLTests.swift b/Tests/GRDBTests/AssociationHasManyThroughFirstSQLTests.swift index 429e70618..6342c3c52 100644 --- a/Tests/GRDBTests/AssociationHasManyThroughFirstSQLTests.swift +++ b/Tests/GRDBTests/AssociationHasManyThroughFirstSQLTests.swift @@ -39,25 +39,25 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { SELECT "a".*, "c".* \ FROM "a" \ JOIN "b" ON "b"."id" = "a"."bId" \ - JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) """) try assertEqualSQL(db, A.all().including(optional: association), """ SELECT "a".*, "c".* \ FROM "a" \ LEFT JOIN "b" ON "b"."id" = "a"."bId" \ - LEFT JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + LEFT JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) """) try assertEqualSQL(db, A.all().joining(required: association), """ SELECT "a".* \ FROM "a" \ JOIN "b" ON "b"."id" = "a"."bId" \ - JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) """) try assertEqualSQL(db, A.all().joining(optional: association), """ SELECT "a".* \ FROM "a" \ LEFT JOIN "b" ON "b"."id" = "a"."bId" \ - LEFT JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + LEFT JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) """) try assertEqualSQL(db, A().request(for: association), """ SELECT "c".* \ @@ -103,25 +103,25 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { SELECT "a".*, "c".* \ FROM "a" \ JOIN "b" ON "b"."aId" = "a"."id" \ - JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) """) try assertEqualSQL(db, A.all().including(optional: association), """ SELECT "a".*, "c".* \ FROM "a" \ LEFT JOIN "b" ON "b"."aId" = "a"."id" \ - LEFT JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + LEFT JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) """) try assertEqualSQL(db, A.all().joining(required: association), """ SELECT "a".* \ FROM "a" \ JOIN "b" ON "b"."aId" = "a"."id" \ - JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) """) try assertEqualSQL(db, A.all().joining(optional: association), """ SELECT "a".* \ FROM "a" \ LEFT JOIN "b" ON "b"."aId" = "a"."id" \ - LEFT JOIN "c" ON "c"."rowid" = (SELECT "c"."rowid" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) + LEFT JOIN "c" ON "c"."id" = (SELECT "c"."id" FROM "c" WHERE "c"."bId" = "b"."id" LIMIT 1) """) try assertEqualSQL(db, A().request(for: association), """ SELECT "c".* \ @@ -165,24 +165,24 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, A.all().including(required: association), """ SELECT "a".*, "c".* \ FROM "a" \ - JOIN "b" ON "b"."rowid" = (SELECT "b"."rowid" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1) \ + JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1) \ JOIN "c" ON "c"."id" = "b"."cId" """) try assertEqualSQL(db, A.all().including(optional: association), """ SELECT "a".*, "c".* \ FROM "a" \ - LEFT JOIN "b" ON "b"."rowid" = (SELECT "b"."rowid" FROM "b" LEFT JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1) \ + LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1) \ LEFT JOIN "c" ON "c"."id" = "b"."cId" """) try assertEqualSQL(db, A.all().joining(required: association), """ SELECT "a".* \ FROM "a" \ - JOIN "b" ON "b"."rowid" = (SELECT "b"."rowid" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1) + JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1) """) try assertEqualSQL(db, A.all().joining(optional: association), """ SELECT "a".* \ FROM "a" \ - LEFT JOIN "b" ON "b"."rowid" = (SELECT "b"."rowid" FROM "b" LEFT JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1) + LEFT JOIN "b" ON "b"."id" = (SELECT "b"."id" FROM "b" LEFT JOIN "c" ON "c"."id" = "b"."cId" WHERE "b"."aId" = "a"."id" LIMIT 1) """) // TODO: is is possible? // TODO: test request(for: hasManyThrough(hasMany.first, hasMany) (all comments of the last message in a chat) @@ -226,8 +226,8 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, A.all().including(required: association), """ SELECT "parent".*, "child".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" \ @@ -236,8 +236,8 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, A.all().including(optional: association), """ SELECT "parent".*, "child".* \ FROM "parent" \ - LEFT JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + LEFT JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" \ @@ -246,8 +246,8 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, A.all().joining(required: association), """ SELECT "parent".* \ FROM "parent" \ - JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" \ @@ -256,8 +256,8 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { try assertEqualSQL(db, A.all().joining(optional: association), """ SELECT "parent".* \ FROM "parent" \ - LEFT JOIN "child" ON "child"."rowid" = (\ - SELECT "child"."rowid" \ + LEFT JOIN "child" ON "child"."id" = (\ + SELECT "child"."id" \ FROM "child" \ WHERE "child"."parentId" = "parent"."id" \ ORDER BY "child"."id" \ @@ -295,6 +295,7 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { 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 @@ -316,21 +317,21 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { SELECT "parent".*, "pet".* \ FROM "parent" \ JOIN "child" ON "child"."parentId" = "parent"."id" \ - JOIN "toy" ON "toy"."childId" = "child"."rowid" \ - JOIN "pet" ON "pet"."childId" = "child"."rowid" + JOIN "toy" ON "toy"."childId" = "child"."id" \ + JOIN "pet" ON "pet"."childId" = "child"."id" """) try assertEqualSQL(db, Parent.all().joining(required: association), """ SELECT "parent".* \ FROM "parent" \ JOIN "child" ON "child"."parentId" = "parent"."id" \ - JOIN "toy" ON "toy"."childId" = "child"."rowid" \ - JOIN "pet" ON "pet"."childId" = "child"."rowid" + JOIN "toy" ON "toy"."childId" = "child"."id" \ + JOIN "pet" ON "pet"."childId" = "child"."id" """) try assertEqualSQL(db, Parent().request(for: association), """ SELECT "pet".* \ FROM "pet" \ - JOIN "child" ON ("child"."rowid" = "pet"."childId") AND ("child"."parentId" = 1) \ - JOIN "toy" ON "toy"."childId" = "child"."rowid" + JOIN "child" ON ("child"."id" = "pet"."childId") AND ("child"."parentId" = 1) \ + JOIN "toy" ON "toy"."childId" = "child"."id" """) } do { @@ -343,24 +344,24 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { SELECT "parent".*, "pet".* \ FROM "parent" \ JOIN "child" "child1" ON ("child1"."parentId" = "parent"."id") AND (1 + 1) \ - JOIN "pet" ON "pet"."childId" = "child1"."rowid" \ - JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1) \ - JOIN "toy" ON "toy"."childId" = "child2"."rowid" + JOIN "pet" ON "pet"."childId" = "child1"."id" \ + JOIN "child" "child2" ON ("child2"."id" = "pet"."childId") AND (1) \ + JOIN "toy" ON "toy"."childId" = "child2"."id" """) try assertEqualSQL(db, Parent.all().joining(required: association), """ SELECT "parent".* \ FROM "parent" \ JOIN "child" "child1" ON ("child1"."parentId" = "parent"."id") AND (1 + 1) \ - JOIN "pet" ON "pet"."childId" = "child1"."rowid" \ - JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1) \ - JOIN "toy" ON "toy"."childId" = "child2"."rowid" + JOIN "pet" ON "pet"."childId" = "child1"."id" \ + JOIN "child" "child2" ON ("child2"."id" = "pet"."childId") AND (1) \ + JOIN "toy" ON "toy"."childId" = "child2"."id" """) try assertEqualSQL(db, Parent().request(for: association), """ SELECT "pet".* \ FROM "pet" \ - JOIN "child" "child1" ON ("child1"."rowid" = "pet"."childId") AND (1) \ - JOIN "toy" ON "toy"."childId" = "child1"."rowid" \ - JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1 + 1) AND ("child2"."parentId" = 1) + JOIN "child" "child1" ON ("child1"."id" = "pet"."childId") AND (1) \ + JOIN "toy" ON "toy"."childId" = "child1"."id" \ + JOIN "child" "child2" ON ("child2"."id" = "pet"."childId") AND (1 + 1) AND ("child2"."parentId" = 1) """) } do { @@ -374,24 +375,24 @@ class AssociationHasManyThroughFirstSQLTests: GRDBTestCase { SELECT "parent".*, "pet".* \ FROM "parent" \ JOIN "child" "child1" ON ("child1"."parentId" = "parent"."id") AND (1 + 1) \ - JOIN "pet" ON "pet"."childId" = "child1"."rowid" \ - JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1) \ - JOIN "toy" ON "toy"."childId" = "child2"."rowid" + JOIN "pet" ON "pet"."childId" = "child1"."id" \ + JOIN "child" "child2" ON ("child2"."id" = "pet"."childId") AND (1) \ + JOIN "toy" ON "toy"."childId" = "child2"."id" """) try assertEqualSQL(db, Parent.all().joining(required: association), """ SELECT "parent".* \ FROM "parent" \ JOIN "child" "child1" ON ("child1"."parentId" = "parent"."id") AND (1 + 1) \ - JOIN "pet" ON "pet"."childId" = "child1"."rowid" \ - JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1) \ - JOIN "toy" ON "toy"."childId" = "child2"."rowid" + JOIN "pet" ON "pet"."childId" = "child1"."id" \ + JOIN "child" "child2" ON ("child2"."id" = "pet"."childId") AND (1) \ + JOIN "toy" ON "toy"."childId" = "child2"."id" """) try assertEqualSQL(db, Parent().request(for: association), """ SELECT "pet".* \ FROM "pet" \ - JOIN "child" "child1" ON ("child1"."rowid" = "pet"."childId") AND (1) \ - JOIN "toy" ON "toy"."childId" = "child1"."rowid" \ - JOIN "child" "child2" ON ("child2"."rowid" = "pet"."childId") AND (1 + 1) AND ("child2"."parentId" = 1) + JOIN "child" "child1" ON ("child1"."id" = "pet"."childId") AND (1) \ + JOIN "toy" ON "toy"."childId" = "child1"."id" \ + JOIN "child" "child2" ON ("child2"."id" = "pet"."childId") AND (1 + 1) AND ("child2"."parentId" = 1) """) } } diff --git a/Tests/GRDBTests/MutablePersistableRecordDeleteTests.swift b/Tests/GRDBTests/MutablePersistableRecordDeleteTests.swift index d2af2f8db..a22d34815 100644 --- a/Tests/GRDBTests/MutablePersistableRecordDeleteTests.swift +++ b/Tests/GRDBTests/MutablePersistableRecordDeleteTests.swift @@ -205,8 +205,8 @@ class MutablePersistableRecordDeleteTests: GRDBTestCase { do { try Player.including(required: Player.team).deleteAll(db) XCTAssertEqual(self.lastSQLQuery, """ - DELETE FROM "player" WHERE rowid IN (\ - SELECT "player"."rowid" \ + DELETE FROM "player" WHERE "id" IN (\ + SELECT "player"."id" \ FROM "player" \ JOIN "team" ON "team"."id" = "player"."teamId") """) @@ -215,8 +215,8 @@ class MutablePersistableRecordDeleteTests: GRDBTestCase { let alias = TableAlias(name: "p") try Player.aliased(alias).including(required: Player.team).deleteAll(db) XCTAssertEqual(self.lastSQLQuery, """ - DELETE FROM "player" WHERE rowid IN (\ - SELECT "p"."rowid" \ + DELETE FROM "player" WHERE "id" IN (\ + SELECT "p"."id" \ FROM "player" "p" \ JOIN "team" ON "team"."id" = "p"."teamId") """) @@ -224,8 +224,8 @@ class MutablePersistableRecordDeleteTests: GRDBTestCase { do { try Team.having(Team.players.isEmpty).deleteAll(db) XCTAssertEqual(self.lastSQLQuery, """ - DELETE FROM "team" WHERE rowid IN (\ - SELECT "team"."rowid" \ + DELETE FROM "team" WHERE "id" IN (\ + SELECT "team"."id" \ FROM "team" \ LEFT JOIN "player" ON "player"."teamId" = "team"."id" \ GROUP BY "team"."id" \ @@ -261,8 +261,8 @@ class MutablePersistableRecordDeleteTests: GRDBTestCase { do { try Player.all().groupByPrimaryKey().deleteAll(db) XCTAssertEqual(self.lastSQLQuery, """ - DELETE FROM "player" WHERE rowid IN (\ - SELECT "rowid" \ + DELETE FROM "player" WHERE "id" IN (\ + SELECT "id" \ FROM "player" \ GROUP BY "id") """) @@ -270,8 +270,8 @@ class MutablePersistableRecordDeleteTests: GRDBTestCase { do { try Player.all().group(Column.rowID).deleteAll(db) XCTAssertEqual(self.lastSQLQuery, """ - DELETE FROM "player" WHERE rowid IN (\ - SELECT "rowid" \ + DELETE FROM "player" WHERE "id" IN (\ + SELECT "id" \ FROM "player" \ GROUP BY "rowid") """) @@ -279,7 +279,7 @@ class MutablePersistableRecordDeleteTests: GRDBTestCase { do { try Passport.all().groupByPrimaryKey().deleteAll(db) XCTAssertEqual(self.lastSQLQuery, """ - DELETE FROM "passport" WHERE rowid IN (\ + DELETE FROM "passport" WHERE "rowid" IN (\ SELECT "rowid" \ FROM "passport" \ GROUP BY "countryCode", "citizenId") @@ -288,7 +288,7 @@ class MutablePersistableRecordDeleteTests: GRDBTestCase { do { try Passport.all().group(Column.rowID).deleteAll(db) XCTAssertEqual(self.lastSQLQuery, """ - DELETE FROM "passport" WHERE rowid IN (\ + DELETE FROM "passport" WHERE "rowid" IN (\ SELECT "rowid" \ FROM "passport" \ GROUP BY "rowid") diff --git a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift index cc43f4fef..dc22d59d0 100644 --- a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift +++ b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift @@ -422,8 +422,8 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { do { try Player.including(required: Player.team).updateAll(db, Column("score").set(to: 0)) XCTAssertEqual(self.lastSQLQuery, """ - UPDATE "player" SET "score" = 0 WHERE rowid IN (\ - SELECT "player"."rowid" \ + UPDATE "player" SET "score" = 0 WHERE "id" IN (\ + SELECT "player"."id" \ FROM "player" \ JOIN "team" ON "team"."id" = "player"."teamId") """) @@ -432,8 +432,8 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { // Regression test for https://github.com/groue/GRDB.swift/issues/758 try Player.including(required: Player.team.filter(Column("active") == 1)).updateAll(db, Column("score").set(to: 0)) XCTAssertEqual(self.lastSQLQuery, """ - UPDATE "player" SET "score" = 0 WHERE rowid IN (\ - SELECT "player"."rowid" \ + UPDATE "player" SET "score" = 0 WHERE "id" IN (\ + SELECT "player"."id" \ FROM "player" \ JOIN "team" ON ("team"."id" = "player"."teamId") AND ("team"."active" = 1)) """) @@ -442,8 +442,8 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { let alias = TableAlias(name: "p") try Player.aliased(alias).including(required: Player.team).updateAll(db, Column("score").set(to: 0)) XCTAssertEqual(self.lastSQLQuery, """ - UPDATE "player" SET "score" = 0 WHERE rowid IN (\ - SELECT "p"."rowid" \ + UPDATE "player" SET "score" = 0 WHERE "id" IN (\ + SELECT "p"."id" \ FROM "player" "p" \ JOIN "team" ON "team"."id" = "p"."teamId") """) @@ -451,8 +451,8 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { do { try Team.having(Team.players.isEmpty).updateAll(db, Column("active").set(to: false)) XCTAssertEqual(self.lastSQLQuery, """ - UPDATE "team" SET "active" = 0 WHERE rowid IN (\ - SELECT "team"."rowid" \ + UPDATE "team" SET "active" = 0 WHERE "id" IN (\ + SELECT "team"."id" \ FROM "team" \ LEFT JOIN "player" ON "player"."teamId" = "team"."id" \ GROUP BY "team"."id" \ @@ -489,8 +489,8 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { do { try Player.all().groupByPrimaryKey().updateAll(db, Column("score").set(to: 0)) XCTAssertEqual(self.lastSQLQuery, """ - UPDATE "player" SET "score" = 0 WHERE rowid IN (\ - SELECT "rowid" \ + UPDATE "player" SET "score" = 0 WHERE "id" IN (\ + SELECT "id" \ FROM "player" \ GROUP BY "id") """) @@ -498,8 +498,8 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { do { try Player.all().group(Column.rowID).updateAll(db, Column("score").set(to: 0)) XCTAssertEqual(self.lastSQLQuery, """ - UPDATE "player" SET "score" = 0 WHERE rowid IN (\ - SELECT "rowid" \ + UPDATE "player" SET "score" = 0 WHERE "id" IN (\ + SELECT "id" \ FROM "player" \ GROUP BY "rowid") """) @@ -507,7 +507,7 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { do { try Passport.all().groupByPrimaryKey().updateAll(db, Column("active").set(to: true)) XCTAssertEqual(self.lastSQLQuery, """ - UPDATE "passport" SET "active" = 1 WHERE rowid IN (\ + UPDATE "passport" SET "active" = 1 WHERE "rowid" IN (\ SELECT "rowid" \ FROM "passport" \ GROUP BY "countryCode", "citizenId") @@ -516,7 +516,7 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { do { try Passport.all().group(Column.rowID).updateAll(db, Column("active").set(to: true)) XCTAssertEqual(self.lastSQLQuery, """ - UPDATE "passport" SET "active" = 1 WHERE rowid IN (\ + UPDATE "passport" SET "active" = 1 WHERE "rowid" IN (\ SELECT "rowid" \ FROM "passport" \ GROUP BY "rowid")