diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 1c0a5d6a6..b77842e0a 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -263,85 +263,6 @@ extension Association { } } -/// 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. -/// -/// :nodoc: -public /* TODO: make internal when possible */ struct JoinCondition { - /// 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) - } -} - -/// 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: -extension JoinCondition: Equatable { } - extension Association { /// Creates an association that includes another one. The columns of the /// associated record are selected. The returned association does not diff --git a/GRDB/QueryInterface/QueryInterfaceQuery.swift b/GRDB/QueryInterface/QueryInterfaceQuery.swift index e70167aa8..175289d4b 100644 --- a/GRDB/QueryInterface/QueryInterfaceQuery.swift +++ b/GRDB/QueryInterface/QueryInterfaceQuery.swift @@ -471,6 +471,85 @@ public /* TODO: make internal when possible */ 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. +/// +/// :nodoc: +public /* TODO: make internal when possible */ struct JoinCondition { + /// 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) + } +} + +/// 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: +extension JoinCondition: Equatable { } + struct Join { var joinOperator: JoinOperator var joinCondition: JoinCondition