Skip to content

Commit b47aed4

Browse files
committed
New InWhenPresent Conditions
These conditions will not render if empty regardless of configuration settings. This is consistent with the other "when present" conditions.
1 parent 9aea401 commit b47aed4

File tree

6 files changed

+347
-29
lines changed

6 files changed

+347
-29
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanWithSubselect;
7777
import org.mybatis.dynamic.sql.where.condition.IsIn;
7878
import org.mybatis.dynamic.sql.where.condition.IsInCaseInsensitive;
79+
import org.mybatis.dynamic.sql.where.condition.IsInCaseInsensitiveWhenPresent;
80+
import org.mybatis.dynamic.sql.where.condition.IsInWhenPresent;
7981
import org.mybatis.dynamic.sql.where.condition.IsInWithSubselect;
8082
import org.mybatis.dynamic.sql.where.condition.IsLessThan;
8183
import org.mybatis.dynamic.sql.where.condition.IsLessThanColumn;
@@ -91,6 +93,8 @@
9193
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToWithSubselect;
9294
import org.mybatis.dynamic.sql.where.condition.IsNotIn;
9395
import org.mybatis.dynamic.sql.where.condition.IsNotInCaseInsensitive;
96+
import org.mybatis.dynamic.sql.where.condition.IsNotInCaseInsensitiveWhenPresent;
97+
import org.mybatis.dynamic.sql.where.condition.IsNotInWhenPresent;
9498
import org.mybatis.dynamic.sql.where.condition.IsNotInWithSubselect;
9599
import org.mybatis.dynamic.sql.where.condition.IsNotLike;
96100
import org.mybatis.dynamic.sql.where.condition.IsNotLikeCaseInsensitive;
@@ -764,12 +768,12 @@ static <T> IsInWithSubselect<T> isIn(Buildable<SelectModel> selectModelBuilder)
764768
}
765769

766770
@SafeVarargs
767-
static <T> IsIn<T> isInWhenPresent(T... values) {
768-
return IsIn.of(values).filter(Objects::nonNull);
771+
static <T> IsInWhenPresent<T> isInWhenPresent(T... values) {
772+
return IsInWhenPresent.of(values);
769773
}
770774

771-
static <T> IsIn<T> isInWhenPresent(Collection<T> values) {
772-
return values == null ? IsIn.empty() : IsIn.of(values).filter(Objects::nonNull);
775+
static <T> IsInWhenPresent<T> isInWhenPresent(Collection<T> values) {
776+
return values == null ? IsInWhenPresent.empty() : IsInWhenPresent.of(values);
773777
}
774778

775779
@SafeVarargs
@@ -786,12 +790,12 @@ static <T> IsNotInWithSubselect<T> isNotIn(Buildable<SelectModel> selectModelBui
786790
}
787791

788792
@SafeVarargs
789-
static <T> IsNotIn<T> isNotInWhenPresent(T... values) {
790-
return IsNotIn.of(values).filter(Objects::nonNull);
793+
static <T> IsNotInWhenPresent<T> isNotInWhenPresent(T... values) {
794+
return IsNotInWhenPresent.of(values);
791795
}
792796

793-
static <T> IsNotIn<T> isNotInWhenPresent(Collection<T> values) {
794-
return values == null ? IsNotIn.empty() : IsNotIn.of(values).filter(Objects::nonNull);
797+
static <T> IsNotInWhenPresent<T> isNotInWhenPresent(Collection<T> values) {
798+
return values == null ? IsNotInWhenPresent.empty() : IsNotInWhenPresent.of(values);
795799
}
796800

797801
static <T> IsBetween.Builder<T> isBetween(T value1) {
@@ -909,12 +913,12 @@ static IsInCaseInsensitive isInCaseInsensitive(Collection<String> values) {
909913
return IsInCaseInsensitive.of(values);
910914
}
911915

912-
static IsInCaseInsensitive isInCaseInsensitiveWhenPresent(String... values) {
913-
return IsInCaseInsensitive.of(values).filter(Objects::nonNull);
916+
static IsInCaseInsensitiveWhenPresent isInCaseInsensitiveWhenPresent(String... values) {
917+
return IsInCaseInsensitiveWhenPresent.of(values);
914918
}
915919

916-
static IsInCaseInsensitive isInCaseInsensitiveWhenPresent(Collection<String> values) {
917-
return values == null ? IsInCaseInsensitive.empty() : IsInCaseInsensitive.of(values).filter(Objects::nonNull);
920+
static IsInCaseInsensitiveWhenPresent isInCaseInsensitiveWhenPresent(Collection<String> values) {
921+
return values == null ? IsInCaseInsensitiveWhenPresent.empty() : IsInCaseInsensitiveWhenPresent.of(values);
918922
}
919923

920924
static IsNotInCaseInsensitive isNotInCaseInsensitive(String... values) {
@@ -925,13 +929,13 @@ static IsNotInCaseInsensitive isNotInCaseInsensitive(Collection<String> values)
925929
return IsNotInCaseInsensitive.of(values);
926930
}
927931

928-
static IsNotInCaseInsensitive isNotInCaseInsensitiveWhenPresent(String... values) {
929-
return IsNotInCaseInsensitive.of(values).filter(Objects::nonNull);
932+
static IsNotInCaseInsensitiveWhenPresent isNotInCaseInsensitiveWhenPresent(String... values) {
933+
return IsNotInCaseInsensitiveWhenPresent.of(values);
930934
}
931935

932-
static IsNotInCaseInsensitive isNotInCaseInsensitiveWhenPresent(Collection<String> values) {
933-
return values == null ? IsNotInCaseInsensitive.empty() :
934-
IsNotInCaseInsensitive.of(values).filter(Objects::nonNull);
936+
static IsNotInCaseInsensitiveWhenPresent isNotInCaseInsensitiveWhenPresent(Collection<String> values) {
937+
return values == null ? IsNotInCaseInsensitiveWhenPresent.empty() :
938+
IsNotInCaseInsensitiveWhenPresent.of(values);
935939
}
936940

937941
// order by support
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.where.condition;
17+
18+
import java.util.Arrays;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.Objects;
22+
import java.util.function.Predicate;
23+
import java.util.function.UnaryOperator;
24+
import java.util.stream.Collectors;
25+
26+
import org.mybatis.dynamic.sql.AbstractListValueCondition;
27+
import org.mybatis.dynamic.sql.render.RenderingContext;
28+
import org.mybatis.dynamic.sql.util.StringUtilities;
29+
30+
public class IsInCaseInsensitiveWhenPresent extends AbstractListValueCondition<String>
31+
implements CaseInsensitiveVisitableCondition {
32+
private static final IsInCaseInsensitiveWhenPresent EMPTY = new IsInCaseInsensitiveWhenPresent(Collections.emptyList());
33+
34+
public static IsInCaseInsensitiveWhenPresent empty() {
35+
return EMPTY;
36+
}
37+
38+
protected IsInCaseInsensitiveWhenPresent(Collection<String> values) {
39+
super(values.stream().filter(Objects::nonNull).collect(Collectors.toList()));
40+
}
41+
42+
@Override
43+
public boolean shouldRender(RenderingContext renderingContext) {
44+
return !isEmpty();
45+
}
46+
47+
@Override
48+
public String operator() {
49+
return "in"; //$NON-NLS-1$
50+
}
51+
52+
@Override
53+
public IsInCaseInsensitiveWhenPresent filter(Predicate<? super String> predicate) {
54+
return filterSupport(predicate, IsInCaseInsensitiveWhenPresent::new, this, IsInCaseInsensitiveWhenPresent::empty);
55+
}
56+
57+
/**
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).
60+
*
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.
64+
*/
65+
public IsInCaseInsensitiveWhenPresent map(UnaryOperator<String> mapper) {
66+
return mapSupport(mapper, IsInCaseInsensitiveWhenPresent::new, IsInCaseInsensitiveWhenPresent::empty);
67+
}
68+
69+
public static IsInCaseInsensitiveWhenPresent of(String... values) {
70+
return of(Arrays.asList(values));
71+
}
72+
73+
public static IsInCaseInsensitiveWhenPresent of(Collection<String> values) {
74+
return new IsInCaseInsensitiveWhenPresent(values).map(StringUtilities::safelyUpperCase);
75+
}
76+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.where.condition;
17+
18+
import java.util.Arrays;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.Objects;
22+
import java.util.function.Function;
23+
import java.util.function.Predicate;
24+
import java.util.stream.Collectors;
25+
26+
import org.mybatis.dynamic.sql.AbstractListValueCondition;
27+
import org.mybatis.dynamic.sql.render.RenderingContext;
28+
29+
public class IsInWhenPresent<T> extends AbstractListValueCondition<T> {
30+
private static final IsInWhenPresent<?> EMPTY = new IsInWhenPresent<>(Collections.emptyList());
31+
32+
public static <T> IsInWhenPresent<T> empty() {
33+
@SuppressWarnings("unchecked")
34+
IsInWhenPresent<T> t = (IsInWhenPresent<T>) EMPTY;
35+
return t;
36+
}
37+
38+
protected IsInWhenPresent(Collection<T> values) {
39+
super(values.stream().filter(Objects::nonNull).collect(Collectors.toList()));
40+
}
41+
42+
@Override
43+
public boolean shouldRender(RenderingContext renderingContext) {
44+
return !isEmpty();
45+
}
46+
47+
@Override
48+
public String operator() {
49+
return "in"; //$NON-NLS-1$
50+
}
51+
52+
@Override
53+
public IsInWhenPresent<T> filter(Predicate<? super T> predicate) {
54+
return filterSupport(predicate, IsInWhenPresent::new, this, IsInWhenPresent::empty);
55+
}
56+
57+
/**
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).
60+
*
61+
* @param mapper a mapping function to apply to the values, if renderable
62+
* @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.
65+
*/
66+
public <R> IsInWhenPresent<R> map(Function<? super T, ? extends R> mapper) {
67+
Function<Collection<R>, IsInWhenPresent<R>> constructor = IsInWhenPresent::new;
68+
return mapSupport(mapper, constructor, IsInWhenPresent::empty);
69+
}
70+
71+
@SafeVarargs
72+
public static <T> IsInWhenPresent<T> of(T... values) {
73+
return of(Arrays.asList(values));
74+
}
75+
76+
public static <T> IsInWhenPresent<T> of(Collection<T> values) {
77+
return new IsInWhenPresent<>(values);
78+
}
79+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2016-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.where.condition;
17+
18+
import java.util.Arrays;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.Objects;
22+
import java.util.function.Predicate;
23+
import java.util.function.UnaryOperator;
24+
import java.util.stream.Collectors;
25+
26+
import org.mybatis.dynamic.sql.AbstractListValueCondition;
27+
import org.mybatis.dynamic.sql.render.RenderingContext;
28+
import org.mybatis.dynamic.sql.util.StringUtilities;
29+
30+
public class IsNotInCaseInsensitiveWhenPresent extends AbstractListValueCondition<String>
31+
implements CaseInsensitiveVisitableCondition {
32+
private static final IsNotInCaseInsensitiveWhenPresent EMPTY = new IsNotInCaseInsensitiveWhenPresent(Collections.emptyList());
33+
34+
public static IsNotInCaseInsensitiveWhenPresent empty() {
35+
return EMPTY;
36+
}
37+
38+
protected IsNotInCaseInsensitiveWhenPresent(Collection<String> values) {
39+
super(values.stream().filter(Objects::nonNull).collect(Collectors.toList()));
40+
}
41+
42+
@Override
43+
public boolean shouldRender(RenderingContext renderingContext) {
44+
return !isEmpty();
45+
}
46+
47+
@Override
48+
public String operator() {
49+
return "not in"; //$NON-NLS-1$
50+
}
51+
52+
@Override
53+
public IsNotInCaseInsensitiveWhenPresent filter(Predicate<? super String> predicate) {
54+
return filterSupport(predicate, IsNotInCaseInsensitiveWhenPresent::new, this, IsNotInCaseInsensitiveWhenPresent::empty);
55+
}
56+
57+
/**
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).
60+
*
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.
64+
*/
65+
public IsNotInCaseInsensitiveWhenPresent map(UnaryOperator<String> mapper) {
66+
return mapSupport(mapper, IsNotInCaseInsensitiveWhenPresent::new, IsNotInCaseInsensitiveWhenPresent::empty);
67+
}
68+
69+
public static IsNotInCaseInsensitiveWhenPresent of(String... values) {
70+
return of(Arrays.asList(values));
71+
}
72+
73+
public static IsNotInCaseInsensitiveWhenPresent of(Collection<String> values) {
74+
return new IsNotInCaseInsensitiveWhenPresent(values).map(StringUtilities::safelyUpperCase);
75+
}
76+
}

0 commit comments

Comments
 (0)