修复SqlUtil.PATTERN_IN_CLAUSE逻辑缺陷导致in语句参数不正确的问题(pr#4203@Github)

This commit is contained in:
Looly
2025-12-31 22:14:48 +08:00
parent 69217634a8
commit 0fe68aaf57
2 changed files with 69 additions and 1 deletions

View File

@@ -49,7 +49,7 @@ public class SqlUtil {
/**
* SQL中的in语句部分的正则
*/
private static final Pattern PATTERN_IN_CLAUSE = PatternPool.get("\\s+in\\s+[(]", Pattern.CASE_INSENSITIVE);
private static final Pattern PATTERN_IN_CLAUSE = PatternPool.get("\\s+in\\s+[(]\\s*$", Pattern.CASE_INSENSITIVE);
/**
* 构件相等条件的where语句<br>

View File

@@ -0,0 +1,68 @@
package cn.hutool.v7.db.sql;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class Issue4200Test {
/**
* issues中提及的SQL
*/
private final static String TEST_SQL = "select case when 1 in (1) and 2 = any(:number) then 1 else 0 end";
/**
* 基础测试in子句测试
*/
@Test
public void isInClauseTest0() {
final String sql = "select case when 1=1 and 2 in ( ";
assertTrue(SqlUtil.isInClause(sql));
}
/**
* 基础测试in子句测试
*/
@Test
public void isInClauseTest1() {
final String sql = "select case when 1=1 and 2 in (";
assertTrue(SqlUtil.isInClause(sql));
}
/**
* 基础测试非in子句测试
*/
@Test
public void isInClauseTest2() {
// 测试基本的ORDER BY移除
//final String sql = "select case when 1 in (1) and 2 = any(:number) then 1 else 0 end";
final String sql = "select case when 1=1 and 2 = any(";
assertFalse(SqlUtil.isInClause(sql));
}
/**
* 测试 包含in但非param对应的in子句测试
*/
@Test
public void isInClauseTest3() {
// 截断前sql
//final String sql =
final String sql = "select case when 1 in (?,?,?) and 2 = any(";
assertFalse(SqlUtil.isInClause(sql));
}
/**
* 测试 包含in但非param对应的in子句测试
*/
@Test
public void isInClauseTest4() {
// 截断前sql
//final String sql =
final String sql = "select case when 1 in (?,?,?) and 2 = any(";
assertFalse(SqlUtil.isInClause(sql));
}
}