Skip to content

Commit

Permalink
update readme for tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed Sep 13, 2020
1 parent 8c3d135 commit d752bd1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,12 @@ The `mapColumns` function is used to obtain the results of a column:
val names = database.employees.mapColumns { it.name }
```

Additionally, if we want to select two or more columns, we can change to `mapColumns2` or `mapColumns3`, then we need to wrap our selected columns by `Pair` or `Triple` in the closure, and the function’s return type becomes `List<Pair<C1?, C2?>>` or `List<Triple<C1?, C2?, C3?>>`.
Additionally, if we want to select two or more columns, we just need to wrap our selected columns by `tupleOf` in the closure, and the function’s return type becomes `List<TupleN<C1?, C2?, .. Cn?>>`.

```kotlin
database.employees
.filter { it.departmentId eq 1 }
.mapColumns2 { Pair(it.id, it.name) }
.mapColumns { tupleOf(it.id, it.name) }
.forEach { (id, name) ->
println("$id:$name")
}
Expand Down Expand Up @@ -436,12 +436,12 @@ val max = database.employees
.aggregateColumns { max(it.salary) }
```

Also, if we want to aggregate two or more columns, we can change to `aggregateColumns2` or `aggregateColumns3`, then we need to wrap our aggregate expressions by `Pair` or `Triple` in the closure, and the function’s return type becomes `Pair<C1?, C2?>` or `Triple<C1?, C2?, C3?>`. The example below obtains the average and the range of salaries in department 1:
Also, if we want to aggregate two or more columns, we just need to wrap our aggregate expressions by `tupleOf` in the closure, and the function’s return type becomes `TupleN<C1?, C2?, .. Cn?>`. The example below obtains the average and the range of salaries in department 1:

```kotlin
val (avg, diff) = database.employees
.filter { it.departmentId eq 1 }
.aggregateColumns2 { Pair(avg(it.salary), max(it.salary) - min(it.salary)) }
.aggregateColumns { tupleOf(avg(it.salary), max(it.salary) - min(it.salary)) }
```

Generated SQL:
Expand Down
2 changes: 1 addition & 1 deletion README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ val max = database.employees
.aggregateColumns { max(it.salary) }
```

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

```kotlin
val (avg, diff) = database.employees
Expand Down
8 changes: 4 additions & 4 deletions README_jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,12 @@ val employees = database.employees.toCollection(ArrayList())
val names = database.employees.mapColumns { it.name }
```

さらに、2つ以上のカラムを選択したい場合は`mapColumns2``mapColumns3`に変更し、選択したカラムを `Pair` あるいは `Triple` でクロージャでラップする必要があります。この関数の戻り値の型は `List<Pair<C1?, C2?>>``List<Triple<C1?, C2?, C3?>>`になります:
さらに、2つ以上の列を選択する場合は、選択した列をクロージャの `tupleOf` でラップするだけでよく、関数の戻り値の型は `List<TupleN<C1?, C2?, .. Cn?>>` になります:

```kotlin
database.employees
.filter { it.departmentId eq 1 }
.mapColumns2 { Pair(it.id, it.name) }
.mapColumns { tupleOf(it.id, it.name) }
.forEach { (id, name) ->
println("$id:$name")
}
Expand Down Expand Up @@ -436,12 +436,12 @@ val max = database.employees
.aggregateColumns { max(it.salary) }
```

また、2つ以上の列を集約したい場合は `aggregateColumns2``aggregateColumns3` に変更し、クロージャ内で `Pair``Triple` で集約式をラップする必要があり、関数の戻り値の型は `Pair<C1?, C2?>``Triple<C1?, C2?, C3?>` となります。以下の例では、第1部門の給与の平均と範囲を求めています。:
また、2つ以上の列を集計する場合は、クロージャー内の `tupleOf` で集計式をラップするだけでよく、関数の戻り値の型は `TupleN<C1?, C2?, .. Cn?>` になります。 以下の例では、部門1の給与の平均と範囲を取得しています。

```kotlin
val (avg, diff) = database.employees
.filter { it.departmentId eq 1 }
.aggregateColumns2 { Pair(avg(it.salary), max(it.salary) - min(it.salary)) }
.aggregateColumns { tupleOf(avg(it.salary), max(it.salary) - min(it.salary)) }
```

生成される SQL文:
Expand Down

0 comments on commit d752bd1

Please sign in to comment.