Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package org.mybatis.dynamic.sql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class AbstractListValueCondition<T> implements VisitableCondition<T> {
Expand All @@ -32,12 +32,13 @@ protected AbstractListValueCondition(Collection<T> values) {
}

protected AbstractListValueCondition(Collection<T> values, UnaryOperator<Stream<T>> valueStreamTransformer) {
this.values = new ArrayList<>(Objects.requireNonNull(values));
this.valueStreamTransformer = Objects.requireNonNull(valueStreamTransformer);
this.values = valueStreamTransformer.apply(Objects.requireNonNull(values).stream())
.collect(Collectors.toList());
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 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 Down Expand Up @@ -51,7 +51,9 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
* @return new condition with the specified transformer
*/
public IsIn<T> then(UnaryOperator<Stream<T>> valueStreamTransformer) {
return new IsIn<>(values, valueStreamTransformer);
IsIn<T> answer = new IsIn<>(values, valueStreamTransformer);
answer.renderWhenEmpty = renderWhenEmpty;
return answer;
}

public static <T> IsIn<T> of(Collection<T> values) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 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 Down Expand Up @@ -50,7 +50,9 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
* @return new condition with the specified transformer
*/
public IsInCaseInsensitive then(UnaryOperator<Stream<String>> valueStreamTransformer) {
return new IsInCaseInsensitive(values, valueStreamTransformer);
IsInCaseInsensitive answer = new IsInCaseInsensitive(values, valueStreamTransformer);
answer.renderWhenEmpty = renderWhenEmpty;
return answer;
}

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-2019 the original author or authors.
* Copyright 2016-2020 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 Down Expand Up @@ -52,7 +52,9 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
* @return new condition with the specified transformer
*/
public IsNotIn<T> then(UnaryOperator<Stream<T>> valueStreamTransformer) {
return new IsNotIn<>(values, valueStreamTransformer);
IsNotIn<T> answer = new IsNotIn<>(values, valueStreamTransformer);
answer.renderWhenEmpty = renderWhenEmpty;
return answer;
}

public static <T> IsNotIn<T> of(Collection<T> values) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 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 Down Expand Up @@ -51,7 +51,9 @@ public String renderCondition(String columnName, Stream<String> placeholders) {
* @return new condition with the specified transformer
*/
public IsNotInCaseInsensitive then(UnaryOperator<Stream<String>> valueStreamTransformer) {
return new IsNotInCaseInsensitive(values, valueStreamTransformer);
IsNotInCaseInsensitive answer = new IsNotInCaseInsensitive(values, valueStreamTransformer);
answer.renderWhenEmpty = renderWhenEmpty;
return answer;
}

public static IsNotInCaseInsensitive of(Collection<String> values) {
Expand Down
92 changes: 92 additions & 0 deletions src/test/java/examples/animal/data/AnimalDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.apache.ibatis.exceptions.PersistenceException;
Expand All @@ -57,6 +59,7 @@
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
import org.mybatis.dynamic.sql.where.condition.IsIn;
import org.mybatis.dynamic.sql.where.condition.IsNotIn;
import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;

class AnimalDataTest {
Expand Down Expand Up @@ -569,6 +572,47 @@ void testInCondition() {
}
}

@Test
void testInConditionWithEventuallyEmptyList() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);

SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
.from(animalData)
.where(id, isIn(null, 22, null).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
.build()
.render(RenderingStrategies.MYBATIS3);

assertThat(selectStatement.getSelectStatement())
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData");
List<AnimalData> animals = mapper.selectMany(selectStatement);
assertThat(animals).hasSize(65);
}
}

@Test
void testInConditionWithEventuallyEmptyListForceRendering() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);

List<Integer> inValues = new ArrayList<>();
inValues.add(null);
inValues.add(22);
inValues.add(null);

SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
.from(animalData)
.where(id, IsInRequired.isIn(inValues).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
.build()
.render(RenderingStrategies.MYBATIS3);

assertThat(selectStatement.getSelectStatement())
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where id in ()");

assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> mapper.selectMany(selectStatement));
}
}

@Test
void testInConditionWithEmptyList() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Expand Down Expand Up @@ -659,6 +703,54 @@ void testNotInCaseSensitiveConditionWithNull() {
}
}

@Test
void testNotInConditionWithEventuallyEmptyList() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);

SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
.from(animalData)
.where(id, isNotIn(null, 22, null).then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
.build()
.render(RenderingStrategies.MYBATIS3);

assertThat(selectStatement.getSelectStatement())
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData");
List<AnimalData> animals = mapper.selectMany(selectStatement);
assertThat(animals).hasSize(65);
}
}

@Test
void testNotInConditionWithEventuallyEmptyListForceRendering() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);

SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
.from(animalData)
.where(id, IsNotInRequired.isNotIn(null, 22, null)
.then(s -> s.filter(Objects::nonNull).filter(i -> i != 22)))
.build()
.render(RenderingStrategies.MYBATIS3);

assertThat(selectStatement.getSelectStatement())
.isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where id not in ()");

assertThatExceptionOfType(PersistenceException.class).isThrownBy(() -> mapper.selectMany(selectStatement));
}
}

public static class IsNotInRequired<T> extends IsNotIn<T> {
protected IsNotInRequired(Collection<T> values) {
super(values);
forceRenderingWhenEmpty();
}

@SafeVarargs
public static <T> IsNotInRequired<T> isNotIn(T...values) {
return new IsNotInRequired<>(Arrays.asList(values));
}
}
@Test
void testLikeCondition() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Expand Down