mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
mysql parser ddl support
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLDropIndexStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private SQLExpr indexName;
|
||||
private SQLExpr tableName;
|
||||
|
||||
public SQLExpr getIndexName() {
|
||||
return indexName;
|
||||
}
|
||||
|
||||
public void setIndexName(SQLExpr indexName) {
|
||||
this.indexName = indexName;
|
||||
}
|
||||
|
||||
public SQLExpr getTableName() {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public void setTableName(SQLExpr tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, indexName);
|
||||
acceptChild(visitor, tableName);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertInto;
|
||||
@@ -126,6 +127,10 @@ public class SQLStatementParser extends SQLParser {
|
||||
SQLStatement stmt = parseDropUser();
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.INDEX) {
|
||||
SQLStatement stmt = parseDropIndex();
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
@@ -136,7 +141,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.USE) {
|
||||
SQLStatement stmt = parseUse();
|
||||
statementList.add(stmt);
|
||||
@@ -156,14 +161,14 @@ public class SQLStatementParser extends SQLParser {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public SQLUseStatement parseUse() {
|
||||
accept(Token.USE);
|
||||
SQLUseStatement stmt = new SQLUseStatement();
|
||||
stmt.setDatabase(this.exprParser.name());
|
||||
return stmt;
|
||||
}
|
||||
|
||||
|
||||
public SQLStatement parseAlter() {
|
||||
accept(Token.ALTER);
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
@@ -213,7 +218,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
|
||||
public SQLStatement parseInsert() {
|
||||
SQLInsertStatement insertStatement = new SQLInsertStatement();
|
||||
|
||||
|
||||
if (lexer.token() == Token.INSERT) {
|
||||
accept(Token.INSERT);
|
||||
}
|
||||
@@ -276,6 +281,15 @@ public class SQLStatementParser extends SQLParser {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
|
||||
public SQLStatement parseDropIndex() throws ParserException {
|
||||
accept(Token.INDEX);
|
||||
SQLDropIndexStatement stmt = new SQLDropIndexStatement();
|
||||
stmt.setIndexName(this.exprParser.name());
|
||||
accept(Token.ON);
|
||||
stmt.setTableName(this.exprParser.name());
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLCallStatement parseCall() throws ParserException {
|
||||
acceptIdentifier("CALL");
|
||||
|
||||
@@ -314,7 +328,6 @@ public class SQLStatementParser extends SQLParser {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public SQLStatement parseCreate() throws ParserException {
|
||||
char mark_ch = lexer.current();
|
||||
int mark_bp = lexer.bp();
|
||||
@@ -405,9 +418,9 @@ public class SQLStatementParser extends SQLParser {
|
||||
return udpateStatement;
|
||||
}
|
||||
|
||||
protected SQLUpdateStatement createUpdateStatement() {
|
||||
return new SQLUpdateStatement();
|
||||
}
|
||||
protected SQLUpdateStatement createUpdateStatement() {
|
||||
return new SQLUpdateStatement();
|
||||
}
|
||||
|
||||
public SQLDeleteStatement parseDeleteStatement() throws ParserException {
|
||||
SQLDeleteStatement deleteStatement = new SQLDeleteStatement();
|
||||
|
||||
@@ -70,6 +70,7 @@ import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource.JoinType;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableAddColumn;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
@@ -1026,4 +1027,13 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
public void endVisit(SQLAlterTableAddColumn x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropIndexStatement x) {
|
||||
print("DROP INDEX ");
|
||||
x.getIndexName().accept(this);
|
||||
print(" ON ");
|
||||
x.getTableName().accept(this);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
@@ -345,4 +346,8 @@ public interface SQLASTVisitor {
|
||||
boolean visit(SQLAlterTableDropColumnItem x);
|
||||
|
||||
void endVisit(SQLAlterTableDropColumnItem x);
|
||||
|
||||
boolean visit(SQLDropIndexStatement x);
|
||||
|
||||
void endVisit(SQLDropIndexStatement x);
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
@@ -606,4 +607,14 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
public void endVisit(SQLAlterTableDropColumnItem x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropIndexStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLDropIndexStatement x) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,6 +135,25 @@ public class DDLParserTest extends TestCase {
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("CREATE SPATIAL INDEX index_name ON tb (col(id))", output);
|
||||
}
|
||||
|
||||
public void test_drop_index_0() throws Exception {
|
||||
String sql = "drop index index_name on tb1";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("DROP INDEX index_name ON tb1", output);
|
||||
}
|
||||
|
||||
public void test_drop_index_1() throws Exception {
|
||||
String sql = "DROP INDEX `PRIMARY` ON t;";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("DROP INDEX `PRIMARY` ON t", output);
|
||||
}
|
||||
|
||||
|
||||
// public void testDDLStmt() throws Exception {
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user