Skip to content

Allow the "in when present" conditions to accept a null Collection #346

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 2 commits into from
Apr 16, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Kotlin DSL.
- Refactored the built-in conditions ([#331](https://github.com/mybatis/mybatis-dynamic-sql/pull/331)) ([#336](https://github.com/mybatis/mybatis-dynamic-sql/pull/336))
- Added composition functions for WhereApplier ([#335](https://github.com/mybatis/mybatis-dynamic-sql/pull/335))
- Added a mapping for general insert and update statements that will render null values as "null" in the SQL ([#343](https://github.com/mybatis/mybatis-dynamic-sql/pull/343))
- Allow the "in when present" conditions to accept a null Collection as a parameter ([#346](https://github.com/mybatis/mybatis-dynamic-sql/pull/346))

## Release 1.2.1 - September 29, 2020

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ static <T> IsIn<T> isInWhenPresent(T...values) {
}

static <T> IsIn<T> isInWhenPresent(Collection<T> values) {
return IsIn.of(values).filter(Objects::nonNull);
return values == null ? IsIn.empty() : IsIn.of(values).filter(Objects::nonNull);
}

@SafeVarargs
Expand All @@ -599,7 +599,7 @@ static <T> IsNotIn<T> isNotInWhenPresent(T...values) {
}

static <T> IsNotIn<T> isNotInWhenPresent(Collection<T> values) {
return IsNotIn.of(values).filter(Objects::nonNull);
return values == null ? IsNotIn.empty() : IsNotIn.of(values).filter(Objects::nonNull);
}

static <T> IsBetween.Builder<T> isBetween(T value1) {
Expand Down Expand Up @@ -722,7 +722,7 @@ static IsInCaseInsensitive isInCaseInsensitiveWhenPresent(String...values) {
}

static IsInCaseInsensitive isInCaseInsensitiveWhenPresent(Collection<String> values) {
return IsInCaseInsensitive.of(values).filter(Objects::nonNull);
return values == null ? IsInCaseInsensitive.empty() : IsInCaseInsensitive.of(values).filter(Objects::nonNull);
}

static IsNotInCaseInsensitive isNotInCaseInsensitive(String...values) {
Expand All @@ -738,7 +738,8 @@ static IsNotInCaseInsensitive isNotInCaseInsensitiveWhenPresent(String...values)
}

static IsNotInCaseInsensitive isNotInCaseInsensitiveWhenPresent(Collection<String> values) {
return IsNotInCaseInsensitive.of(values).filter(Objects::nonNull);
return values == null ? IsNotInCaseInsensitive.empty() :
IsNotInCaseInsensitive.of(values).filter(Objects::nonNull);
}

// order by support
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2021 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 @@ -21,6 +21,7 @@
import static org.mybatis.dynamic.sql.SqlBuilder.*;

import java.sql.JDBCType;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -370,4 +371,48 @@ void testNotInCaseInsensitiveWhenPresentEmptyList() {
selectModel.render(RenderingStrategies.MYBATIS3)
).withMessage("Fred");
}

@Test
void testInWhenPresentNullList() {
SelectStatementProvider selectStatement = select(column1, column3)
.from(table)
.where(column3, isInWhenPresent((Collection<String>) null))
.build()
.render(RenderingStrategies.MYBATIS3);

assertThat(selectStatement.getSelectStatement()).isEqualTo("select column1, column3 from foo");
}

@Test
void testInCaseInsensitiveWhenPresentNullList() {
SelectStatementProvider selectStatement = select(column1, column3)
.from(table)
.where(column3, isInCaseInsensitiveWhenPresent((Collection<String>) null))
.build()
.render(RenderingStrategies.MYBATIS3);

assertThat(selectStatement.getSelectStatement()).isEqualTo("select column1, column3 from foo");
}

@Test
void testNotInWhenPresentNullList() {
SelectStatementProvider selectStatement = select(column1, column3)
.from(table)
.where(column3, isNotInWhenPresent((Collection<String>) null))
.build()
.render(RenderingStrategies.MYBATIS3);

assertThat(selectStatement.getSelectStatement()).isEqualTo("select column1, column3 from foo");
}

@Test
void testNotInCaseInsensitiveWhenPresentNullList() {
SelectStatementProvider selectStatement = select(column1, column3)
.from(table)
.where(column3, isNotInCaseInsensitiveWhenPresent((Collection<String>) null))
.build()
.render(RenderingStrategies.MYBATIS3);

assertThat(selectStatement.getSelectStatement()).isEqualTo("select column1, column3 from foo");
}
}