Skip to content

Commit ad4448c

Browse files
committed
Coverage and Style
1 parent c13c9ae commit ad4448c

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

src/main/java/org/mybatis/dynamic/sql/util/ValueOrNullMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util;
1717

18-
import org.mybatis.dynamic.sql.SqlColumn;
19-
2018
import java.util.Objects;
2119
import java.util.Optional;
2220
import java.util.function.Supplier;
2321

22+
import org.mybatis.dynamic.sql.SqlColumn;
23+
2424
public class ValueOrNullMapping<T> extends AbstractColumnMapping {
2525

2626
private final Supplier<T> valueSupplier;

src/test/java/examples/animal/data/AnimalDataTest.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,46 @@ void testUpdate() {
14061406
}
14071407
}
14081408

1409+
@Test
1410+
void testUpdateValueOrNullWithValue() {
1411+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
1412+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
1413+
AnimalData record = new AnimalData();
1414+
record.setBodyWeight(2.6);
1415+
1416+
UpdateStatementProvider updateStatement = update(animalData)
1417+
.set(animalName).equalToOrNull("fred")
1418+
.where(id, isEqualTo(1))
1419+
.build()
1420+
.render(RenderingStrategies.MYBATIS3);
1421+
1422+
assertThat(updateStatement.getUpdateStatement()).isEqualTo(
1423+
"update AnimalData set animal_name = #{parameters.p1,jdbcType=VARCHAR} where id = #{parameters.p2,jdbcType=INTEGER}");
1424+
int rows = mapper.update(updateStatement);
1425+
assertThat(rows).isEqualTo(1);
1426+
}
1427+
}
1428+
1429+
@Test
1430+
void testUpdateValueOrNullWithNull() {
1431+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
1432+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
1433+
AnimalData record = new AnimalData();
1434+
record.setBodyWeight(2.6);
1435+
1436+
UpdateStatementProvider updateStatement = update(animalData)
1437+
.set(animalName).equalToOrNull((String) null)
1438+
.where(id, isEqualTo(1))
1439+
.build()
1440+
.render(RenderingStrategies.MYBATIS3);
1441+
1442+
assertThat(updateStatement.getUpdateStatement()).isEqualTo(
1443+
"update AnimalData set animal_name = null where id = #{parameters.p1,jdbcType=INTEGER}");
1444+
int rows = mapper.update(updateStatement);
1445+
assertThat(rows).isEqualTo(1);
1446+
}
1447+
}
1448+
14091449
@Test
14101450
void testInsert() {
14111451
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
@@ -1994,6 +2034,61 @@ void testGeneralInsert() {
19942034
}
19952035
}
19962036

2037+
@Test
2038+
void testGeneralInsertValueOrNullWithValue() {
2039+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
2040+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
2041+
2042+
GeneralInsertStatementProvider insertStatement = insertInto(animalData)
2043+
.set(id).toValue(101)
2044+
.set(animalName).toValueOrNull("Fred")
2045+
.set(brainWeight).toConstant("2.2")
2046+
.set(bodyWeight).toValue(4.5)
2047+
.build()
2048+
.render(RenderingStrategies.MYBATIS3);
2049+
2050+
String expected = "insert into AnimalData (id, animal_name, brain_weight, body_weight) "
2051+
+ "values (#{parameters.p1,jdbcType=INTEGER}, #{parameters.p2,jdbcType=VARCHAR}, 2.2, "
2052+
+ "#{parameters.p3,jdbcType=DOUBLE})";
2053+
2054+
assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
2055+
assertThat(insertStatement.getParameters()).hasSize(3);
2056+
assertThat(insertStatement.getParameters()).containsEntry("p1", 101);
2057+
assertThat(insertStatement.getParameters()).containsEntry("p2", "Fred");
2058+
assertThat(insertStatement.getParameters()).containsEntry("p3", 4.5);
2059+
2060+
int rows = mapper.generalInsert(insertStatement);
2061+
assertThat(rows).isEqualTo(1);
2062+
}
2063+
}
2064+
2065+
@Test
2066+
void testGeneralInsertValueOrNullWithNull() {
2067+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
2068+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
2069+
2070+
GeneralInsertStatementProvider insertStatement = insertInto(animalData)
2071+
.set(id).toValue(101)
2072+
.set(animalName).toValueOrNull((String) null)
2073+
.set(brainWeight).toConstant("2.2")
2074+
.set(bodyWeight).toValue(4.5)
2075+
.build()
2076+
.render(RenderingStrategies.MYBATIS3);
2077+
2078+
String expected = "insert into AnimalData (id, animal_name, brain_weight, body_weight) "
2079+
+ "values (#{parameters.p1,jdbcType=INTEGER}, null, 2.2, "
2080+
+ "#{parameters.p2,jdbcType=DOUBLE})";
2081+
2082+
assertThat(insertStatement.getInsertStatement()).isEqualTo(expected);
2083+
assertThat(insertStatement.getParameters()).hasSize(2);
2084+
assertThat(insertStatement.getParameters()).containsEntry("p1", 101);
2085+
assertThat(insertStatement.getParameters()).containsEntry("p2", 4.5);
2086+
2087+
int rows = mapper.generalInsert(insertStatement);
2088+
assertThat(rows).isEqualTo(1);
2089+
}
2090+
}
2091+
19972092
@Test
19982093
void testUpdateWithSelect() {
19992094
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

src/test/java/org/mybatis/dynamic/sql/util/ColumnMappingVisitorTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ void testThatInsertVisitorErrorsForValueMapping() {
8787
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
8888
}
8989

90+
@Test
91+
void testThatInsertVisitorErrorsForValueOrNullMapping() {
92+
TestTable table = new TestTable();
93+
InsertVisitor tv = new InsertVisitor();
94+
ValueOrNullMapping<Integer> mapping = ValueOrNullMapping.of(table.id, () -> 3);
95+
96+
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping));
97+
}
98+
9099
@Test
91100
void testThatInsertVisitorErrorsForValueWhenPresentMapping() {
92101
TestTable table = new TestTable();
@@ -226,7 +235,7 @@ public String visit(PropertyMapping mapping) {
226235

227236
@Override
228237
public String visit(PropertyWhenPresentMapping mapping) {
229-
return "Property Whn Present Mapping";
238+
return "Property When Present Mapping";
230239
}
231240
}
232241

0 commit comments

Comments
 (0)