Skip to content

Commit 972cee0

Browse files
committed
Remove "then" conditions on some In conditions
The case insensitive in conditions are actually specializations of the regular in conditions and not intended to be extended.
1 parent c4a05d1 commit 972cee0

File tree

5 files changed

+19
-85
lines changed

5 files changed

+19
-85
lines changed

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525

2626
public class IsInCaseInsensitive extends AbstractListValueCondition<String> {
2727

28-
protected IsInCaseInsensitive(Collection<String> values) {
29-
super(values, s -> s.map(StringUtilities::safelyUpperCase));
30-
}
31-
3228
protected IsInCaseInsensitive(Collection<String> values, UnaryOperator<Stream<String>> valueStreamTransformer) {
33-
super(values, StringUtilities.upperCaseAfter(valueStreamTransformer));
29+
super(values, valueStreamTransformer);
3430
}
3531

3632
@Override
@@ -39,21 +35,7 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
3935
placeholders.collect(Collectors.joining(",", "in (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
4036
}
4137

42-
/**
43-
* This method allows you to modify the condition's values before they are placed into the parameter map.
44-
* For example, you could filter nulls, or trim strings, etc. This process will run before final rendering of SQL.
45-
* If you filter values out of the stream, then final condition will not reference those values. If you filter all
46-
* values out of the stream, then the condition will not render.
47-
*
48-
* @param valueStreamTransformer a UnaryOperator that will transform the value stream before
49-
* the values are placed in the parameter map
50-
* @return new condition with the specified transformer
51-
*/
52-
public IsInCaseInsensitive then(UnaryOperator<Stream<String>> valueStreamTransformer) {
53-
return new IsInCaseInsensitive(values, valueStreamTransformer);
54-
}
55-
5638
public static IsInCaseInsensitive of(Collection<String> values) {
57-
return new IsInCaseInsensitive(values);
39+
return new IsInCaseInsensitive(values, s -> s.map(StringUtilities::safelyUpperCase));
5840
}
5941
}

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

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

18+
import org.mybatis.dynamic.sql.util.StringUtilities;
19+
1820
import java.util.Collection;
1921
import java.util.Objects;
22+
import java.util.function.UnaryOperator;
23+
import java.util.stream.Stream;
2024

2125
public class IsInCaseInsensitiveWhenPresent extends IsInCaseInsensitive {
2226

23-
protected IsInCaseInsensitiveWhenPresent(Collection<String> values) {
24-
super(values, s -> s.filter(Objects::nonNull));
27+
protected IsInCaseInsensitiveWhenPresent(Collection<String> values, UnaryOperator<Stream<String>> valueStreamTransformer) {
28+
super(values, valueStreamTransformer);
2529
}
2630

2731
public static IsInCaseInsensitiveWhenPresent of(Collection<String> values) {
28-
return new IsInCaseInsensitiveWhenPresent(values);
32+
return new IsInCaseInsensitiveWhenPresent(values,
33+
s -> s.filter(Objects::nonNull).map(StringUtilities::safelyUpperCase));
2934
}
3035
}

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525

2626
public class IsNotInCaseInsensitive extends AbstractListValueCondition<String> {
2727

28-
protected IsNotInCaseInsensitive(Collection<String> values) {
29-
super(values, s -> s.map(StringUtilities::safelyUpperCase));
30-
}
31-
3228
protected IsNotInCaseInsensitive(Collection<String> values, UnaryOperator<Stream<String>> valueStreamTransformer) {
33-
super(values, StringUtilities.upperCaseAfter(valueStreamTransformer));
29+
super(values, valueStreamTransformer);
3430
}
3531

3632
@Override
@@ -40,21 +36,7 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
4036
Collectors.joining(",", "not in (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
4137
}
4238

43-
/**
44-
* This method allows you to modify the condition's values before they are placed into the parameter map.
45-
* For example, you could filter nulls, or trim strings, etc. This process will run before final rendering of SQL.
46-
* If you filter values out of the stream, then final condition will not reference those values. If you filter all
47-
* values out of the stream, then the condition will not render.
48-
*
49-
* @param valueStreamTransformer a UnaryOperator that will transform the value stream before
50-
* the values are placed in the parameter map
51-
* @return new condition with the specified transformer
52-
*/
53-
public IsNotInCaseInsensitive then(UnaryOperator<Stream<String>> valueStreamTransformer) {
54-
return new IsNotInCaseInsensitive(values, valueStreamTransformer);
55-
}
56-
5739
public static IsNotInCaseInsensitive of(Collection<String> values) {
58-
return new IsNotInCaseInsensitive(values);
40+
return new IsNotInCaseInsensitive(values, s -> s.map(StringUtilities::safelyUpperCase));
5941
}
6042
}

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

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

18+
import org.mybatis.dynamic.sql.util.StringUtilities;
19+
1820
import java.util.Collection;
1921
import java.util.Objects;
22+
import java.util.function.UnaryOperator;
23+
import java.util.stream.Stream;
2024

2125
public class IsNotInCaseInsensitiveWhenPresent extends IsNotInCaseInsensitive {
2226

23-
protected IsNotInCaseInsensitiveWhenPresent(Collection<String> values) {
24-
super(values, s -> s.filter(Objects::nonNull));
27+
protected IsNotInCaseInsensitiveWhenPresent(Collection<String> values, UnaryOperator<Stream<String>> valueStreamTransformer) {
28+
super(values, valueStreamTransformer);
2529
}
2630

2731
public static IsNotInCaseInsensitiveWhenPresent of(Collection<String> values) {
28-
return new IsNotInCaseInsensitiveWhenPresent(values);
32+
return new IsNotInCaseInsensitiveWhenPresent(values, s -> s.filter(Objects::nonNull).map(StringUtilities::safelyUpperCase));
2933
}
3034
}

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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -514,25 +514,6 @@ void testValueStreamTransformerWithCustomCondition() {
514514
}
515515
}
516516

517-
@Test
518-
void testIsInCaseInsensitiveWhenWithSomeValues() {
519-
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
520-
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
521-
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
522-
.from(animalData)
523-
.where(animalName, isInCaseInsensitive("mouse", null, "musk shrew").then(s -> s.filter(Objects::nonNull)))
524-
.orderBy(id)
525-
.build()
526-
.render(RenderingStrategies.MYBATIS3);
527-
List<AnimalData> animals = mapper.selectMany(selectStatement);
528-
assertAll(
529-
() -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where upper(animal_name) in (#{parameters.p1,jdbcType=VARCHAR},#{parameters.p2,jdbcType=VARCHAR}) order by id"),
530-
() -> assertThat(animals).hasSize(2),
531-
() -> assertThat(animals.get(0).getId()).isEqualTo(4)
532-
);
533-
}
534-
}
535-
536517
@Test
537518
void testIsInCaseInsensitiveWhenWithNoValues() {
538519
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
@@ -613,26 +594,6 @@ void testIsNotInCaseInsensitiveWhenWithValue() {
613594
}
614595
}
615596

616-
@Test
617-
void testIsNotInCaseInsensitiveWhenWithSomeValues() {
618-
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
619-
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
620-
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
621-
.from(animalData)
622-
.where(animalName, isNotInCaseInsensitive("mouse", null, "musk shrew").then(s -> s.filter(Objects::nonNull)))
623-
.and(id, isLessThanOrEqualTo(10))
624-
.orderBy(id)
625-
.build()
626-
.render(RenderingStrategies.MYBATIS3);
627-
List<AnimalData> animals = mapper.selectMany(selectStatement);
628-
assertAll(
629-
() -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where upper(animal_name) not in (#{parameters.p1,jdbcType=VARCHAR},#{parameters.p2,jdbcType=VARCHAR}) and id <= #{parameters.p3,jdbcType=INTEGER} order by id"),
630-
() -> assertThat(animals).hasSize(8),
631-
() -> assertThat(animals.get(0).getId()).isEqualTo(1)
632-
);
633-
}
634-
}
635-
636597
@Test
637598
void testIsNotInCaseInsensitiveWhenWithNoValues() {
638599
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)