Skip to content

Commit 8cef8a9

Browse files
committed
Kotlin Support for Map to Row
1 parent d77debd commit 8cef8a9

File tree

6 files changed

+169
-0
lines changed

6 files changed

+169
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.mybatis.dynamic.sql.util.ConstantMapping
2121
import org.mybatis.dynamic.sql.util.NullMapping
2222
import org.mybatis.dynamic.sql.util.PropertyMapping
2323
import org.mybatis.dynamic.sql.util.PropertyWhenPresentMapping
24+
import org.mybatis.dynamic.sql.util.RowMapping
2425
import org.mybatis.dynamic.sql.util.StringConstantMapping
2526
import org.mybatis.dynamic.sql.util.ValueMapping
2627
import org.mybatis.dynamic.sql.util.ValueOrNullMapping
@@ -44,6 +45,8 @@ class MultiRowInsertColumnMapCompleter<T>(
4445
: AbstractInsertColumnMapCompleter<T>(column, mappingConsumer) {
4546

4647
infix fun toProperty(property: String) = mappingConsumer.invoke(PropertyMapping.of(column, property))
48+
49+
fun toRow() = mappingConsumer.invoke(RowMapping.of(column))
4750
}
4851

4952
class SingleRowInsertColumnMapCompleter<T>(
@@ -55,6 +58,8 @@ class SingleRowInsertColumnMapCompleter<T>(
5558

5659
fun toPropertyWhenPresent(property: String, valueSupplier: () -> T?) =
5760
mappingConsumer.invoke(PropertyWhenPresentMapping.of(column, property, valueSupplier))
61+
62+
fun toRow() = mappingConsumer.invoke(RowMapping.of(column))
5863
}
5964

6065
class GeneralInsertColumnSetCompleter<T>(
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2016-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package examples.kotlin.spring.canonical
17+
18+
import org.mybatis.dynamic.sql.SqlTable
19+
import org.mybatis.dynamic.sql.util.kotlin.elements.column
20+
21+
object CompoundKeyDynamicSqlSupport {
22+
val compoundKey = CompoundKey()
23+
val id1 = compoundKey.id1
24+
val id2 = compoundKey.id2
25+
26+
class CompoundKey : SqlTable("CompoundKey") {
27+
val id1 = column<Int>(name = "id1")
28+
val id2 = column<Int>(name = "id2")
29+
}
30+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ data class GeneratedAlwaysRecord(
6767
var lastName: String?,
6868
var fullName: String?
6969
)
70+
71+
data class CompoundKeyRow(
72+
var id1: Int,
73+
var id2: Int
74+
)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ val personWithAddressRowMapper: (ResultSet, Int) -> PersonWithAddress = { rs, _
5555
)
5656
)
5757
}
58+
59+
val compoundKeyRowMapper: (ResultSet, Int) -> CompoundKeyRow = { rs, _ ->
60+
CompoundKeyRow(
61+
id1 = rs.getInt(1),
62+
id2 = rs.getInt(2)
63+
)
64+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2016-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package examples.kotlin.spring.canonical
17+
18+
import examples.kotlin.spring.canonical.CompoundKeyDynamicSqlSupport.compoundKey
19+
import examples.kotlin.spring.canonical.CompoundKeyDynamicSqlSupport.id1
20+
import examples.kotlin.spring.canonical.CompoundKeyDynamicSqlSupport.id2
21+
import org.assertj.core.api.Assertions.assertThat
22+
import org.junit.jupiter.api.Test
23+
import org.mybatis.dynamic.sql.util.kotlin.spring.insert
24+
import org.mybatis.dynamic.sql.util.kotlin.spring.insertBatch
25+
import org.mybatis.dynamic.sql.util.kotlin.spring.insertMultiple
26+
import org.mybatis.dynamic.sql.util.kotlin.spring.select
27+
import org.mybatis.dynamic.sql.util.kotlin.spring.selectList
28+
import org.springframework.beans.factory.annotation.Autowired
29+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
30+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig
31+
import org.springframework.transaction.annotation.Transactional
32+
33+
@SpringJUnitConfig(classes = [SpringConfiguration::class])
34+
@Transactional
35+
open class SpringKotlinMapToRowTest {
36+
@Autowired
37+
private lateinit var template: NamedParameterJdbcTemplate
38+
39+
@Test
40+
fun testInsertOne() {
41+
val i = 1
42+
43+
val insertStatement = insert(i) {
44+
into(compoundKey)
45+
map(id1).toConstant("22")
46+
map(id2).toRow()
47+
}
48+
49+
val expected = "insert into CompoundKey (id1, id2) values (22, :row)"
50+
assertThat(insertStatement.insertStatement).isEqualTo(expected)
51+
52+
val rows = template.insert(insertStatement)
53+
assertThat(rows).isEqualTo(1)
54+
55+
val selectStatement = select(id1, id2) {
56+
from(compoundKey)
57+
orderBy(id1, id2)
58+
}
59+
60+
val records = template.selectList(selectStatement, compoundKeyRowMapper)
61+
assertThat(records).hasSize(1)
62+
}
63+
64+
@Test
65+
open fun testInsertMultiple() {
66+
val integers = listOf(1, 2, 3)
67+
68+
val insertStatement = insertMultiple(integers) {
69+
into(compoundKey)
70+
map(id1).toConstant("22")
71+
map(id2).toRow()
72+
}
73+
74+
val expected =
75+
"insert into CompoundKey (id1, id2) values (22, :records[0]), (22, :records[1]), (22, :records[2])"
76+
assertThat(insertStatement.insertStatement).isEqualTo(expected)
77+
78+
val rows = template.insertMultiple(insertStatement)
79+
assertThat(rows).isEqualTo(3)
80+
81+
val selectStatement = select(id1, id2) {
82+
from(compoundKey)
83+
orderBy(id1, id2)
84+
}
85+
86+
val records = template.selectList(selectStatement, compoundKeyRowMapper)
87+
assertThat(records).hasSize(3)
88+
}
89+
90+
@Test
91+
open fun testInsertBatch() {
92+
val integers = listOf(1, 2, 3)
93+
94+
val insertStatement = insertBatch(integers) {
95+
into(compoundKey)
96+
map(id1).toConstant("22")
97+
map(id2).toRow()
98+
}
99+
100+
val expected = "insert into CompoundKey (id1, id2) values (22, :row)"
101+
assertThat(insertStatement.insertStatementSQL).isEqualTo(expected)
102+
103+
val rowCounts = template.insertBatch(insertStatement)
104+
assertThat(rowCounts).hasSize(3)
105+
assertThat(rowCounts.sum()).isEqualTo(3)
106+
107+
val selectStatement = select(id1, id2) {
108+
from(compoundKey)
109+
orderBy(id1, id2)
110+
}
111+
112+
val records = template.selectList(selectStatement, compoundKeyRowMapper)
113+
assertThat(records).hasSize(3)
114+
}
115+
}

src/test/resources/examples/kotlin/spring/CreateSimpleDB.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
drop table Address if exists;
1818
drop table Person if exists;
19+
drop table CompoundKey if exists;
1920

2021
create table Address (
2122
address_id int not null,
@@ -36,6 +37,12 @@ create table Person (
3637
primary key(id)
3738
);
3839

40+
create table CompoundKey (
41+
id1 int not null,
42+
id2 int not null,
43+
primary key (id1, id2)
44+
);
45+
3946
insert into Address (address_id, street_address, city, state)
4047
values(1, '123 Main Street', 'Bedrock', 'IN');
4148

0 commit comments

Comments
 (0)