mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix db bug
This commit is contained in:
@@ -5,6 +5,7 @@ import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.text.StrSpliter;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -444,7 +445,7 @@ public class Condition extends CloneSupport<Condition> {
|
||||
return;
|
||||
}
|
||||
|
||||
valueStr = valueStr.trim();
|
||||
valueStr = StrUtil.trim(valueStr);
|
||||
|
||||
// 处理null
|
||||
if (StrUtil.endWithIgnoreCase(valueStr, "null")) {
|
||||
@@ -463,7 +464,7 @@ public class Condition extends CloneSupport<Condition> {
|
||||
}
|
||||
}
|
||||
|
||||
List<String> strs = StrUtil.split(valueStr, StrUtil.C_SPACE, 2);
|
||||
final List<String> strs = StrUtil.split(valueStr, StrUtil.C_SPACE, 2);
|
||||
if (strs.size() < 2) {
|
||||
return;
|
||||
}
|
||||
@@ -472,7 +473,8 @@ public class Condition extends CloneSupport<Condition> {
|
||||
final String firstPart = strs.get(0).trim().toUpperCase();
|
||||
if (OPERATORS.contains(firstPart)) {
|
||||
this.operator = firstPart;
|
||||
this.value = strs.get(1).trim();
|
||||
// 比较符号后跟大部分为数字,此处做转换
|
||||
this.value = tryToNumber(strs.get(1));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -526,5 +528,20 @@ public class Condition extends CloneSupport<Condition> {
|
||||
}
|
||||
return value.substring(from, to);
|
||||
}
|
||||
|
||||
/**
|
||||
* 尝试转换为数字,转换失败返回字符串
|
||||
*
|
||||
* @param value 被转换的字符串值
|
||||
* @return 转换后的值
|
||||
*/
|
||||
private static Object tryToNumber(String value){
|
||||
value = StrUtil.trim(value);
|
||||
try{
|
||||
return NumberUtil.parseNumber(value);
|
||||
} catch (Exception ignore){
|
||||
return value;
|
||||
}
|
||||
}
|
||||
// ----------------------------------------------------------------------------------------------- Private method end
|
||||
}
|
||||
|
@@ -4,52 +4,60 @@ import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConditionTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void toStringTest() {
|
||||
Condition conditionNull = new Condition("user", null);
|
||||
Assert.assertEquals("user IS NULL", conditionNull.toString());
|
||||
|
||||
|
||||
Condition conditionNotNull = new Condition("user", "!= null");
|
||||
Assert.assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
||||
|
||||
|
||||
Condition condition2 = new Condition("user", "= zhangsan");
|
||||
Assert.assertEquals("user = ?", condition2.toString());
|
||||
|
||||
|
||||
Condition conditionLike = new Condition("user", "like %aaa");
|
||||
Assert.assertEquals("user LIKE ?", conditionLike.toString());
|
||||
|
||||
|
||||
Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||
Assert.assertEquals("user IN (?,?,?)", conditionIn.toString());
|
||||
|
||||
|
||||
Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||
Assert.assertEquals("user BETWEEN ? AND ?", conditionBetween.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void toStringNoPlaceHolderTest() {
|
||||
Condition conditionNull = new Condition("user", null);
|
||||
conditionNull.setPlaceHolder(false);
|
||||
Assert.assertEquals("user IS NULL", conditionNull.toString());
|
||||
|
||||
|
||||
Condition conditionNotNull = new Condition("user", "!= null");
|
||||
conditionNotNull.setPlaceHolder(false);
|
||||
Assert.assertEquals("user IS NOT NULL", conditionNotNull.toString());
|
||||
|
||||
|
||||
Condition conditionEquals = new Condition("user", "= zhangsan");
|
||||
conditionEquals.setPlaceHolder(false);
|
||||
Assert.assertEquals("user = zhangsan", conditionEquals.toString());
|
||||
|
||||
|
||||
Condition conditionLike = new Condition("user", "like %aaa");
|
||||
conditionLike.setPlaceHolder(false);
|
||||
Assert.assertEquals("user LIKE %aaa", conditionLike.toString());
|
||||
|
||||
|
||||
Condition conditionIn = new Condition("user", "in 1,2,3");
|
||||
conditionIn.setPlaceHolder(false);
|
||||
Assert.assertEquals("user IN (1,2,3)", conditionIn.toString());
|
||||
|
||||
|
||||
Condition conditionBetween = new Condition("user", "between 12 and 13");
|
||||
conditionBetween.setPlaceHolder(false);
|
||||
Assert.assertEquals("user BETWEEN 12 AND 13", conditionBetween.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseTest(){
|
||||
final Condition age = Condition.parse("age", "< 10");
|
||||
Assert.assertEquals("age < ?", age.toString());
|
||||
// issue I38LTM
|
||||
Assert.assertSame(Long.class, age.getValue().getClass());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user