!366 修复当条件满足忽略规则,when又设置为true,NPE异常

Merge pull request !366 from 简单风/fix-bug-I86T6H
This commit is contained in:
Michael Yang 2023-10-12 09:48:19 +00:00 committed by Gitee
commit 80d67103ad
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 32 additions and 4 deletions

View File

@ -46,9 +46,22 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
//两个条件直接的连接符
protected SqlConnector connector;
/**
* 是否为空条件默认false
*/
private boolean empty = false;
protected boolean notEmpty() {
return !empty;
}
protected QueryCondition setEmpty(boolean empty) {
this.empty = empty;
return this;
}
public static QueryCondition createEmpty() {
return new QueryCondition().when(false);
return new QueryCondition().when(false).setEmpty(true);
}
@ -111,7 +124,9 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
* @return {@link QueryCondition}
*/
public QueryCondition when(boolean effective) {
if (notEmpty()) {
this.effective = effective;
}
return this;
}
@ -123,7 +138,9 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
* @return {@link QueryCondition}
*/
public QueryCondition when(BooleanSupplier fn) {
if (notEmpty()) {
this.effective = fn.getAsBoolean();
}
return this;
}

View File

@ -20,7 +20,6 @@ import com.mybatisflex.core.constant.SqlConnector;
import com.mybatisflex.core.query.*;
import com.mybatisflex.core.util.StringUtil;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@ -173,4 +172,16 @@ public class DynamicConditionTest {
QueryColumnBehavior.setIgnoreFunction(Objects::isNull);
}
@Test
public void test10() {
QueryWrapper queryWrapper = QueryWrapper.create()
.select()
.from(ACCOUNT)
// 满足忽略规则但动态条件又是true
.where(ACCOUNT.USER_NAME.eq(null).when(true));
String sql = queryWrapper.toSQL();
System.out.println(sql);
assertEquals("SELECT * FROM `tb_account`", sql);
}
}