Skip to content

Add the ability to alter any condition value before it is placed in the parameter map #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles

### Added

- 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
- 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
- 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
- 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)
- 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)
- 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)
- 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)
- 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)
- 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)


## Release 1.1.1 - April 7, 2019
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@

public abstract class AbstractListValueCondition<T> implements VisitableCondition<T> {
protected Collection<T> values;
protected UnaryOperator<Stream<T>> valueStreamOperations;
protected UnaryOperator<Stream<T>> valueStreamTransformer;

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

protected AbstractListValueCondition(Collection<T> values, UnaryOperator<Stream<T>> valueStreamOperations) {
protected AbstractListValueCondition(Collection<T> values, UnaryOperator<Stream<T>> valueStreamTransformer) {
this.values = new ArrayList<>(Objects.requireNonNull(values));
this.valueStreamOperations = Objects.requireNonNull(valueStreamOperations);
this.valueStreamTransformer = Objects.requireNonNull(valueStreamTransformer);
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.function.BiPredicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.mybatis.dynamic.sql.AbstractTwoValueCondition;

Expand Down Expand Up @@ -53,4 +54,9 @@ public static <T> Builder<T> isBetween(Supplier<T> valueSupplier1) {
public IsBetween<T> when(BiPredicate<T, T> predicate) {
return new IsBetween<>(valueSupplier1, valueSupplier2, predicate);
}

public IsBetween<T> then(UnaryOperator<T> transformer1, UnaryOperator<T> transformer2) {
return shouldRender() ? new IsBetween<>(() -> transformer1.apply(value1()),
() -> transformer2.apply(value2())) : this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
package org.mybatis.dynamic.sql.where.condition;

import java.util.function.Supplier;
import java.util.function.UnaryOperator;

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

Expand All @@ -39,4 +40,10 @@ protected IsBetweenWhenPresent<T> build() {
public static <T> Builder<T> isBetweenWhenPresent(Supplier<T> valueSupplier) {
return new Builder<>(valueSupplier);
}

@Override
public IsBetweenWhenPresent<T> then(UnaryOperator<T> transformer1, UnaryOperator<T> transformer2) {
return shouldRender() ? new IsBetweenWhenPresent<>(() -> transformer1.apply(value1()),
() -> transformer2.apply(value2())) : this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.mybatis.dynamic.sql.AbstractSingleValueCondition;

Expand All @@ -42,4 +43,8 @@ public static <T> IsEqualTo<T> of(Supplier<T> valueSupplier) {
public IsEqualTo<T> when(Predicate<T> predicate) {
return new IsEqualTo<>(valueSupplier, predicate);
}

public IsEqualTo<T> then(UnaryOperator<T> transformer) {
return shouldRender() ? new IsEqualTo<>(() -> transformer.apply(value())) : this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.Objects;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

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

Expand All @@ -27,4 +28,9 @@ protected IsEqualToWhenPresent(Supplier<T> valueSupplier) {
public static <T> IsEqualToWhenPresent<T> of(Supplier<T> valueSupplier) {
return new IsEqualToWhenPresent<>(valueSupplier);
}

@Override
public IsEqualToWhenPresent<T> then(UnaryOperator<T> transformer) {
return shouldRender() ? new IsEqualToWhenPresent<>(() -> transformer.apply(value())) : this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.mybatis.dynamic.sql.AbstractSingleValueCondition;

Expand All @@ -42,4 +43,8 @@ public static <T> IsGreaterThan<T> of(Supplier<T> valueSupplier) {
public IsGreaterThan<T> when(Predicate<T> predicate) {
return new IsGreaterThan<>(valueSupplier, predicate);
}

public IsGreaterThan<T> then(UnaryOperator<T> transformer) {
return shouldRender() ? new IsGreaterThan<>(() -> transformer.apply(value())) : this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.mybatis.dynamic.sql.AbstractSingleValueCondition;

Expand All @@ -42,4 +43,8 @@ public static <T> IsGreaterThanOrEqualTo<T> of(Supplier<T> valueSupplier) {
public IsGreaterThanOrEqualTo<T> when(Predicate<T> predicate) {
return new IsGreaterThanOrEqualTo<>(valueSupplier, predicate);
}

public IsGreaterThanOrEqualTo<T> then(UnaryOperator<T> transformer) {
return shouldRender() ? new IsGreaterThanOrEqualTo<>(() -> transformer.apply(value())) : this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.Objects;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

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

Expand All @@ -27,4 +28,9 @@ protected IsGreaterThanOrEqualToWhenPresent(Supplier<T> valueSupplier) {
public static <T> IsGreaterThanOrEqualToWhenPresent<T> of(Supplier<T> valueSupplier) {
return new IsGreaterThanOrEqualToWhenPresent<>(valueSupplier);
}

@Override
public IsGreaterThanOrEqualToWhenPresent<T> then(UnaryOperator<T> transformer) {
return shouldRender() ? new IsGreaterThanOrEqualToWhenPresent<>(() -> transformer.apply(value())) : this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.Objects;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

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

Expand All @@ -27,4 +28,9 @@ protected IsGreaterThanWhenPresent(Supplier<T> valueSupplier) {
public static <T> IsGreaterThanWhenPresent<T> of(Supplier<T> valueSupplier) {
return new IsGreaterThanWhenPresent<>(valueSupplier);
}

@Override
public IsGreaterThanWhenPresent<T> then(UnaryOperator<T> transformer) {
return shouldRender() ? new IsGreaterThanWhenPresent<>(() -> transformer.apply(value())) : this;
}
}
35 changes: 31 additions & 4 deletions src/main/java/org/mybatis/dynamic/sql/where/condition/IsIn.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

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

protected IsIn(Collection<T> values, UnaryOperator<Stream<T>> valueStreamOperations) {
super(values, valueStreamOperations);
protected IsIn(Collection<T> values, UnaryOperator<Stream<T>> valueStreamTransformer) {
super(values, valueStreamTransformer);
}

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

public IsIn<T> withValueStreamOperations(UnaryOperator<Stream<T>> valueStreamOperations) {
return new IsIn<>(values, valueStreamOperations);
/**
* This method allows you to modify the condition's values before they are placed into the parameter map.
* For example, you could filter nulls, or trim strings, etc. This process will run before final rendering of SQL.
* If you filter values out of the stream, then final condition will not reference those values. If you filter all
* values out of the stream, then the condition will not render.
*
* @param valueStreamTransformer a UnaryOperator that will transform the value stream before
* the values are placed in the parameter map
* @return new condition with the specified transformer
*/
public IsIn<T> then(UnaryOperator<Stream<T>> valueStreamTransformer) {
return new IsIn<>(values, valueStreamTransformer);
}

/**
* This method allows you to modify the condition's values before they are placed into the parameter map.
* For example, you could filter nulls, or trim strings, etc. This process will run before final rendering of SQL.
* If you filter values out of the stream, then final condition will not reference those values. If you filter all
* values out of the stream, then the condition will not render.
*
* @param valueStreamTransformer a UnaryOperator that will transform the value stream before
* the values are placed in the parameter map
* @return new condition with the specified transformer
*
* @deprecated See {@link IsIn#then(UnaryOperator)}
*/
@Deprecated
public IsIn<T> withValueStreamOperations(UnaryOperator<Stream<T>> valueStreamTransformer) {
return then(valueStreamTransformer);
}

public static <T> IsIn<T> of(Collection<T> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ protected IsInCaseInsensitive(Collection<String> values) {
super(values, s -> s.map(StringUtilities::safelyUpperCase));
}

protected IsInCaseInsensitive(Collection<String> values, UnaryOperator<Stream<String>> valueStreamOperations) {
super(values, StringUtilities.upperCaseAfter(valueStreamOperations));
protected IsInCaseInsensitive(Collection<String> values, UnaryOperator<Stream<String>> valueStreamTransformer) {
super(values, StringUtilities.upperCaseAfter(valueStreamTransformer));
}

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

public IsInCaseInsensitive withValueStreamOperations(UnaryOperator<Stream<String>> valueStreamOperations) {
return new IsInCaseInsensitive(values, valueStreamOperations);
/**
* This method allows you to modify the condition's values before they are placed into the parameter map.
* For example, you could filter nulls, or trim strings, etc. This process will run before final rendering of SQL.
* If you filter values out of the stream, then final condition will not reference those values. If you filter all
* values out of the stream, then the condition will not render.
*
* @param valueStreamTransformer a UnaryOperator that will transform the value stream before
* the values are placed in the parameter map
* @return new condition with the specified transformer
*/
public IsInCaseInsensitive then(UnaryOperator<Stream<String>> valueStreamTransformer) {
return new IsInCaseInsensitive(values, valueStreamTransformer);
}

/**
* This method allows you to modify the condition's values before they are placed into the parameter map.
* For example, you could filter nulls, or trim strings, etc. This process will run before final rendering of SQL.
* If you filter values out of the stream, then final condition will not reference those values. If you filter all
* values out of the stream, then the condition will not render.
*
* @param valueStreamTransformer a UnaryOperator that will transform the value stream before
* the values are placed in the parameter map
* @return new condition with the specified transformer
*
* @deprecated See {@link IsInCaseInsensitive#then(UnaryOperator)}
*/
@Deprecated
public IsInCaseInsensitive withValueStreamOperations(UnaryOperator<Stream<String>> valueStreamTransformer) {
return then(valueStreamTransformer);
}

public static IsInCaseInsensitive of(Collection<String> values) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.mybatis.dynamic.sql.AbstractSingleValueCondition;

Expand All @@ -42,4 +43,8 @@ public static <T> IsLessThan<T> of(Supplier<T> valueSupplier) {
public IsLessThan<T> when(Predicate<T> predicate) {
return new IsLessThan<>(valueSupplier, predicate);
}

public IsLessThan<T> then(UnaryOperator<T> transformer) {
return shouldRender() ? new IsLessThan<>(() -> transformer.apply(value())) : this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

import org.mybatis.dynamic.sql.AbstractSingleValueCondition;

Expand All @@ -42,4 +43,8 @@ public static <T> IsLessThanOrEqualTo<T> of(Supplier<T> valueSupplier) {
public IsLessThanOrEqualTo<T> when(Predicate<T> predicate) {
return new IsLessThanOrEqualTo<>(valueSupplier, predicate);
}

public IsLessThanOrEqualTo<T> then(UnaryOperator<T> transformer) {
return shouldRender() ? new IsLessThanOrEqualTo<>(() -> transformer.apply(value())) : this;
}
}
Loading