Skip to content

Commit 2f3db58

Browse files
committed
Fix the awkward single row insert for Kotlin
1 parent 6a57b76 commit 2f3db58

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.mybatis.dynamic.sql.insert.InsertModel
2424
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL
2525
import org.mybatis.dynamic.sql.insert.MultiRowInsertModel
2626
import org.mybatis.dynamic.sql.util.Buildable
27+
import org.mybatis.dynamic.sql.util.kotlin.spring.insert
2728
import org.mybatis.dynamic.sql.util.kotlin.spring.insertMultiple
2829
import org.mybatis.dynamic.sql.util.kotlin.spring.into
2930
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
@@ -40,3 +41,8 @@ class MultiRowInsertHelper<T>(private val records: List<T>, private val template
4041
template.insertMultiple(SqlBuilder.insertMultiple(records).into(table, completer))
4142
}
4243

44+
@MyBatisDslMarker
45+
class SingleRowInsertHelper<T>(private val record: T, private val template: NamedParameterJdbcTemplate) {
46+
fun into(table: SqlTable, completer: InsertCompleter<T>) =
47+
template.insert(SqlBuilder.insert(record).into(table, completer))
48+
}

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/NamedParameterJdbcTemplateExtensions.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import org.mybatis.dynamic.sql.util.kotlin.GeneralInsertCompleter
3333
import org.mybatis.dynamic.sql.util.kotlin.InsertCompleter
3434
import org.mybatis.dynamic.sql.util.kotlin.MultiRowInsertHelper
3535
import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter
36+
import org.mybatis.dynamic.sql.util.kotlin.SingleRowInsertHelper
3637
import org.mybatis.dynamic.sql.util.kotlin.UpdateCompleter
3738
import org.springframework.dao.EmptyResultDataAccessException
3839
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource
@@ -57,18 +58,28 @@ fun NamedParameterJdbcTemplate.delete(deleteStatement: DeleteStatementProvider)
5758
fun NamedParameterJdbcTemplate.deleteFrom(table: SqlTable, completer: DeleteCompleter) =
5859
delete(org.mybatis.dynamic.sql.util.kotlin.spring.deleteFrom(table, completer))
5960

61+
// single record insert
6062
fun <T> NamedParameterJdbcTemplate.insert(insertStatement: InsertStatementProvider<T>) =
6163
update(insertStatement.insertStatement, BeanPropertySqlParameterSource(insertStatement.record))
6264

65+
fun <T> NamedParameterJdbcTemplate.insert(record: T) =
66+
SingleRowInsertHelper(record, this)
67+
68+
@Deprecated(
69+
message = "Deprecated for being awkward and inconsistent",
70+
replaceWith = ReplaceWith("insert(record).into(table, completer)")
71+
)
6372
fun <T> NamedParameterJdbcTemplate.insert(record: T, table: SqlTable, completer: InsertCompleter<T>) =
6473
insert(SqlBuilder.insert(record).into(table, completer))
6574

75+
// general insert
6676
fun NamedParameterJdbcTemplate.insert(insertStatement: GeneralInsertStatementProvider) =
6777
update(insertStatement.insertStatement, insertStatement.parameters)
6878

6979
fun NamedParameterJdbcTemplate.insertInto(table: SqlTable, completer: GeneralInsertCompleter) =
7080
insert(org.mybatis.dynamic.sql.util.kotlin.spring.insertInto(table, completer))
7181

82+
// multiple record insert
7283
fun <T> NamedParameterJdbcTemplate.insertMultiple(vararg records: T) =
7384
insertMultiple(records.asList())
7485

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class CanonicalSpringKotlinTemplateDirectTest {
158158
}
159159

160160
@Test
161-
fun testInsert() {
161+
fun testDeprecatedInsert() {
162162
val record = PersonRecord(100, "Joe", LastName("Jones"), Date(), true, "Developer", 1)
163163

164164
val rows = template.insert(record, Person) {
@@ -174,6 +174,23 @@ class CanonicalSpringKotlinTemplateDirectTest {
174174
assertThat(rows).isEqualTo(1)
175175
}
176176

177+
@Test
178+
fun testInsert() {
179+
val record = PersonRecord(100, "Joe", LastName("Jones"), Date(), true, "Developer", 1)
180+
181+
val rows = template.insert(record).into(Person) {
182+
map(id).toProperty("id")
183+
map(firstName).toProperty("firstName")
184+
map(lastName).toProperty("lastNameAsString")
185+
map(birthDate).toProperty("birthDate")
186+
map(employed).toProperty("employedAsString")
187+
map(occupation).toPropertyWhenPresent("occupation", record::occupation)
188+
map(addressId).toProperty("addressId")
189+
}
190+
191+
assertThat(rows).isEqualTo(1)
192+
}
193+
177194
@Test
178195
fun testGeneralInsert() {
179196
val rows = template.insertInto(Person) {

0 commit comments

Comments
 (0)