Skip to content

Commit

Permalink
select in sql参数化的时候支持带加号的数字归一化成1个问号 #5381
Browse files Browse the repository at this point in the history
select in sql参数化的时候支持带加号的数字归一化成1个问号 #5381
顺便修复前面兼容taosdata数据库的commit导致mvn validate出错的问题
mvn validate验证通过,单测验证通过,无新增错误
  • Loading branch information
lizongbo authored and wenshao committed Oct 10, 2023
1 parent dbdd534 commit 56300eb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ public PhysicalConnectionInfo createPhysicalConnection() throws SQLException {
if (socketTimeout > 0) {
physicalConnectProperties.put("socketTimeout", Long.toString(TimeUnit.MILLISECONDS.toSeconds(socketTimeout)));
}
} else if (dbTypeName!=null && DbType.sqlserver.name().equals(dbTypeName)) {
} else if (dbTypeName != null && DbType.sqlserver.name().equals(dbTypeName)) {
// see https://learn.microsoft.com/en-us/sql/connect/jdbc/setting-the-connection-properties?view=sql-server-ver16
physicalConnectProperties.put("loginTimeout", Long.toString(TimeUnit.MILLISECONDS.toSeconds(connectTimeout)));
if (socketTimeout > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,8 @@ public boolean visit(SQLInListExpr x) {

boolean allLiteral = true;
for (SQLExpr item : targetList) {
if (!(item instanceof SQLLiteralExpr || item instanceof SQLVariantRefExpr)) {
if (!((item instanceof SQLUnaryExpr && ((SQLUnaryExpr) item).getExpr() instanceof SQLLiteralExpr) || item instanceof SQLLiteralExpr
|| item instanceof SQLVariantRefExpr)) {
if (item instanceof SQLListExpr) {
SQLListExpr list = (SQLListExpr) item;
for (SQLExpr listItem : list.getItems()) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/alibaba/druid/util/JdbcUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ public static DbType getDbTypeRaw(String rawUrl, String driverClassName) {
return DbType.greenplum;
} else if (rawUrl.startsWith("jdbc:opengauss:") || rawUrl.startsWith("jdbc:gaussdb:") || rawUrl.startsWith("jdbc:dws:iam:")) {
return DbType.gaussdb;
} else if (rawUrl.startsWith("jdbc:TAOS:")||rawUrl.startsWith("jdbc:TAOS-RS:")){
} else if (rawUrl.startsWith("jdbc:TAOS:") || rawUrl.startsWith("jdbc:TAOS-RS:")) {
return DbType.taosdata;
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.alibaba.druid.bvt.sql.mysql.issues;

import java.util.Map;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author lizongbo
* @see <a href="https://github.com/alibaba/druid/issues/5381">Issue来源</a>
*/
public class Issue5381 {

@Test
public void test_select_in_parameterized() throws Exception {
for (DbType dbType : new DbType[]{DbType.mysql}) {
String[] orgSqls = new String[]{
"select aaa from table_demo where id in (100, 200, 300)",
"select aaa from table_demo where id in (ccc, 200, 300)",
"select aaa from table_demo where id in (+ccc, 200, 300)",
"select bbb from table_demo where id in (+100, 200, 300, 400, 500)",
"select bbb from table_demo where id in (+100.02, 200, 300.0, 400, 500)",
"select aaa from table_demo where id in (100, +200, 300)",
"select aaa from table_demo where id in (-100, 200, 300)",
"select aaa from table_demo where id in (100, -200, 300)",
"select aaa from table_demo where id in (100, 200, -300)",
};
String[] parameterizedSqls = new String[]{
"SELECT aaa FROM table_demo WHERE id IN (?)",
"SELECT aaa FROM table_demo WHERE id IN (ccc, ?, ?)",
"SELECT aaa FROM table_demo WHERE id IN (+ccc, ?, ?)",
"SELECT bbb FROM table_demo WHERE id IN (?)",
"SELECT bbb FROM table_demo WHERE id IN (?)",
"SELECT aaa FROM table_demo WHERE id IN (?)",
"SELECT aaa FROM table_demo WHERE id IN (?)",
"SELECT aaa FROM table_demo WHERE id IN (?)",
"SELECT aaa FROM table_demo WHERE id IN (?)",
};
for (int i = 0; i < orgSqls.length; i++) {
String sql = orgSqls[i];
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
SQLStatement statement = parser.parseStatement();
System.out.println("原始的sql===" + sql);
//String newSql = statement.toString().replace("\n", " ").replace('\'', '"') + ";";
// System.out.println("生成的sql===" + newSql);
//String formatSql = SQLUtils.format(sql, dbType).replace("\n", " ").replace('\'', '"') + ";";
//System.out.println("格式化sql===" + formatSql);
String parameterizedSql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType).replace("\n", " ").replace('\'', '"');
System.out.println("归一化sql===" + parameterizedSql);
assertEquals(parameterizedSqls[i], parameterizedSql);
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(dbType);
statement.accept(visitor);
System.out.println("getTables==" + visitor.getTables());
Map<TableStat.Name, TableStat> tableMap = visitor.getTables();
assertFalse(tableMap.isEmpty());

}
}
}
}

0 comments on commit 56300eb

Please sign in to comment.