Skip to content

Commit 8ce5b5b

Browse files
committed
Remove configuration for empty list rendering
With this change, the "in" conditions will always render. The "in when present" conditions will only render if the input collection is non-null and contains at least one non-null value.
1 parent 0c27e97 commit 8ce5b5b

File tree

16 files changed

+72
-131
lines changed

16 files changed

+72
-131
lines changed

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.util.stream.Collectors;
2424
import java.util.stream.Stream;
2525

26-
import org.mybatis.dynamic.sql.render.RenderingContext;
27-
2826
public abstract class AbstractListValueCondition<T> implements VisitableCondition<T> {
2927
protected final Collection<T> values;
3028

@@ -41,15 +39,6 @@ public boolean isEmpty() {
4139
return values.isEmpty();
4240
}
4341

44-
@Override
45-
public boolean shouldRender(RenderingContext renderingContext) {
46-
if (isEmpty()) {
47-
return renderingContext.isEmptyListConditionRenderingAllowed();
48-
} else {
49-
return true;
50-
}
51-
}
52-
5342
@Override
5443
public <R> R accept(ConditionVisitor<T, R> visitor) {
5544
return visitor.visit(this);
@@ -85,14 +74,12 @@ protected <R, S extends AbstractListValueCondition<R>> S mapSupport(Function<? s
8574
}
8675

8776
/**
88-
* If renderable, apply the predicate to each value in the list and return a new condition with the filtered values.
89-
* Else returns a condition that will not render (this). If all values are filtered out of the value
90-
* list, then the condition will not render.
77+
* If not empty, apply the predicate to each value in the list and return a new condition with the filtered values.
78+
* Else returns an empty condition (this).
9179
*
92-
* @param predicate
93-
* predicate applied to the values, if renderable
80+
* @param predicate predicate applied to the values, if not empty
9481
*
95-
* @return a new condition with filtered values if renderable, otherwise a condition that will not render.
82+
* @return a new condition with filtered values if renderable, otherwise an empty condition
9683
*/
9784
public abstract AbstractListValueCondition<T> filter(Predicate<? super T> predicate);
9885

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ static <T> IsInWithSubselect<T> isIn(Buildable<SelectModel> selectModelBuilder)
769769

770770
@SafeVarargs
771771
static <T> IsInWhenPresent<T> isInWhenPresent(T... values) {
772-
return IsInWhenPresent.of(values);
772+
return values == null ? IsInWhenPresent.empty() : IsInWhenPresent.of(values);
773773
}
774774

775775
static <T> IsInWhenPresent<T> isInWhenPresent(Collection<T> values) {
@@ -791,7 +791,7 @@ static <T> IsNotInWithSubselect<T> isNotIn(Buildable<SelectModel> selectModelBui
791791

792792
@SafeVarargs
793793
static <T> IsNotInWhenPresent<T> isNotInWhenPresent(T... values) {
794-
return IsNotInWhenPresent.of(values);
794+
return values == null ? IsNotInWhenPresent.empty() : IsNotInWhenPresent.of(values);
795795
}
796796

797797
static <T> IsNotInWhenPresent<T> isNotInWhenPresent(Collection<T> values) {
@@ -914,7 +914,7 @@ static IsInCaseInsensitive isInCaseInsensitive(Collection<String> values) {
914914
}
915915

916916
static IsInCaseInsensitiveWhenPresent isInCaseInsensitiveWhenPresent(String... values) {
917-
return IsInCaseInsensitiveWhenPresent.of(values);
917+
return values == null ? IsInCaseInsensitiveWhenPresent.empty() : IsInCaseInsensitiveWhenPresent.of(values);
918918
}
919919

920920
static IsInCaseInsensitiveWhenPresent isInCaseInsensitiveWhenPresent(Collection<String> values) {
@@ -930,7 +930,8 @@ static IsNotInCaseInsensitive isNotInCaseInsensitive(Collection<String> values)
930930
}
931931

932932
static IsNotInCaseInsensitiveWhenPresent isNotInCaseInsensitiveWhenPresent(String... values) {
933-
return IsNotInCaseInsensitiveWhenPresent.of(values);
933+
return values == null ? IsNotInCaseInsensitiveWhenPresent.empty() :
934+
IsNotInCaseInsensitiveWhenPresent.of(values);
934935
}
935936

936937
static IsNotInCaseInsensitiveWhenPresent isNotInCaseInsensitiveWhenPresent(Collection<String> values) {

src/main/java/org/mybatis/dynamic/sql/configuration/GlobalConfiguration.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class GlobalConfiguration {
2626
public static final String CONFIGURATION_FILE_PROPERTY = "mybatis-dynamic-sql.configurationFile"; //$NON-NLS-1$
2727
private static final String DEFAULT_PROPERTY_FILE = "mybatis-dynamic-sql.properties"; //$NON-NLS-1$
2828
private boolean isNonRenderingWhereClauseAllowed = false;
29-
private boolean isEmptyListConditionRenderingAllowed = true;
3029
private final Properties properties = new Properties();
3130

3231
public GlobalConfiguration() {
@@ -66,16 +65,9 @@ void loadProperties(InputStream inputStream, String propertyFile) {
6665
private void initializeKnownProperties() {
6766
String value = properties.getProperty("nonRenderingWhereClauseAllowed", "false"); //$NON-NLS-1$ //$NON-NLS-2$
6867
isNonRenderingWhereClauseAllowed = Boolean.parseBoolean(value);
69-
70-
value = properties.getProperty("emptyListConditionRenderingAllowed", "true"); //$NON-NLS-1$ //$NON-NLS-2$
71-
isEmptyListConditionRenderingAllowed = Boolean.parseBoolean(value);
7268
}
7369

7470
public boolean isIsNonRenderingWhereClauseAllowed() {
7571
return isNonRenderingWhereClauseAllowed;
7672
}
77-
78-
public boolean isEmptyListConditionRenderingAllowed() {
79-
return isEmptyListConditionRenderingAllowed;
80-
}
8173
}

src/main/java/org/mybatis/dynamic/sql/configuration/StatementConfiguration.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
* Configurable behaviors are detailed below:
2626
*
2727
* <dl>
28-
* <dt>emptyListConditionRenderingAllowed</dt>
29-
* <dd>If false (default), the framework will not render list conditions that are empty in a where clause.
30-
* This is beneficial in that it will not allow the library to generate invalid SQL, but it has a
31-
* potentially dangerous side effect where a statement could be generated that impacts more rows
32-
* then expected. If true, an empty list will be rendered as "in ()", "not in ()", etc. which will likely
33-
* cause an SQLException at runtime.
34-
* </dd>
3528
* <dt>nonRenderingWhereClauseAllowed</dt>
3629
* <dd>If false (default), the framework will throw a {@link NonRenderingWhereClauseException}
3730
* if a where clause is specified in the statement, but it fails to render because all
@@ -51,9 +44,6 @@ public class StatementConfiguration {
5144
private boolean isNonRenderingWhereClauseAllowed =
5245
GlobalContext.getConfiguration().isIsNonRenderingWhereClauseAllowed();
5346

54-
private boolean isEmptyListConditionRenderingAllowed =
55-
GlobalContext.getConfiguration().isEmptyListConditionRenderingAllowed();
56-
5747
public boolean isNonRenderingWhereClauseAllowed() {
5848
return isNonRenderingWhereClauseAllowed;
5949
}
@@ -62,13 +52,4 @@ public StatementConfiguration setNonRenderingWhereClauseAllowed(boolean nonRende
6252
isNonRenderingWhereClauseAllowed = nonRenderingWhereClauseAllowed;
6353
return this;
6454
}
65-
66-
public boolean isEmptyListConditionRenderingAllowed() {
67-
return isEmptyListConditionRenderingAllowed;
68-
}
69-
70-
public StatementConfiguration setEmptyListConditionRenderingAllowed(boolean emptyListConditionRenderingAllowed) {
71-
isEmptyListConditionRenderingAllowed = emptyListConditionRenderingAllowed;
72-
return this;
73-
}
7455
}

src/main/java/org/mybatis/dynamic/sql/render/RenderingContext.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ public boolean isNonRenderingClauseAllowed() {
101101
return statementConfiguration.isNonRenderingWhereClauseAllowed();
102102
}
103103

104-
public boolean isEmptyListConditionRenderingAllowed() {
105-
return statementConfiguration.isEmptyListConditionRenderingAllowed();
106-
}
107-
108104
/**
109105
* Create a new rendering context based on this, with the table alias calculator modified to include the
110106
* specified child table alias calculator. This is used by the query expression renderer when the alias calculator

src/main/java/org/mybatis/dynamic/sql/where/condition/IsIn.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.function.Predicate;
2323

2424
import org.mybatis.dynamic.sql.AbstractListValueCondition;
25+
import org.mybatis.dynamic.sql.render.RenderingContext;
2526

2627
public class IsIn<T> extends AbstractListValueCondition<T> {
2728
private static final IsIn<?> EMPTY = new IsIn<>(Collections.emptyList());
@@ -36,6 +37,11 @@ protected IsIn(Collection<T> values) {
3637
super(values);
3738
}
3839

40+
@Override
41+
public boolean shouldRender(RenderingContext renderingContext) {
42+
return true;
43+
}
44+
3945
@Override
4046
public String operator() {
4147
return "in"; //$NON-NLS-1$
@@ -47,13 +53,12 @@ public IsIn<T> filter(Predicate<? super T> predicate) {
4753
}
4854

4955
/**
50-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
51-
* Else return a condition that will not render (this).
56+
* If not empty, apply the mapping to each value in the list return a new condition with the mapped values.
57+
* Else return an empty condition (this).
5258
*
53-
* @param mapper a mapping function to apply to the values, if renderable
59+
* @param mapper a mapping function to apply to the values, if not empty
5460
* @param <R> type of the new condition
55-
* @return a new condition with mapped values if renderable, otherwise a condition
56-
* that will not render.
61+
* @return a new condition with mapped values if renderable, otherwise an empty condition
5762
*/
5863
public <R> IsIn<R> map(Function<? super T, ? extends R> mapper) {
5964
Function<Collection<R>, IsIn<R>> constructor = IsIn::new;

src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitive.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.function.UnaryOperator;
2323

2424
import org.mybatis.dynamic.sql.AbstractListValueCondition;
25+
import org.mybatis.dynamic.sql.render.RenderingContext;
2526
import org.mybatis.dynamic.sql.util.StringUtilities;
2627

2728
public class IsInCaseInsensitive extends AbstractListValueCondition<String>
@@ -36,6 +37,11 @@ protected IsInCaseInsensitive(Collection<String> values) {
3637
super(values);
3738
}
3839

40+
@Override
41+
public boolean shouldRender(RenderingContext renderingContext) {
42+
return true;
43+
}
44+
3945
@Override
4046
public String operator() {
4147
return "in"; //$NON-NLS-1$
@@ -47,12 +53,11 @@ public IsInCaseInsensitive filter(Predicate<? super String> predicate) {
4753
}
4854

4955
/**
50-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
51-
* Else return a condition that will not render (this).
56+
* If not empty, apply the mapping to each value in the list return a new condition with the mapped values.
57+
* Else return an empty condition (this).
5258
*
53-
* @param mapper a mapping function to apply to the values, if renderable
54-
* @return a new condition with mapped values if renderable, otherwise a condition
55-
* that will not render.
59+
* @param mapper a mapping function to apply to the values, if not empty
60+
* @return a new condition with mapped values if renderable, otherwise an empty condition
5661
*/
5762
public IsInCaseInsensitive map(UnaryOperator<String> mapper) {
5863
return mapSupport(mapper, IsInCaseInsensitive::new, IsInCaseInsensitive::empty);

src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitiveWhenPresent.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.stream.Collectors;
2525

2626
import org.mybatis.dynamic.sql.AbstractListValueCondition;
27-
import org.mybatis.dynamic.sql.render.RenderingContext;
2827
import org.mybatis.dynamic.sql.util.StringUtilities;
2928

3029
public class IsInCaseInsensitiveWhenPresent extends AbstractListValueCondition<String>
@@ -39,11 +38,6 @@ protected IsInCaseInsensitiveWhenPresent(Collection<String> values) {
3938
super(values.stream().filter(Objects::nonNull).collect(Collectors.toList()));
4039
}
4140

42-
@Override
43-
public boolean shouldRender(RenderingContext renderingContext) {
44-
return !isEmpty();
45-
}
46-
4741
@Override
4842
public String operator() {
4943
return "in"; //$NON-NLS-1$
@@ -55,12 +49,11 @@ public IsInCaseInsensitiveWhenPresent filter(Predicate<? super String> predicate
5549
}
5650

5751
/**
58-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
59-
* Else return a condition that will not render (this).
52+
* If not empty, apply the mapping to each value in the list return a new condition with the mapped values.
53+
* Else return an empty condition (this).
6054
*
61-
* @param mapper a mapping function to apply to the values, if renderable
62-
* @return a new condition with mapped values if renderable, otherwise a condition
63-
* that will not render.
55+
* @param mapper a mapping function to apply to the values, if not empty
56+
* @return a new condition with mapped values if renderable, otherwise an empty condition
6457
*/
6558
public IsInCaseInsensitiveWhenPresent map(UnaryOperator<String> mapper) {
6659
return mapSupport(mapper, IsInCaseInsensitiveWhenPresent::new, IsInCaseInsensitiveWhenPresent::empty);

src/main/java/org/mybatis/dynamic/sql/where/condition/IsInWhenPresent.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.stream.Collectors;
2525

2626
import org.mybatis.dynamic.sql.AbstractListValueCondition;
27-
import org.mybatis.dynamic.sql.render.RenderingContext;
2827

2928
public class IsInWhenPresent<T> extends AbstractListValueCondition<T> {
3029
private static final IsInWhenPresent<?> EMPTY = new IsInWhenPresent<>(Collections.emptyList());
@@ -39,11 +38,6 @@ protected IsInWhenPresent(Collection<T> values) {
3938
super(values.stream().filter(Objects::nonNull).collect(Collectors.toList()));
4039
}
4140

42-
@Override
43-
public boolean shouldRender(RenderingContext renderingContext) {
44-
return !isEmpty();
45-
}
46-
4741
@Override
4842
public String operator() {
4943
return "in"; //$NON-NLS-1$
@@ -55,13 +49,12 @@ public IsInWhenPresent<T> filter(Predicate<? super T> predicate) {
5549
}
5650

5751
/**
58-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
59-
* Else return a condition that will not render (this).
52+
* If not empty, apply the mapping to each value in the list return a new condition with the mapped values.
53+
* Else return an empty condition (this).
6054
*
61-
* @param mapper a mapping function to apply to the values, if renderable
55+
* @param mapper a mapping function to apply to the values, if not empty
6256
* @param <R> type of the new condition
63-
* @return a new condition with mapped values if renderable, otherwise a condition
64-
* that will not render.
57+
* @return a new condition with mapped values if renderable, otherwise an empty condition
6558
*/
6659
public <R> IsInWhenPresent<R> map(Function<? super T, ? extends R> mapper) {
6760
Function<Collection<R>, IsInWhenPresent<R>> constructor = IsInWhenPresent::new;

src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotIn.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.function.Predicate;
2323

2424
import org.mybatis.dynamic.sql.AbstractListValueCondition;
25+
import org.mybatis.dynamic.sql.render.RenderingContext;
2526

2627
public class IsNotIn<T> extends AbstractListValueCondition<T> {
2728
private static final IsNotIn<?> EMPTY = new IsNotIn<>(Collections.emptyList());
@@ -36,6 +37,11 @@ protected IsNotIn(Collection<T> values) {
3637
super(values);
3738
}
3839

40+
@Override
41+
public boolean shouldRender(RenderingContext renderingContext) {
42+
return true;
43+
}
44+
3945
@Override
4046
public String operator() {
4147
return "not in"; //$NON-NLS-1$
@@ -47,13 +53,12 @@ public IsNotIn<T> filter(Predicate<? super T> predicate) {
4753
}
4854

4955
/**
50-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
51-
* Else return a condition that will not render (this).
56+
* If not empty, apply the mapping to each value in the list return a new condition with the mapped values.
57+
* Else return an empty condition (this).
5258
*
53-
* @param mapper a mapping function to apply to the values, if renderable
59+
* @param mapper a mapping function to apply to the values, if not empty
5460
* @param <R> type of the new condition
55-
* @return a new condition with mapped values if renderable, otherwise a condition
56-
* that will not render.
61+
* @return a new condition with mapped values if renderable, otherwise an empty condition
5762
*/
5863
public <R> IsNotIn<R> map(Function<? super T, ? extends R> mapper) {
5964
Function<Collection<R>, IsNotIn<R>> constructor = IsNotIn::new;

src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotInCaseInsensitive.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.function.UnaryOperator;
2323

2424
import org.mybatis.dynamic.sql.AbstractListValueCondition;
25+
import org.mybatis.dynamic.sql.render.RenderingContext;
2526
import org.mybatis.dynamic.sql.util.StringUtilities;
2627

2728
public class IsNotInCaseInsensitive extends AbstractListValueCondition<String>
@@ -36,6 +37,11 @@ protected IsNotInCaseInsensitive(Collection<String> values) {
3637
super(values);
3738
}
3839

40+
@Override
41+
public boolean shouldRender(RenderingContext renderingContext) {
42+
return true;
43+
}
44+
3945
@Override
4046
public String operator() {
4147
return "not in"; //$NON-NLS-1$
@@ -47,12 +53,11 @@ public IsNotInCaseInsensitive filter(Predicate<? super String> predicate) {
4753
}
4854

4955
/**
50-
* If renderable, apply the mapping to each value in the list return a new condition with the mapped values.
51-
* Else return a condition that will not render (this).
56+
* If not empty, apply the mapping to each value in the list return a new condition with the mapped values.
57+
* Else return an empty condition (this).
5258
*
53-
* @param mapper a mapping function to apply to the values, if renderable
54-
* @return a new condition with mapped values if renderable, otherwise a condition
55-
* that will not render.
59+
* @param mapper a mapping function to apply to the values, if not empty
60+
* @return a new condition with mapped values if renderable, otherwise an empty condition
5661
*/
5762
public IsNotInCaseInsensitive map(UnaryOperator<String> mapper) {
5863
return mapSupport(mapper, IsNotInCaseInsensitive::new, IsNotInCaseInsensitive::empty);

0 commit comments

Comments
 (0)