From 1e3de49251fb6bbc15417f1f9f382cfea682bc9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 28 Apr 2019 13:25:56 +0200 Subject: [PATCH] Rename SQLJoin.Condition to SQLJoinCondition --- .../Association/Association.swift | 4 +- .../Association/BelongsToAssociation.swift | 2 +- .../Association/HasManyAssociation.swift | 2 +- .../Association/HasOneAssociation.swift | 2 +- GRDB/QueryInterface/SQLRelation.swift | 160 +++++++++--------- .../SQLSelectQueryGenerator.swift | 2 +- 6 files changed, 87 insertions(+), 85 deletions(-) diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 99765fbdb..cd22fb01e 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -410,7 +410,7 @@ public /* TODO: internal */ struct SQLAssociation { // SQLAssociation is a non-empty array of association elements private struct Element { var key: String - var condition: SQLJoin.Condition + var condition: SQLJoinCondition var relation: SQLRelation } private var head: Element @@ -423,7 +423,7 @@ public /* TODO: internal */ struct SQLAssociation { self.tail = tail } - init(key: String, condition: SQLJoin.Condition, relation: SQLRelation) { + init(key: String, condition: SQLJoinCondition, relation: SQLRelation) { head = Element(key: key, condition: condition, relation: relation) tail = [] } diff --git a/GRDB/QueryInterface/Association/BelongsToAssociation.swift b/GRDB/QueryInterface/Association/BelongsToAssociation.swift index d4d7fbb6e..f3d89f70b 100644 --- a/GRDB/QueryInterface/Association/BelongsToAssociation.swift +++ b/GRDB/QueryInterface/Association/BelongsToAssociation.swift @@ -146,7 +146,7 @@ extension TableRecord { destinationTable: Destination.databaseTableName, foreignKey: foreignKey) - let condition = SQLJoin.Condition( + let condition = SQLJoinCondition( foreignKeyRequest: foreignKeyRequest, originIsLeft: true) diff --git a/GRDB/QueryInterface/Association/HasManyAssociation.swift b/GRDB/QueryInterface/Association/HasManyAssociation.swift index eaecd468f..c3bbb9267 100644 --- a/GRDB/QueryInterface/Association/HasManyAssociation.swift +++ b/GRDB/QueryInterface/Association/HasManyAssociation.swift @@ -146,7 +146,7 @@ extension TableRecord { destinationTable: databaseTableName, foreignKey: foreignKey) - let condition = SQLJoin.Condition( + let condition = SQLJoinCondition( foreignKeyRequest: foreignKeyRequest, originIsLeft: false) diff --git a/GRDB/QueryInterface/Association/HasOneAssociation.swift b/GRDB/QueryInterface/Association/HasOneAssociation.swift index 2605ba97c..03700fffa 100644 --- a/GRDB/QueryInterface/Association/HasOneAssociation.swift +++ b/GRDB/QueryInterface/Association/HasOneAssociation.swift @@ -148,7 +148,7 @@ extension TableRecord { destinationTable: databaseTableName, foreignKey: foreignKey) - let condition = SQLJoin.Condition( + let condition = SQLJoinCondition( foreignKeyRequest: foreignKeyRequest, originIsLeft: false) diff --git a/GRDB/QueryInterface/SQLRelation.swift b/GRDB/QueryInterface/SQLRelation.swift index 2fcc310be..b30fa3ce4 100644 --- a/GRDB/QueryInterface/SQLRelation.swift +++ b/GRDB/QueryInterface/SQLRelation.swift @@ -198,89 +198,91 @@ extension SQLRelation { } } +// MARK: - SQLJoinCondition + +/// 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, Condition +/// is the type we'll need to update. +/// +/// Condition 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)) +struct SQLJoinCondition: 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 + + var reversed: SQLJoinCondition { + return SQLJoinCondition(foreignKeyRequest: foreignKeyRequest, originIsLeft: !originIsLeft) + } + + /// 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.fetchMapping(db) + 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) + } +} + // MARK: - SQLJoin struct SQLJoin { - /// 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, Condition - /// is the type we'll need to update. - /// - /// Condition 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)) - struct Condition: 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 - - var reversed: Condition { - return Condition(foreignKeyRequest: foreignKeyRequest, originIsLeft: !originIsLeft) - } - - /// 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.fetchMapping(db) - 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) - } - } - var isRequired: Bool - var condition: Condition + var condition: SQLJoinCondition var relation: SQLRelation } diff --git a/GRDB/QueryInterface/SQLSelectQueryGenerator.swift b/GRDB/QueryInterface/SQLSelectQueryGenerator.swift index 5ac95d52c..f512e95b1 100644 --- a/GRDB/QueryInterface/SQLSelectQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLSelectQueryGenerator.swift @@ -405,7 +405,7 @@ private enum SQLQualifiedSource { /// A "qualified" join, where all tables are identified with a table alias. private struct SQLQualifiedJoin { private let isRequired: Bool - private let condition: SQLJoin.Condition + private let condition: SQLJoinCondition let relation: SQLQualifiedRelation init(_ join: SQLJoin) {