mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
修复Condition的Condition("discount_end_time", "!=", (String) null)方法生成SQL时,生成SQL不符合预期要求的错误(pr#4042@Github)
This commit is contained in:
@@ -285,6 +285,16 @@ public class Condition implements Cloneable, Serializable {
|
|||||||
return OPERATOR_IS.equalsIgnoreCase(this.operator);
|
return OPERATOR_IS.equalsIgnoreCase(this.operator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否 IS NOT条件
|
||||||
|
*
|
||||||
|
* @return 是否IS NOT条件
|
||||||
|
* @since 5.8.41
|
||||||
|
*/
|
||||||
|
public boolean isOperatorIsNot() {
|
||||||
|
return OPERATOR_IS_NOT.equalsIgnoreCase(this.operator);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否LIKE条件
|
* 是否LIKE条件
|
||||||
*
|
*
|
||||||
@@ -296,13 +306,18 @@ public class Condition implements Cloneable, Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查值是否为null,如果为null转换为 "IS NULL"形式
|
* 检查值是否为null,如果为null转换为 "IS NULL"或"IS NOT NULL"形式
|
||||||
*
|
*
|
||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
public Condition checkValueNull() {
|
public Condition checkValueNull() {
|
||||||
if (null == this.value) {
|
if (null == this.value) {
|
||||||
this.operator = OPERATOR_IS;
|
// 检查是否为"不等于"操作符(包括!=和<>)
|
||||||
|
if ("!=".equalsIgnoreCase(this.operator) || "<>".equalsIgnoreCase(this.operator)) {
|
||||||
|
this.operator = OPERATOR_IS_NOT;
|
||||||
|
} else {
|
||||||
|
this.operator = OPERATOR_IS;
|
||||||
|
}
|
||||||
this.value = VALUE_NULL;
|
this.value = VALUE_NULL;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@@ -375,8 +390,8 @@ public class Condition implements Cloneable, Serializable {
|
|||||||
// 类似:" (?,?,?)" 或者 " (1,2,3,4)"
|
// 类似:" (?,?,?)" 或者 " (1,2,3,4)"
|
||||||
buildValuePartForIN(conditionStrBuilder, paramValues);
|
buildValuePartForIN(conditionStrBuilder, paramValues);
|
||||||
} else {
|
} else {
|
||||||
if (isPlaceHolder() && !isOperatorIs()) {
|
if (isPlaceHolder() && !isOperatorIs() && !isOperatorIsNot()) {
|
||||||
// 使用条件表达式占位符,条件表达式并不适用于 IS NULL
|
// 使用条件表达式占位符,条件表达式并不适用于 IS NULL和 IS NOT NULL
|
||||||
conditionStrBuilder.append(" ?");
|
conditionStrBuilder.append(" ?");
|
||||||
if (null != paramValues) {
|
if (null != paramValues) {
|
||||||
paramValues.add(this.value);
|
paramValues.add(this.value);
|
||||||
|
|||||||
@@ -21,60 +21,71 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class ConditionTest {
|
public class ConditionTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toStringTest() {
|
public void toStringTest() {
|
||||||
final Condition conditionNull = new Condition("user", null);
|
final Condition conditionNull = new Condition("user", null);
|
||||||
Assertions.assertEquals("user IS NULL", conditionNull.toString());
|
assertEquals("user IS NULL", conditionNull.toString());
|
||||||
|
|
||||||
|
final Condition conditionNull2 = new Condition("user", "=", (String) null);
|
||||||
|
assertEquals("user IS NULL", conditionNull2.toString());
|
||||||
|
|
||||||
final Condition conditionNotNull = new Condition("user", "!= null");
|
final Condition conditionNotNull = new Condition("user", "!= null");
|
||||||
Assertions.assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
||||||
|
|
||||||
|
final Condition conditionNotNull2 = new Condition("user", "!=", (String) null);
|
||||||
|
assertEquals("user IS NOT NULL", conditionNotNull2.toString());
|
||||||
|
|
||||||
|
final Condition conditionNotNull3 = new Condition("user", "<>", (String) null);
|
||||||
|
assertEquals("user IS NOT NULL", conditionNotNull3.toString());
|
||||||
|
|
||||||
final Condition condition2 = new Condition("user", "= zhangsan");
|
final Condition condition2 = new Condition("user", "= zhangsan");
|
||||||
Assertions.assertEquals("user = ?", condition2.toString());
|
assertEquals("user = ?", condition2.toString());
|
||||||
|
|
||||||
final Condition conditionLike = new Condition("user", "like %aaa");
|
final Condition conditionLike = new Condition("user", "like %aaa");
|
||||||
Assertions.assertEquals("user LIKE ?", conditionLike.toString());
|
assertEquals("user LIKE ?", conditionLike.toString());
|
||||||
|
|
||||||
final Condition conditionIn = new Condition("user", "in 1,2,3");
|
final Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||||
Assertions.assertEquals("user IN (?,?,?)", conditionIn.toString());
|
assertEquals("user IN (?,?,?)", conditionIn.toString());
|
||||||
|
|
||||||
final Condition conditionBetween = new Condition("user", "between 12 and 13");
|
final Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||||
Assertions.assertEquals("user BETWEEN ? AND ?", conditionBetween.toString());
|
assertEquals("user BETWEEN ? AND ?", conditionBetween.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toStringNoPlaceHolderTest() {
|
public void toStringNoPlaceHolderTest() {
|
||||||
final Condition conditionNull = new Condition("user", null);
|
final Condition conditionNull = new Condition("user", null);
|
||||||
conditionNull.setPlaceHolder(false);
|
conditionNull.setPlaceHolder(false);
|
||||||
Assertions.assertEquals("user IS NULL", conditionNull.toString());
|
assertEquals("user IS NULL", conditionNull.toString());
|
||||||
|
|
||||||
final Condition conditionNotNull = new Condition("user", "!= null");
|
final Condition conditionNotNull = new Condition("user", "!= null");
|
||||||
conditionNotNull.setPlaceHolder(false);
|
conditionNotNull.setPlaceHolder(false);
|
||||||
Assertions.assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
||||||
|
|
||||||
final Condition conditionEquals = new Condition("user", "= zhangsan");
|
final Condition conditionEquals = new Condition("user", "= zhangsan");
|
||||||
conditionEquals.setPlaceHolder(false);
|
conditionEquals.setPlaceHolder(false);
|
||||||
Assertions.assertEquals("user = zhangsan", conditionEquals.toString());
|
assertEquals("user = zhangsan", conditionEquals.toString());
|
||||||
|
|
||||||
final Condition conditionLike = new Condition("user", "like %aaa");
|
final Condition conditionLike = new Condition("user", "like %aaa");
|
||||||
conditionLike.setPlaceHolder(false);
|
conditionLike.setPlaceHolder(false);
|
||||||
Assertions.assertEquals("user LIKE '%aaa'", conditionLike.toString());
|
assertEquals("user LIKE '%aaa'", conditionLike.toString());
|
||||||
|
|
||||||
final Condition conditionIn = new Condition("user", "in 1,2,3");
|
final Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||||
conditionIn.setPlaceHolder(false);
|
conditionIn.setPlaceHolder(false);
|
||||||
Assertions.assertEquals("user IN (1,2,3)", conditionIn.toString());
|
assertEquals("user IN (1,2,3)", conditionIn.toString());
|
||||||
|
|
||||||
final Condition conditionBetween = new Condition("user", "between 12 and 13");
|
final Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||||
conditionBetween.setPlaceHolder(false);
|
conditionBetween.setPlaceHolder(false);
|
||||||
Assertions.assertEquals("user BETWEEN 12 AND 13", conditionBetween.toString());
|
assertEquals("user BETWEEN 12 AND 13", conditionBetween.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parseTest(){
|
public void parseTest(){
|
||||||
final Condition age = Condition.parse("age", "< 10");
|
final Condition age = Condition.parse("age", "< 10");
|
||||||
Assertions.assertEquals("age < ?", age.toString());
|
assertEquals("age < ?", age.toString());
|
||||||
// issue I38LTM
|
// issue I38LTM
|
||||||
Assertions.assertSame(BigDecimal.class, age.getValue().getClass());
|
Assertions.assertSame(BigDecimal.class, age.getValue().getClass());
|
||||||
}
|
}
|
||||||
@@ -82,12 +93,12 @@ public class ConditionTest {
|
|||||||
@Test
|
@Test
|
||||||
public void parseInTest(){
|
public void parseInTest(){
|
||||||
final Condition age = Condition.parse("age", "in 1,2,3");
|
final Condition age = Condition.parse("age", "in 1,2,3");
|
||||||
Assertions.assertEquals("age IN (?,?,?)", age.toString());
|
assertEquals("age IN (?,?,?)", age.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void notInTest() {
|
void notInTest() {
|
||||||
final Condition age = Condition.parse("age", "not in 1,2,3");
|
final Condition age = Condition.parse("age", "not in 1,2,3");
|
||||||
Assertions.assertEquals("age NOT IN (?,?,?)", age.toString());
|
assertEquals("age NOT IN (?,?,?)", age.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user