Skip to content

Commit 384d32b

Browse files
committed
Build out Kotlin support for limit and order by in delete and update statements
1 parent 37696a4 commit 384d32b

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util.kotlin
1717

18+
import org.mybatis.dynamic.sql.SortSpecification
1819
import org.mybatis.dynamic.sql.delete.DeleteDSL
1920
import org.mybatis.dynamic.sql.delete.DeleteModel
2021
import org.mybatis.dynamic.sql.util.Buildable
@@ -24,6 +25,10 @@ typealias DeleteCompleter = KotlinDeleteBuilder.() -> Unit
2425
class KotlinDeleteBuilder(private val dsl: DeleteDSL<DeleteModel>) :
2526
KotlinBaseBuilder<DeleteDSL<DeleteModel>>(), Buildable<DeleteModel> {
2627

28+
fun orderBy(vararg columns: SortSpecification) {
29+
dsl.orderBy(columns.toList())
30+
}
31+
2732
fun limit(limit: Long) {
2833
dsl.limit(limit)
2934
}

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

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

1818
import org.mybatis.dynamic.sql.BasicColumn
19+
import org.mybatis.dynamic.sql.SortSpecification
1920
import org.mybatis.dynamic.sql.SqlColumn
2021
import org.mybatis.dynamic.sql.update.UpdateDSL
2122
import org.mybatis.dynamic.sql.update.UpdateModel
@@ -28,6 +29,14 @@ class KotlinUpdateBuilder(private val dsl: UpdateDSL<UpdateModel>) :
2829

2930
fun <T> set(column: SqlColumn<T>): KotlinSetClauseFinisher<T> = KotlinSetClauseFinisher(column)
3031

32+
fun orderBy(vararg columns: SortSpecification) {
33+
dsl.orderBy(columns.toList())
34+
}
35+
36+
fun limit(limit: Long) {
37+
dsl.limit(limit)
38+
}
39+
3140
override fun build(): UpdateModel = dsl.build()
3241

3342
override fun getDsl(): UpdateDSL<UpdateModel> = dsl

src/test/kotlin/examples/kotlin/mybatis3/mariadb/KItemsDynamicSQLSupport.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ object KItemsDynamicSQLSupport {
2323
val items = Items()
2424
val id = items.id
2525
val description = items.description
26+
val amount = items.amount
2627

2728
class Items : SqlTable("items") {
2829
val id = column<Int>(name = "id", jdbcType = JDBCType.INTEGER)
2930
val description = column<String>(name = "description", jdbcType = JDBCType.VARCHAR)
31+
val amount = column<Int>(name = "amount", jdbcType = JDBCType.INTEGER)
3032
}
3133
}

src/test/kotlin/examples/kotlin/mybatis3/mariadb/KMariaDBTest.kt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package examples.kotlin.mybatis3.mariadb
1717

1818
import config.TestContainersConfiguration
19+
import examples.kotlin.mybatis3.mariadb.KItemsDynamicSQLSupport.amount
1920
import examples.kotlin.mybatis3.mariadb.KItemsDynamicSQLSupport.id
2021
import examples.kotlin.mybatis3.mariadb.KItemsDynamicSQLSupport.items
2122
import examples.kotlin.mybatis3.mariadb.KItemsDynamicSQLSupport.description
@@ -30,10 +31,14 @@ import org.assertj.core.api.Assertions.assertThat
3031
import org.junit.jupiter.api.BeforeAll
3132
import org.junit.jupiter.api.Test
3233
import org.junit.jupiter.api.TestInstance
34+
import org.mybatis.dynamic.sql.util.kotlin.elements.add
35+
import org.mybatis.dynamic.sql.util.kotlin.elements.constant
3336
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.deleteFrom
3437
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
38+
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.update
3539
import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper
3640
import org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper
41+
import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper
3742
import org.testcontainers.containers.MariaDBContainer
3843
import org.testcontainers.junit.jupiter.Container
3944
import org.testcontainers.junit.jupiter.Testcontainers
@@ -56,6 +61,7 @@ class KMariaDBTest {
5661
with(Configuration(environment)) {
5762
addMapper(CommonDeleteMapper::class.java)
5863
addMapper(CommonSelectMapper::class.java)
64+
addMapper(CommonUpdateMapper::class.java)
5965
sqlSessionFactory = SqlSessionFactoryBuilder().build(this)
6066
}
6167
}
@@ -92,6 +98,58 @@ class KMariaDBTest {
9298
}
9399
}
94100

101+
@Test
102+
fun testDeleteWithOrderBy() {
103+
newSession().use {
104+
val mapper = it.getMapper(CommonDeleteMapper::class.java)
105+
106+
val deleteStatement = deleteFrom(items) {
107+
orderBy(id)
108+
}
109+
110+
assertThat(deleteStatement.deleteStatement).isEqualTo("delete from items order by id")
111+
112+
val rows = mapper.delete(deleteStatement)
113+
assertThat(rows).isEqualTo(20)
114+
}
115+
}
116+
117+
@Test
118+
fun testUpdateWithLimit() {
119+
newSession().use {
120+
val mapper = it.getMapper(CommonUpdateMapper::class.java)
121+
122+
val updateStatement = update(items) {
123+
set(amount) equalTo add(amount, constant<Int>("100"))
124+
limit(4)
125+
}
126+
127+
assertThat(updateStatement.updateStatement)
128+
.isEqualTo("update items set amount = (amount + 100) limit #{parameters.p1}")
129+
130+
val rows = mapper.update(updateStatement)
131+
assertThat(rows).isEqualTo(4)
132+
}
133+
}
134+
135+
@Test
136+
fun testUpdateWithOrderBy() {
137+
newSession().use {
138+
val mapper = it.getMapper(CommonUpdateMapper::class.java)
139+
140+
val updateStatement = update(items) {
141+
set(amount) equalTo add(amount, constant<Int>("100"))
142+
orderBy(id)
143+
}
144+
145+
assertThat(updateStatement.updateStatement)
146+
.isEqualTo("update items set amount = (amount + 100) order by id")
147+
148+
val rows = mapper.update(updateStatement)
149+
assertThat(rows).isEqualTo(20)
150+
}
151+
}
152+
95153
companion object {
96154
@Container
97155
private val mariadb = MariaDBContainer(TestContainersConfiguration.MARIADB_LATEST)

0 commit comments

Comments
 (0)