Skip to content

Support custom conditional rendering on all conditions #68

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 1 commit into from
Dec 24, 2018
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
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<junit.jupiter.version>5.3.0</junit.jupiter.version>
<junit.platform.version>1.3.0</junit.platform.version>
<junit.jupiter.version>5.3.2</junit.jupiter.version>
<junit.platform.version>1.3.2</junit.platform.version>
<clirr.comparisonVersion>1.1.0</clirr.comparisonVersion>
</properties>

Expand Down Expand Up @@ -105,7 +105,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.9.RELEASE</version>
<version>5.1.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,30 @@
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

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

protected AbstractListValueCondition(AbstractBuilder<T, ?> builder) {
values = Objects.requireNonNull(builder.values);
protected AbstractListValueCondition(List<T> values) {
this(values, UnaryOperator.identity());
}

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

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

@Override
public <R> R accept(ConditionVisitor<T, R> visitor) {
return visitor.visit(this);
}

/**
* This method allows subclasses to alter the value before it is placed
* into the parameter map. An example of this is when the case insensitive
* conditions will change a value to upper case.
*
* <p>We do not expose the values stream because we cannot allow subclasses
* to change the order or number of values.
*
* @param value the value
* @return the mapped value - in most cases the value is not changed
*/
protected T mapValue(T value) {
return value;
}

public abstract String renderCondition(String columnName, Stream<String> placeholders);

public abstract static class AbstractBuilder<T, B extends AbstractBuilder<T, B>> {
private List<T> values = new ArrayList<>();

public B withValues(List<T> values) {
this.values.addAll(values);
return getThis();
}

public abstract B getThis();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 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 @@ -15,8 +15,26 @@
*/
package org.mybatis.dynamic.sql;

import java.util.Objects;
import java.util.function.BooleanSupplier;

public abstract class AbstractNoValueCondition<T> implements VisitableCondition<T> {

private BooleanSupplier booleanSupplier;

protected AbstractNoValueCondition() {
booleanSupplier = () -> true;
}

protected AbstractNoValueCondition(BooleanSupplier booleanSupplier) {
this.booleanSupplier = Objects.requireNonNull(booleanSupplier);
}

@Override
public boolean shouldRender() {
return booleanSupplier.getAsBoolean();
}

@Override
public <R> R accept(ConditionVisitor<T,R> visitor) {
return visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 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,19 +16,32 @@
package org.mybatis.dynamic.sql;

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

public abstract class AbstractSingleValueCondition<T> implements VisitableCondition<T> {
private Supplier<T> valueSupplier;
protected Supplier<T> valueSupplier;
private Predicate<T> predicate;

protected AbstractSingleValueCondition(Supplier<T> valueSupplier) {
this.valueSupplier = Objects.requireNonNull(valueSupplier);
predicate = v -> true;
}

protected AbstractSingleValueCondition(Supplier<T> valueSupplier, Predicate<T> predicate) {
this.valueSupplier = Objects.requireNonNull(valueSupplier);
this.predicate = Objects.requireNonNull(predicate);
}

public T value() {
return valueSupplier.get();
}

@Override
public boolean shouldRender() {
return predicate.test(value());
}

@Override
public <R> R accept(ConditionVisitor<T,R> visitor) {
return visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 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,15 +16,24 @@
package org.mybatis.dynamic.sql;

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

public abstract class AbstractTwoValueCondition<T> implements VisitableCondition<T> {
private Supplier<T> valueSupplier1;
private Supplier<T> valueSupplier2;
protected Supplier<T> valueSupplier1;
protected Supplier<T> valueSupplier2;
private BiPredicate<T, T> predicate;

protected AbstractTwoValueCondition(Supplier<T> valueSupplier1, Supplier<T> valueSupplier2) {
this.valueSupplier1 = Objects.requireNonNull(valueSupplier1);
this.valueSupplier2 = Objects.requireNonNull(valueSupplier2);
predicate = (v1, v2) -> true;
}

protected AbstractTwoValueCondition(Supplier<T> valueSupplier1, Supplier<T> valueSupplier2,
BiPredicate<T, T> predicate) {
this(valueSupplier1, valueSupplier2);
this.predicate = Objects.requireNonNull(predicate);
}

public T value1() {
Expand All @@ -34,7 +43,12 @@ public T value1() {
public T value2() {
return valueSupplier2.get();
}


@Override
public boolean shouldRender() {
return predicate.test(value1(), value2());
}

@Override
public <R> R accept(ConditionVisitor<T,R> visitor) {
return visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public enum JoinType {

private String shortType;

private JoinType() {
JoinType() {
}

private JoinType(String shortType) {
JoinType(String shortType) {
this.shortType = shortType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public boolean hasMultipleFragments() {
return fragments.size() > 1;
}

public boolean isEmpty() {
return fragments.isEmpty();
}

public static Collector<FragmentAndParameters, FragmentCollector, FragmentCollector> collect() {
return Collector.of(FragmentCollector::new,
FragmentCollector::add,
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/util/Predicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2016-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util;

import java.util.function.BiPredicate;

public class Predicates {
private Predicates() {}

public static <T> BiPredicate<T, T> bothPresent() {
return (v1, v2) -> v1 != null && v2 != null;
}
}
13 changes: 12 additions & 1 deletion src/main/java/org/mybatis/dynamic/sql/util/StringUtilities.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2018 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,8 @@
package org.mybatis.dynamic.sql.util;

import java.util.Optional;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

public interface StringUtilities {

Expand All @@ -36,4 +38,13 @@ static String spaceBefore(Optional<String> in) {
static String spaceBefore(String in) {
return " " + in; //$NON-NLS-1$
}

static String safelyUpperCase(String s) {
return s == null ? null : s.toUpperCase();
}

static UnaryOperator<Stream<String>> upperCaseAfter(UnaryOperator<Stream<String>> valueModifier) {
UnaryOperator<Stream<String>> ua = s -> s.map(StringUtilities::safelyUpperCase);
return t -> ua.apply(valueModifier.apply(t));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2016-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.where.condition;

import java.util.function.Supplier;

/**
* Utility class supporting the "and" part of a between condition. This class supports builders,
* so it is mutable.
*
* @author Jeff Butler
*
* @param <T> the type of field for the between condition
* @param <R> the type of condition being built
*/
public abstract class AndGatherer<T, R> {
protected Supplier<T> valueSupplier1;
protected Supplier<T> valueSupplier2;

protected AndGatherer(Supplier<T> valueSupplier1) {
this.valueSupplier1 = valueSupplier1;
}

public R and(T value2) {
return and(() -> value2);
}

public R and(Supplier<T> valueSupplier2) {
this.valueSupplier2 = valueSupplier2;
return build();
}

protected abstract R build();
}
Loading