-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathktorm.tuples-codegen.gradle.kts
358 lines (304 loc) · 15.7 KB
/
ktorm.tuples-codegen.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
plugins {
id("kotlin")
}
val generatedSourceDir = "${project.layout.buildDirectory.asFile.get()}/generated/source/main/kotlin"
val maxTupleNumber = 9
fun generateTuple(writer: java.io.Writer, tupleNumber: Int) {
val typeParams = (1..tupleNumber).joinToString(separator = ", ") { "out E$it" }
val propertyDefinitions = (1..tupleNumber).joinToString(separator = ",\n ") { "val element$it: E$it" }
val toStringTemplate = (1..tupleNumber).joinToString(separator = ", ") { "\$element$it" }
writer.write("""
/**
* Represents a tuple of $tupleNumber values.
*
* There is no meaning attached to values in this class, it can be used for any purpose.
* Two tuples are equal if all the components are equal.
*/
public data class Tuple$tupleNumber<$typeParams>(
$propertyDefinitions
) : Serializable {
override fun toString(): String {
return "($toStringTemplate)"
}
private companion object {
private const val serialVersionUID = 1L
}
}
""".trimIndent())
}
fun generateTupleOf(writer: java.io.Writer, tupleNumber: Int) {
val typeParams = (1..tupleNumber).joinToString(separator = ", ") { "E$it" }
val params = (1..tupleNumber).joinToString(separator = ",\n ") { "element$it: E$it" }
val elements = (1..tupleNumber).joinToString(separator = ", ") { "element$it" }
writer.write("""
/**
* Create a tuple of $tupleNumber values.
*
* @since 2.7
*/
public fun <$typeParams> tupleOf(
$params
): Tuple$tupleNumber<$typeParams> {
return Tuple$tupleNumber($elements)
}
""".trimIndent())
}
fun generateToList(writer: java.io.Writer, tupleNumber: Int) {
val typeParams = (1..tupleNumber).joinToString(separator = ", ") { "E" }
val elements = (1..tupleNumber).joinToString(separator = ", ") { "element$it" }
writer.write("""
/**
* Convert this tuple into a list.
*
* @since 2.7
*/
public fun <E> Tuple$tupleNumber<$typeParams>.toList(): List<E> {
return listOf($elements)
}
""".trimIndent())
}
fun generateMapColumns(writer: java.io.Writer, tupleNumber: Int) {
val typeParams = (1..tupleNumber).joinToString(separator = ", ") { "C$it : Any" }
val columnDeclarings = (1..tupleNumber).joinToString(separator = ", ") { "ColumnDeclaring<C$it>" }
val resultTypes = (1..tupleNumber).joinToString(separator = ", ") { "C$it?" }
val variableNames = (1..tupleNumber).joinToString(separator = ", ") { "c$it" }
val resultExtractors = (1..tupleNumber).joinToString(separator = ", ") { "c${it}.sqlType.getResult(row, $it)" }
writer.write("""
/**
* Customize the selected columns of the internal query by the given [columnSelector] function, and return a [List]
* containing the query results.
*
* This function is similar to [EntitySequence.map], but the [columnSelector] closure accepts the current table
* object [T] as the parameter, so what we get in the closure by `it` is the table object instead of an entity
* element. Besides, the closure’s return type is a tuple of `ColumnDeclaring<C>`s, and we should return some
* columns or expressions to customize the `select` clause of the generated SQL.
*
* Ktorm supports selecting two or more columns, we just need to wrap our selected columns by [tupleOf]
* in the closure, then the function’s return type becomes `List<TupleN<C1?, C2?, .. Cn?>>`.
*
* The operation is terminal.
*
* @param isDistinct specify if the query is distinct, the generated SQL becomes `select distinct` if it's set to true.
* @param columnSelector a function in which we should return a tuple of columns or expressions to be selected.
* @return a list of the query results.
* @since 3.1.0
*/
@JvmName("_mapColumns$tupleNumber")
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E : Any, T : BaseTable<E>, $typeParams> EntitySequence<E, T>.mapColumns(
isDistinct: Boolean = false,
columnSelector: (T) -> Tuple$tupleNumber<$columnDeclarings>
): List<Tuple$tupleNumber<$resultTypes>> {
return mapColumnsTo(ArrayList(), isDistinct, columnSelector)
}
/**
* Customize the selected columns of the internal query by the given [columnSelector] function, and append the query
* results to the given [destination].
*
* This function is similar to [EntitySequence.mapTo], but the [columnSelector] closure accepts the current table
* object [T] as the parameter, so what we get in the closure by `it` is the table object instead of an entity
* element. Besides, the closure’s return type is a tuple of `ColumnDeclaring<C>`s, and we should return some
* columns or expressions to customize the `select` clause of the generated SQL.
*
* Ktorm supports selecting two or more columns, we just need to wrap our selected columns by [tupleOf]
* in the closure, then the function’s return type becomes `List<TupleN<C1?, C2?, .. Cn?>>`.
*
* The operation is terminal.
*
* @param destination a [MutableCollection] used to store the results.
* @param isDistinct specify if the query is distinct, the generated SQL becomes `select distinct` if it's set to true.
* @param columnSelector a function in which we should return a tuple of columns or expressions to be selected.
* @return the [destination] collection of the query results.
* @since 3.1.0
*/
@JvmName("_mapColumns${tupleNumber}To")
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E : Any, T : BaseTable<E>, $typeParams, R> EntitySequence<E, T>.mapColumnsTo(
destination: R,
isDistinct: Boolean = false,
columnSelector: (T) -> Tuple$tupleNumber<$columnDeclarings>
): R where R : MutableCollection<in Tuple$tupleNumber<$resultTypes>> {
val ($variableNames) = columnSelector(sourceTable)
val expr = expression.copy(
columns = listOf($variableNames).map { it.aliased(null) },
isDistinct = isDistinct
)
return Query(database, expr).mapTo(destination) { row -> tupleOf($resultExtractors) }
}
""".trimIndent())
}
fun generateAggregateColumns(writer: java.io.Writer, tupleNumber: Int) {
val typeParams = (1..tupleNumber).joinToString(separator = ", ") { "C$it : Any" }
val columnDeclarings = (1..tupleNumber).joinToString(separator = ", ") { "ColumnDeclaring<C$it>" }
val resultTypes = (1..tupleNumber).joinToString(separator = ", ") { "C$it?" }
val variableNames = (1..tupleNumber).joinToString(separator = ", ") { "c$it" }
val resultExtractors = (1..tupleNumber).joinToString(separator = ", ") { "c${it}.sqlType.getResult(rowSet, $it)" }
writer.write("""
/**
* Perform a tuple of aggregations given by [aggregationSelector] for all elements in the sequence,
* and return the aggregate results.
*
* Ktorm supports aggregating two or more columns, we just need to wrap our aggregate expressions by
* [tupleOf] in the closure, then the function’s return type becomes `TupleN<C1?, C2?, .. Cn?>`.
*
* The operation is terminal.
*
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
* @return a tuple of the aggregate results.
* @since 3.1.0
*/
@JvmName("_aggregateColumns$tupleNumber")
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E : Any, T : BaseTable<E>, $typeParams> EntitySequence<E, T>.aggregateColumns(
aggregationSelector: (T) -> Tuple$tupleNumber<$columnDeclarings>
): Tuple$tupleNumber<$resultTypes> {
val ($variableNames) = aggregationSelector(sourceTable)
val expr = expression.copy(
columns = listOf($variableNames).map { it.aliased(null) }
)
val rowSet = Query(database, expr).rowSet
if (rowSet.size() == 1) {
check(rowSet.next())
return tupleOf($resultExtractors)
} else {
val (sql, _) = database.formatExpression(expr, beautifySql = true)
throw IllegalStateException("Expected 1 row but ${'$'}{rowSet.size()} returned from sql: \n\n${'$'}sql")
}
}
""".trimIndent())
}
fun generateGroupingAggregateColumns(writer: java.io.Writer, tupleNumber: Int) {
val typeParams = (1..tupleNumber).joinToString(separator = ", ") { "C$it : Any" }
val columnDeclarings = (1..tupleNumber).joinToString(separator = ", ") { "ColumnDeclaring<C$it>" }
val resultTypes = (1..tupleNumber).joinToString(separator = ", ") { "C$it?" }
val variableNames = (1..tupleNumber).joinToString(separator = ", ") { "c$it" }
val resultExtractors = (1..tupleNumber).joinToString(separator = ", ") { "c${it}.sqlType.getResult(row, ${it + 1})" }
writer.write("""
/**
* Group elements from the source sequence by key and perform the given aggregations for elements in each group,
* then store the results in a new [Map].
*
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
* `select key, aggregation from source group by key`.
*
* Ktorm supports aggregating two or more columns, we just need to wrap our aggregate expressions by [tupleOf]
* in the closure, then the function’s return type becomes `Map<K?, TupleN<C1?, C2?, .. Cn?>>`.
*
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
* @return a [Map] associating the key of each group with the results of aggregations of the group elements.
* @since 3.1.0
*/
@JvmName("_aggregateColumns$tupleNumber")
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E : Any, T : BaseTable<E>, K : Any, $typeParams> EntityGrouping<E, T, K>.aggregateColumns(
aggregationSelector: (T) -> Tuple$tupleNumber<$columnDeclarings>
): Map<K?, Tuple$tupleNumber<$resultTypes>> {
return aggregateColumnsTo(LinkedHashMap(), aggregationSelector)
}
/**
* Group elements from the source sequence by key and perform the given aggregations for elements in each group,
* then store the results in the [destination] map.
*
* The key for each group is provided by the [EntityGrouping.keySelector] function, and the generated SQL is like:
* `select key, aggregation from source group by key`.
*
* Ktorm supports aggregating two or more columns, we just need to wrap our aggregate expressions by [tupleOf]
* in the closure, then the function’s return type becomes `Map<K?, TupleN<C1?, C2?, .. Cn?>>`.
*
* @param destination a [MutableMap] used to store the results.
* @param aggregationSelector a function that accepts the source table and returns a tuple of aggregate expressions.
* @return the [destination] map associating the key of each group with the result of aggregations of the group elements.
* @since 3.1.0
*/
@JvmName("_aggregateColumns${tupleNumber}To")
@OptIn(ExperimentalTypeInference::class)
@OverloadResolutionByLambdaReturnType
public inline fun <E : Any, T : BaseTable<E>, K : Any, $typeParams, M> EntityGrouping<E, T, K>.aggregateColumnsTo(
destination: M,
aggregationSelector: (T) -> Tuple$tupleNumber<$columnDeclarings>
): M where M : MutableMap<in K?, in Tuple$tupleNumber<$resultTypes>> {
val keyColumn = keySelector(sequence.sourceTable)
val ($variableNames) = aggregationSelector(sequence.sourceTable)
val expr = sequence.expression.copy(
columns = listOf(keyColumn, $variableNames).map { it.aliased(null) },
groupBy = listOf(keyColumn.asExpression())
)
for (row in Query(sequence.database, expr)) {
val key = keyColumn.sqlType.getResult(row, 1)
destination[key] = tupleOf($resultExtractors)
}
return destination
}
""".trimIndent())
}
val generateTuples by tasks.registering {
doLast {
val outputFile = file("$generatedSourceDir/org/ktorm/entity/Tuples.kt")
outputFile.parentFile.mkdirs()
outputFile.bufferedWriter().use { writer ->
writer.write("""
/*
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Auto-generated by ktorm.tuples-codegen.gradle.kts, DO NOT EDIT!
package org.ktorm.entity
import org.ktorm.dsl.Query
import org.ktorm.dsl.mapTo
import org.ktorm.schema.ColumnDeclaring
import org.ktorm.schema.BaseTable
import java.io.Serializable
import kotlin.experimental.ExperimentalTypeInference
/**
* Set a typealias `Tuple2` for `Pair`.
*/
public typealias Tuple2<E1, E2> = Pair<E1, E2>
/**
* Set a typealias `Tuple3` for `Triple`.
*/
public typealias Tuple3<E1, E2, E3> = Triple<E1, E2, E3>
""".trimIndent())
for (num in (4..maxTupleNumber)) {
generateTuple(writer, num)
}
for (num in (2..maxTupleNumber)) {
generateTupleOf(writer, num)
}
for (num in (4..maxTupleNumber)) {
generateToList(writer, num)
}
for (num in (2..maxTupleNumber)) {
generateMapColumns(writer, num)
}
for (num in (2..maxTupleNumber)) {
generateAggregateColumns(writer, num)
}
for (num in (2..maxTupleNumber)) {
generateGroupingAggregateColumns(writer, num)
}
}
}
}
tasks {
"codegen" {
dependsOn(generateTuples)
}
}
sourceSets.main {
kotlin.srcDir(generatedSourceDir)
}