Skip to content

Commit b67ea69

Browse files
authored
Merge pull request #346 from jeffgbutler/allow-nulls
Allow the "in when present" conditions to accept a null Collection
2 parents 68c37d4 + 6bf7bee commit b67ea69

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ Kotlin DSL.
9898
- Refactored the built-in conditions ([#331](https://github.com/mybatis/mybatis-dynamic-sql/pull/331)) ([#336](https://github.com/mybatis/mybatis-dynamic-sql/pull/336))
9999
- Added composition functions for WhereApplier ([#335](https://github.com/mybatis/mybatis-dynamic-sql/pull/335))
100100
- Added a mapping for general insert and update statements that will render null values as "null" in the SQL ([#343](https://github.com/mybatis/mybatis-dynamic-sql/pull/343))
101+
- Allow the "in when present" conditions to accept a null Collection as a parameter ([#346](https://github.com/mybatis/mybatis-dynamic-sql/pull/346))
101102

102103
## Release 1.2.1 - September 29, 2020
103104

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ static <T> IsIn<T> isInWhenPresent(T...values) {
577577
}
578578

579579
static <T> IsIn<T> isInWhenPresent(Collection<T> values) {
580-
return IsIn.of(values).filter(Objects::nonNull);
580+
return values == null ? IsIn.empty() : IsIn.of(values).filter(Objects::nonNull);
581581
}
582582

583583
@SafeVarargs
@@ -599,7 +599,7 @@ static <T> IsNotIn<T> isNotInWhenPresent(T...values) {
599599
}
600600

601601
static <T> IsNotIn<T> isNotInWhenPresent(Collection<T> values) {
602-
return IsNotIn.of(values).filter(Objects::nonNull);
602+
return values == null ? IsNotIn.empty() : IsNotIn.of(values).filter(Objects::nonNull);
603603
}
604604

605605
static <T> IsBetween.Builder<T> isBetween(T value1) {
@@ -722,7 +722,7 @@ static IsInCaseInsensitive isInCaseInsensitiveWhenPresent(String...values) {
722722
}
723723

724724
static IsInCaseInsensitive isInCaseInsensitiveWhenPresent(Collection<String> values) {
725-
return IsInCaseInsensitive.of(values).filter(Objects::nonNull);
725+
return values == null ? IsInCaseInsensitive.empty() : IsInCaseInsensitive.of(values).filter(Objects::nonNull);
726726
}
727727

728728
static IsNotInCaseInsensitive isNotInCaseInsensitive(String...values) {
@@ -738,7 +738,8 @@ static IsNotInCaseInsensitive isNotInCaseInsensitiveWhenPresent(String...values)
738738
}
739739

740740
static IsNotInCaseInsensitive isNotInCaseInsensitiveWhenPresent(Collection<String> values) {
741-
return IsNotInCaseInsensitive.of(values).filter(Objects::nonNull);
741+
return values == null ? IsNotInCaseInsensitive.empty() :
742+
IsNotInCaseInsensitive.of(values).filter(Objects::nonNull);
742743
}
743744

744745
// order by support

src/test/java/org/mybatis/dynamic/sql/select/SelectStatementTest.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import static org.mybatis.dynamic.sql.SqlBuilder.*;
2222

2323
import java.sql.JDBCType;
24+
import java.util.Collection;
2425
import java.util.Collections;
2526
import java.util.Date;
2627
import java.util.List;
@@ -370,4 +371,48 @@ void testNotInCaseInsensitiveWhenPresentEmptyList() {
370371
selectModel.render(RenderingStrategies.MYBATIS3)
371372
).withMessage("Fred");
372373
}
374+
375+
@Test
376+
void testInWhenPresentNullList() {
377+
SelectStatementProvider selectStatement = select(column1, column3)
378+
.from(table)
379+
.where(column3, isInWhenPresent((Collection<String>) null))
380+
.build()
381+
.render(RenderingStrategies.MYBATIS3);
382+
383+
assertThat(selectStatement.getSelectStatement()).isEqualTo("select column1, column3 from foo");
384+
}
385+
386+
@Test
387+
void testInCaseInsensitiveWhenPresentNullList() {
388+
SelectStatementProvider selectStatement = select(column1, column3)
389+
.from(table)
390+
.where(column3, isInCaseInsensitiveWhenPresent((Collection<String>) null))
391+
.build()
392+
.render(RenderingStrategies.MYBATIS3);
393+
394+
assertThat(selectStatement.getSelectStatement()).isEqualTo("select column1, column3 from foo");
395+
}
396+
397+
@Test
398+
void testNotInWhenPresentNullList() {
399+
SelectStatementProvider selectStatement = select(column1, column3)
400+
.from(table)
401+
.where(column3, isNotInWhenPresent((Collection<String>) null))
402+
.build()
403+
.render(RenderingStrategies.MYBATIS3);
404+
405+
assertThat(selectStatement.getSelectStatement()).isEqualTo("select column1, column3 from foo");
406+
}
407+
408+
@Test
409+
void testNotInCaseInsensitiveWhenPresentNullList() {
410+
SelectStatementProvider selectStatement = select(column1, column3)
411+
.from(table)
412+
.where(column3, isNotInCaseInsensitiveWhenPresent((Collection<String>) null))
413+
.build()
414+
.render(RenderingStrategies.MYBATIS3);
415+
416+
assertThat(selectStatement.getSelectStatement()).isEqualTo("select column1, column3 from foo");
417+
}
373418
}

0 commit comments

Comments
 (0)