improved mysql parser add_index support. for issue #1645

This commit is contained in:
wenshao
2017-03-23 23:10:58 +08:00
parent c607356412
commit cd8b7dce11
3 changed files with 40 additions and 2 deletions
@@ -2126,6 +2126,16 @@ public class SQLStatementParser extends SQLParser {
lexer.nextToken();
} else {
item.setName(this.exprParser.name());
if (JdbcConstants.MYSQL.equals(dbType)) {
if (identifierEquals("USING")) {
lexer.nextToken();
String indexType = lexer.stringVal;
item.setType(indexType);
accept(Token.IDENTIFIER);
}
}
accept(Token.LPAREN);
}
@@ -2799,8 +2799,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
@Override
public boolean visit(SQLAlterTableAddIndex x) {
print0(ucase ? "ADD " : "add ");
if (x.getType() != null) {
print0(x.getType());
String type = x.getType();
boolean mysql = JdbcConstants.MYSQL.equals(dbType);
if (type != null && !mysql) {
print0(type);
print(' ');
}
@@ -2818,6 +2822,13 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
x.getName().accept(this);
print(' ');
}
if (type != null && mysql) {
print0(ucase ? "USING " : "using ");
print0(type);
print(' ');
}
print('(');
printAndAccept(x.getItems(), ", ");
print(')');
@@ -0,0 +1,17 @@
package com.alibaba.druid.bvt.bug;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
/**
* Created by wenshao on 23/03/2017.
*/
public class Issue1654 extends TestCase {
public void test_for_issue() throws Exception {
String sql = "ALTER TABLE db_manage.zcy_gpcatalog_node_t ADD INDEX index_code USING BTREE (code);";
String formatedSql = SQLUtils.format(sql, JdbcConstants.MYSQL);
assertEquals("ALTER TABLE db_manage.zcy_gpcatalog_node_t\n" +
"\tADD INDEX index_code USING BTREE (code);", formatedSql);
}
}