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:
@@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAlterTableDropColumnItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
@@ -48,6 +49,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDeleteStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDescribeStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropUser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlDropViewStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlExecuteStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlKillStatement;
|
||||
@@ -1736,6 +1738,42 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
protected SQLDropViewStatement parseDropView(boolean acceptDrop) {
|
||||
if (acceptDrop) {
|
||||
accept(Token.DROP);
|
||||
}
|
||||
|
||||
MySqlDropViewStatement stmt = new MySqlDropViewStatement();
|
||||
|
||||
accept(Token.VIEW);
|
||||
|
||||
if (lexer.token() == Token.IF) {
|
||||
lexer.nextToken();
|
||||
accept(Token.EXISTS);
|
||||
stmt.setIfExists(true);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
SQLName name = this.exprParser.name();
|
||||
stmt.addTableSource(name);
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (identifierEquals("RESTRICT")) {
|
||||
stmt.setOption("RESTRICT");
|
||||
lexer.nextToken();
|
||||
} else if (identifierEquals("CASCADE")) {
|
||||
stmt.setOption("CASCADE");
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLSelectParser createSQLSelectParser() {
|
||||
return new MySqlSelectParser(this.lexer);
|
||||
|
||||
@@ -30,6 +30,7 @@ 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.SQLDropViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertInto;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
@@ -131,6 +132,10 @@ public class SQLStatementParser extends SQLParser {
|
||||
SQLStatement stmt = parseDropIndex();
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else if (lexer.token() == Token.VIEW) {
|
||||
SQLStatement stmt = parseDropView(false);
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
@@ -203,6 +208,26 @@ public class SQLStatementParser extends SQLParser {
|
||||
}
|
||||
return stmt;
|
||||
}
|
||||
|
||||
protected SQLDropViewStatement parseDropView(boolean acceptDrop) {
|
||||
if (acceptDrop) {
|
||||
accept(Token.DROP);
|
||||
}
|
||||
accept(Token.VIEW);
|
||||
|
||||
SQLDropViewStatement stmt = new SQLDropViewStatement();
|
||||
|
||||
for (;;) {
|
||||
SQLName name = this.exprParser.name();
|
||||
stmt.getTableSources().add(new SQLExprTableSource(name));
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLStatement parseTruncate() {
|
||||
accept(Token.TRUNCATE);
|
||||
|
||||
@@ -199,7 +199,23 @@ public class DDLParserTest extends TestCase {
|
||||
Assert.assertEquals("RENAME TABLE tb1 TO ntb1, tb2 TO ntb2", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_drop_view_1() throws Exception {
|
||||
String sql = "DROP VIEW IF EXISTS tb1, tb2, tb3 CASCADE";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("DROP VIEW IF EXISTS tb1, tb2, tb3 CASCADE", output);
|
||||
}
|
||||
|
||||
public void test_drop_view_2() throws Exception {
|
||||
String sql = "DROP VIEW t1";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("DROP VIEW t1", output);
|
||||
}
|
||||
// public void testDDLStmt() throws Exception {
|
||||
//
|
||||
// sql = "rename table tb1 to ntb1,tb2 to ntb2";
|
||||
|
||||
Reference in New Issue
Block a user