Skip to content

Commit 6e3ebd4

Browse files
committed
[Kotlin] Rework insert methods
1 parent 1c58946 commit 6e3ebd4

13 files changed

+585
-71
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2016-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util.kotlin
17+
18+
import org.mybatis.dynamic.sql.SqlBuilder
19+
import org.mybatis.dynamic.sql.SqlColumn
20+
import org.mybatis.dynamic.sql.SqlTable
21+
import org.mybatis.dynamic.sql.insert.BatchInsertDSL
22+
import org.mybatis.dynamic.sql.insert.BatchInsertModel
23+
import org.mybatis.dynamic.sql.util.Buildable
24+
25+
typealias KotlinBatchInsertCompleter<T> = KotlinBatchInsertBuilder<T>.() -> Unit
26+
27+
@MyBatisDslMarker
28+
class KotlinBatchInsertBuilder<T> (private val rows: Collection<T>): Buildable<BatchInsertModel<T>> {
29+
30+
private lateinit var dsl: BatchInsertDSL<T>
31+
32+
fun into(table: SqlTable) {
33+
dsl = SqlBuilder.insertBatch(rows).into(table)
34+
}
35+
36+
infix fun <C : Any> map(column: SqlColumn<C>) = MapCompleter(column)
37+
38+
override fun build(): BatchInsertModel<T> {
39+
return getDsl().build()
40+
}
41+
42+
private fun getDsl(): BatchInsertDSL<T> {
43+
try {
44+
return dsl
45+
} catch (e: UninitializedPropertyAccessException) {
46+
throw UninitializedPropertyAccessException(
47+
"You must specify an \"into\" clause before any other clauses in an insertBatch statement", e
48+
)
49+
}
50+
}
51+
52+
@MyBatisDslMarker
53+
inner class MapCompleter<C : Any> (private val column: SqlColumn<C>) {
54+
infix fun toProperty(property: String) =
55+
applyToDsl {
56+
map(column).toProperty(property)
57+
}
58+
59+
fun toNull() =
60+
applyToDsl {
61+
map(column).toNull()
62+
}
63+
64+
infix fun toConstant(constant: String) =
65+
applyToDsl {
66+
map(column).toConstant(constant)
67+
}
68+
69+
infix fun toStringConstant(constant: String) =
70+
applyToDsl {
71+
map(column).toStringConstant(constant)
72+
}
73+
74+
private fun applyToDsl(block: BatchInsertDSL<T>.() -> Unit) {
75+
this@KotlinBatchInsertBuilder.getDsl().apply(block)
76+
}
77+
}
78+
}

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinInsertHelpers.kt renamed to src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinGeneralInsertBuilder.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,12 @@
1616
package org.mybatis.dynamic.sql.util.kotlin
1717

1818
import org.mybatis.dynamic.sql.SqlColumn
19-
import org.mybatis.dynamic.sql.insert.BatchInsertDSL
2019
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL
2120
import org.mybatis.dynamic.sql.insert.GeneralInsertModel
22-
import org.mybatis.dynamic.sql.insert.InsertDSL
23-
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
2421
import org.mybatis.dynamic.sql.util.Buildable
2522

2623
typealias GeneralInsertCompleter = @MyBatisDslMarker KotlinGeneralInsertBuilder.() -> Unit
2724

28-
typealias InsertCompleter<T> = @MyBatisDslMarker InsertDSL<T>.() -> Unit
29-
30-
typealias MultiRowInsertCompleter<T> = @MyBatisDslMarker MultiRowInsertDSL<T>.() -> Unit
31-
32-
typealias BatchInsertCompleter<T> = @MyBatisDslMarker BatchInsertDSL<T>.() -> Unit
33-
34-
typealias InsertSelectCompleter = @MyBatisDslMarker KotlinInsertSelectSubQueryBuilder.() -> Unit
35-
3625
@MyBatisDslMarker
3726
class KotlinGeneralInsertBuilder(private val dsl: GeneralInsertDSL) : Buildable<GeneralInsertModel> {
3827

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2016-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util.kotlin
17+
18+
import org.mybatis.dynamic.sql.SqlBuilder
19+
import org.mybatis.dynamic.sql.SqlColumn
20+
import org.mybatis.dynamic.sql.SqlTable
21+
import org.mybatis.dynamic.sql.insert.InsertDSL
22+
import org.mybatis.dynamic.sql.insert.InsertModel
23+
import org.mybatis.dynamic.sql.util.Buildable
24+
25+
typealias KotlinInsertCompleter<T> = KotlinInsertBuilder<T>.() -> Unit
26+
27+
@MyBatisDslMarker
28+
class KotlinInsertBuilder<T> (private val row: T): Buildable<InsertModel<T>> {
29+
30+
private lateinit var dsl: InsertDSL<T>
31+
32+
fun into(table: SqlTable) {
33+
dsl = SqlBuilder.insert(row).into(table)
34+
}
35+
36+
fun <C : Any> map(column: SqlColumn<C>) = MapCompleter(column)
37+
38+
override fun build(): InsertModel<T> {
39+
return getDsl().build()
40+
}
41+
42+
private fun getDsl(): InsertDSL<T> {
43+
try {
44+
return dsl
45+
} catch (e: UninitializedPropertyAccessException) {
46+
throw UninitializedPropertyAccessException(
47+
"You must specify an \"into\" clause before any other clauses in an insert statement", e
48+
)
49+
}
50+
}
51+
52+
@MyBatisDslMarker
53+
inner class MapCompleter<C : Any> (private val column: SqlColumn<C>) {
54+
infix fun toProperty(property: String) =
55+
applyToDsl {
56+
map(column).toProperty(property)
57+
}
58+
59+
fun toNull() =
60+
applyToDsl {
61+
map(column).toNull()
62+
}
63+
64+
infix fun toConstant(constant: String) =
65+
applyToDsl {
66+
map(column).toConstant(constant)
67+
}
68+
69+
infix fun toStringConstant(constant: String) =
70+
applyToDsl {
71+
map(column).toStringConstant(constant)
72+
}
73+
74+
fun toPropertyWhenPresent(property: String, valueSupplier: () -> C?) =
75+
applyToDsl {
76+
map(column).toPropertyWhenPresent(property, valueSupplier)
77+
}
78+
79+
private fun applyToDsl(block: InsertDSL<T>.() -> Unit) {
80+
this@KotlinInsertBuilder.getDsl().apply(block)
81+
}
82+
}
83+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2016-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util.kotlin
17+
18+
import org.mybatis.dynamic.sql.SqlBuilder
19+
import org.mybatis.dynamic.sql.SqlColumn
20+
import org.mybatis.dynamic.sql.SqlTable
21+
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
22+
import org.mybatis.dynamic.sql.insert.MultiRowInsertModel
23+
import org.mybatis.dynamic.sql.util.Buildable
24+
25+
typealias KotlinMultiRowInsertCompleter<T> = KotlinMultiRowInsertBuilder<T>.() -> Unit
26+
27+
@MyBatisDslMarker
28+
class KotlinMultiRowInsertBuilder<T> (private val rows: Collection<T>): Buildable<MultiRowInsertModel<T>> {
29+
30+
private lateinit var dsl: MultiRowInsertDSL<T>
31+
32+
fun into(table: SqlTable) {
33+
dsl = SqlBuilder.insertMultiple(rows).into(table)
34+
}
35+
36+
infix fun <C : Any> map(column: SqlColumn<C>) = MapCompleter(column)
37+
38+
override fun build(): MultiRowInsertModel<T> {
39+
return getDsl().build()
40+
}
41+
42+
private fun getDsl(): MultiRowInsertDSL<T> {
43+
try {
44+
return dsl
45+
} catch (e: UninitializedPropertyAccessException) {
46+
throw UninitializedPropertyAccessException(
47+
"You must specify an \"into\" clause before any other clauses in an insertMultiple statement", e
48+
)
49+
}
50+
}
51+
52+
@MyBatisDslMarker
53+
inner class MapCompleter<C : Any> (private val column: SqlColumn<C>) {
54+
infix fun toProperty(property: String) =
55+
applyToDsl {
56+
map(column).toProperty(property)
57+
}
58+
59+
fun toNull() =
60+
applyToDsl {
61+
map(column).toNull()
62+
}
63+
64+
infix fun toConstant(constant: String) =
65+
applyToDsl {
66+
map(column).toConstant(constant)
67+
}
68+
69+
infix fun toStringConstant(constant: String) =
70+
applyToDsl {
71+
map(column).toStringConstant(constant)
72+
}
73+
74+
private fun applyToDsl(block: MultiRowInsertDSL<T>.() -> Unit) {
75+
this@KotlinMultiRowInsertBuilder.getDsl().apply(block)
76+
}
77+
}
78+
}

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinSubQueryBuilders.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class KotlinQualifiedSubQueryBuilder : KotlinBaseSubQueryBuilder() {
5959
}
6060
}
6161

62+
typealias InsertSelectCompleter = KotlinInsertSelectSubQueryBuilder.() -> Unit
63+
6264
class KotlinInsertSelectSubQueryBuilder : KotlinBaseSubQueryBuilder() {
6365
private lateinit var lateColumnList: List<SqlColumn<*>>
6466

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/InsertStatements.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,12 +22,17 @@ import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
2222

2323
// These insert functions help avoid the use of org.mybatis.dynamic.sql.SqlBuilder in Kotlin
2424

25+
@Deprecated("Please see the deprecation message on the following \"into\" function for advice")
2526
fun <T> insert(row: T): InsertDSL.IntoGatherer<T> = SqlBuilder.insert(row)
2627

28+
@Deprecated("Please see the deprecation message on the following \"into\" function for advice")
2729
fun <T> insertBatch(vararg records: T): BatchInsertDSL.IntoGatherer<T> = insertBatch(records.asList())
2830

31+
@Deprecated("Please see the deprecation message on the following \"into\" function for advice")
2932
fun <T> insertBatch(records: Collection<T>): BatchInsertDSL.IntoGatherer<T> = SqlBuilder.insertBatch(records)
3033

34+
@Deprecated("Please see the deprecation message on the following \"into\" function for advice")
3135
fun <T> insertMultiple(vararg records: T): MultiRowInsertDSL.IntoGatherer<T> = insertMultiple(records.asList())
3236

37+
@Deprecated("Please see the deprecation message on the following \"into\" function for advice")
3338
fun <T> insertMultiple(records: Collection<T>): MultiRowInsertDSL.IntoGatherer<T> = SqlBuilder.insertMultiple(records)

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/model/ModelBuilderFunctions.kt

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,19 +31,22 @@ import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
3131
import org.mybatis.dynamic.sql.insert.MultiRowInsertModel
3232
import org.mybatis.dynamic.sql.select.SelectModel
3333
import org.mybatis.dynamic.sql.update.UpdateModel
34-
import org.mybatis.dynamic.sql.util.kotlin.BatchInsertCompleter
3534
import org.mybatis.dynamic.sql.util.kotlin.CountCompleter
3635
import org.mybatis.dynamic.sql.util.kotlin.DeleteCompleter
3736
import org.mybatis.dynamic.sql.util.kotlin.GeneralInsertCompleter
38-
import org.mybatis.dynamic.sql.util.kotlin.InsertCompleter
3937
import org.mybatis.dynamic.sql.util.kotlin.InsertSelectCompleter
38+
import org.mybatis.dynamic.sql.util.kotlin.KotlinBatchInsertBuilder
39+
import org.mybatis.dynamic.sql.util.kotlin.KotlinBatchInsertCompleter
4040
import org.mybatis.dynamic.sql.util.kotlin.KotlinCountBuilder
4141
import org.mybatis.dynamic.sql.util.kotlin.KotlinDeleteBuilder
4242
import org.mybatis.dynamic.sql.util.kotlin.KotlinGeneralInsertBuilder
43+
import org.mybatis.dynamic.sql.util.kotlin.KotlinInsertBuilder
44+
import org.mybatis.dynamic.sql.util.kotlin.KotlinInsertCompleter
4345
import org.mybatis.dynamic.sql.util.kotlin.KotlinInsertSelectSubQueryBuilder
46+
import org.mybatis.dynamic.sql.util.kotlin.KotlinMultiRowInsertBuilder
47+
import org.mybatis.dynamic.sql.util.kotlin.KotlinMultiRowInsertCompleter
4448
import org.mybatis.dynamic.sql.util.kotlin.KotlinSelectBuilder
4549
import org.mybatis.dynamic.sql.util.kotlin.KotlinUpdateBuilder
46-
import org.mybatis.dynamic.sql.util.kotlin.MultiRowInsertCompleter
4750
import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter
4851
import org.mybatis.dynamic.sql.util.kotlin.UpdateCompleter
4952

@@ -60,9 +63,18 @@ fun countFrom(table: SqlTable, completer: CountCompleter): SelectModel =
6063
fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteModel =
6164
KotlinDeleteBuilder(SqlBuilder.deleteFrom(table)).apply(completer).build()
6265

66+
fun <T> insert(row: T, completer: KotlinInsertCompleter<T>): InsertModel<T> =
67+
KotlinInsertBuilder(row).apply(completer).build()
68+
69+
fun <T> insertBatch(rows: Collection<T>, completer: KotlinBatchInsertCompleter<T>): BatchInsertModel<T> =
70+
KotlinBatchInsertBuilder(rows).apply(completer).build()
71+
6372
fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): GeneralInsertModel =
6473
KotlinGeneralInsertBuilder(GeneralInsertDSL.insertInto(table)).apply(completer).build()
6574

75+
fun <T> insertMultiple(rows: Collection<T>, completer: KotlinMultiRowInsertCompleter<T>): MultiRowInsertModel<T> =
76+
KotlinMultiRowInsertBuilder(rows).apply(completer).build()
77+
6678
fun insertSelect(table: SqlTable, completer: InsertSelectCompleter): InsertSelectModel =
6779
with(KotlinInsertSelectSubQueryBuilder().apply(completer)) {
6880
SqlBuilder.insertInto(table)
@@ -71,15 +83,21 @@ fun insertSelect(table: SqlTable, completer: InsertSelectCompleter): InsertSelec
7183
.build()
7284
}
7385

74-
fun <T> BatchInsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: BatchInsertCompleter<T>): BatchInsertModel<T> =
86+
@Deprecated("Please switch to the insertBatch statement in the model package")
87+
fun <T> BatchInsertDSL.IntoGatherer<T>.into(
88+
table: SqlTable,
89+
completer: BatchInsertDSL<T>.() -> Unit
90+
): BatchInsertModel<T> =
7591
into(table).apply(completer).build()
7692

77-
fun <T> InsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: InsertCompleter<T>): InsertModel<T> =
93+
@Deprecated("Please switch to the insert statement in the model package")
94+
fun <T> InsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: InsertDSL<T>.() -> Unit): InsertModel<T> =
7895
into(table).apply(completer).build()
7996

97+
@Deprecated("Please switch to the insertMultiple statement in the model package")
8098
fun <T> MultiRowInsertDSL.IntoGatherer<T>.into(
8199
table: SqlTable,
82-
completer: MultiRowInsertCompleter<T>
100+
completer: MultiRowInsertDSL<T>.() -> Unit
83101
): MultiRowInsertModel<T> =
84102
into(table).apply(completer).build()
85103

0 commit comments

Comments
 (0)