浏览代码

fix db bug

Looly 4 年之前
父节点
当前提交
d615b5147b

+ 2 - 1
CHANGELOG.md

@@ -3,7 +3,7 @@
 
 -------------------------------------------------------------------------------------------------------------
 
-# 5.5.9 (2021-02-20)
+# 5.5.9 (2021-02-25)
 
 ### 新特性
 * 【crypto 】     PemUtil.readPemKey支持EC(pr#1366@Github)
@@ -21,6 +21,7 @@
 * 【poi    】     修复ExcelPicUtil中图表报错问题(issue#I38857@Gitee)
 * 【core   】     修复ListUtil.page方法返回空列表无法编辑问题(issue#1415@Github)
 * 【core   】     修复ListUtil.sub中step不通结果不一致问题(issue#1409@Github)
+* 【db     】     修复Condition转换参数值时未转换数字异常(issue#I38LTM@Gitee)
 
 -------------------------------------------------------------------------------------------------------------
 

+ 20 - 3
hutool-db/src/main/java/cn/hutool/db/sql/Condition.java

@@ -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
 }

+ 20 - 12
hutool-db/src/test/java/cn/hutool/db/sql/ConditionTest.java

@@ -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());
+	}
 }