fix-bug:#I86T6H 当条件满足忽略规则,when又设置为true,NPE异常

This commit is contained in:
chenjh3 2023-10-12 17:34:32 +08:00
parent 83a22fa5b0
commit 6ffb2b23a1
2 changed files with 32 additions and 4 deletions

View File

@ -46,9 +46,22 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
//两个条件直接的连接符 //两个条件直接的连接符
protected SqlConnector connector; 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() { 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} * @return {@link QueryCondition}
*/ */
public QueryCondition when(boolean effective) { public QueryCondition when(boolean effective) {
this.effective = effective; if (notEmpty()) {
this.effective = effective;
}
return this; return this;
} }
@ -123,7 +138,9 @@ public class QueryCondition implements CloneSupport<QueryCondition> {
* @return {@link QueryCondition} * @return {@link QueryCondition}
*/ */
public QueryCondition when(BooleanSupplier fn) { public QueryCondition when(BooleanSupplier fn) {
this.effective = fn.getAsBoolean(); if (notEmpty()) {
this.effective = fn.getAsBoolean();
}
return this; return this;
} }

View File

@ -20,7 +20,6 @@ import com.mybatisflex.core.constant.SqlConnector;
import com.mybatisflex.core.query.*; import com.mybatisflex.core.query.*;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -173,4 +172,16 @@ public class DynamicConditionTest {
QueryColumnBehavior.setIgnoreFunction(Objects::isNull); 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);
}
} }