fix NamedSql bug

This commit is contained in:
Looly 2020-10-16 00:04:45 +08:00
parent 8aec62a59b
commit 741babe2c8
3 changed files with 21 additions and 4 deletions

View File

@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.4.5 (2020-10-14)
# 5.4.5 (2020-10-16)
### 新特性
* 【core 】 ConsoleTable代码优化pr#190@Gitee
@ -27,6 +27,7 @@
* 【json 】 解决JSON中转换为double小数精度丢失问题pr#192@Gitee
* 【core 】 修复CaseInsensitiveMap的remove等方法并没有忽略大小写的问题pr#1163@Gitee
* 【poi 】 修复合并单元格值读取错误的问题
* 【poi 】 修复NamedSql解析形如col::numeric出错问题issue#I1YHBX@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -78,7 +78,7 @@ public class NamedSql {
return;
}
int len = namedSql.length();
final int len = namedSql.length();
final StrBuilder name = StrUtil.strBuilder();
final StrBuilder sqlBuilder = StrUtil.strBuilder();
@ -87,10 +87,9 @@ public class NamedSql {
for (int i = 0; i < len; i++) {
c = namedSql.charAt(i);
if (ArrayUtil.contains(NAME_START_CHARS, c)) {
nameStartChar = c;
// 新的变量开始符出现要处理之前的变量
replaceVar(nameStartChar, name, sqlBuilder, paramMap);
nameStartChar = c;
} else if (null != nameStartChar) {
// 变量状态
if (isGenerateChar(c)) {
@ -126,6 +125,10 @@ public class NamedSql {
*/
private void replaceVar(Character nameStartChar, StrBuilder name, StrBuilder sqlBuilder, Map<String, Object> paramMap){
if(name.isEmpty()){
if(null != nameStartChar){
// 类似于:的情况需要补上:
sqlBuilder.append(nameStartChar);
}
// 无变量按照普通字符处理
return;
}

View File

@ -60,6 +60,19 @@ public class NamedSqlTest {
Assert.assertEquals(sql, namedSql.getSql());
}
@Test
public void parseTest4() {
// 测试postgre中形如data_value::numeric是否出错
String sql = "select device_key, min(data_value::numeric) as data_value from device";
Map<String, Object> paramMap = MapUtil
.builder("name1", (Object)"张三")
.build();
NamedSql namedSql = new NamedSql(sql, paramMap);
Assert.assertEquals(sql, namedSql.getSql());
}
@Test
public void queryTest() throws SQLException {
Map<String, Object> paramMap = MapUtil