Skip to content

Commit ca920a5

Browse files
committed
Correct nullability for insert methods
1 parent 4edac4f commit ca920a5

File tree

8 files changed

+44
-54
lines changed

8 files changed

+44
-54
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.mybatis.dynamic.sql.util.Buildable
2525
typealias KotlinBatchInsertCompleter<T> = KotlinBatchInsertBuilder<T>.() -> Unit
2626

2727
@MyBatisDslMarker
28-
class KotlinBatchInsertBuilder<T> (private val rows: Collection<T & Any>): Buildable<BatchInsertModel<T>> {
28+
class KotlinBatchInsertBuilder<T : Any> (private val rows: Collection<T>): Buildable<BatchInsertModel<T>> {
2929
private var table: SqlTable? = null
3030
private val columnMappings = mutableListOf<AbstractColumnMapping>()
3131

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.mybatis.dynamic.sql.util.Buildable
2525
typealias KotlinInsertCompleter<T> = KotlinInsertBuilder<T>.() -> Unit
2626

2727
@MyBatisDslMarker
28-
class KotlinInsertBuilder<T> (private val row: T & Any): Buildable<InsertModel<T>> {
28+
class KotlinInsertBuilder<T : Any> (private val row: T): Buildable<InsertModel<T>> {
2929
private var table: SqlTable? = null
3030
private val columnMappings = mutableListOf<AbstractColumnMapping>()
3131

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.mybatis.dynamic.sql.util.Buildable
2525
typealias KotlinMultiRowInsertCompleter<T> = KotlinMultiRowInsertBuilder<T>.() -> Unit
2626

2727
@MyBatisDslMarker
28-
class KotlinMultiRowInsertBuilder<T> (private val rows: Collection<T & Any>): Buildable<MultiRowInsertModel<T>> {
28+
class KotlinMultiRowInsertBuilder<T : Any> (private val rows: Collection<T>): Buildable<MultiRowInsertModel<T>> {
2929
private var table: SqlTable? = null
3030
private val columnMappings = mutableListOf<AbstractColumnMapping>()
3131

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,18 @@ fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteModel =
6565
fun deleteFrom(table: SqlTable, tableAlias: String, completer: DeleteCompleter): DeleteModel =
6666
KotlinDeleteBuilder(SqlBuilder.deleteFrom(table, tableAlias)).apply(completer).build()
6767

68-
fun <T> insert(row: T & Any, completer: KotlinInsertCompleter<T>): InsertModel<T> {
69-
val builder : KotlinInsertBuilder<T> = KotlinInsertBuilder(row)
70-
return builder.apply(completer).build()
71-
}
68+
fun <T : Any> insert(row: T, completer: KotlinInsertCompleter<T>): InsertModel<T> =
69+
KotlinInsertBuilder(row).apply(completer).build()
7270

73-
fun <T> insertBatch(rows: Collection<T & Any>, completer: KotlinBatchInsertCompleter<T>): BatchInsertModel<T> {
74-
val builder: KotlinBatchInsertBuilder<T> = KotlinBatchInsertBuilder(rows)
75-
return builder.apply(completer).build()
71+
fun <T : Any> insertBatch(rows: Collection<T>, completer: KotlinBatchInsertCompleter<T>): BatchInsertModel<T> {
72+
return KotlinBatchInsertBuilder(rows).apply(completer).build()
7673
}
7774

7875
fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): GeneralInsertModel =
7976
KotlinGeneralInsertBuilder(table).apply(completer).build()
8077

81-
fun <T> insertMultiple(rows: Collection<T & Any>, completer: KotlinMultiRowInsertCompleter<T>): MultiRowInsertModel<T> {
82-
val builder: KotlinMultiRowInsertBuilder<T> = KotlinMultiRowInsertBuilder(rows)
83-
return builder.apply(completer).build()
84-
}
78+
fun <T : Any> insertMultiple(rows: Collection<T>, completer: KotlinMultiRowInsertCompleter<T>): MultiRowInsertModel<T> =
79+
KotlinMultiRowInsertBuilder(rows).apply(completer).build()
8580

8681
fun insertSelect(table: SqlTable, completer: InsertSelectCompleter): InsertSelectModel =
8782
with(KotlinInsertSelectSubQueryBuilder().apply(completer)) {

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/MapperSupportFunctions.kt

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package org.mybatis.dynamic.sql.util.kotlin.mybatis3
1919
import org.mybatis.dynamic.sql.BasicColumn
2020
import org.mybatis.dynamic.sql.SqlTable
2121
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider
22-
import org.mybatis.dynamic.sql.insert.render.BatchInsert
2322
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider
2423
import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider
2524
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
@@ -64,9 +63,9 @@ fun countFrom(mapper: (SelectStatementProvider) -> Long, table: SqlTable, comple
6463
fun deleteFrom(mapper: (DeleteStatementProvider) -> Int, table: SqlTable, completer: DeleteCompleter): Int =
6564
mapper(deleteFrom(table, completer))
6665

67-
fun <T> insert(
66+
fun <T : Any> insert(
6867
mapper: (InsertStatementProvider<T>) -> Int,
69-
row: T & Any,
68+
row: T,
7069
table: SqlTable,
7170
completer: KotlinInsertCompleter<T>
7271
): Int =
@@ -82,19 +81,16 @@ fun <T> insert(
8281
* list will be [org.apache.ibatis.executor.BatchExecutor.BATCH_UPDATE_RETURN_VALUE]).
8382
* To retrieve update counts, execute [org.apache.ibatis.session.SqlSession.flushStatements].
8483
*/
85-
fun <T> insertBatch(
84+
fun <T : Any> insertBatch(
8685
mapper: (InsertStatementProvider<T>) -> Int,
87-
records: Collection<T & Any>,
86+
records: Collection<T>,
8887
table: SqlTable,
8988
completer: KotlinBatchInsertCompleter<T>
90-
): List<Int> {
91-
val batchInsert: BatchInsert<T> = insertBatch(records) {
89+
): List<Int> =
90+
insertBatch(records) {
9291
into(table)
9392
run(completer)
94-
}
95-
96-
return batchInsert.insertStatements().map(mapper)
97-
}
93+
}.insertStatements().map(mapper)
9894

9995
fun insertInto(
10096
mapper: (GeneralInsertStatementProvider) -> Int,
@@ -103,9 +99,9 @@ fun insertInto(
10399
): Int =
104100
mapper(insertInto(table, completer))
105101

106-
fun <T> insertMultiple(
102+
fun <T : Any> insertMultiple(
107103
mapper: (MultiRowInsertStatementProvider<T>) -> Int,
108-
records: Collection<T & Any>,
104+
records: Collection<T>,
109105
table: SqlTable,
110106
completer: KotlinMultiRowInsertCompleter<T>
111107
): Int =
@@ -114,20 +110,19 @@ fun <T> insertMultiple(
114110
run(completer)
115111
})
116112

117-
fun <T> insertMultipleWithGeneratedKeys(
113+
fun <T : Any> insertMultipleWithGeneratedKeys(
118114
mapper: (String, List<T>) -> Int,
119-
records: Collection<T & Any>,
115+
records: Collection<T>,
120116
table: SqlTable,
121117
completer: KotlinMultiRowInsertCompleter<T>
122-
): Int {
123-
val provider: MultiRowInsertStatementProvider<T> = insertMultiple(records) {
118+
): Int =
119+
insertMultiple(records) {
124120
into(table)
125121
run(completer)
122+
}.run {
123+
mapper(insertStatement, this.records)
126124
}
127125

128-
return mapper(provider.insertStatement, provider.records)
129-
}
130-
131126
fun insertSelect(
132127
mapper: (InsertSelectStatementProvider) -> Int,
133128
table: SqlTable,

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/ProviderBuilderFunctions.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteStatementProv
6868
fun deleteFrom(table: SqlTable, tableAlias: String, completer: DeleteCompleter): DeleteStatementProvider =
6969
deleteFrom(table, tableAlias, completer).render(RenderingStrategies.MYBATIS3)
7070

71-
fun <T> insert(row: T & Any, completer: KotlinInsertCompleter<T>): InsertStatementProvider<T> =
71+
fun <T : Any> insert(row: T, completer: KotlinInsertCompleter<T>): InsertStatementProvider<T> =
7272
insert(row, completer).render(RenderingStrategies.MYBATIS3)
7373

74-
fun <T> insertBatch(rows: Collection<T & Any>, completer: KotlinBatchInsertCompleter<T>): BatchInsert<T> =
74+
fun <T : Any> insertBatch(rows: Collection<T>, completer: KotlinBatchInsertCompleter<T>): BatchInsert<T> =
7575
insertBatch(rows, completer).render(RenderingStrategies.MYBATIS3)
7676

7777
fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): GeneralInsertStatementProvider =
7878
insertInto(table, completer).render(RenderingStrategies.MYBATIS3)
7979

80-
fun <T> insertMultiple(
81-
rows: Collection<T & Any>,
80+
fun <T : Any> insertMultiple(
81+
rows: Collection<T>,
8282
completer: KotlinMultiRowInsertCompleter<T>
8383
): MultiRowInsertStatementProvider<T> =
8484
insertMultiple(rows, completer).render(RenderingStrategies.MYBATIS3)

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/NamedParameterJdbcTemplateExtensions.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ fun NamedParameterJdbcTemplate.deleteFrom(table: SqlTable, completer: DeleteComp
6868
fun <T> NamedParameterJdbcTemplate.insertBatch(insertStatement: BatchInsert<T>): IntArray =
6969
batchUpdate(insertStatement.insertStatementSQL, SqlParameterSourceUtils.createBatch(insertStatement.records))
7070

71-
fun <T> NamedParameterJdbcTemplate.insertBatch(
72-
vararg records: T & Any,
71+
fun <T : Any> NamedParameterJdbcTemplate.insertBatch(
72+
vararg records: T,
7373
completer: KotlinBatchInsertCompleter<T>
7474
): IntArray =
7575
insertBatch(records.asList(), completer)
7676

77-
fun <T> NamedParameterJdbcTemplate.insertBatch(
78-
records: List<T & Any>,
77+
fun <T : Any> NamedParameterJdbcTemplate.insertBatch(
78+
records: List<T>,
7979
completer: KotlinBatchInsertCompleter<T>
8080
): IntArray =
8181
insertBatch(org.mybatis.dynamic.sql.util.kotlin.spring.insertBatch(records, completer))
@@ -98,7 +98,7 @@ fun <T> NamedParameterJdbcTemplate.insert(
9898
): Int =
9999
update(insertStatement.insertStatement, BeanPropertySqlParameterSource(insertStatement.row), keyHolder)
100100

101-
fun <T> NamedParameterJdbcTemplate.insert(row: T & Any, completer: KotlinInsertCompleter<T>): Int =
101+
fun <T : Any> NamedParameterJdbcTemplate.insert(row: T, completer: KotlinInsertCompleter<T>): Int =
102102
insert(org.mybatis.dynamic.sql.util.kotlin.spring.insert(row, completer))
103103

104104
@Deprecated("Please move the into phrase inside the lambda")
@@ -119,14 +119,14 @@ fun NamedParameterJdbcTemplate.insertInto(table: SqlTable, completer: GeneralIns
119119
generalInsert(org.mybatis.dynamic.sql.util.kotlin.spring.insertInto(table, completer))
120120

121121
// multiple row insert
122-
fun <T> NamedParameterJdbcTemplate.insertMultiple(
123-
vararg records: T & Any,
122+
fun <T : Any> NamedParameterJdbcTemplate.insertMultiple(
123+
vararg records: T,
124124
completer: KotlinMultiRowInsertCompleter<T>
125125
): Int =
126126
insertMultiple(records.asList(), completer)
127127

128-
fun <T> NamedParameterJdbcTemplate.insertMultiple(
129-
records: List<T & Any>,
128+
fun <T : Any> NamedParameterJdbcTemplate.insertMultiple(
129+
records: List<T>,
130130
completer: KotlinMultiRowInsertCompleter<T>
131131
): Int =
132132
insertMultiple(org.mybatis.dynamic.sql.util.kotlin.spring.insertMultiple(records, completer))
@@ -284,17 +284,17 @@ class KeyHolderHelper(private val keyHolder: KeyHolder, private val template: Na
284284
fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): Int =
285285
template.generalInsert(org.mybatis.dynamic.sql.util.kotlin.spring.insertInto(table, completer), keyHolder)
286286

287-
fun <T> insert(row: T & Any, completer: KotlinInsertCompleter<T>): Int =
287+
fun <T : Any> insert(row: T, completer: KotlinInsertCompleter<T>): Int =
288288
template.insert(org.mybatis.dynamic.sql.util.kotlin.spring.insert(row, completer), keyHolder)
289289

290290
@Deprecated("Please move the into phrase inside the lambda")
291291
fun <T : Any> insert(row: T): SingleRowInsertWithKeyHolderHelper<T> =
292292
SingleRowInsertWithKeyHolderHelper(row, template, keyHolder)
293293

294-
fun <T> insertMultiple(vararg records: T & Any, completer: KotlinMultiRowInsertCompleter<T>): Int =
294+
fun <T : Any> insertMultiple(vararg records: T, completer: KotlinMultiRowInsertCompleter<T>): Int =
295295
insertMultiple(records.asList(), completer)
296296

297-
fun <T> insertMultiple(records: List<T & Any>, completer: KotlinMultiRowInsertCompleter<T>): Int =
297+
fun <T : Any> insertMultiple(records: List<T>, completer: KotlinMultiRowInsertCompleter<T>): Int =
298298
template.insertMultiple(org.mybatis.dynamic.sql.util.kotlin.spring.insertMultiple(records, completer),
299299
keyHolder)
300300

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/ProviderBuilderFunctions.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteStatementProv
6868
fun deleteFrom(table: SqlTable, tableAlias: String, completer: DeleteCompleter): DeleteStatementProvider =
6969
deleteFrom(table, tableAlias, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER)
7070

71-
fun <T> insert(row: T & Any, completer: KotlinInsertCompleter<T>): InsertStatementProvider<T> =
71+
fun <T : Any> insert(row: T, completer: KotlinInsertCompleter<T>): InsertStatementProvider<T> =
7272
insert(row, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER)
7373

74-
fun <T> insertBatch(rows: Collection<T & Any>, completer: KotlinBatchInsertCompleter<T>): BatchInsert<T> =
74+
fun <T : Any> insertBatch(rows: Collection<T>, completer: KotlinBatchInsertCompleter<T>): BatchInsert<T> =
7575
insertBatch(rows, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER)
7676

7777
fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): GeneralInsertStatementProvider =
7878
insertInto(table, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER)
7979

80-
fun <T> insertMultiple(
81-
rows: Collection<T & Any>,
80+
fun <T : Any> insertMultiple(
81+
rows: Collection<T>,
8282
completer: KotlinMultiRowInsertCompleter<T>
8383
): MultiRowInsertStatementProvider<T> =
8484
insertMultiple(rows, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER)

0 commit comments

Comments
 (0)