improved sql parser parameterized sharding support. issue #1488

This commit is contained in:
wenshao
2016-11-19 23:32:46 +08:00
parent df440aa78c
commit 4a273c6fe4
2 changed files with 31 additions and 6 deletions
@@ -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;
@@ -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);
}
}