From 331938c64707e238fa1da4ae542bb22c10c44ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 4 May 2019 15:18:39 +0200 Subject: [PATCH] More including(all:) --- .../Association/Association.swift | 96 ++++++++++++++----- .../QueryInterfaceRequest+Association.swift | 10 +- GRDB/QueryInterface/SQLRelation.swift | 18 ++-- .../SQLSelectQueryGenerator.swift | 16 ++-- 4 files changed, 98 insertions(+), 42 deletions(-) diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 5dab853cf..d7b0243f5 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -241,11 +241,46 @@ extension Association { } } +extension SQLRelation { + /// Creates an relation that prefetches another one. + func including(all sqlAssociation: SQLAssociation) -> SQLRelation { + return sqlAssociation.extendedRelation(self, kind: .allPrefetched) + } + + /// Creates an relation that includes another one. The columns of the + /// associated record are selected. The returned relation does not + /// require that the associated database table contains a matching row. + func including(optional sqlAssociation: SQLAssociation) -> SQLRelation { + return sqlAssociation.extendedRelation(self, kind: .oneOptional) + } + + /// Creates an relation that includes another one. The columns of the + /// associated record are selected. The returned relation requires + /// that the associated database table contains a matching row. + func including(required sqlAssociation: SQLAssociation) -> SQLRelation { + return sqlAssociation.extendedRelation(self, kind: .oneRequired) + } + + /// Creates an relation that joins another one. The columns of the + /// associated record are not selected. The returned relation does not + /// require that the associated database table contains a matching row. + func joining(optional sqlAssociation: SQLAssociation) -> SQLRelation { + return sqlAssociation.mapRelation { $0.select([]) }.extendedRelation(self, kind: .oneOptional) + } + + /// Creates an relation that joins another one. The columns of the + /// associated record are not selected. The returned relation requires + /// that the associated database table contains a matching row. + func joining(required sqlAssociation: SQLAssociation) -> SQLRelation { + return sqlAssociation.mapRelation { $0.select([]) }.extendedRelation(self, kind: .oneRequired) + } +} + extension Association { /// Creates an association that prefetches another one. public func including(all association: A) -> Self where A.OriginRowDecoder == RowDecoder { return mapRelation { - _ in fatalError("TODO") + $0.including(all: association.sqlAssociation) } } @@ -254,7 +289,7 @@ extension Association { /// require that the associated database table contains a matching row. public func including(optional association: A) -> Self where A.OriginRowDecoder == RowDecoder { return mapRelation { - association.sqlAssociation.extendedRelation($0, required: false) + $0.including(optional: association.sqlAssociation) } } @@ -263,7 +298,7 @@ extension Association { /// that the associated database table contains a matching row. public func including(required association: A) -> Self where A.OriginRowDecoder == RowDecoder { return mapRelation { - association.sqlAssociation.extendedRelation($0, required: true) + $0.including(required: association.sqlAssociation) } } @@ -272,7 +307,7 @@ extension Association { /// require that the associated database table contains a matching row. public func joining(optional association: A) -> Self where A.OriginRowDecoder == RowDecoder { return mapRelation { - association.select([]).sqlAssociation.extendedRelation($0, required: false) + $0.joining(optional: association.sqlAssociation) } } @@ -281,7 +316,7 @@ extension Association { /// that the associated database table contains a matching row. public func joining(required association: A) -> Self where A.OriginRowDecoder == RowDecoder { return mapRelation { - association.select([]).sqlAssociation.extendedRelation($0, required: true) + $0.joining(required: association.sqlAssociation) } } } @@ -409,7 +444,7 @@ public protocol AssociationToOne: Association { } /// Origin.including(required: association) /// /// For direct associations such as BelongTo or HasMany, the chain contains a -/// single element, the "destination", without intermediate pivot: +/// single element, the "destination", without intermediate step: /// /// // "Origin" belongsTo "destination": /// // SELECT origin.*, destination.* @@ -525,16 +560,16 @@ public /* TODO: internal */ struct SQLAssociation { /// HasMany in the above examples, but also for indirect associations such /// as HasManyThrough, which have any number of pivot relations between the /// origin and the destination. - func extendedRelation(_ origin: SQLRelation, required: Bool) -> SQLRelation { + func extendedRelation(_ origin: SQLRelation, kind: SQLRelation.Child.Kind) -> SQLRelation { let destinationChild = SQLRelation.Child( - kind: required ? .oneRequired : .oneOptional, + kind: kind, condition: destination.condition, relation: destination.relation) let initialSteps = steps.dropLast() if initialSteps.isEmpty { // This is a direct join from origin to destination, without - // intermediate pivot. + // intermediate step. // // SELECT origin.*, destination.* // FROM origin @@ -562,12 +597,22 @@ public /* TODO: internal */ struct SQLAssociation { // Let's recurse toward a direct join, by making a new association which // ends on the last pivot, to which we join our destination: var reducedAssociation = SQLAssociation(steps: Array(initialSteps)) - reducedAssociation = reducedAssociation.mapRelation { lastPivot in - lastPivot - .select([]) // pivot is not included in the selection - .appendingChild(destinationChild, forKey: destination.key) + reducedAssociation = reducedAssociation.mapRelation { + $0.appendingChild(destinationChild, forKey: destination.key) } - return reducedAssociation.extendedRelation(origin, required: required) + // Intermediate steps are not prefetched + reducedAssociation = reducedAssociation.mapRelation { + $0.select([]) + } + // Intermediate steps are not prefetched: change the kind if necessary + let reducedKind: SQLRelation.Child.Kind + switch kind { + case .oneRequired, .oneOptional, .allNotPrefetched: + reducedKind = kind + case .allPrefetched: + reducedKind = .allNotPrefetched + } + return reducedAssociation.extendedRelation(origin, kind: reducedKind) } /// Given an origin alias and rows, returns the destination of the @@ -614,7 +659,7 @@ public /* TODO: internal */ struct SQLAssociation { // Filter the pivot let pivot = steps[0] let pivotAlias = TableAlias() - let pivotRelation = pivot.relation + let filteredPivotRelation = pivot.relation .qualified(with: pivotAlias) .filter({ db in // `pivot.originId = 123` or `pivot.originId IN (1, 2, 3)` @@ -623,7 +668,7 @@ public /* TODO: internal */ struct SQLAssociation { if steps.count == 1 { // This is a direct join from origin to destination, without - // intermediate pivot. + // intermediate step. // // SELECT destination.* // FROM destination @@ -631,11 +676,11 @@ public /* TODO: internal */ struct SQLAssociation { // // let association = Origin.hasMany(Destination.self) // Origin(id: 1).request(for: association) - return pivotRelation + return filteredPivotRelation } // This is an indirect join from origin to destination, through - // some pivot(s): + // some intermediate steps: // // SELECT destination.* // FROM destination @@ -647,18 +692,23 @@ public /* TODO: internal */ struct SQLAssociation { // via: Pivot.belongsTo(Destination.self)) // Origin(id: 1).request(for: association) let reversedSteps = zip(steps, steps.dropFirst()) - .map { (step, nextStep) in - AssociationStep( + .map { (step, nextStep) -> AssociationStep in + // Intermediate steps are not included in the selection, and + // don't have any child. + let relation = step.relation.select([]).deletingChildren() + return AssociationStep( key: step.key, condition: nextStep.condition.reversed, - relation: step.relation.select([])) + relation: relation) } .reversed() var reversedAssociation = SQLAssociation(steps: Array(reversedSteps)) + // Replace pivot with the filtered one (not included in the selection, + // without children). reversedAssociation = reversedAssociation.mapRelation { _ in - pivotRelation.select([]) // pivot is not included in the selection + filteredPivotRelation.select([]).deletingChildren() } - return reversedAssociation.extendedRelation(destination.relation, required: true) + return reversedAssociation.extendedRelation(destination.relation, kind: .oneRequired) } } diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index b042e9424..c1a7da4a7 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -5,7 +5,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { public func including(all association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { return mapQuery { $0.mapRelation { - _ in fatalError("TODO") + $0.including(all: association.sqlAssociation) } } } @@ -16,7 +16,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { public func including(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { return mapQuery { $0.mapRelation { - association.sqlAssociation.extendedRelation($0, required: false) + $0.including(optional: association.sqlAssociation) } } } @@ -27,7 +27,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { public func including(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { return mapQuery { $0.mapRelation { - association.sqlAssociation.extendedRelation($0, required: true) + $0.including(required: association.sqlAssociation) } } } @@ -38,7 +38,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { public func joining(optional association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { return mapQuery { $0.mapRelation { - association.select([]).sqlAssociation.extendedRelation($0, required: false) + $0.joining(optional: association.sqlAssociation) } } } @@ -49,7 +49,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { public func joining(required association: A) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { return mapQuery { $0.mapRelation { - association.select([]).sqlAssociation.extendedRelation($0, required: true) + $0.joining(required: association.sqlAssociation) } } } diff --git a/GRDB/QueryInterface/SQLRelation.swift b/GRDB/QueryInterface/SQLRelation.swift index 1e4506b66..067ae0ae4 100644 --- a/GRDB/QueryInterface/SQLRelation.swift +++ b/GRDB/QueryInterface/SQLRelation.swift @@ -19,7 +19,7 @@ struct SQLRelation { // Record.including(all: association) case allPrefetched // Record.including(all: associationThroughPivot) - case all + case allNotPrefetched } var kind: Kind @@ -34,7 +34,7 @@ struct SQLRelation { switch kind { case .oneOptional, .oneRequired: return true - case .all, .allPrefetched: + case .allPrefetched, .allNotPrefetched: return false } } @@ -118,6 +118,12 @@ extension SQLRelation { return relation } + func deletingChildren() -> SQLRelation { + var relation = self + relation.children = [:] + return relation + } + func qualified(with alias: TableAlias) -> SQLRelation { var relation = self relation.source = source.qualified(with: alias) @@ -513,8 +519,8 @@ extension SQLRelation.Child.Kind { return .oneOptional case (.allPrefetched, .allPrefetched), - (.allPrefetched, .all), - (.all, .allPrefetched): + (.allPrefetched, .allNotPrefetched), + (.allNotPrefetched, .allPrefetched): // Prefetches both Pivot and Destination: // // Record @@ -522,13 +528,13 @@ extension SQLRelation.Child.Kind { // .including(all: associationToPivot) return .allPrefetched - case (.all, .all): + case (.allNotPrefetched, .allNotPrefetched): // Equivalent to Record.including(all: association) // // Record // .including(all: association) // .including(all: association) - return .all + return .allNotPrefetched default: // Likely a programmer error: diff --git a/GRDB/QueryInterface/SQLSelectQueryGenerator.swift b/GRDB/QueryInterface/SQLSelectQueryGenerator.swift index 1f71b1ee7..2d9dee565 100644 --- a/GRDB/QueryInterface/SQLSelectQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLSelectQueryGenerator.swift @@ -420,8 +420,8 @@ private enum SQLQualifiedSource { /// A "qualified" join, where all tables are identified with a table alias. private struct SQLQualifiedJoin { private enum Kind: String { - case left = "LEFT JOIN" - case inner = "JOIN" + case leftJoin = "LEFT JOIN" + case innerJoin = "JOIN" } private let kind: Kind private let condition: SQLAssociationCondition @@ -430,11 +430,11 @@ private struct SQLQualifiedJoin { init?(_ child: SQLRelation.Child) { switch child.kind { case .oneRequired: - self.kind = .inner + self.kind = .innerJoin case .oneOptional: - self.kind = .left - case .all, .allPrefetched: - // This relation child is not implemented with an SQL join. + self.kind = .leftJoin + case .allPrefetched, .allNotPrefetched: + // This relation child is not fetched with an SQL join. return nil } @@ -451,12 +451,12 @@ private struct SQLQualifiedJoin { var sql = "" switch self.kind { - case .inner: + case .innerJoin: guard allowsInnerJoin else { // TODO: chainOptionalRequired fatalError("Not implemented: chaining a required association behind an optional association") } - case .left: + case .leftJoin: allowsInnerJoin = false } sql += kind.rawValue