From 4f8e97a07ccf0c185044eb82a4e1c4bfe1da7c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 7 Oct 2018 12:08:25 +0200 Subject: [PATCH] AssociationAggregate is now generic on its anchor type only --- .../Association/AssociationAggregate.swift | 55 ++++++++-------- .../Association/HasManyAssociation.swift | 63 +++++++++---------- .../QueryInterfaceRequest+Association.swift | 16 ++--- 3 files changed, 63 insertions(+), 71 deletions(-) diff --git a/GRDB/QueryInterface/Association/AssociationAggregate.swift b/GRDB/QueryInterface/Association/AssociationAggregate.swift index 15b36195a..3faf36be3 100644 --- a/GRDB/QueryInterface/Association/AssociationAggregate.swift +++ b/GRDB/QueryInterface/Association/AssociationAggregate.swift @@ -9,20 +9,23 @@ import Foundation /// TODO -public struct AssociationAggregate { - let association: A +public struct AssociationAggregate { + let aggregatedRequest: (QueryInterfaceRequest, TableAlias) -> QueryInterfaceRequest let expression: SQLExpression var alias: String? - init(association: A, expression: SQLExpression) { - self.association = association + init( + expression: SQLExpression, + aggregatedRequest: @escaping (QueryInterfaceRequest, TableAlias) -> QueryInterfaceRequest) + { + self.aggregatedRequest = aggregatedRequest self.expression = expression } } extension AssociationAggregate { /// TODO - public func aliased(_ name: String) -> AssociationAggregate { + public func aliased(_ name: String) -> AssociationAggregate { var aggregate = self aggregate.alias = name return aggregate @@ -30,50 +33,50 @@ extension AssociationAggregate { } /// TODO -public prefix func ! (aggregate: AssociationAggregate) -> AssociationAggregate { +public prefix func ! (aggregate: AssociationAggregate) -> AssociationAggregate { return AssociationAggregate( - association: aggregate.association, - expression: !aggregate.expression) + expression: !aggregate.expression, + aggregatedRequest: aggregate.aggregatedRequest) } /// TODO -public func == (lhs: AssociationAggregate, rhs: SQLExpressible?) -> AssociationAggregate { +public func == (lhs: AssociationAggregate, rhs: SQLExpressible?) -> AssociationAggregate { return AssociationAggregate( - association: lhs.association, - expression: lhs.expression == rhs) + expression: lhs.expression == rhs, + aggregatedRequest: lhs.aggregatedRequest) } /// TODO -public func == (lhs: SQLExpressible?, rhs: AssociationAggregate) -> AssociationAggregate { +public func == (lhs: AssociationAggregate, rhs: Bool) -> AssociationAggregate { return AssociationAggregate( - association: rhs.association, - expression: lhs == rhs.expression) + expression: lhs.expression == rhs, + aggregatedRequest: lhs.aggregatedRequest) } /// TODO -public func == (lhs: AssociationAggregate, rhs: Bool) -> AssociationAggregate { +public func == (lhs: SQLExpressible?, rhs: AssociationAggregate) -> AssociationAggregate { return AssociationAggregate( - association: lhs.association, - expression: lhs.expression == rhs) + expression: lhs == rhs.expression, + aggregatedRequest: rhs.aggregatedRequest) } /// TODO -public func != (lhs: AssociationAggregate, rhs: SQLExpressible?) -> AssociationAggregate { +public func != (lhs: AssociationAggregate, rhs: SQLExpressible?) -> AssociationAggregate { return AssociationAggregate( - association: lhs.association, - expression: lhs.expression != rhs) + expression: lhs.expression != rhs, + aggregatedRequest: lhs.aggregatedRequest) } /// TODO -public func != (lhs: SQLExpressible?, rhs: AssociationAggregate) -> AssociationAggregate { +public func != (lhs: SQLExpressible?, rhs: AssociationAggregate) -> AssociationAggregate { return AssociationAggregate( - association: rhs.association, - expression: lhs != rhs.expression) + expression: lhs != rhs.expression, + aggregatedRequest: rhs.aggregatedRequest) } /// TODO -public func != (lhs: AssociationAggregate, rhs: Bool) -> AssociationAggregate { +public func != (lhs: AssociationAggregate, rhs: Bool) -> AssociationAggregate { return AssociationAggregate( - association: lhs.association, - expression: lhs.expression != rhs) + expression: lhs.expression != rhs, + aggregatedRequest: lhs.aggregatedRequest) } diff --git a/GRDB/QueryInterface/Association/HasManyAssociation.swift b/GRDB/QueryInterface/Association/HasManyAssociation.swift index d07e71fc4..5cc0975fd 100644 --- a/GRDB/QueryInterface/Association/HasManyAssociation.swift +++ b/GRDB/QueryInterface/Association/HasManyAssociation.swift @@ -93,13 +93,23 @@ extension HasManyAssociation: TableRequest where Destination: TableRecord { public var databaseTableName: String { return Destination.databaseTableName } } -extension HasManyAssociation where Destination: TableRecord { +extension HasManyAssociation where Origin: TableRecord, Destination: TableRecord { + private func aggregate(_ expression: SQLExpression) -> AssociationAggregate { + return AssociationAggregate( + expression: expression, + aggregatedRequest: { request, tableAlias in + request + .joining(optional: self.aliased(tableAlias)) + .groupByPrimaryKey() + }) + } + /// The number of associated records. /// /// For example: /// /// Team.annotated(with: Team.players.count()) - public func count() -> AssociationAggregate { + public func count() -> AssociationAggregate { return count(aliased: "\(key)Count") } @@ -108,11 +118,8 @@ extension HasManyAssociation where Destination: TableRecord { /// For example: /// /// Team.annotated(with: Team.players.count(aliased: "numberOfPlayers")) - public func count(aliased name: String) -> AssociationAggregate { - return AssociationAggregate( - association: self, - expression: SQLExpressionCountDistinct(Column.rowID)) - .aliased(name) + public func count(aliased name: String) -> AssociationAggregate { + return aggregate(SQLExpressionCountDistinct(Column.rowID)).aliased(name) } /// An aggregate that is true if there exists no associated records. @@ -122,10 +129,8 @@ extension HasManyAssociation where Destination: TableRecord { /// Team.having(Team.players.isEmpty()) /// Team.having(!Team.players.isEmpty()) /// Team.having(Team.players.isEmpty() == false) - public func isEmpty() -> AssociationAggregate { - return AssociationAggregate( - association: self, - expression: SQLExpressionIsEmpty(SQLExpressionCountDistinct(Column.rowID))) + public func isEmpty() -> AssociationAggregate { + return aggregate(SQLExpressionIsEmpty(SQLExpressionCountDistinct(Column.rowID))) } /// The average value of the given column in associated records. @@ -133,16 +138,13 @@ extension HasManyAssociation where Destination: TableRecord { /// For example: /// /// Team.annotated(with: Team.players.average(Column("score"))) - public func average(_ column: Column) -> AssociationAggregate { + public func average(_ column: Column) -> AssociationAggregate { return average(column, aliased: "average\(key.uppercasingFirstCharacter)\(column.name.uppercasingFirstCharacter)") } /// The average value of the given expression in associated records. - public func average(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate { - return AssociationAggregate( - association: self, - expression: SQLExpressionFunction(.avg, arguments: value)) - .aliased(name) + public func average(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate { + return aggregate(SQLExpressionFunction(.avg, arguments: value)).aliased(name) } /// The maximum value of the given column in associated records. @@ -150,16 +152,13 @@ extension HasManyAssociation where Destination: TableRecord { /// For example: /// /// Team.annotated(with: Team.players.max(Column("score"))) - public func max(_ column: Column) -> AssociationAggregate { + public func max(_ column: Column) -> AssociationAggregate { return max(column, aliased: "max\(key.uppercasingFirstCharacter)\(column.name.uppercasingFirstCharacter)") } /// The maximum value of the given expression in associated records. - public func max(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate { - return AssociationAggregate( - association: self, - expression: SQLExpressionFunction(.max, arguments: value)) - .aliased(name) + public func max(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate { + return aggregate(SQLExpressionFunction(.max, arguments: value)).aliased(name) } /// The minimum value of the given column in associated records. @@ -167,16 +166,13 @@ extension HasManyAssociation where Destination: TableRecord { /// For example: /// /// Team.annotated(with: Team.players.min(Column("score"))) - public func min(_ column: Column) -> AssociationAggregate { + public func min(_ column: Column) -> AssociationAggregate { return min(column, aliased: "min\(key.uppercasingFirstCharacter)\(column.name.uppercasingFirstCharacter)") } /// The minimum value of the given expression in associated records. - public func min(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate { - return AssociationAggregate( - association: self, - expression: SQLExpressionFunction(.min, arguments: value)) - .aliased(name) + public func min(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate { + return aggregate(SQLExpressionFunction(.min, arguments: value)).aliased(name) } /// The sum of the given column in associated records. @@ -184,16 +180,13 @@ extension HasManyAssociation where Destination: TableRecord { /// For example: /// /// Team.annotated(with: Team.players.min(Column("score"))) - public func sum(_ column: Column) -> AssociationAggregate { + public func sum(_ column: Column) -> AssociationAggregate { return sum(column, aliased: "\(key)\(column.name.uppercasingFirstCharacter)Sum") } /// The sum of the given expression in associated records. - public func sum(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate { - return AssociationAggregate( - association: self, - expression: SQLExpressionFunction(.sum, arguments: value)) - .aliased(name) + public func sum(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate { + return aggregate(SQLExpressionFunction(.sum, arguments: value)).aliased(name) } } diff --git a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift index 97b2d29b4..c39f2c67e 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest+Association.swift @@ -43,7 +43,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { // MARK: - Association Aggregates /// TODO - public func annotated(with aggregate: AssociationAggregate) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { + public func annotated(with aggregate: AssociationAggregate) -> QueryInterfaceRequest { // SELECT author.*, COUNT(book.rowID) AS bookCount // LEFT JOIN book ON ... // GROUP BY author.id @@ -54,21 +54,17 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord { } else { selectable = tableAlias[aggregate.expression] } - return appendingSelection([selectable]) - .joining(optional: aggregate.association.aliased(tableAlias)) - .groupByPrimaryKey() + return aggregate.aggregatedRequest(appendingSelection([selectable]), tableAlias) } /// TODO - public func having(_ aggregate: AssociationAggregate) -> QueryInterfaceRequest where A.OriginRowDecoder == RowDecoder { + public func having(_ aggregate: AssociationAggregate) -> QueryInterfaceRequest { // SELECT author.* // LEFT JOIN book ON ... // GROUP BY author.id // HAVING COUNT(book.rowID) > 0 let tableAlias = TableAlias() - return having(tableAlias[aggregate.expression]) - .joining(optional: aggregate.association.aliased(tableAlias)) - .groupByPrimaryKey() + return aggregate.aggregatedRequest(having(tableAlias[aggregate.expression]), tableAlias) } } @@ -126,12 +122,12 @@ extension TableRecord { // MARK: - Association Aggregates /// TODO - public static func annotated(with aggregate: AssociationAggregate) -> QueryInterfaceRequest where A.OriginRowDecoder == Self { + public static func annotated(with aggregate: AssociationAggregate) -> QueryInterfaceRequest { return all().annotated(with: aggregate) } /// TODO - public static func having(_ aggregate: AssociationAggregate) -> QueryInterfaceRequest where A.OriginRowDecoder == Self { + public static func having(_ aggregate: AssociationAggregate) -> QueryInterfaceRequest { return all().having(aggregate) } }