Skip to content

Commit 0b39d41

Browse files
committed
Make sure the type converters work with nulls
1 parent d228b8d commit 0b39d41

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,4 +925,36 @@ class CanonicalSpringKotlinTest {
925925

926926
assertThat(rows).isEqualTo(2)
927927
}
928+
929+
@Test
930+
fun testUpdateWithTypeConverterAndNullValue() {
931+
val record = PersonRecord(id = 3, firstName = "Sam")
932+
933+
val updateStatement = update(Person) {
934+
set(firstName).equalTo(record::firstName)
935+
set(lastName).equalTo(record::lastName)
936+
where(id, isEqualTo(record::id))
937+
}
938+
939+
assertThat(updateStatement.updateStatement).isEqualTo(
940+
"update Person" +
941+
" set first_name = :p1," +
942+
" last_name = :p2" +
943+
" where id = :p3"
944+
)
945+
946+
val rows = template.update(updateStatement)
947+
948+
assertThat(rows).isEqualTo(1)
949+
950+
val selectStatement = select(
951+
id, firstName, lastName, birthDate, employed, occupation, addressId
952+
).from(Person) {
953+
where(id, isEqualTo(record::id))
954+
}
955+
956+
val returnedRecord = template.selectOne(selectStatement, ::personRowMapper)
957+
assertThat(returnedRecord).isNotNull()
958+
assertThat(returnedRecord!!.lastName).isNull()
959+
}
928960
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ package examples.kotlin.spring.canonical
1717

1818
import java.sql.ResultSet
1919

20-
fun personRowMapper(rs: ResultSet, rowNum : Int) =
20+
fun personRowMapper(rs: ResultSet, rowNum: Int) =
2121
PersonRecord().apply {
2222
id = rs.getInt(1)
2323
firstName = rs.getString(2)
24-
lastName = LastName(rs.getString(3))
24+
lastName = rs.getString(3)?.let { LastName(it) }
2525
birthDate = rs.getTimestamp(4)
2626
employed = "Yes" == rs.getString(5)
2727
occupation = rs.getString(6)
@@ -32,7 +32,7 @@ fun personWithAddressRowMapper(rs: ResultSet, rowNum: Int) =
3232
PersonWithAddress().apply {
3333
id = rs.getInt(1)
3434
firstName = rs.getString(2)
35-
lastName = LastName(rs.getString(3))
35+
lastName = rs.getString(3)?.let { LastName(it) }
3636
birthDate = rs.getTimestamp(4)
3737
employed = "Yes" == rs.getString(5)
3838
occupation = rs.getString(6)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ create table Address (
2828
create table Person (
2929
id int not null,
3030
first_name varchar(30) not null,
31-
last_name varchar(30) not null,
31+
last_name varchar(30) null,
3232
birth_date date not null,
3333
employed varchar(3) not null,
3434
occupation varchar(30) null,

0 commit comments

Comments
 (0)