Skip to content

Commit 9920fcb

Browse files
committed
Allow insertSelect with no columns
1 parent a9825b7 commit 9920fcb

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import org.mybatis.dynamic.sql.SqlColumn
2121
import org.mybatis.dynamic.sql.select.SelectModel
2222
import org.mybatis.dynamic.sql.util.Buildable
2323
import org.mybatis.dynamic.sql.util.Messages
24-
import java.lang.NullPointerException
2524

2625
@MyBatisDslMarker
2726
sealed class KotlinBaseSubQueryBuilder : Buildable<SelectModel> {
@@ -42,11 +41,7 @@ sealed class KotlinBaseSubQueryBuilder : Buildable<SelectModel> {
4241
}
4342

4443
override fun build(): SelectModel =
45-
try {
46-
selectBuilder!!.build()
47-
} catch (e: NullPointerException) {
48-
throw KInvalidSQLException(Messages.getString("ERROR.28")) //$NON-NLS-1$
49-
}
44+
selectBuilder?.build()?: throw KInvalidSQLException(Messages.getString("ERROR.28")) //$NON-NLS-1$
5045
}
5146

5247
class KotlinSubQueryBuilder : KotlinBaseSubQueryBuilder()
@@ -62,10 +57,7 @@ class KotlinQualifiedSubQueryBuilder : KotlinBaseSubQueryBuilder() {
6257
typealias InsertSelectCompleter = KotlinInsertSelectSubQueryBuilder.() -> Unit
6358

6459
class KotlinInsertSelectSubQueryBuilder : KotlinBaseSubQueryBuilder() {
65-
private var columnList: List<SqlColumn<*>>? = null
66-
67-
internal fun columnList(): List<SqlColumn<*>> =
68-
columnList?: throw KInvalidSQLException(Messages.getString("ERROR.29")) //$NON-NLS-1$
60+
internal var columnList: List<SqlColumn<*>>? = null
6961

7062
fun columns(vararg columnList: SqlColumn<*>): Unit = columns(columnList.asList())
7163

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,16 @@ fun <T : Any> insertMultiple(rows: Collection<T>, completer: KotlinMultiRowInser
7979

8080
fun insertSelect(table: SqlTable, completer: InsertSelectCompleter): InsertSelectModel =
8181
with(KotlinInsertSelectSubQueryBuilder().apply(completer)) {
82-
SqlBuilder.insertInto(table)
83-
.withColumnList(columnList())
84-
.withSelectStatement(this)
85-
.build()
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+
}
8692
}
8793

8894
@Deprecated("Please switch to the insertBatch statement in the model package")

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,3 @@ 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=You must specify a column list in an insert select statement

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import examples.kotlin.spring.canonical.PersonDynamicSqlSupport.occupation
2828
import org.assertj.core.api.Assertions.assertThat
2929
import org.assertj.core.api.Assertions.assertThatExceptionOfType
3030
import org.junit.jupiter.api.Test
31+
import org.mybatis.dynamic.sql.exception.InvalidSqlException
3132
import org.mybatis.dynamic.sql.util.Messages
3233
import org.mybatis.dynamic.sql.util.kotlin.KInvalidSQLException
3334
import org.mybatis.dynamic.sql.util.kotlin.elements.`as`
@@ -548,14 +549,34 @@ open class CanonicalSpringKotlinTest {
548549

549550
@Test
550551
fun testInsertSelectNoColumns() {
551-
assertThatExceptionOfType(KInvalidSQLException::class.java).isThrownBy {
552+
val insertStatement = insertSelect(person) {
553+
select(add(id, constant<Int>("100")), firstName, lastName, birthDate, employed, occupation, addressId) {
554+
from(person)
555+
orderBy(id)
556+
}
557+
}
558+
559+
assertThat(insertStatement.insertStatement).isEqualTo(
560+
"insert into Person " +
561+
"select (id + 100), first_name, last_name, birth_date, employed, occupation, address_id " +
562+
"from Person " +
563+
"order by id"
564+
)
565+
val rows = template.insertSelect(insertStatement)
566+
assertThat(rows).isEqualTo(6)
567+
}
568+
569+
@Test
570+
fun testInsertSelectEmptyColumnList() {
571+
assertThatExceptionOfType(InvalidSqlException::class.java).isThrownBy {
552572
insertSelect(person) {
573+
columns()
553574
select(add(id, constant<Int>("100")), firstName, lastName, birthDate, employed, occupation, addressId) {
554575
from(person)
555576
orderBy(id)
556577
}
557578
}
558-
}.withMessage(Messages.getString("ERROR.29")) //$NON-NLS-1$
579+
}.withMessage(Messages.getString("ERROR.4")) //$NON-NLS-1$
559580
}
560581

561582
@Test

0 commit comments

Comments
 (0)