Skip to content

Commit 1d9fa00

Browse files
refactor grouping aggregateColumnsN
1 parent 9d9e853 commit 1d9fa00

File tree

6 files changed

+66
-8
lines changed

6 files changed

+66
-8
lines changed

ktorm-core/generate-tuples.gradle

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,30 +253,82 @@ def generateGroupingAggregateColumns(Writer writer, int tupleNumber) {
253253
* Group elements from the source sequence by key and perform the given aggregations for elements in each group,
254254
* then store the results in a new [Map].
255255
*
256-
* See [EntityGrouping.aggregateColumns] for more details.
256+
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
257+
* `select key, aggregation from source group by key`.
257258
*
258259
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
259260
* @return a [Map] associating the key of each group with the results of aggregations of the group elements.
260261
*/
262+
@Deprecated(
263+
message = "This function will be removed in the future. Please use aggregateColumns { .. } instead.",
264+
replaceWith = ReplaceWith("aggregateColumns(aggregationSelector)")
265+
)
261266
public inline fun <E : Any, T : BaseTable<E>, K : Any, $typeParams> EntityGrouping<E, T, K>.aggregateColumns$tupleNumber(
262267
aggregationSelector: (T) -> Tuple$tupleNumber<$columnDeclarings>
263268
): Map<K?, Tuple$tupleNumber<$resultTypes>> {
264-
return aggregateColumns${tupleNumber}To(LinkedHashMap(), aggregationSelector)
269+
return aggregateColumns(aggregationSelector)
265270
}
266271
267272
/**
268273
* Group elements from the source sequence by key and perform the given aggregations for elements in each group,
269274
* then store the results in the [destination] map.
270275
*
271-
* See [EntityGrouping.aggregateColumnsTo] for more details.
276+
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
277+
* `select key, aggregation from source group by key`.
272278
*
273279
* @param destination a [MutableMap] used to store the results.
274280
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
275281
* @return the [destination] map associating the key of each group with the result of aggregations of the group elements.
276282
*/
283+
@Deprecated(
284+
message = "This function will be removed in the future. Please use aggregateColumns(destination) { .. } instead.",
285+
replaceWith = ReplaceWith("aggregateColumns(destination, aggregationSelector)")
286+
)
277287
public inline fun <E : Any, T : BaseTable<E>, K : Any, $typeParams, M> EntityGrouping<E, T, K>.aggregateColumns${tupleNumber}To(
278288
destination: M,
279289
aggregationSelector: (T) -> Tuple$tupleNumber<$columnDeclarings>
290+
): M where M : MutableMap<in K?, in Tuple$tupleNumber<$resultTypes>> {
291+
return aggregateColumnsTo(destination, aggregationSelector)
292+
}
293+
294+
/**
295+
* Group elements from the source sequence by key and perform the given aggregations for elements in each group,
296+
* then store the results in a new [Map].
297+
*
298+
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
299+
* `select key, aggregation from source group by key`.
300+
*
301+
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
302+
* @return a [Map] associating the key of each group with the results of aggregations of the group elements.
303+
* @since 3.1.0
304+
*/
305+
@JvmName("_aggregateColumns$tupleNumber")
306+
@OptIn(ExperimentalTypeInference::class)
307+
@OverloadResolutionByLambdaReturnType
308+
public inline fun <E : Any, T : BaseTable<E>, K : Any, $typeParams> EntityGrouping<E, T, K>.aggregateColumns(
309+
aggregationSelector: (T) -> Tuple$tupleNumber<$columnDeclarings>
310+
): Map<K?, Tuple$tupleNumber<$resultTypes>> {
311+
return aggregateColumnsTo(LinkedHashMap(), aggregationSelector)
312+
}
313+
314+
/**
315+
* Group elements from the source sequence by key and perform the given aggregations for elements in each group,
316+
* then store the results in the [destination] map.
317+
*
318+
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
319+
* `select key, aggregation from source group by key`.
320+
*
321+
* @param destination a [MutableMap] used to store the results.
322+
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
323+
* @return the [destination] map associating the key of each group with the result of aggregations of the group elements.
324+
* @since 3.1.0
325+
*/
326+
@JvmName("_aggregateColumns${tupleNumber}To")
327+
@OptIn(ExperimentalTypeInference::class)
328+
@OverloadResolutionByLambdaReturnType
329+
public inline fun <E : Any, T : BaseTable<E>, K : Any, $typeParams, M> EntityGrouping<E, T, K>.aggregateColumnsTo(
330+
destination: M,
331+
aggregationSelector: (T) -> Tuple$tupleNumber<$columnDeclarings>
280332
): M where M : MutableMap<in K?, in Tuple$tupleNumber<$resultTypes>> {
281333
val keyColumn = keySelector(sequence.sourceTable)
282334
val ($variableNames) = aggregationSelector(sequence.sourceTable)
@@ -288,7 +340,7 @@ def generateGroupingAggregateColumns(Writer writer, int tupleNumber) {
288340
289341
for (row in Query(sequence.database, expr)) {
290342
val key = keyColumn.sqlType.getResult(row, 1)
291-
destination[key] = Tuple$tupleNumber($resultExtractors)
343+
destination[key] = tupleOf($resultExtractors)
292344
}
293345
294346
return destination

ktorm-core/src/main/kotlin/me/liuwj/ktorm/entity/EntityGrouping.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import me.liuwj.ktorm.schema.ColumnDeclaring
2222
import java.util.*
2323
import kotlin.collections.ArrayList
2424
import kotlin.collections.LinkedHashMap
25+
import kotlin.experimental.ExperimentalTypeInference
2526

2627
/**
2728
* Wraps an [EntitySequence] with a [keySelector] function, which can be applied to each record to get its key,
@@ -88,6 +89,8 @@ public class EntityGrouping<E : Any, T : BaseTable<E>, K : Any>(
8889
* @param aggregationSelector a function that accepts the source table and returns the aggregate expression.
8990
* @return a [Map] associating the key of each group with the result of aggregation of the group elements.
9091
*/
92+
@OptIn(ExperimentalTypeInference::class)
93+
@OverloadResolutionByLambdaReturnType
9194
public inline fun <E : Any, T : BaseTable<E>, K, C> EntityGrouping<E, T, K>.aggregateColumns(
9295
aggregationSelector: (T) -> ColumnDeclaring<C>
9396
): Map<K?, C?> where K : Any, C : Any {
@@ -109,6 +112,8 @@ public inline fun <E : Any, T : BaseTable<E>, K, C> EntityGrouping<E, T, K>.aggr
109112
* @param aggregationSelector a function that accepts the source table and returns the aggregate expression.
110113
* @return the [destination] map associating the key of each group with the result of aggregation of the group elements.
111114
*/
115+
@OptIn(ExperimentalTypeInference::class)
116+
@OverloadResolutionByLambdaReturnType
112117
public inline fun <E : Any, T : BaseTable<E>, K, C, M> EntityGrouping<E, T, K>.aggregateColumnsTo(
113118
destination: M,
114119
aggregationSelector: (T) -> ColumnDeclaring<C>

ktorm-core/src/main/kotlin/me/liuwj/ktorm/entity/EntitySequence.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ public fun <E : Any, T : BaseTable<E>> EntitySequence<E, T>.take(n: Int): Entity
652652
* @param aggregationSelector a function that accepts the source table and returns the aggregate expression.
653653
* @return the aggregate result.
654654
*/
655+
@OptIn(ExperimentalTypeInference::class)
656+
@OverloadResolutionByLambdaReturnType
655657
public inline fun <E : Any, T : BaseTable<E>, C : Any> EntitySequence<E, T>.aggregateColumns(
656658
aggregationSelector: (T) -> ColumnDeclaring<C>
657659
): C? {

ktorm-core/src/test/kotlin/me/liuwj/ktorm/dsl/AggregationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class AggregationTest : BaseTest() {
7878
fun testGroupAggregate3() {
7979
database.employees
8080
.groupingBy { it.departmentId }
81-
.aggregateColumns2 { tupleOf(max(it.salary), min(it.salary)) }
81+
.aggregateColumns { tupleOf(max(it.salary), min(it.salary)) }
8282
.forEach { departmentId, (max, min) ->
8383
println("$departmentId:$max:$min")
8484
}

ktorm-core/src/test/kotlin/me/liuwj/ktorm/entity/DataClassTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class DataClassTest : BaseTest() {
167167
fun testGroupingAggregate() {
168168
database.staffs
169169
.groupingBy { it.sectionId }
170-
.aggregateColumns2 { tupleOf(max(it.salary), min(it.salary)) }
170+
.aggregateColumns { tupleOf(max(it.salary), min(it.salary)) }
171171
.forEach { sectionId, (max, min) ->
172172
println("$sectionId:$max:$min")
173173
}

ktorm-global/src/test/kotlin/me/liuwj/ktorm/global/GlobalAggregationTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package me.liuwj.ktorm.global
22

33
import me.liuwj.ktorm.dsl.*
44
import me.liuwj.ktorm.entity.aggregateColumns
5-
import me.liuwj.ktorm.entity.aggregateColumns2
65
import me.liuwj.ktorm.entity.groupingBy
76
import me.liuwj.ktorm.entity.tupleOf
87
import org.junit.Test
@@ -82,7 +81,7 @@ class GlobalAggregationTest : BaseGlobalTest() {
8281
Employees
8382
.asSequence()
8483
.groupingBy { it.departmentId }
85-
.aggregateColumns2 { tupleOf(max(it.salary), min(it.salary)) }
84+
.aggregateColumns { tupleOf(max(it.salary), min(it.salary)) }
8685
.forEach { departmentId, (max, min) ->
8786
println("$departmentId:$max:$min")
8887
}

0 commit comments

Comments
 (0)