Skip to content

Commit 1221aee

Browse files
committed
tweak
1 parent c46d7bf commit 1221aee

File tree

7 files changed

+43
-37
lines changed

7 files changed

+43
-37
lines changed

docs/tutorial.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,10 @@ db.run(query) ==> Seq(
848848
```
849849
850850
You can also perform aggregates as part of your window function by using
851-
the `.mapAggregate` function; this provides a `SelectProxy[Q]` rather than
851+
the `.mapAggregate` function; this provides a `Aggregatable.Proxy[Q]` rather than
852852
a `Q`, letting you perform aggregates like `.sumBy` that you can then use
853853
as window functions via `.over`. You can reference normal columns by referencing
854-
the `.expr` member on each `SelectProxy`.
854+
the `.expr` member on each `Aggregatable.Proxy`.
855855
```scala
856856
val query = City.select
857857
.mapAggregate((c, cs) =>

scalasql/core/src/Aggregatable.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@ package scalasql.core
22

33
/**
44
* Something that supports aggregate operations. Most commonly a [[Select]], but
5-
* also could be a [[SelectProxy]]
5+
* also could be a [[Aggregatable.Proxy]]
66
*/
77
trait Aggregatable[Q] extends WithSqlExpr[Q] {
88
def aggregateExpr[V: TypeMapper](f: Q => Context => SqlStr)(
99
implicit qr: Queryable.Row[Expr[V], V]
1010
): Expr[V]
1111
}
12+
13+
object Aggregatable {
14+
15+
/**
16+
* A reference that aggregations for usage within [[Select.aggregate]], to allow
17+
* the caller to perform multiple aggregations within a single query.
18+
*/
19+
class Proxy[Q](val expr: Q) extends Aggregatable[Q] {
20+
def aggregateExpr[V: TypeMapper](f: Q => Context => SqlStr)(
21+
implicit qr: Queryable.Row[Expr[V], V]
22+
): Expr[V] = { Expr[V] { implicit c => f(expr)(c) } }
23+
}
24+
25+
}

scalasql/query/src/Select.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,15 @@ trait Select[Q, R]
101101
/**
102102
* Performs one or more aggregates in a single [[Select]]
103103
*/
104-
def aggregate[E, V](f: SelectProxy[Q] => E)(implicit qr: Queryable.Row[E, V]): Aggregate[E, V]
104+
def aggregate[E, V](f: Aggregatable.Proxy[Q] => E)(
105+
implicit qr: Queryable.Row[E, V]
106+
): Aggregate[E, V]
105107

106108
/**
107-
* Performs a `.map` which additionally provides a [[SelectProxy]] that allows you to perform aggregate
109+
* Performs a `.map` which additionally provides a [[Aggregatable.Proxy]] that allows you to perform aggregate
108110
* functions.
109111
*/
110-
def mapAggregate[Q2, R2](f: (Q, SelectProxy[Q]) => Q2)(
112+
def mapAggregate[Q2, R2](f: (Q, Aggregatable.Proxy[Q]) => Q2)(
111113
implicit qr: Queryable.Row[Q2, R2]
112114
): Select[Q2, R2]
113115

@@ -116,7 +118,7 @@ trait Select[Q, R]
116118
* a function specifying the group-aggregate.
117119
*/
118120
def groupBy[K, V, R2, R3](groupKey: Q => K)(
119-
groupAggregate: SelectProxy[Q] => V
121+
groupAggregate: Aggregatable.Proxy[Q] => V
120122
)(implicit qrk: Queryable.Row[K, R2], qrv: Queryable.Row[V, R3]): Select[(K, V), (R2, R3)]
121123

122124
/**
@@ -310,16 +312,16 @@ object Select {
310312

311313
override def filter(f: Q => Expr[Boolean]): Select[Q, R] = selectToSimpleSelect().filter(f)
312314

313-
override def aggregate[E, V](f: SelectProxy[Q] => E)(
315+
override def aggregate[E, V](f: Aggregatable.Proxy[Q] => E)(
314316
implicit qr: Queryable.Row[E, V]
315317
): Aggregate[E, V] = selectToSimpleSelect().aggregate(f)
316318

317-
override def mapAggregate[Q2, R2](f: (Q, SelectProxy[Q]) => Q2)(
319+
override def mapAggregate[Q2, R2](f: (Q, Aggregatable.Proxy[Q]) => Q2)(
318320
implicit qr: Queryable.Row[Q2, R2]
319321
): Select[Q2, R2] = selectToSimpleSelect().mapAggregate(f)
320322

321323
override def groupBy[K, V, R2, R3](groupKey: Q => K)(
322-
groupAggregate: SelectProxy[Q] => V
324+
groupAggregate: Aggregatable.Proxy[Q] => V
323325
)(implicit qrk: Queryable.Row[K, R2], qrv: Queryable.Row[V, R3]): Select[(K, V), (R2, R3)] =
324326
selectToSimpleSelect().groupBy(groupKey)(groupAggregate)
325327

scalasql/query/src/SelectProxy.scala

Lines changed: 0 additions & 13 deletions
This file was deleted.

scalasql/query/src/SimpleSelect.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package scalasql.query
22

33
import scalasql.core.{
4+
Aggregatable,
45
Context,
56
DialectTypeMappers,
7+
Expr,
8+
ExprsToSql,
69
JoinNullable,
710
LiveExprs,
811
Queryable,
9-
Expr,
10-
ExprsToSql,
1112
SqlStr,
1213
TypeMapper
1314
}
@@ -140,30 +141,32 @@ class SimpleSelect[Q, R](
140141
joinCopy(other, Some(on), "FULL OUTER JOIN")((e, o) => (JoinNullable(e), JoinNullable(o)))
141142
}
142143

143-
def aggregate[E, V](f: SelectProxy[Q] => E)(implicit qr: Queryable.Row[E, V]): Aggregate[E, V] = {
144-
val selectProxyExpr = f(new SelectProxy[Q](expr))
145-
val copied = this.copy(expr = selectProxyExpr)
144+
def aggregate[E, V](
145+
f: Aggregatable.Proxy[Q] => E
146+
)(implicit qr: Queryable.Row[E, V]): Aggregate[E, V] = {
147+
val aggregateProxy = f(new Aggregatable.Proxy[Q](expr))
148+
val copied = this.copy(expr = aggregateProxy)
146149
new Aggregate[E, V](
147150
implicit ctx => copied.renderSql(ctx),
148151
r => Query.construct(copied, r).head,
149-
selectProxyExpr,
152+
aggregateProxy,
150153
qr
151154
)
152155
}
153156

154157
def mapAggregate[Q2, R2](
155-
f: (Q, SelectProxy[Q]) => Q2
158+
f: (Q, Aggregatable.Proxy[Q]) => Q2
156159
)(implicit qr: Queryable.Row[Q2, R2]): Select[Q2, R2] = {
157-
val selectProxyExpr = f(expr, new SelectProxy[Q](expr))
158-
this.copy(expr = selectProxyExpr)
160+
val aggregateProxy = f(expr, new Aggregatable.Proxy[Q](expr))
161+
this.copy(expr = aggregateProxy)
159162
}
160163

161164
def groupBy[K, V, R1, R2](groupKey: Q => K)(
162-
groupAggregate: SelectProxy[Q] => V
165+
groupAggregate: Aggregatable.Proxy[Q] => V
163166
)(implicit qrk: Queryable.Row[K, R1], qrv: Queryable.Row[V, R2]): Select[(K, V), (R1, R2)] = {
164167
val groupKeyValue = groupKey(expr)
165168
val Seq(groupKeyExpr) = qrk.walkExprs(groupKeyValue)
166-
val newExpr = (groupKeyValue, groupAggregate(new SelectProxy[Q](this.expr)))
169+
val newExpr = (groupKeyValue, groupAggregate(new Aggregatable.Proxy[Q](this.expr)))
167170

168171
// Weird hack to store the post-groupby `Select` as part of the `GroupBy`
169172
// object, because `.flatMap` sometimes need us to roll back any subsequent

scalasql/query/src/SqlWindow.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ case class SqlWindow[T](
2020
val partitionBySql = SqlStr.opt(partitionBy0) { p => sql"PARTITION BY $p" }
2121
val sortBySql = CompoundSelect.orderToSqlStr(orderBy, ctx)
2222
val overClause = SqlStr.join(
23-
Seq(partitionBySql, sortBySql).filter(!SqlStr.flatten(_).queryParts.forall(_.isEmpty)),
23+
Seq(partitionBySql, sortBySql).filter(!SqlStr.flatten(_).queryParts.forall(_.length == 0)),
2424
sql" "
2525
)
2626

scalasql/test/src/WorldSqlTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,10 +896,10 @@ object WorldSqlTests extends TestSuite {
896896
test("aggregate") {
897897
// +DOCS
898898
// You can also perform aggregates as part of your window function by using
899-
// the `.mapAggregate` function; this provides a `SelectProxy[Q]` rather than
899+
// the `.mapAggregate` function; this provides a `Aggregatable.Proxy[Q]` rather than
900900
// a `Q`, letting you perform aggregates like `.sumBy` that you can then use
901901
// as window functions via `.over`. You can reference normal columns by referencing
902-
// the `.expr` member on each `SelectProxy`.
902+
// the `.expr` member on each `Aggregatable.Proxy`.
903903
val query = City.select
904904
.mapAggregate((c, cs) =>
905905
(

0 commit comments

Comments
 (0)