Skip to content

Commit 0b47e49

Browse files
committed
Kotlin support and test coverage
1 parent 4987a07 commit 0b47e49

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.mybatis.dynamic.sql.util.kotlin
1818
import org.mybatis.dynamic.sql.BasicColumn
1919
import org.mybatis.dynamic.sql.SortSpecification
2020
import org.mybatis.dynamic.sql.SqlBuilder
21+
import org.mybatis.dynamic.sql.configuration.StatementConfiguration
2122
import org.mybatis.dynamic.sql.select.MultiSelectDSL
2223
import org.mybatis.dynamic.sql.select.MultiSelectModel
2324
import org.mybatis.dynamic.sql.util.Buildable
@@ -74,6 +75,10 @@ class KotlinMultiSelectBuilder: Buildable<MultiSelectModel> {
7475
getDsl().fetchFirst(fetchFirstRows).rowsOnly()
7576
}
7677

78+
fun configureStatement(c: StatementConfiguration.() -> Unit) {
79+
getDsl().configureStatement(c)
80+
}
81+
7782
override fun build(): MultiSelectModel =
7883
getDsl().build()
7984

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.mybatis.dynamic.sql.BasicColumn
1919
import org.mybatis.dynamic.sql.SqlBuilder
2020
import org.mybatis.dynamic.sql.SqlColumn
2121
import org.mybatis.dynamic.sql.SqlTable
22+
import org.mybatis.dynamic.sql.configuration.StatementConfiguration
2223
import org.mybatis.dynamic.sql.insert.InsertSelectModel
2324
import org.mybatis.dynamic.sql.select.SelectModel
2425
import org.mybatis.dynamic.sql.util.Buildable
@@ -63,6 +64,7 @@ typealias InsertSelectCompleter = KotlinInsertSelectSubQueryBuilder.() -> Unit
6364
class KotlinInsertSelectSubQueryBuilder : KotlinBaseSubQueryBuilder(), Buildable<InsertSelectModel> {
6465
private var columnList: List<SqlColumn<*>>? = null
6566
private var table: SqlTable? = null
67+
private var statementConfigurator: (StatementConfiguration.() -> Unit)? = null
6668

6769
fun into(table: SqlTable) {
6870
this.table = table
@@ -74,18 +76,24 @@ class KotlinInsertSelectSubQueryBuilder : KotlinBaseSubQueryBuilder(), Buildable
7476
this.columnList = columnList
7577
}
7678

79+
fun configureStatement(c: StatementConfiguration.() -> Unit) {
80+
statementConfigurator = c
81+
}
82+
7783
override fun build(): InsertSelectModel {
7884
assertNotNull(table, "ERROR.29") //$NON-NLS-1$
7985

80-
return if (columnList == null) {
86+
val dsl = if (columnList == null) {
8187
SqlBuilder.insertInto(table)
8288
.withSelectStatement { buildSelectModel() }
83-
.build()
8489
} else {
8590
SqlBuilder.insertInto(table)
8691
.withColumnList(columnList)
8792
.withSelectStatement { buildSelectModel() }
88-
.build()
8993
}
94+
95+
statementConfigurator?.let { dsl.configureStatement(it) }
96+
97+
return dsl.build()
9098
}
9199
}

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.person
2828
import org.apache.ibatis.session.ExecutorType
2929
import org.apache.ibatis.session.SqlSessionFactory
3030
import org.assertj.core.api.Assertions.assertThat
31+
import org.assertj.core.api.Assertions.assertThatExceptionOfType
3132
import org.junit.jupiter.api.BeforeAll
3233
import org.junit.jupiter.api.Test
3334
import org.junit.jupiter.api.TestInstance
3435
import org.junit.jupiter.api.TestInstance.Lifecycle
36+
import org.mybatis.dynamic.sql.exception.NonRenderingWhereClauseException
3537
import org.mybatis.dynamic.sql.util.kotlin.elements.add
3638
import org.mybatis.dynamic.sql.util.kotlin.elements.constant
3739
import org.mybatis.dynamic.sql.util.kotlin.elements.isIn
@@ -824,4 +826,83 @@ class PersonMapperTest {
824826
}
825827
}
826828
}
829+
830+
@Test
831+
fun testMultiSelectWithNonRenderingWhereClauseDisAllowed() {
832+
assertThatExceptionOfType(NonRenderingWhereClauseException::class.java).isThrownBy {
833+
multiSelect {
834+
select(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId) {
835+
from(person)
836+
where { id isLessThanOrEqualTo 2 }
837+
orderBy(id)
838+
limit(1)
839+
}
840+
union {
841+
select(id.`as`("A_ID"), firstName, lastName, birthDate, employed, occupation, addressId) {
842+
from(person)
843+
where { id isGreaterThanOrEqualToWhenPresent null }
844+
orderBy(id.descending())
845+
limit(1)
846+
}
847+
}
848+
orderBy(sortColumn("A_ID"))
849+
limit(2)
850+
offset(1)
851+
}
852+
}
853+
}
854+
855+
@Test
856+
fun testMultiSelectWithNonRenderingWhereClauseAllowed() {
857+
val selectStatement = multiSelect {
858+
select(id, firstName) {
859+
from(person)
860+
where { id isLessThanOrEqualTo 2 }
861+
}
862+
union {
863+
select(id, firstName) {
864+
from(person)
865+
where { id isGreaterThanOrEqualToWhenPresent null }
866+
// following should be ignored in favor of the statement configuration...
867+
configureStatement { isNonRenderingWhereClauseAllowed = false }
868+
}
869+
}
870+
configureStatement { isNonRenderingWhereClauseAllowed = true }
871+
}
872+
873+
val expected = "(select id, first_name from Person where id <= #{parameters.p1,jdbcType=INTEGER}) " +
874+
"union (select id, first_name from Person)"
875+
assertThat(selectStatement.selectStatement).isEqualTo(expected)
876+
}
877+
878+
@Test
879+
fun testInsertSelectWithNonRenderingWhereClauseDisAllowed() {
880+
assertThatExceptionOfType(NonRenderingWhereClauseException::class.java).isThrownBy {
881+
insertSelect {
882+
into(person)
883+
select(id, firstName, lastName, birthDate, employed, occupation, addressId) {
884+
from(person)
885+
where { id isGreaterThanOrEqualToWhenPresent null }
886+
}
887+
}
888+
}
889+
}
890+
891+
@Test
892+
fun testInsertSelectWithNonRenderingWhereClauseAllowed() {
893+
val insertStatement = insertSelect {
894+
into(person)
895+
select(id, firstName, lastName, birthDate, employed, occupation, addressId) {
896+
from(person)
897+
where { id isGreaterThanOrEqualToWhenPresent null }
898+
// following should be ignored in favor of the statement configuration...
899+
configureStatement { isNonRenderingWhereClauseAllowed = false }
900+
}
901+
configureStatement { isNonRenderingWhereClauseAllowed = true }
902+
}
903+
904+
val expected = "insert into Person " +
905+
"select id, first_name, last_name, birth_date, employed, occupation, address_id from Person"
906+
assertThat(insertStatement.insertStatement).isEqualTo(expected)
907+
}
827908
}

0 commit comments

Comments
 (0)