Support for the TOTAL SQL aggregate function

This commit is contained in:
Gwendal Roué committed 2021-12-12 14:46:43 +01:00
1 parent 8ba89a398a
commit ae609cec3b
7 files changed
+149 -2

No files matched your search

@@ -227,6 +227,9 @@ extension AssociationToMany {
/// let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
///
/// let teams: [Team] = try Team.having(Team.players.sum(Column("score")) > 100).fetchAll(db)
///
/// This aggregate invokes the `SUM` SQL function. See also `total(_:)`, and
/// <https://www.sqlite.org/lang_aggfunc.html#sumunc>.
@available(*, deprecated, message: "Did you mean sum(Column(...))? If not, prefer sum(value.databaseValue) instead.") // swiftlint:disable:this line_length
public func sum(_ expression: SQLExpressible) -> AssociationAggregate<OriginRowDecoder> {
let aggregate = makeAggregate(.aggregate("SUM", [expression.sqlExpression]))
@@ -255,6 +258,9 @@ extension AssociationToMany {
/// let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
///
/// let teams: [Team] = try Team.having(Team.players.sum(Column("score")) > 100).fetchAll(db)
///
/// This aggregate invokes the `SUM` SQL function. See also `total(_:)`, and
/// <https://www.sqlite.org/lang_aggfunc.html#sumunc>.
public func sum(_ expression: SQLSpecificExpressible) -> AssociationAggregate<OriginRowDecoder> {
let aggregate = makeAggregate(.aggregate("SUM", [expression.sqlExpression]))
if let column = expression as? ColumnExpression {
@@ -264,6 +270,38 @@ extension AssociationToMany {
return aggregate
}
}
/// Creates an aggregate which evaluate to the sum of the given expression
/// in associated records.
///
/// When the summed expression is a column, the aggregate has a default
/// name which is "[key][Column]Sum", where key is the key of the
/// association. For example:
///
/// For example:
///
/// struct TeamInfo: FetchableRecord, Decodable {
/// var team: Team
/// var playerScoreSum: Double
/// }
/// let request = Team.annotated(with: Team.players.total(Column("score")))
/// let infos: [TeamInfo] = try TeamInfo.fetchAll(db, request)
///
/// let teams: [Team] = try Team.having(Team.players.total(Column("score")) > 100).fetchAll(db)
///
/// This aggregate invokes the `TOTAL` SQL function. See also `sum(_:)`, and
/// <https://www.sqlite.org/lang_aggfunc.html#sumunc>.
public func total(_ expression: SQLSpecificExpressible) -> AssociationAggregate<OriginRowDecoder> {
let aggregate = makeAggregate(.aggregate("TOTAL", [expression.sqlExpression]))
if let column = expression as? ColumnExpression {
let name = key.singularizedName
// Yes we use the `Sum` suffix instead of `Total`. Both `total(_:)`
// and `sum(_:)` compute sums.
return aggregate.forKey("\(name)\(column.name.uppercasingFirstCharacter)Sum")
} else {
return aggregate
}
}
}
/// An AssociationAggregate is able to compute aggregated values from a
@@ -101,6 +101,10 @@ public func min(_ value: SQLSpecificExpressible) -> SQLExpression {
/// Returns an expression that evaluates the `SUM` SQL function.
///
/// See <https://www.sqlite.org/lang_aggfunc.html#sumunc>.
///
/// See also `total(_:)`.
///
/// // SUM(amount)
/// sum(Column("amount"))
public func sum(_ value: SQLSpecificExpressible) -> SQLExpression {
@@ -108,6 +112,21 @@ public func sum(_ value: SQLSpecificExpressible) -> SQLExpression {
}
// MARK: - TOTAL(...)
/// Returns an expression that evaluates the `TOTAL` SQL function.
///
/// See <https://www.sqlite.org/lang_aggfunc.html#sumunc>.
///
/// See also `sum(_:)`.
///
/// // TOTAL(amount)
/// total(Column("amount"))
public func total(_ value: SQLSpecificExpressible) -> SQLExpression {
.aggregate("TOTAL", [value.sqlExpression])
}
// MARK: - String functions
/// :nodoc: