Skip to content

Commit 66cda22

Browse files
authored
Merge pull request #240 from jeffgbutler/gh-239
Apply value transform immediately on construction
2 parents c4a05d1 + 867fd91 commit 66cda22

File tree

6 files changed

+112
-11
lines changed

6 files changed

+112
-11
lines changed

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

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

18-
import java.util.ArrayList;
1918
import java.util.Collection;
2019
import java.util.Objects;
2120
import java.util.function.Function;
2221
import java.util.function.UnaryOperator;
22+
import java.util.stream.Collectors;
2323
import java.util.stream.Stream;
2424

2525
public abstract class AbstractListValueCondition<T> implements VisitableCondition<T> {
@@ -32,12 +32,13 @@ protected AbstractListValueCondition(Collection<T> values) {
3232
}
3333

3434
protected AbstractListValueCondition(Collection<T> values, UnaryOperator<Stream<T>> valueStreamTransformer) {
35-
this.values = new ArrayList<>(Objects.requireNonNull(values));
3635
this.valueStreamTransformer = Objects.requireNonNull(valueStreamTransformer);
36+
this.values = valueStreamTransformer.apply(Objects.requireNonNull(values).stream())
37+
.collect(Collectors.toList());
3738
}
3839

3940
public final <R> Stream<R> mapValues(Function<T, R> mapper) {
40-
return valueStreamTransformer.apply(values.stream()).map(mapper);
41+
return values.stream().map(mapper);
4142
}
4243

4344
@Override

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

Lines changed: 4 additions & 2 deletions
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.
@@ -51,7 +51,9 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
5151
* @return new condition with the specified transformer
5252
*/
5353
public IsIn<T> then(UnaryOperator<Stream<T>> valueStreamTransformer) {
54-
return new IsIn<>(values, valueStreamTransformer);
54+
IsIn<T> answer = new IsIn<>(values, valueStreamTransformer);
55+
answer.renderWhenEmpty = renderWhenEmpty;
56+
return answer;
5557
}
5658

5759
public static <T> IsIn<T> of(Collection<T> values) {

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

Lines changed: 4 additions & 2 deletions
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.
@@ -50,7 +50,9 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
5050
* @return new condition with the specified transformer
5151
*/
5252
public IsInCaseInsensitive then(UnaryOperator<Stream<String>> valueStreamTransformer) {
53-
return new IsInCaseInsensitive(values, valueStreamTransformer);
53+
IsInCaseInsensitive answer = new IsInCaseInsensitive(values, valueStreamTransformer);
54+
answer.renderWhenEmpty = renderWhenEmpty;
55+
return answer;
5456
}
5557

5658
public static IsInCaseInsensitive of(Collection<String> values) {

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

Lines changed: 4 additions & 2 deletions
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.
@@ -52,7 +52,9 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
5252
* @return new condition with the specified transformer
5353
*/
5454
public IsNotIn<T> then(UnaryOperator<Stream<T>> valueStreamTransformer) {
55-
return new IsNotIn<>(values, valueStreamTransformer);
55+
IsNotIn<T> answer = new IsNotIn<>(values, valueStreamTransformer);
56+
answer.renderWhenEmpty = renderWhenEmpty;
57+
return answer;
5658
}
5759

5860
public static <T> IsNotIn<T> of(Collection<T> values) {

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

Lines changed: 4 additions & 2 deletions
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.
@@ -51,7 +51,9 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
5151
* @return new condition with the specified transformer
5252
*/
5353
public IsNotInCaseInsensitive then(UnaryOperator<Stream<String>> valueStreamTransformer) {
54-
return new IsNotInCaseInsensitive(values, valueStreamTransformer);
54+
IsNotInCaseInsensitive answer = new IsNotInCaseInsensitive(values, valueStreamTransformer);
55+
answer.renderWhenEmpty = renderWhenEmpty;
56+
return answer;
5557
}
5658

5759
public static IsNotInCaseInsensitive of(Collection<String> values) {

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
import java.sql.Connection;
2828
import java.sql.DriverManager;
2929
import java.util.ArrayList;
30+
import java.util.Arrays;
3031
import java.util.Collection;
3132
import java.util.Collections;
3233
import java.util.List;
3334
import java.util.Map;
35+
import java.util.Objects;
3436

3537
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
3638
import org.apache.ibatis.exceptions.PersistenceException;
@@ -57,6 +59,7 @@
5759
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
5860
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
5961
import org.mybatis.dynamic.sql.where.condition.IsIn;
62+
import org.mybatis.dynamic.sql.where.condition.IsNotIn;
6063
import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;
6164

6265
class AnimalDataTest {
@@ -569,6 +572,47 @@ void testInCondition() {
569572
}
570573
}
571574

575+
@Test
576+
void testInConditionWithEventuallyEmptyList() {
577+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
578+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
579+
580+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
581+
.from(animalData)
582+
.where(id, isIn(null, 22, null).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
583+
.build()
584+
.render(RenderingStrategies.MYBATIS3);
585+
586+
assertThat(selectStatement.getSelectStatement())
587+
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData");
588+
List<AnimalData> animals = mapper.selectMany(selectStatement);
589+
assertThat(animals).hasSize(65);
590+
}
591+
}
592+
593+
@Test
594+
void testInConditionWithEventuallyEmptyListForceRendering() {
595+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
596+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
597+
598+
List<Integer> inValues = new ArrayList<>();
599+
inValues.add(null);
600+
inValues.add(22);
601+
inValues.add(null);
602+
603+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
604+
.from(animalData)
605+
.where(id, IsInRequired.isIn(inValues).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
606+
.build()
607+
.render(RenderingStrategies.MYBATIS3);
608+
609+
assertThat(selectStatement.getSelectStatement())
610+
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where id in ()");
611+
612+
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> mapper.selectMany(selectStatement));
613+
}
614+
}
615+
572616
@Test
573617
void testInConditionWithEmptyList() {
574618
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
@@ -659,6 +703,54 @@ void testNotInCaseSensitiveConditionWithNull() {
659703
}
660704
}
661705

706+
@Test
707+
void testNotInConditionWithEventuallyEmptyList() {
708+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
709+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
710+
711+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
712+
.from(animalData)
713+
.where(id, isNotIn(null, 22, null).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
714+
.build()
715+
.render(RenderingStrategies.MYBATIS3);
716+
717+
assertThat(selectStatement.getSelectStatement())
718+
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData");
719+
List<AnimalData> animals = mapper.selectMany(selectStatement);
720+
assertThat(animals).hasSize(65);
721+
}
722+
}
723+
724+
@Test
725+
void testNotInConditionWithEventuallyEmptyListForceRendering() {
726+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
727+
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
728+
729+
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
730+
.from(animalData)
731+
.where(id, IsNotInRequired.isNotIn(null, 22, null)
732+
.then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
733+
.build()
734+
.render(RenderingStrategies.MYBATIS3);
735+
736+
assertThat(selectStatement.getSelectStatement())
737+
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where id not in ()");
738+
739+
assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> mapper.selectMany(selectStatement));
740+
}
741+
}
742+
743+
public static class IsNotInRequired<T> extends IsNotIn<T> {
744+
protected IsNotInRequired(Collection<T> values) {
745+
super(values);
746+
forceRenderingWhenEmpty();
747+
}
748+
749+
@SafeVarargs
750+
public static <T> IsNotInRequired<T> isNotIn(T...values) {
751+
return new IsNotInRequired<>(Arrays.asList(values));
752+
}
753+
}
662754
@Test
663755
void testLikeCondition() {
664756
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)