diff --git a/GRDB/QueryInterface/SQLRelation.swift b/GRDB/QueryInterface/SQLRelation.swift index b45d178a1..f8172f3c5 100644 --- a/GRDB/QueryInterface/SQLRelation.swift +++ b/GRDB/QueryInterface/SQLRelation.swift @@ -367,3 +367,202 @@ extension SQLRelation { } } } + +// MARK: - Join + +/// Not to be mismatched with SQL join operators (inner join, left join). +/// +/// JoinOperator is designed to be hierarchically nested, unlike +/// SQL join operators. +/// +/// Consider the following request for (A, B, C) tuples: +/// +/// let r = A.including(optional: A.b.including(required: B.c)) +/// +/// It chains three associations, the first optional, the second required. +/// +/// It looks like it means: "Give me all As, along with their Bs, granted those +/// Bs have their Cs. For As whose B has no C, give me a nil B". +/// +/// It can not be expressed as one left join, and a regular join, as below, +/// Because this would not honor the first optional: +/// +/// -- dubious +/// SELECT a.*, b.*, c.* +/// FROM a +/// LEFT JOIN b ON ... +/// JOIN c ON ... +/// +/// Instead, it should: +/// - allow (A + missing (B + C)) +/// - prevent (A + (B + missing C)). +/// +/// This can be expressed in SQL with two left joins, and an extra condition: +/// +/// -- likely correct +/// SELECT a.*, b.*, c.* +/// FROM a +/// LEFT JOIN b ON ... +/// LEFT JOIN c ON ... +/// WHERE NOT((b.id IS NOT NULL) AND (c.id IS NULL)) -- no B without C +/// +/// This is currently not implemented, and requires a little more thought. +/// I don't even know if inventing a whole new way to perform joins should even +/// be on the table. But we have a hierarchical way to express joined queries, +/// and they have a meaning: +/// +/// // what is my meaning? +/// A.including(optional: A.b.including(required: B.c)) +/// +/// :nodoc: +public /* TODO: internal */ enum JoinOperator { + case required, optional +} + +/// The condition that links two joined tables. +/// +/// Currently, we only support one kind of join condition: foreign keys. +/// +/// SELECT ... FROM book JOIN author ON author.id = book.authorId +/// <- the join condition --> +/// +/// When we eventually add support for new ways to join tables, JoinCondition +/// is the type we'll need to update. +/// +/// JoinCondition equality allows merging of associations: +/// +/// // request1 and request2 are equivalent +/// let request1 = Book +/// .including(required: Book.author) +/// let request2 = Book +/// .including(required: Book.author) +/// .including(required: Book.author) +/// +/// // request3 and request4 are equivalent +/// let request3 = Book +/// .including(required: Book.author.filter(condition1 && condition2)) +/// let request4 = Book +/// .joining(required: Book.author.filter(condition1)) +/// .including(optional: Book.author.filter(condition2)) +/// +/// :nodoc: +public /* TODO: internal */ struct JoinCondition: Equatable { + /// Definition of a foreign key + var foreignKeyRequest: ForeignKeyRequest + + /// True if the table at the origin of the foreign key is on the left of + /// the sql JOIN operator. + /// + /// Let's consider the `book.authorId -> author.id` foreign key. + /// Its origin table is `book`. + /// + /// The origin table `book` is on the left of the JOIN operator for + /// the BelongsTo association: + /// + /// -- Book.including(required: Book.author) + /// SELECT ... FROM book JOIN author ON author.id = book.authorId + /// + /// The origin table `book`is on the right of the JOIN operator for + /// the HasMany and HasOne associations: + /// + /// -- Author.including(required: Author.books) + /// SELECT ... FROM author JOIN book ON author.id = book.authorId + var originIsLeft: Bool + + /// Returns an SQL expression for the join condition. + /// + /// SELECT ... FROM book JOIN author ON author.id = book.authorId + /// <- the SQL expression --> + /// + /// - parameter db: A database connection. + /// - parameter leftAlias: A TableAlias for the table on the left of the + /// JOIN operator. + /// - parameter rightAlias: A TableAlias for the table on the right of the + /// JOIN operator. + /// - Returns: An SQL expression. + func sqlExpression(_ db: Database, leftAlias: TableAlias, rightAlias: TableAlias) throws -> SQLExpression { + let foreignKeyMapping = try foreignKeyRequest.fetch(db).mapping + let columnMapping: [(left: Column, right: Column)] + if originIsLeft { + columnMapping = foreignKeyMapping.map { (left: Column($0.origin), right: Column($0.destination)) } + } else { + columnMapping = foreignKeyMapping.map { (left: Column($0.destination), right: Column($0.origin)) } + } + + return columnMapping + .map { $0.right.qualifiedExpression(with: rightAlias) == $0.left.qualifiedExpression(with: leftAlias) } + .joined(operator: .and) + } +} + +struct Join { + var joinOperator: JoinOperator + var joinCondition: JoinCondition + var relation: SQLRelation + + var finalizedJoin: Join { + var join = self + join.relation = relation.finalizedRelation + return join + } + + /// - precondition: relation is the result of finalizedRelation + func joinSQL(_ db: Database,_ context: inout SQLGenerationContext, leftAlias: TableAlias, isRequiredAllowed: Bool) throws -> String { + var isRequiredAllowed = isRequiredAllowed + var sql = "" + switch joinOperator { + case .optional: + isRequiredAllowed = false + sql += "LEFT JOIN" + case .required: + guard isRequiredAllowed else { + // TODO: chainOptionalRequired + fatalError("Not implemented: chaining a required association behind an optional association") + } + sql += "JOIN" + } + + sql += try " " + relation.source.sourceSQL(db, &context) + + let rightAlias = relation.alias! + let filters = try [ + joinCondition.sqlExpression(db, leftAlias: leftAlias, rightAlias: rightAlias), + relation.filterPromise.resolve(db) + ].compactMap { $0 } + if !filters.isEmpty { + sql += " ON " + filters.joined(operator: .and).expressionSQL(&context) + } + + for (_, join) in relation.joins { + sql += try " " + join.joinSQL(db, &context, leftAlias: rightAlias, isRequiredAllowed: isRequiredAllowed) + } + + return sql + } + + /// Returns nil if joins can't be merged (conflict in condition, relation...) + func merged(with other: Join) -> Join? { + guard joinCondition == other.joinCondition else { + // can't merge + return nil + } + + guard let mergedRelation = relation.merged(with: other.relation) else { + // can't merge + return nil + } + + let mergedJoinOperator: JoinOperator + switch (joinOperator, other.joinOperator) { + case (.required, _), (_, .required): + mergedJoinOperator = .required + default: + mergedJoinOperator = .optional + } + + return Join( + joinOperator: mergedJoinOperator, + joinCondition: joinCondition, + relation: mergedRelation) + } +} diff --git a/GRDB/QueryInterface/SQLSelectQuery.swift b/GRDB/QueryInterface/SQLSelectQuery.swift index e00dd886a..24d2d4875 100644 --- a/GRDB/QueryInterface/SQLSelectQuery.swift +++ b/GRDB/QueryInterface/SQLSelectQuery.swift @@ -312,205 +312,6 @@ extension SQLSelectQuery { } } -// MARK: - Join - -/// Not to be mismatched with SQL join operators (inner join, left join). -/// -/// JoinOperator is designed to be hierarchically nested, unlike -/// SQL join operators. -/// -/// Consider the following request for (A, B, C) tuples: -/// -/// let r = A.including(optional: A.b.including(required: B.c)) -/// -/// It chains three associations, the first optional, the second required. -/// -/// It looks like it means: "Give me all As, along with their Bs, granted those -/// Bs have their Cs. For As whose B has no C, give me a nil B". -/// -/// It can not be expressed as one left join, and a regular join, as below, -/// Because this would not honor the first optional: -/// -/// -- dubious -/// SELECT a.*, b.*, c.* -/// FROM a -/// LEFT JOIN b ON ... -/// JOIN c ON ... -/// -/// Instead, it should: -/// - allow (A + missing (B + C)) -/// - prevent (A + (B + missing C)). -/// -/// This can be expressed in SQL with two left joins, and an extra condition: -/// -/// -- likely correct -/// SELECT a.*, b.*, c.* -/// FROM a -/// LEFT JOIN b ON ... -/// LEFT JOIN c ON ... -/// WHERE NOT((b.id IS NOT NULL) AND (c.id IS NULL)) -- no B without C -/// -/// This is currently not implemented, and requires a little more thought. -/// I don't even know if inventing a whole new way to perform joins should even -/// be on the table. But we have a hierarchical way to express joined queries, -/// and they have a meaning: -/// -/// // what is my meaning? -/// A.including(optional: A.b.including(required: B.c)) -/// -/// :nodoc: -public /* TODO: internal */ enum JoinOperator { - case required, optional -} - -/// The condition that links two joined tables. -/// -/// Currently, we only support one kind of join condition: foreign keys. -/// -/// SELECT ... FROM book JOIN author ON author.id = book.authorId -/// <- the join condition --> -/// -/// When we eventually add support for new ways to join tables, JoinCondition -/// is the type we'll need to update. -/// -/// JoinCondition equality allows merging of associations: -/// -/// // request1 and request2 are equivalent -/// let request1 = Book -/// .including(required: Book.author) -/// let request2 = Book -/// .including(required: Book.author) -/// .including(required: Book.author) -/// -/// // request3 and request4 are equivalent -/// let request3 = Book -/// .including(required: Book.author.filter(condition1 && condition2)) -/// let request4 = Book -/// .joining(required: Book.author.filter(condition1)) -/// .including(optional: Book.author.filter(condition2)) -/// -/// :nodoc: -public /* TODO: internal */ struct JoinCondition: Equatable { - /// Definition of a foreign key - var foreignKeyRequest: ForeignKeyRequest - - /// True if the table at the origin of the foreign key is on the left of - /// the sql JOIN operator. - /// - /// Let's consider the `book.authorId -> author.id` foreign key. - /// Its origin table is `book`. - /// - /// The origin table `book` is on the left of the JOIN operator for - /// the BelongsTo association: - /// - /// -- Book.including(required: Book.author) - /// SELECT ... FROM book JOIN author ON author.id = book.authorId - /// - /// The origin table `book`is on the right of the JOIN operator for - /// the HasMany and HasOne associations: - /// - /// -- Author.including(required: Author.books) - /// SELECT ... FROM author JOIN book ON author.id = book.authorId - var originIsLeft: Bool - - /// Returns an SQL expression for the join condition. - /// - /// SELECT ... FROM book JOIN author ON author.id = book.authorId - /// <- the SQL expression --> - /// - /// - parameter db: A database connection. - /// - parameter leftAlias: A TableAlias for the table on the left of the - /// JOIN operator. - /// - parameter rightAlias: A TableAlias for the table on the right of the - /// JOIN operator. - /// - Returns: An SQL expression. - func sqlExpression(_ db: Database, leftAlias: TableAlias, rightAlias: TableAlias) throws -> SQLExpression { - let foreignKeyMapping = try foreignKeyRequest.fetch(db).mapping - let columnMapping: [(left: Column, right: Column)] - if originIsLeft { - columnMapping = foreignKeyMapping.map { (left: Column($0.origin), right: Column($0.destination)) } - } else { - columnMapping = foreignKeyMapping.map { (left: Column($0.destination), right: Column($0.origin)) } - } - - return columnMapping - .map { $0.right.qualifiedExpression(with: rightAlias) == $0.left.qualifiedExpression(with: leftAlias) } - .joined(operator: .and) - } -} - -struct Join { - var joinOperator: JoinOperator - var joinCondition: JoinCondition - var relation: SQLRelation - - var finalizedJoin: Join { - var join = self - join.relation = relation.finalizedRelation - return join - } - - /// - precondition: relation is the result of finalizedRelation - func joinSQL(_ db: Database,_ context: inout SQLGenerationContext, leftAlias: TableAlias, isRequiredAllowed: Bool) throws -> String { - var isRequiredAllowed = isRequiredAllowed - var sql = "" - switch joinOperator { - case .optional: - isRequiredAllowed = false - sql += "LEFT JOIN" - case .required: - guard isRequiredAllowed else { - // TODO: chainOptionalRequired - fatalError("Not implemented: chaining a required association behind an optional association") - } - sql += "JOIN" - } - - sql += try " " + relation.source.sourceSQL(db, &context) - - let rightAlias = relation.alias! - let filters = try [ - joinCondition.sqlExpression(db, leftAlias: leftAlias, rightAlias: rightAlias), - relation.filterPromise.resolve(db) - ].compactMap { $0 } - if !filters.isEmpty { - sql += " ON " + filters.joined(operator: .and).expressionSQL(&context) - } - - for (_, join) in relation.joins { - sql += try " " + join.joinSQL(db, &context, leftAlias: rightAlias, isRequiredAllowed: isRequiredAllowed) - } - - return sql - } - - /// Returns nil if joins can't be merged (conflict in condition, relation...) - func merged(with other: Join) -> Join? { - guard joinCondition == other.joinCondition else { - // can't merge - return nil - } - - guard let mergedRelation = relation.merged(with: other.relation) else { - // can't merge - return nil - } - - let mergedJoinOperator: JoinOperator - switch (joinOperator, other.joinOperator) { - case (.required, _), (_, .required): - mergedJoinOperator = .required - default: - mergedJoinOperator = .optional - } - - return Join( - joinOperator: mergedJoinOperator, - joinCondition: joinCondition, - relation: mergedRelation) - } -} - struct SQLLimit { let limit: Int let offset: Int? @@ -525,7 +326,7 @@ struct SQLLimit { } extension SQLCount { - var sqlSelectable: SQLSelectable { + fileprivate var sqlSelectable: SQLSelectable { switch self { case .all: return SQLExpressionCount(AllColumns())