Skip to content

Commit cfe58a3

Browse files
committed
Enable rendering of empty list conditions
This will enable the list conditions to render invalid SQL, so it should be used with caution. But it will make transition from some legacy code bases easier. Resolves #228
1 parent 52d939d commit cfe58a3

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -25,6 +25,7 @@
2525
public abstract class AbstractListValueCondition<T> implements VisitableCondition<T> {
2626
protected Collection<T> values;
2727
protected UnaryOperator<Stream<T>> valueStreamTransformer;
28+
protected boolean skipRenderingWhenEmpty = true;
2829

2930
protected AbstractListValueCondition(Collection<T> values) {
3031
this(values, UnaryOperator.identity());
@@ -39,6 +40,10 @@ public final <R> Stream<R> mapValues(Function<T, R> mapper) {
3940
return valueStreamTransformer.apply(values.stream()).map(mapper);
4041
}
4142

43+
public boolean skipRenderingWhenEmpty() {
44+
return skipRenderingWhenEmpty;
45+
}
46+
4247
@Override
4348
public <R> R accept(ConditionVisitor<T, R> visitor) {
4449
return visitor.visit(this);

src/main/java/org/mybatis/dynamic/sql/where/render/WhereConditionVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Optional<FragmentAndParameters> visit(AbstractListValueCondition<T> condi
5555
FragmentCollector fc = condition.mapValues(this::toFragmentAndParameters)
5656
.collect(FragmentCollector.collect());
5757

58-
if (fc.isEmpty()) {
58+
if (fc.isEmpty() && condition.skipRenderingWhenEmpty()) {
5959
return Optional.empty();
6060
}
6161

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static examples.animal.data.AnimalDataDynamicSqlSupport.*;
1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2021
import static org.assertj.core.api.Assertions.within;
2122
import static org.junit.jupiter.api.Assertions.assertAll;
2223
import static org.mybatis.dynamic.sql.SqlBuilder.*;
@@ -26,10 +27,13 @@
2627
import java.sql.Connection;
2728
import java.sql.DriverManager;
2829
import java.util.ArrayList;
30+
import java.util.Collection;
31+
import java.util.Collections;
2932
import java.util.List;
3033
import java.util.Map;
3134

3235
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
36+
import org.apache.ibatis.exceptions.PersistenceException;
3337
import org.apache.ibatis.jdbc.ScriptRunner;
3438
import org.apache.ibatis.mapping.Environment;
3539
import org.apache.ibatis.session.Configuration;
@@ -52,6 +56,7 @@
5256
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
5357
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
5458
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
59+
import org.mybatis.dynamic.sql.where.condition.IsIn;
5560
import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;
5661

5762
class AnimalDataTest {
@@ -564,6 +569,34 @@ void testInCondition() {
564569
}
565570
}
566571

572+
@Test
573+
void testInConditionWithEmptyList() {
574+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
575+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
576+
577+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
578+
.from(animalData)
579+
.where(id, IsInRequired.isIn(Collections.emptyList()))
580+
.build()
581+
.render(RenderingStrategies.MYBATIS3);
582+
583+
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> {
584+
mapper.selectMany(selectStatement);
585+
});
586+
}
587+
}
588+
589+
public static class IsInRequired<T> extends IsIn<T> {
590+
protected IsInRequired(Collection<T> values) {
591+
super(values);
592+
skipRenderingWhenEmpty = false;
593+
}
594+
595+
public static <T> IsInRequired<T> isIn(Collection<T> values) {
596+
return new IsInRequired<>(values);
597+
}
598+
}
599+
567600
@Test
568601
void testInCaseSensitiveCondition() {
569602
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)