AssociationAggregate is now generic on its anchor type only

This commit is contained in:
Gwendal Roué committed 2018-10-07 12:08:25 +02:00
1 parent 39bc140dc8
commit 4f8e97a07c
3 files changed
+63 -71

No files matched your search

@@ -9,20 +9,23 @@
import Foundation
/// TODO
public struct AssociationAggregate<A: Association> {
let association: A
public struct AssociationAggregate<RowDecoder> {
let aggregatedRequest: (QueryInterfaceRequest<RowDecoder>, TableAlias) -> QueryInterfaceRequest<RowDecoder>
let expression: SQLExpression
var alias: String?
init(association: A, expression: SQLExpression) {
self.association = association
init(
expression: SQLExpression,
aggregatedRequest: @escaping (QueryInterfaceRequest<RowDecoder>, TableAlias) -> QueryInterfaceRequest<RowDecoder>)
{
self.aggregatedRequest = aggregatedRequest
self.expression = expression
}
}
extension AssociationAggregate {
/// TODO
public func aliased(_ name: String) -> AssociationAggregate<A> {
public func aliased(_ name: String) -> AssociationAggregate<RowDecoder> {
var aggregate = self
aggregate.alias = name
return aggregate
@@ -30,50 +33,50 @@ extension AssociationAggregate {
}
/// TODO
public prefix func ! <A>(aggregate: AssociationAggregate<A>) -> AssociationAggregate<A> {
public prefix func ! <RowDecoder>(aggregate: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
return AssociationAggregate(
association: aggregate.association,
expression: !aggregate.expression)
expression: !aggregate.expression,
aggregatedRequest: aggregate.aggregatedRequest)
}
/// TODO
public func == <A>(lhs: AssociationAggregate<A>, rhs: SQLExpressible?) -> AssociationAggregate<A> {
public func == <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible?) -> AssociationAggregate<RowDecoder> {
return AssociationAggregate(
association: lhs.association,
expression: lhs.expression == rhs)
expression: lhs.expression == rhs,
aggregatedRequest: lhs.aggregatedRequest)
}
/// TODO
public func == <A>(lhs: SQLExpressible?, rhs: AssociationAggregate<A>) -> AssociationAggregate<A> {
public func == <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: Bool) -> AssociationAggregate<RowDecoder> {
return AssociationAggregate(
association: rhs.association,
expression: lhs == rhs.expression)
expression: lhs.expression == rhs,
aggregatedRequest: lhs.aggregatedRequest)
}
/// TODO
public func == <A>(lhs: AssociationAggregate<A>, rhs: Bool) -> AssociationAggregate<A> {
public func == <RowDecoder>(lhs: SQLExpressible?, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
return AssociationAggregate(
association: lhs.association,
expression: lhs.expression == rhs)
expression: lhs == rhs.expression,
aggregatedRequest: rhs.aggregatedRequest)
}
/// TODO
public func != <A>(lhs: AssociationAggregate<A>, rhs: SQLExpressible?) -> AssociationAggregate<A> {
public func != <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible?) -> AssociationAggregate<RowDecoder> {
return AssociationAggregate(
association: lhs.association,
expression: lhs.expression != rhs)
expression: lhs.expression != rhs,
aggregatedRequest: lhs.aggregatedRequest)
}
/// TODO
public func != <A>(lhs: SQLExpressible?, rhs: AssociationAggregate<A>) -> AssociationAggregate<A> {
public func != <RowDecoder>(lhs: SQLExpressible?, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
return AssociationAggregate(
association: rhs.association,
expression: lhs != rhs.expression)
expression: lhs != rhs.expression,
aggregatedRequest: rhs.aggregatedRequest)
}
/// TODO
public func != <A>(lhs: AssociationAggregate<A>, rhs: Bool) -> AssociationAggregate<A> {
public func != <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: Bool) -> AssociationAggregate<RowDecoder> {
return AssociationAggregate(
association: lhs.association,
expression: lhs.expression != rhs)
expression: lhs.expression != rhs,
aggregatedRequest: lhs.aggregatedRequest)
}
@@ -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<Origin> {
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<HasManyAssociation> {
public func count() -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
return AssociationAggregate(
association: self,
expression: SQLExpressionCountDistinct(Column.rowID))
.aliased(name)
public func count(aliased name: String) -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
return AssociationAggregate(
association: self,
expression: SQLExpressionIsEmpty(SQLExpressionCountDistinct(Column.rowID)))
public func isEmpty() -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
public func average(_ column: Column) -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
return AssociationAggregate(
association: self,
expression: SQLExpressionFunction(.avg, arguments: value))
.aliased(name)
public func average(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
public func max(_ column: Column) -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
return AssociationAggregate(
association: self,
expression: SQLExpressionFunction(.max, arguments: value))
.aliased(name)
public func max(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
public func min(_ column: Column) -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
return AssociationAggregate(
association: self,
expression: SQLExpressionFunction(.min, arguments: value))
.aliased(name)
public func min(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
public func sum(_ column: Column) -> AssociationAggregate<Origin> {
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<HasManyAssociation> {
return AssociationAggregate(
association: self,
expression: SQLExpressionFunction(.sum, arguments: value))
.aliased(name)
public func sum(_ value: SQLSpecificExpressible, aliased name: String) -> AssociationAggregate<Origin> {
return aggregate(SQLExpressionFunction(.sum, arguments: value)).aliased(name)
}
}
@@ -43,7 +43,7 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
// MARK: - Association Aggregates
/// TODO
public func annotated<A: Association>(with aggregate: AssociationAggregate<A>) -> QueryInterfaceRequest<RowDecoder> where A.OriginRowDecoder == RowDecoder {
public func annotated(with aggregate: AssociationAggregate<RowDecoder>) -> QueryInterfaceRequest<RowDecoder> {
// 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<A: Association>(_ aggregate: AssociationAggregate<A>) -> QueryInterfaceRequest<T> where A.OriginRowDecoder == RowDecoder {
public func having(_ aggregate: AssociationAggregate<RowDecoder>) -> QueryInterfaceRequest<RowDecoder> {
// 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<A: Association>(with aggregate: AssociationAggregate<A>) -> QueryInterfaceRequest<Self> where A.OriginRowDecoder == Self {
public static func annotated(with aggregate: AssociationAggregate<Self>) -> QueryInterfaceRequest<Self> {
return all().annotated(with: aggregate)
}
/// TODO
public static func having<A: Association>(_ aggregate: AssociationAggregate<A>) -> QueryInterfaceRequest<Self> where A.OriginRowDecoder == Self {
public static func having(_ aggregate: AssociationAggregate<Self>) -> QueryInterfaceRequest<Self> {
return all().having(aggregate)
}
}