16
16
package examples.kotlin.mybatis3.mariadb
17
17
18
18
import config.TestContainersConfiguration
19
+ import examples.kotlin.mybatis3.mariadb.KItemsDynamicSQLSupport.amount
19
20
import examples.kotlin.mybatis3.mariadb.KItemsDynamicSQLSupport.id
20
21
import examples.kotlin.mybatis3.mariadb.KItemsDynamicSQLSupport.items
21
22
import examples.kotlin.mybatis3.mariadb.KItemsDynamicSQLSupport.description
@@ -30,10 +31,14 @@ import org.assertj.core.api.Assertions.assertThat
30
31
import org.junit.jupiter.api.BeforeAll
31
32
import org.junit.jupiter.api.Test
32
33
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
33
36
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.deleteFrom
34
37
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.select
38
+ import org.mybatis.dynamic.sql.util.kotlin.mybatis3.update
35
39
import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper
36
40
import org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper
41
+ import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper
37
42
import org.testcontainers.containers.MariaDBContainer
38
43
import org.testcontainers.junit.jupiter.Container
39
44
import org.testcontainers.junit.jupiter.Testcontainers
@@ -56,6 +61,7 @@ class KMariaDBTest {
56
61
with (Configuration (environment)) {
57
62
addMapper(CommonDeleteMapper ::class .java)
58
63
addMapper(CommonSelectMapper ::class .java)
64
+ addMapper(CommonUpdateMapper ::class .java)
59
65
sqlSessionFactory = SqlSessionFactoryBuilder ().build(this )
60
66
}
61
67
}
@@ -92,6 +98,58 @@ class KMariaDBTest {
92
98
}
93
99
}
94
100
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
+
95
153
companion object {
96
154
@Container
97
155
private val mariadb = MariaDBContainer (TestContainersConfiguration .MARIADB_LATEST )
0 commit comments