Skip to content

Commit 4f76d0e

Browse files
committed
[Kotlin] Improve the insertSelect DSL methods
1 parent 194e726 commit 4f76d0e

File tree

10 files changed

+163
-14
lines changed

10 files changed

+163
-14
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package org.mybatis.dynamic.sql.util.kotlin
1818
import org.mybatis.dynamic.sql.BasicColumn
1919
import org.mybatis.dynamic.sql.SqlBuilder
2020
import org.mybatis.dynamic.sql.SqlColumn
21+
import org.mybatis.dynamic.sql.SqlTable
22+
import org.mybatis.dynamic.sql.insert.InsertSelectModel
2123
import org.mybatis.dynamic.sql.select.SelectModel
2224
import org.mybatis.dynamic.sql.util.Buildable
2325
import org.mybatis.dynamic.sql.util.Messages
@@ -57,11 +59,34 @@ class KotlinQualifiedSubQueryBuilder : KotlinBaseSubQueryBuilder() {
5759
typealias InsertSelectCompleter = KotlinInsertSelectSubQueryBuilder.() -> Unit
5860

5961
class KotlinInsertSelectSubQueryBuilder : KotlinBaseSubQueryBuilder() {
60-
internal var columnList: List<SqlColumn<*>>? = null
62+
private var columnList: List<SqlColumn<*>>? = null
63+
private var table: SqlTable? = null
64+
65+
fun into(table: SqlTable) {
66+
this.table = table
67+
}
6168

6269
fun columns(vararg columnList: SqlColumn<*>): Unit = columns(columnList.asList())
6370

6471
fun columns(columnList: List<SqlColumn<*>>) {
6572
this.columnList = columnList
6673
}
74+
75+
// TODO - should just be build()
76+
fun buildInsertSelectModel(): InsertSelectModel {
77+
if (table == null) {
78+
throw KInvalidSQLException(Messages.getString("ERROR.29")) //$NON-NLS-1$
79+
}
80+
81+
return if (columnList == null) {
82+
SqlBuilder.insertInto(table)
83+
.withSelectStatement(this)
84+
.build()
85+
} else {
86+
SqlBuilder.insertInto(table)
87+
.withColumnList(columnList)
88+
.withSelectStatement(this)
89+
.build()
90+
}
91+
}
6792
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,17 @@ fun insertInto(table: SqlTable, completer: GeneralInsertCompleter): GeneralInser
7777
fun <T : Any> insertMultiple(rows: Collection<T>, completer: KotlinMultiRowInsertCompleter<T>): MultiRowInsertModel<T> =
7878
KotlinMultiRowInsertBuilder(rows).apply(completer).build()
7979

80+
@Deprecated("Please use the new form - move the table into the lambda with into(table)")
8081
fun insertSelect(table: SqlTable, completer: InsertSelectCompleter): InsertSelectModel =
81-
with(KotlinInsertSelectSubQueryBuilder().apply(completer)) {
82-
if (columnList == null) {
83-
SqlBuilder.insertInto(table)
84-
.withSelectStatement(this)
85-
.build()
86-
} else {
87-
SqlBuilder.insertInto(table)
88-
.withColumnList(columnList)
89-
.withSelectStatement(this)
90-
.build()
91-
}
82+
with(KotlinInsertSelectSubQueryBuilder()) {
83+
into(table)
84+
apply(completer)
85+
buildInsertSelectModel()
9286
}
9387

88+
fun insertSelect(completer: InsertSelectCompleter): InsertSelectModel =
89+
KotlinInsertSelectSubQueryBuilder().apply(completer).buildInsertSelectModel()
90+
9491
@Deprecated("Please switch to the insertBatch statement in the model package")
9592
fun <T> BatchInsertDSL.IntoGatherer<T>.into(
9693
table: SqlTable,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ fun insertSelect(
128128
table: SqlTable,
129129
completer: InsertSelectCompleter
130130
): Int =
131-
insertSelect(table, completer).run(mapper)
131+
insertSelect {
132+
into(table)
133+
run(completer)
134+
}.run(mapper)
132135

133136
fun <T> selectDistinct(
134137
mapper: (SelectStatementProvider) -> List<T>,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ fun <T : Any> insertMultiple(
8383
): MultiRowInsertStatementProvider<T> =
8484
insertMultiple(rows, completer).render(RenderingStrategies.MYBATIS3)
8585

86+
@Deprecated("Please use the new form - move the table into the lambda with into(table)")
8687
fun insertSelect(table: SqlTable, completer: InsertSelectCompleter): InsertSelectStatementProvider =
8788
insertSelect(table, completer).render(RenderingStrategies.MYBATIS3)
8889

90+
fun insertSelect(completer: InsertSelectCompleter): InsertSelectStatementProvider =
91+
insertSelect(completer).render(RenderingStrategies.MYBATIS3)
92+
8993
@Deprecated("Please switch to the insertBatch statement in the mybatis3 package")
9094
fun <T> BatchInsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: BatchInsertDSL<T>.() -> Unit): BatchInsert<T> =
9195
into(table, completer).render(RenderingStrategies.MYBATIS3)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,13 @@ fun <T> NamedParameterJdbcTemplate.insertMultiple(
148148
): Int =
149149
update(insertStatement.insertStatement, BeanPropertySqlParameterSource(insertStatement), keyHolder)
150150

151+
@Deprecated("Please use the new form - move the table into the lambda with into(table)")
151152
fun NamedParameterJdbcTemplate.insertSelect(table: SqlTable, completer: InsertSelectCompleter): Int =
152153
insertSelect(org.mybatis.dynamic.sql.util.kotlin.spring.insertSelect(table, completer))
153154

155+
fun NamedParameterJdbcTemplate.insertSelect(completer: InsertSelectCompleter): Int =
156+
insertSelect(org.mybatis.dynamic.sql.util.kotlin.spring.insertSelect(completer))
157+
154158
fun NamedParameterJdbcTemplate.insertSelect(insertStatement: InsertSelectStatementProvider): Int =
155159
update(insertStatement.insertStatement, MapSqlParameterSource(insertStatement.parameters))
156160

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ fun <T : Any> insertMultiple(
8383
): MultiRowInsertStatementProvider<T> =
8484
insertMultiple(rows, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER)
8585

86+
@Deprecated("Please use the new form - move the table into the lambda with into(table)")
8687
fun insertSelect(table: SqlTable, completer: InsertSelectCompleter): InsertSelectStatementProvider =
8788
insertSelect(table, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER)
8889

90+
fun insertSelect(completer: InsertSelectCompleter): InsertSelectStatementProvider =
91+
insertSelect(completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER)
92+
8993
@Deprecated("Please switch to the insertBatch statement in the spring package")
9094
fun <T> BatchInsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: BatchInsertDSL<T>.() -> Unit): BatchInsert<T> =
9195
into(table, completer).render(RenderingStrategies.SPRING_NAMED_PARAMETER)

src/main/resources/org/mybatis/dynamic/sql/util/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ ERROR.25=Insert Statements Must Contain an "into" phrase
4444
ERROR.26=Multiple Row Insert Statements Must Contain an "into" phrase
4545
ERROR.27=You must specify a "from" clause before any other clauses in a select statement
4646
ERROR.28=You must specify a select statement in a sub query
47+
ERROR.29=Insert Select Statements Must Contain an "into" phrase

src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import org.mybatis.dynamic.sql.util.kotlin.elements.add
3838
import org.mybatis.dynamic.sql.util.kotlin.elements.constant
3939
import org.mybatis.dynamic.sql.util.kotlin.elements.isIn
4040
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertInto
41+
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertSelect
4142
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
4243
import java.io.InputStreamReader
4344
import java.sql.DriverManager
@@ -258,6 +259,32 @@ class PersonMapperTest {
258259
}
259260
}
260261

262+
@Test
263+
fun testDeprecatedInsertSelect() {
264+
newSession().use { session ->
265+
val mapper = session.getMapper(PersonMapper::class.java)
266+
267+
val insertStatement = insertSelect(person) {
268+
columns(id, firstName, lastName, employed, occupation, addressId, birthDate)
269+
select(add(id, constant<Int>("100")), firstName, lastName, employed, occupation, addressId, birthDate) {
270+
from(person)
271+
orderBy(id)
272+
}
273+
}
274+
275+
val expected = "insert into Person " +
276+
"(id, first_name, last_name, employed, occupation, address_id, birth_date) " +
277+
"select (id + 100), first_name, last_name, employed, occupation, address_id, birth_date " +
278+
"from Person order by id"
279+
280+
assertThat(insertStatement.insertStatement).isEqualTo(expected)
281+
282+
val rows = mapper.insertSelect(insertStatement)
283+
284+
assertThat(rows).isEqualTo(6)
285+
}
286+
}
287+
261288
@Test
262289
fun testInsertBatch() {
263290
newSession(ExecutorType.BATCH).use { session ->

src/test/kotlin/examples/kotlin/spring/canonical/CanonicalSpringKotlinTemplateDirectTest.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,39 @@ open class CanonicalSpringKotlinTemplateDirectTest {
303303

304304
@Test
305305
fun testInsertSelect() {
306+
val rows = template.insertSelect {
307+
into(person)
308+
columns(id, firstName, lastName, birthDate, employed, occupation, addressId)
309+
select(
310+
add(id, constant<Int>("100")), firstName, lastName, birthDate, employed, occupation, addressId
311+
) {
312+
from(person)
313+
orderBy(id)
314+
}
315+
}
316+
317+
assertThat(rows).isEqualTo(6)
318+
319+
val records = template.select(id, firstName, lastName, birthDate, employed, occupation, addressId) {
320+
from(person)
321+
where { id isGreaterThanOrEqualTo 100 }
322+
orderBy(id)
323+
}.withRowMapper(personRowMapper)
324+
325+
assertThat(records).hasSize(6)
326+
with(records[1]) {
327+
assertThat(id).isEqualTo(102)
328+
assertThat(firstName).isEqualTo("Wilma")
329+
assertThat(lastName).isEqualTo(LastName("Flintstone"))
330+
assertThat(birthDate).isNotNull
331+
assertThat(employed).isTrue
332+
assertThat(occupation).isEqualTo("Accountant")
333+
assertThat(addressId).isEqualTo(1)
334+
}
335+
}
336+
337+
@Test
338+
fun testDeprecatedInsertSelect() {
306339
val rows = template.insertSelect(person) {
307340
columns(id, firstName, lastName, birthDate, employed, occupation, addressId)
308341
select(

src/test/kotlin/examples/kotlin/spring/canonical/CanonicalSpringKotlinTest.kt

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ open class CanonicalSpringKotlinTest {
512512

513513
@Test
514514
fun testInsertSelect() {
515-
val insertStatement = insertSelect(person) {
515+
val insertStatement = insertSelect {
516+
into(person)
516517
columns(id, firstName, lastName, birthDate, employed, occupation, addressId)
517518
select(add(id, constant<Int>("100")), firstName, lastName, birthDate, employed, occupation, addressId) {
518519
from(person)
@@ -547,6 +548,56 @@ open class CanonicalSpringKotlinTest {
547548
}
548549
}
549550

551+
@Test
552+
fun testInsertSelectNoTable() {
553+
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
554+
insertSelect {
555+
columns(id, firstName, lastName, birthDate, employed, occupation, addressId)
556+
select(add(id, constant<Int>("100")), firstName, lastName, birthDate, employed, occupation, addressId) {
557+
from(person)
558+
orderBy(id)
559+
}
560+
}
561+
}.withMessage(Messages.getString("ERROR.29"))
562+
}
563+
564+
@Test
565+
fun testDeprecatedInsertSelect() {
566+
val insertStatement = insertSelect(person) {
567+
columns(id, firstName, lastName, birthDate, employed, occupation, addressId)
568+
select(add(id, constant<Int>("100")), firstName, lastName, birthDate, employed, occupation, addressId) {
569+
from(person)
570+
orderBy(id)
571+
}
572+
}
573+
574+
assertThat(insertStatement.insertStatement).isEqualTo(
575+
"insert into Person (id, first_name, last_name, birth_date, employed, occupation, address_id) " +
576+
"select (id + 100), first_name, last_name, birth_date, employed, occupation, address_id " +
577+
"from Person " +
578+
"order by id"
579+
)
580+
val rows = template.insertSelect(insertStatement)
581+
assertThat(rows).isEqualTo(6)
582+
583+
val records = template.select(id, firstName, lastName, birthDate, employed, occupation, addressId) {
584+
from(person)
585+
where { id isGreaterThanOrEqualTo 100 }
586+
orderBy(id)
587+
}.withRowMapper(personRowMapper)
588+
589+
assertThat(records).hasSize(6)
590+
with(records[1]) {
591+
assertThat(id).isEqualTo(102)
592+
assertThat(firstName).isEqualTo("Wilma")
593+
assertThat(lastName).isEqualTo(LastName("Flintstone"))
594+
assertThat(birthDate).isNotNull
595+
assertThat(employed).isTrue
596+
assertThat(occupation).isEqualTo("Accountant")
597+
assertThat(addressId).isEqualTo(1)
598+
}
599+
}
600+
550601
@Test
551602
fun testInsertSelectNoColumns() {
552603
val insertStatement = insertSelect(person) {

0 commit comments

Comments
 (0)