mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Tests for association aggregate operators
This commit is contained in:
1 parent
5f92209c59
commit
8ffeb28b02
4 files changed
+663
-234
No files matched your search
@@ -10,16 +10,12 @@ import Foundation
|
||||
|
||||
/// TODO
|
||||
public struct AssociationAggregate<RowDecoder> {
|
||||
let aggregatedRequest: (QueryInterfaceRequest<RowDecoder>, TableAlias) -> QueryInterfaceRequest<RowDecoder>
|
||||
let expression: SQLExpression
|
||||
// TODO: find a name
|
||||
let run: (QueryInterfaceRequest<RowDecoder>) -> (request: QueryInterfaceRequest<RowDecoder>, expression: SQLExpression)
|
||||
var alias: String?
|
||||
|
||||
init(
|
||||
expression: SQLExpression,
|
||||
aggregatedRequest: @escaping (QueryInterfaceRequest<RowDecoder>, TableAlias) -> QueryInterfaceRequest<RowDecoder>)
|
||||
{
|
||||
self.aggregatedRequest = aggregatedRequest
|
||||
self.expression = expression
|
||||
init(run: @escaping (QueryInterfaceRequest<RowDecoder>) -> (request: QueryInterfaceRequest<RowDecoder>, expression: SQLExpression)) {
|
||||
self.run = run
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,371 +32,405 @@ extension AssociationAggregate {
|
||||
|
||||
/// TODO
|
||||
public prefix func ! <RowDecoder>(aggregate: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: !aggregate.expression,
|
||||
aggregatedRequest: aggregate.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = aggregate.run(request)
|
||||
return (request: request, expression: !expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func && <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression && rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression && rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func && <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression && rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression && rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func && <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs && rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs && expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func || <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression || rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression || rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func || <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression || rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression || rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func || <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs || rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs || expression)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Egality and Identity Operators (=, <>, IS, IS NOT)
|
||||
|
||||
/// TODO
|
||||
public func == <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression == rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression == rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func == <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible?) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression == rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
public func == <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression == rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func == <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs == expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func == <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: Bool) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression == rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func == <RowDecoder>(lhs: SQLExpressible?, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs == rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression == rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func == <RowDecoder>(lhs: Bool, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs == rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs == expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func != <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression != rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression != rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func != <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible?) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression != rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
public func != <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression != rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func != <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs != expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func != <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: Bool) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression != rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func != <RowDecoder>(lhs: SQLExpressible?, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs != rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression != rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func != <RowDecoder>(lhs: Bool, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs != rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs != expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func === <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression === rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression === rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func === <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible?) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression === rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
public func === <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression === rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func === <RowDecoder>(lhs: SQLExpressible?, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs === rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
public func === <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs === expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func !== <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression !== rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression !== rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func !== <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible?) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression !== rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
public func !== <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression !== rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func !== <RowDecoder>(lhs: SQLExpressible?, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs !== rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
public func !== <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs !== expression)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Comparison Operators (<, >, <=, >=)
|
||||
|
||||
/// TODO
|
||||
public func < <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression < rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func < <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression < rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func < <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs < rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func <= <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression <= rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression <= rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func <= <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression <= rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression <= rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func <= <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs <= rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs <= expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func < <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression < rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func < <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression < rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func < <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs < expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func > <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression > rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression > rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func > <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression > rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression > rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func > <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs > rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs > expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func >= <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression >= rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression >= rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func >= <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression >= rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression >= rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func >= <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs >= rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs >= expression)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Arithmetic Operators (+, -, *, /)
|
||||
|
||||
/// TODO
|
||||
public prefix func - <RowDecoder>(aggregate: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: -aggregate.expression,
|
||||
aggregatedRequest: aggregate.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = aggregate.run(request)
|
||||
return (request: request, expression:-expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func + <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression + rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression + rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func + <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression + rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression + rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func + <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs + rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs + expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func - <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression - rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression - rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func - <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression - rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression - rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func - <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs - rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs - expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func * <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression * rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression * rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func * <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression * rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression * rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func * <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs * rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs * expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func / <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression / rhs.expression,
|
||||
aggregatedRequest: { request, tableAlias in
|
||||
rhs.aggregatedRequest(lhs.aggregatedRequest(request, tableAlias), tableAlias)
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let (lRequest, lExpression) = lhs.run(request)
|
||||
let (request, rExpression) = rhs.run(lRequest)
|
||||
return (request: request, expression: lExpression / rExpression)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func / <RowDecoder>(lhs: AssociationAggregate<RowDecoder>, rhs: SQLExpressible) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs.expression / rhs,
|
||||
aggregatedRequest: lhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = lhs.run(request)
|
||||
return (request: request, expression: expression / rhs)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func / <RowDecoder>(lhs: SQLExpressible, rhs: AssociationAggregate<RowDecoder>) -> AssociationAggregate<RowDecoder> {
|
||||
return AssociationAggregate(
|
||||
expression: lhs / rhs.expression,
|
||||
aggregatedRequest: rhs.aggregatedRequest)
|
||||
return AssociationAggregate { request in
|
||||
let (request, expression) = rhs.run(request)
|
||||
return (request: request, expression: lhs / expression)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,13 +95,14 @@ extension HasManyAssociation: TableRequest 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()
|
||||
})
|
||||
return AssociationAggregate { request in
|
||||
let tableAlias = TableAlias()
|
||||
let request = request
|
||||
.joining(optional: self.aliased(tableAlias))
|
||||
.groupByPrimaryKey()
|
||||
let expression = tableAlias[expression]
|
||||
return (request: request, expression: expression)
|
||||
}
|
||||
}
|
||||
|
||||
/// The number of associated records.
|
||||
|
||||
@@ -44,27 +44,18 @@ extension QueryInterfaceRequest where RowDecoder: TableRecord {
|
||||
|
||||
/// TODO
|
||||
public func annotated(with aggregate: AssociationAggregate<RowDecoder>) -> QueryInterfaceRequest<RowDecoder> {
|
||||
// SELECT author.*, COUNT(book.rowID) AS bookCount
|
||||
// LEFT JOIN book ON ...
|
||||
// GROUP BY author.id
|
||||
let tableAlias = TableAlias()
|
||||
let selectable: SQLSelectable
|
||||
let (request, expression) = aggregate.run(self)
|
||||
if let alias = aggregate.alias {
|
||||
selectable = tableAlias[aggregate.expression].aliased(alias)
|
||||
return request.appendingSelection([expression.aliased(alias)])
|
||||
} else {
|
||||
selectable = tableAlias[aggregate.expression]
|
||||
return request.appendingSelection([expression])
|
||||
}
|
||||
return aggregate.aggregatedRequest(appendingSelection([selectable]), tableAlias)
|
||||
}
|
||||
|
||||
/// TODO
|
||||
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 aggregate.aggregatedRequest(having(tableAlias[aggregate.expression]), tableAlias)
|
||||
let (request, expression) = aggregate.run(self)
|
||||
return request.having(expression)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user