mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser parameterized sharding support. issue #1488
This commit is contained in:
@@ -199,17 +199,22 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
final String name = x.getName();
|
||||
boolean computeSharding = isShardingSupport();
|
||||
if (computeSharding) {
|
||||
if (shardingSupport) {
|
||||
SQLObject parent = x.getParent();
|
||||
computeSharding = parent instanceof SQLExprTableSource || parent instanceof SQLPropertyExpr;
|
||||
shardingSupport = parent instanceof SQLExprTableSource || parent instanceof SQLPropertyExpr;
|
||||
}
|
||||
|
||||
if (computeSharding) {
|
||||
if (shardingSupport) {
|
||||
int pos = name.lastIndexOf('_');
|
||||
if (pos != -1 && pos != name.length() - 1) {
|
||||
boolean quote = name.charAt(0) == '`' && name.charAt(name.length() - 1) == '`';
|
||||
boolean isNumber = true;
|
||||
for (int i = pos + 1; i < name.length(); ++i) {
|
||||
|
||||
int end = name.length();
|
||||
if (quote) {
|
||||
end--;
|
||||
}
|
||||
for (int i = pos + 1; i < end; ++i) {
|
||||
char ch = name.charAt(i);
|
||||
if (ch < '0' || ch > '9') {
|
||||
isNumber = false;
|
||||
@@ -217,7 +222,8 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
}
|
||||
if (isNumber) {
|
||||
String realName = name.substring(0, pos);
|
||||
int start = quote ? 1 : 0;
|
||||
String realName = name.substring(start, pos);
|
||||
print0(realName);
|
||||
incrementReplaceCunt();
|
||||
return false;
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.alibaba.druid.bvt.sql.mysql;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 16/8/23.
|
||||
*/
|
||||
public class MySqlParameterizedOutputVisitorTest_13 extends TestCase {
|
||||
public void test_for_parameterize() throws Exception {
|
||||
final String dbType = JdbcConstants.MYSQL;
|
||||
|
||||
String sql = "select * from `user_0000` `user`";
|
||||
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType);
|
||||
assertEquals("SELECT *\n" +
|
||||
"FROM user `user`", psql);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user