Skip to content

Commit 8c3d135

Browse files
update docs for tuples
1 parent 7f94896 commit 8c3d135

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

README_cn.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,12 @@ val employees = database.employees.toCollection(ArrayList())
401401
val names = database.employees.mapColumns { it.name }
402402
```
403403

404-
除此之外,还有 `mapColumns2``mapColumns3` 等更多函数,它们用来同时获取多个列的结果,这时我们需要在闭包中使用 `Pair``Triple` 包装我们的这些字段,函数的返回值也相应变成了 `List<Pair<C1?, C2?>>``List<Triple<C1?, C2?, C3?>>`
404+
除此之外,`mapColumns` 还可以同时获取多个列的结果,这时我们只需要在闭包中使用 `tupleOf` 包装我们的这些字段,函数的返回值也相应变成了 `List<TupleN<C1?, C2?, .. Cn?>>`
405405

406406
```kotlin
407407
database.employees
408408
.filter { it.departmentId eq 1 }
409-
.mapColumns2 { Pair(it.id, it.name) }
409+
.mapColumns { tupleOf(it.id, it.name) }
410410
.forEach { (id, name) ->
411411
println("$id:$name")
412412
}
@@ -438,12 +438,12 @@ val max = database.employees
438438
.aggregateColumns { max(it.salary) }
439439
```
440440

441-
如果你希望同时获取多个聚合结果,可以改用 `aggregateColumns2``aggregateColumns3` 函数,这时我们需要在闭包中使用 `Pair``Triple` 包装我们的这些聚合表达式,函数的返回值也相应变成了 `Pair<C1?, C2?>``Triple<C1?, C2?, C3?>`。下面的例子获取部门 1 中工资的平均值和极差:
441+
如果你希望同时获取多个聚合结果,只需要在闭包中使用 `tupleOf` 包装我们的这些聚合表达式即可,此时函数的返回值就相应变成了 `Pair<C1?, C2?, .. Cn?>`。下面的例子获取部门 1 中工资的平均值和极差:
442442

443443
```kotlin
444444
val (avg, diff) = database.employees
445445
.filter { it.departmentId eq 1 }
446-
.aggregateColumns2 { Pair(avg(it.salary), max(it.salary) - min(it.salary)) }
446+
.aggregateColumns { tupleOf(avg(it.salary), max(it.salary) - min(it.salary)) }
447447
```
448448

449449
生成 SQL:

ktorm-core/generate-tuples.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ def generateMapColumns(Writer writer, int tupleNumber) {
129129
* element. Besides, the function’s return type is a tuple of `ColumnDeclaring<C>`s, and we should return some
130130
* columns or expressions to customize the `select` clause of the generated SQL.
131131
*
132+
* Ktorm supports selecting two or more columns, we just need to wrap our selected columns by [tupleOf]
133+
* in the closure, then the function’s return type becomes `List<TupleN<C1?, C2?, .. Cn?>>`.
134+
*
132135
* The operation is terminal.
133136
*
134137
* @param isDistinct specify if the query is distinct, the generated SQL becomes `select distinct` if it's set to true.
@@ -155,6 +158,9 @@ def generateMapColumns(Writer writer, int tupleNumber) {
155158
* element. Besides, the function’s return type is a tuple of `ColumnDeclaring<C>`s, and we should return some
156159
* columns or expressions to customize the `select` clause of the generated SQL.
157160
*
161+
* Ktorm supports selecting two or more columns, we just need to wrap our selected columns by [tupleOf]
162+
* in the closure, then the function’s return type becomes `List<TupleN<C1?, C2?, .. Cn?>>`.
163+
*
158164
* The operation is terminal.
159165
*
160166
* @param destination a [MutableCollection] used to store the results.
@@ -214,6 +220,9 @@ def generateAggregateColumns(Writer writer, int tupleNumber) {
214220
* Perform a tuple of aggregations given by [aggregationSelector] for all elements in the sequence,
215221
* and return the aggregate results.
216222
*
223+
* Ktorm supports aggregating two or more columns, we just need to wrap our aggregate expressions by
224+
* [tupleOf] in the closure, then the function’s return type becomes `TupleN<C1?, C2?, .. Cn?>`.
225+
*
217226
* The operation is terminal.
218227
*
219228
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
@@ -302,6 +311,9 @@ def generateGroupingAggregateColumns(Writer writer, int tupleNumber) {
302311
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
303312
* `select key, aggregation from source group by key`.
304313
*
314+
* Ktorm supports aggregating two or more columns, we just need to wrap our aggregate expressions by [tupleOf]
315+
* in the closure, then the function’s return type becomes `Map<K?, TupleN<C1?, C2?, .. Cn?>>`.
316+
*
305317
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
306318
* @return a [Map] associating the key of each group with the results of aggregations of the group elements.
307319
* @since 3.1.0
@@ -322,6 +334,9 @@ def generateGroupingAggregateColumns(Writer writer, int tupleNumber) {
322334
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
323335
* `select key, aggregation from source group by key`.
324336
*
337+
* Ktorm supports aggregating two or more columns, we just need to wrap our aggregate expressions by [tupleOf]
338+
* in the closure, then the function’s return type becomes `Map<K?, TupleN<C1?, C2?, .. Cn?>>`.
339+
*
325340
* @param destination a [MutableMap] used to store the results.
326341
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
327342
* @return the [destination] map associating the key of each group with the result of aggregations of the group elements.

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ public class EntityGrouping<E : Any, T : BaseTable<E>, K : Any>(
8282
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
8383
* `select key, aggregation from source group by key`.
8484
*
85-
* Ktorm also supports aggregating two or more columns, we can change to [EntityGrouping.aggregateColumns2] or
86-
* [EntityGrouping.aggregateColumns3], then we need to wrap our aggregate expressions by [Pair] or [Triple] in
87-
* the closure, and the function’s return type becomes `Map<K?, Pair<C1?, C2?>>` or `Map<K?, Triple<C1?, C2?, C3?>>`.
85+
* Ktorm also supports aggregating two or more columns, we just need to wrap our aggregate expressions by [tupleOf]
86+
* in the closure, then the function’s return type becomes `Map<K?, TupleN<C1?, C2?, .. Cn?>>`.
8887
*
8988
* @param aggregationSelector a function that accepts the source table and returns the aggregate expression.
9089
* @return a [Map] associating the key of each group with the result of aggregation of the group elements.
@@ -104,9 +103,8 @@ public inline fun <E : Any, T : BaseTable<E>, K, C> EntityGrouping<E, T, K>.aggr
104103
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
105104
* `select key, aggregation from source group by key`.
106105
*
107-
* Ktorm also supports aggregating two or more columns, we can change to [EntityGrouping.aggregateColumns2To] or
108-
* [EntityGrouping.aggregateColumns3To], then we need to wrap our aggregate expressions by [Pair] or [Triple] in
109-
* the closure, and the function’s return type becomes `Map<K?, Pair<C1?, C2?>>` or `Map<K?, Triple<C1?, C2?, C3?>>`.
106+
* Ktorm also supports aggregating two or more columns, we just need to wrap our aggregate expressions by [tupleOf]
107+
* in the closure, then the function’s return type becomes `Map<K?, TupleN<C1?, C2?, .. Cn?>>`.
110108
*
111109
* @param destination a [MutableMap] used to store the results.
112110
* @param aggregationSelector a function that accepts the source table and returns the aggregate expression.

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,8 @@ public inline fun <E : Any, R, C : MutableCollection<in R>> EntitySequence<E, *>
497497
* element. Besides, the function’s return type is `ColumnDeclaring<C>`, and we should return a column or expression
498498
* to customize the `select` clause of the generated SQL.
499499
*
500-
* Ktorm also supports selecting two or more columns, we can change to [mapColumns2] or [mapColumns3], then we need
501-
* to wrap our selected columns by [Pair] or [Triple] in the closure, and the function’s return type becomes
502-
* `List<Pair<C1?, C2?>>` or `List<Triple<C1?, C2?, C3?>>`.
500+
* Ktorm also supports selecting two or more columns, we just need to wrap our selected columns by [tupleOf]
501+
* in the closure, then the function’s return type becomes `List<TupleN<C1?, C2?, .. Cn?>>`.
503502
*
504503
* The operation is terminal.
505504
*
@@ -525,9 +524,8 @@ public inline fun <E : Any, T : BaseTable<E>, C : Any> EntitySequence<E, T>.mapC
525524
* element. Besides, the function’s return type is `ColumnDeclaring<C>`, and we should return a column or expression
526525
* to customize the `select` clause of the generated SQL.
527526
*
528-
* Ktorm also supports selecting two or more columns, we can change to [mapColumns2To] or [mapColumns3To], then we need
529-
* to wrap our selected columns by [Pair] or [Triple] in the closure, and the function’s return type becomes
530-
* `List<Pair<C1?, C2?>>` or `List<Triple<C1?, C2?, C3?>>`.
527+
* Ktorm also supports selecting two or more columns, we just need to wrap our selected columns by [tupleOf]
528+
* in the closure, then the function’s return type becomes `List<TupleN<C1?, C2?, .. Cn?>>`.
531529
*
532530
* The operation is terminal.
533531
*
@@ -671,9 +669,8 @@ public fun <E : Any, T : BaseTable<E>> EntitySequence<E, T>.take(n: Int): Entity
671669
* Perform an aggregation given by [aggregationSelector] for all elements in the sequence,
672670
* and return the aggregate result.
673671
*
674-
* Ktorm also supports aggregating two or more columns, we can change to [EntitySequence.aggregateColumns2] or
675-
* [EntitySequence.aggregateColumns3], then we need to wrap our aggregate expressions by [Pair] or [Triple] in
676-
* the closure, and the function’s return type becomes `Pair<C1?, C2?>` or `Triple<C1?, C2?, C3?>`.
672+
* Ktorm also supports aggregating two or more columns, we just need to wrap our aggregate expressions by
673+
* [tupleOf] in the closure, then the function’s return type becomes `TupleN<C1?, C2?, .. Cn?>`.
677674
*
678675
* The operation is terminal.
679676
*

0 commit comments

Comments
 (0)