Skip to content

Commit 5269f47

Browse files
authored
Merge pull request #109 from jeffgbutler/master
Add the ability to alter any condition value before it is placed in the parameter map
2 parents b18f7db + 1171118 commit 5269f47

35 files changed

+1338
-53
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
88

99
### Added
1010

11-
- Changed the public SQLBuilder API to accept Collection instead of List for in conditions and batch record inserts. This should have no impact on existing code, but allow for some future flexibility
12-
- Added the ability have have table catalog and/or schema calculated at query runtime. This is useful for situations where there are different database schemas for different environments, or in some sharding situations
13-
- Added the ability to call a builder method on any intermediate object in a select statement and receive a fully rendered statement. This makes it easier to build very dynamic queries
11+
- Changed the public SQLBuilder API to accept Collection instead of List for in conditions and batch record inserts. This should have no impact on existing code, but allow for some future flexibility [#88](https://github.com/mybatis/mybatis-dynamic-sql/pull/88)
12+
- Added the ability have have table catalog and/or schema calculated at query runtime. This is useful for situations where there are different database schemas for different environments, or in some sharding situations [#92](https://github.com/mybatis/mybatis-dynamic-sql/pull/92)
13+
- Add support for paging queries with "offset" and "fetch first" - this seems to be standard on most databases [#96](https://github.com/mybatis/mybatis-dynamic-sql/pull/96)
14+
- Added the ability to call a builder method on any intermediate object in a select statement and receive a fully rendered statement. This makes it easier to build very dynamic queries [#106](https://github.com/mybatis/mybatis-dynamic-sql/pull/106)
15+
- Add the ability to modify values on any condition before they are placed in the parameter map [#105](https://github.com/mybatis/mybatis-dynamic-sql/issues/105)
16+
- Add the ability to call `where()` with no parameters. This aids in constructing very dynamic queries [#107](https://github.com/mybatis/mybatis-dynamic-sql/issues/107)
1417

1518

1619
## Release 1.1.1 - April 7, 2019

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@
2424

2525
public abstract class AbstractListValueCondition<T> implements VisitableCondition<T> {
2626
protected Collection<T> values;
27-
protected UnaryOperator<Stream<T>> valueStreamOperations;
27+
protected UnaryOperator<Stream<T>> valueStreamTransformer;
2828

2929
protected AbstractListValueCondition(Collection<T> values) {
3030
this(values, UnaryOperator.identity());
3131
}
3232

33-
protected AbstractListValueCondition(Collection<T> values, UnaryOperator<Stream<T>> valueStreamOperations) {
33+
protected AbstractListValueCondition(Collection<T> values, UnaryOperator<Stream<T>> valueStreamTransformer) {
3434
this.values = new ArrayList<>(Objects.requireNonNull(values));
35-
this.valueStreamOperations = Objects.requireNonNull(valueStreamOperations);
35+
this.valueStreamTransformer = Objects.requireNonNull(valueStreamTransformer);
3636
}
3737

3838
public final <R> Stream<R> mapValues(Function<T, R> mapper) {
39-
return valueStreamOperations.apply(values.stream()).map(mapper);
39+
return valueStreamTransformer.apply(values.stream()).map(mapper);
4040
}
4141

4242
@Override

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.function.BiPredicate;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractTwoValueCondition;
2223

@@ -53,4 +54,9 @@ public static <T> Builder<T> isBetween(Supplier<T> valueSupplier1) {
5354
public IsBetween<T> when(BiPredicate<T, T> predicate) {
5455
return new IsBetween<>(valueSupplier1, valueSupplier2, predicate);
5556
}
57+
58+
public IsBetween<T> then(UnaryOperator<T> transformer1, UnaryOperator<T> transformer2) {
59+
return shouldRender() ? new IsBetween<>(() -> transformer1.apply(value1()),
60+
() -> transformer2.apply(value2())) : this;
61+
}
5662
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -16,6 +16,7 @@
1616
package org.mybatis.dynamic.sql.where.condition;
1717

1818
import java.util.function.Supplier;
19+
import java.util.function.UnaryOperator;
1920

2021
import org.mybatis.dynamic.sql.util.Predicates;
2122

@@ -39,4 +40,10 @@ protected IsBetweenWhenPresent<T> build() {
3940
public static <T> Builder<T> isBetweenWhenPresent(Supplier<T> valueSupplier) {
4041
return new Builder<>(valueSupplier);
4142
}
43+
44+
@Override
45+
public IsBetweenWhenPresent<T> then(UnaryOperator<T> transformer1, UnaryOperator<T> transformer2) {
46+
return shouldRender() ? new IsBetweenWhenPresent<>(() -> transformer1.apply(value1()),
47+
() -> transformer2.apply(value2())) : this;
48+
}
4249
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.function.Predicate;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

@@ -42,4 +43,8 @@ public static <T> IsEqualTo<T> of(Supplier<T> valueSupplier) {
4243
public IsEqualTo<T> when(Predicate<T> predicate) {
4344
return new IsEqualTo<>(valueSupplier, predicate);
4445
}
46+
47+
public IsEqualTo<T> then(UnaryOperator<T> transformer) {
48+
return shouldRender() ? new IsEqualTo<>(() -> transformer.apply(value())) : this;
49+
}
4550
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.Objects;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
public class IsEqualToWhenPresent<T> extends IsEqualTo<T> {
2223

@@ -27,4 +28,9 @@ protected IsEqualToWhenPresent(Supplier<T> valueSupplier) {
2728
public static <T> IsEqualToWhenPresent<T> of(Supplier<T> valueSupplier) {
2829
return new IsEqualToWhenPresent<>(valueSupplier);
2930
}
31+
32+
@Override
33+
public IsEqualToWhenPresent<T> then(UnaryOperator<T> transformer) {
34+
return shouldRender() ? new IsEqualToWhenPresent<>(() -> transformer.apply(value())) : this;
35+
}
3036
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.function.Predicate;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

@@ -42,4 +43,8 @@ public static <T> IsGreaterThan<T> of(Supplier<T> valueSupplier) {
4243
public IsGreaterThan<T> when(Predicate<T> predicate) {
4344
return new IsGreaterThan<>(valueSupplier, predicate);
4445
}
46+
47+
public IsGreaterThan<T> then(UnaryOperator<T> transformer) {
48+
return shouldRender() ? new IsGreaterThan<>(() -> transformer.apply(value())) : this;
49+
}
4550
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.function.Predicate;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

@@ -42,4 +43,8 @@ public static <T> IsGreaterThanOrEqualTo<T> of(Supplier<T> valueSupplier) {
4243
public IsGreaterThanOrEqualTo<T> when(Predicate<T> predicate) {
4344
return new IsGreaterThanOrEqualTo<>(valueSupplier, predicate);
4445
}
46+
47+
public IsGreaterThanOrEqualTo<T> then(UnaryOperator<T> transformer) {
48+
return shouldRender() ? new IsGreaterThanOrEqualTo<>(() -> transformer.apply(value())) : this;
49+
}
4550
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.Objects;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
public class IsGreaterThanOrEqualToWhenPresent<T> extends IsGreaterThanOrEqualTo<T> {
2223

@@ -27,4 +28,9 @@ protected IsGreaterThanOrEqualToWhenPresent(Supplier<T> valueSupplier) {
2728
public static <T> IsGreaterThanOrEqualToWhenPresent<T> of(Supplier<T> valueSupplier) {
2829
return new IsGreaterThanOrEqualToWhenPresent<>(valueSupplier);
2930
}
31+
32+
@Override
33+
public IsGreaterThanOrEqualToWhenPresent<T> then(UnaryOperator<T> transformer) {
34+
return shouldRender() ? new IsGreaterThanOrEqualToWhenPresent<>(() -> transformer.apply(value())) : this;
35+
}
3036
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.Objects;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
public class IsGreaterThanWhenPresent<T> extends IsGreaterThan<T> {
2223

@@ -27,4 +28,9 @@ protected IsGreaterThanWhenPresent(Supplier<T> valueSupplier) {
2728
public static <T> IsGreaterThanWhenPresent<T> of(Supplier<T> valueSupplier) {
2829
return new IsGreaterThanWhenPresent<>(valueSupplier);
2930
}
31+
32+
@Override
33+
public IsGreaterThanWhenPresent<T> then(UnaryOperator<T> transformer) {
34+
return shouldRender() ? new IsGreaterThanWhenPresent<>(() -> transformer.apply(value())) : this;
35+
}
3036
}

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
public class IsIn<T> extends AbstractListValueCondition<T> {
2828

29-
protected IsIn(Collection<T> values, UnaryOperator<Stream<T>> valueStreamOperations) {
30-
super(values, valueStreamOperations);
29+
protected IsIn(Collection<T> values, UnaryOperator<Stream<T>> valueStreamTransformer) {
30+
super(values, valueStreamTransformer);
3131
}
3232

3333
protected IsIn(Collection<T> values) {
@@ -40,8 +40,35 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
4040
+ placeholders.collect(Collectors.joining(",", "in (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
4141
}
4242

43-
public IsIn<T> withValueStreamOperations(UnaryOperator<Stream<T>> valueStreamOperations) {
44-
return new IsIn<>(values, valueStreamOperations);
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 IsIn<T> then(UnaryOperator<Stream<T>> valueStreamTransformer) {
54+
return new IsIn<>(values, valueStreamTransformer);
55+
}
56+
57+
/**
58+
* This method allows you to modify the condition's values before they are placed into the parameter map.
59+
* For example, you could filter nulls, or trim strings, etc. This process will run before final rendering of SQL.
60+
* If you filter values out of the stream, then final condition will not reference those values. If you filter all
61+
* values out of the stream, then the condition will not render.
62+
*
63+
* @param valueStreamTransformer a UnaryOperator that will transform the value stream before
64+
* the values are placed in the parameter map
65+
* @return new condition with the specified transformer
66+
*
67+
* @deprecated See {@link IsIn#then(UnaryOperator)}
68+
*/
69+
@Deprecated
70+
public IsIn<T> withValueStreamOperations(UnaryOperator<Stream<T>> valueStreamTransformer) {
71+
return then(valueStreamTransformer);
4572
}
4673

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

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ protected IsInCaseInsensitive(Collection<String> values) {
2929
super(values, s -> s.map(StringUtilities::safelyUpperCase));
3030
}
3131

32-
protected IsInCaseInsensitive(Collection<String> values, UnaryOperator<Stream<String>> valueStreamOperations) {
33-
super(values, StringUtilities.upperCaseAfter(valueStreamOperations));
32+
protected IsInCaseInsensitive(Collection<String> values, UnaryOperator<Stream<String>> valueStreamTransformer) {
33+
super(values, StringUtilities.upperCaseAfter(valueStreamTransformer));
3434
}
3535

3636
@Override
@@ -39,8 +39,35 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
3939
placeholders.collect(Collectors.joining(",", "in (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
4040
}
4141

42-
public IsInCaseInsensitive withValueStreamOperations(UnaryOperator<Stream<String>> valueStreamOperations) {
43-
return new IsInCaseInsensitive(values, valueStreamOperations);
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+
56+
/**
57+
* This method allows you to modify the condition's values before they are placed into the parameter map.
58+
* For example, you could filter nulls, or trim strings, etc. This process will run before final rendering of SQL.
59+
* If you filter values out of the stream, then final condition will not reference those values. If you filter all
60+
* values out of the stream, then the condition will not render.
61+
*
62+
* @param valueStreamTransformer a UnaryOperator that will transform the value stream before
63+
* the values are placed in the parameter map
64+
* @return new condition with the specified transformer
65+
*
66+
* @deprecated See {@link IsInCaseInsensitive#then(UnaryOperator)}
67+
*/
68+
@Deprecated
69+
public IsInCaseInsensitive withValueStreamOperations(UnaryOperator<Stream<String>> valueStreamTransformer) {
70+
return then(valueStreamTransformer);
4471
}
4572

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

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.function.Predicate;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

@@ -42,4 +43,8 @@ public static <T> IsLessThan<T> of(Supplier<T> valueSupplier) {
4243
public IsLessThan<T> when(Predicate<T> predicate) {
4344
return new IsLessThan<>(valueSupplier, predicate);
4445
}
46+
47+
public IsLessThan<T> then(UnaryOperator<T> transformer) {
48+
return shouldRender() ? new IsLessThan<>(() -> transformer.apply(value())) : this;
49+
}
4550
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -17,6 +17,7 @@
1717

1818
import java.util.function.Predicate;
1919
import java.util.function.Supplier;
20+
import java.util.function.UnaryOperator;
2021

2122
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
2223

@@ -42,4 +43,8 @@ public static <T> IsLessThanOrEqualTo<T> of(Supplier<T> valueSupplier) {
4243
public IsLessThanOrEqualTo<T> when(Predicate<T> predicate) {
4344
return new IsLessThanOrEqualTo<>(valueSupplier, predicate);
4445
}
46+
47+
public IsLessThanOrEqualTo<T> then(UnaryOperator<T> transformer) {
48+
return shouldRender() ? new IsLessThanOrEqualTo<>(() -> transformer.apply(value())) : this;
49+
}
4550
}

0 commit comments

Comments
 (0)