diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlShowIndexesStatement.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlShowIndexesStatement.java new file mode 100644 index 0000000000..5eff5a0a35 --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlShowIndexesStatement.java @@ -0,0 +1,59 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.sql.dialect.mysql.ast.statement; + +import com.alibaba.druid.sql.ast.SQLName; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; +import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor; + +public class MySqlShowIndexesStatement extends MySqlStatementImpl { + + private static final long serialVersionUID = 1L; + + private SQLName table; + private SQLName database; + + public SQLName getTable() { + return table; + } + + public void setTable(SQLName table) { + if (table instanceof SQLPropertyExpr) { + SQLPropertyExpr propExpr = (SQLPropertyExpr) table; + this.setDatabase((SQLName) propExpr.getOwner()); + this.table = new SQLIdentifierExpr(propExpr.getName()); + return; + } + this.table = table; + } + + public SQLName getDatabase() { + return database; + } + + public void setDatabase(SQLName database) { + this.database = database; + } + + public void accept0(MySqlASTVisitor visitor) { + if (visitor.visit(this)) { + acceptChild(visitor, table); + acceptChild(visitor, database); + } + visitor.endVisit(this); + } +} diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlShowKeysStatement.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlShowKeysStatement.java new file mode 100644 index 0000000000..aabdbf7f50 --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlShowKeysStatement.java @@ -0,0 +1,59 @@ +/* + * Copyright 1999-2011 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.druid.sql.dialect.mysql.ast.statement; + +import com.alibaba.druid.sql.ast.SQLName; +import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr; +import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor; + +public class MySqlShowKeysStatement extends MySqlStatementImpl { + + private static final long serialVersionUID = 1L; + + private SQLName table; + private SQLName database; + + public SQLName getTable() { + return table; + } + + public void setTable(SQLName table) { + if (table instanceof SQLPropertyExpr) { + SQLPropertyExpr propExpr = (SQLPropertyExpr) table; + this.setDatabase((SQLName) propExpr.getOwner()); + this.table = new SQLIdentifierExpr(propExpr.getName()); + return; + } + this.table = table; + } + + public SQLName getDatabase() { + return database; + } + + public void setDatabase(SQLName database) { + this.database = database; + } + + public void accept0(MySqlASTVisitor visitor) { + if (visitor.visit(this)) { + acceptChild(visitor, table); + acceptChild(visitor, database); + } + visitor.endVisit(this); + } +} diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java index b7e0b7dc98..4b19a94f43 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/parser/MySqlStatementParser.java @@ -78,6 +78,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatemen import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; @@ -753,6 +755,44 @@ public class MySqlStatementParser extends SQLStatementParser { return stmt; } + + if (lexer.token() == Token.INDEX || identifierEquals("INDEXES")) { + lexer.nextToken(); + MySqlShowIndexesStatement stmt = new MySqlShowIndexesStatement(); + + if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { + lexer.nextToken(); + SQLName table = exprParser.name(); + stmt.setTable(table); + + if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { + lexer.nextToken(); + SQLName database = exprParser.name(); + stmt.setDatabase(database); + } + } + + return stmt; + } + + if (identifierEquals("KEYS")) { + lexer.nextToken(); + MySqlShowKeysStatement stmt = new MySqlShowKeysStatement(); + + if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { + lexer.nextToken(); + SQLName table = exprParser.name(); + stmt.setTable(table); + + if (lexer.token() == Token.FROM || lexer.token() == Token.IN) { + lexer.nextToken(); + SQLName database = exprParser.name(); + stmt.setDatabase(database); + } + } + + return stmt; + } throw new ParserException("TODO " + lexer.stringVal()); } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitor.java index 5f85d59180..5a0222f4cf 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitor.java @@ -69,6 +69,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatemen import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; @@ -331,4 +333,12 @@ public interface MySqlASTVisitor extends SQLASTVisitor { boolean visit(MySqlUserName x); void endVisit(MySqlUserName x); + + boolean visit(MySqlShowIndexesStatement x); + + void endVisit(MySqlShowIndexesStatement x); + + boolean visit(MySqlShowKeysStatement x); + + void endVisit(MySqlShowKeysStatement x); } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java index 5c21645b3b..b0a06e2936 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlASTVisitorAdapter.java @@ -56,6 +56,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatemen import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; @@ -717,4 +719,24 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements public void endVisit(MySqlUserName x) { } + + @Override + public boolean visit(MySqlShowIndexesStatement x) { + return true; + } + + @Override + public void endVisit(MySqlShowIndexesStatement x) { + + } + + @Override + public boolean visit(MySqlShowKeysStatement x) { + return true; + } + + @Override + public void endVisit(MySqlShowKeysStatement x) { + + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java index c8d8fdf52f..f08de11525 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlOutputVisitor.java @@ -88,6 +88,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatemen import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; @@ -1822,4 +1824,45 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV public void endVisit(MySqlUserName x) { } + + @Override + public boolean visit(MySqlShowIndexesStatement x) { + print("SHOW INDEX"); + + if (x.getTable() != null) { + print(" FROM "); + if (x.getDatabase() != null) { + x.getDatabase().accept(this); + print('.'); + } + x.getTable().accept(this); + } + + return false; + } + + @Override + public void endVisit(MySqlShowIndexesStatement x) { + + } + + @Override + public boolean visit(MySqlShowKeysStatement x) { + print("SHOW KEYS"); + + if (x.getTable() != null) { + print(" FROM "); + if (x.getDatabase() != null) { + x.getDatabase().accept(this); + print('.'); + } + x.getTable().accept(this); + } + return false; + } + + @Override + public void endVisit(MySqlShowKeysStatement x) { + + } } diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java index c6188767f2..9d0eafbb72 100644 --- a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlSchemaStatVisitor.java @@ -65,6 +65,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatemen import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement; @@ -780,4 +782,24 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS public void endVisit(MySqlUserName x) { } + + @Override + public boolean visit(MySqlShowIndexesStatement x) { + return false; + } + + @Override + public void endVisit(MySqlShowIndexesStatement x) { + + } + + @Override + public boolean visit(MySqlShowKeysStatement x) { + return false; + } + + @Override + public void endVisit(MySqlShowKeysStatement x) { + + } } diff --git a/src/test/java/com/alibaba/druid/bvt/sql/cobar/DALParserTest.java b/src/test/java/com/alibaba/druid/bvt/sql/cobar/DALParserTest.java index 6f6fb2c923..2dec0931bf 100755 --- a/src/test/java/com/alibaba/druid/bvt/sql/cobar/DALParserTest.java +++ b/src/test/java/com/alibaba/druid/bvt/sql/cobar/DALParserTest.java @@ -34,6 +34,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEventsStatemen import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionCodeStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowFunctionStatusStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowGrantsStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowIndexesStatement; +import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowKeysStatement; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; import com.alibaba.druid.sql.parser.Token; @@ -640,57 +642,62 @@ public class DALParserTest extends TestCase { Assert.assertEquals("SHOW GRANTS FOR CURRENT_USER()", output); } + public void test_show_index() throws Exception { + String sql = "SHOW index from tb1 from db"; + MySqlStatementParser parser = new MySqlStatementParser(sql); + MySqlShowIndexesStatement show = (MySqlShowIndexesStatement) parser.parseStatementList().get(0); + parser.match(Token.EOF); + String output = SQLUtils.toMySqlString(show); + Assert.assertEquals("SHOW INDEX FROM db.tb1", output); + } + + public void test_show_index_1() throws Exception { + String sql = "SHOW index in tb1 in db"; + MySqlStatementParser parser = new MySqlStatementParser(sql); + MySqlShowIndexesStatement show = (MySqlShowIndexesStatement) parser.parseStatementList().get(0); + parser.match(Token.EOF); + String output = SQLUtils.toMySqlString(show); + Assert.assertEquals("SHOW INDEX FROM db.tb1", output); + } + + public void test_show_index_2() throws Exception { + String sql = "SHOW index in db.tb1"; + MySqlStatementParser parser = new MySqlStatementParser(sql); + MySqlShowIndexesStatement show = (MySqlShowIndexesStatement) parser.parseStatementList().get(0); + parser.match(Token.EOF); + String output = SQLUtils.toMySqlString(show); + Assert.assertEquals("SHOW INDEX FROM db.tb1", output); + } + + public void test_show_key() throws Exception { + String sql = "SHOW keys from tb1 from db"; + MySqlStatementParser parser = new MySqlStatementParser(sql); + MySqlShowKeysStatement show = (MySqlShowKeysStatement) parser.parseStatementList().get(0); + parser.match(Token.EOF); + String output = SQLUtils.toMySqlString(show); + Assert.assertEquals("SHOW KEYS FROM db.tb1", output); + } + + public void test_show_key_1() throws Exception { + String sql = "SHOW keys in tb1 in db"; + MySqlStatementParser parser = new MySqlStatementParser(sql); + MySqlShowKeysStatement show = (MySqlShowKeysStatement) parser.parseStatementList().get(0); + parser.match(Token.EOF); + String output = SQLUtils.toMySqlString(show); + Assert.assertEquals("SHOW KEYS FROM db.tb1", output); + } + + public void test_show_key_2() throws Exception { + String sql = "SHOW keys in db.tb1"; + MySqlStatementParser parser = new MySqlStatementParser(sql); + MySqlShowKeysStatement show = (MySqlShowKeysStatement) parser.parseStatementList().get(0); + parser.match(Token.EOF); + String output = SQLUtils.toMySqlString(show); + Assert.assertEquals("SHOW KEYS FROM db.tb1", output); + } + // // public void testShow() throws Exception { -// -// sql = "SHOW index from tb1 from db"; -// lexer = new SQLLexer(sql); -// parser = new DALParser(lexer, new SQLExprParser(lexer)); -// show = (DALShowStatement) parser.show(); -// parser.match(Token.EOF); -// output = output2MySQL(show, sql); -// Assert.assertEquals("SHOW INDEX IN db.tb1", output); -// -// sql = "SHOW index from tb1 in db"; -// lexer = new SQLLexer(sql); -// parser = new DALParser(lexer, new SQLExprParser(lexer)); -// show = (DALShowStatement) parser.show(); -// parser.match(Token.EOF); -// output = output2MySQL(show, sql); -// Assert.assertEquals("SHOW INDEX IN db.tb1", output); -// -// sql = "SHOW index in tb1 from db"; -// lexer = new SQLLexer(sql); -// parser = new DALParser(lexer, new SQLExprParser(lexer)); -// show = (DALShowStatement) parser.show(); -// parser.match(Token.EOF); -// output = output2MySQL(show, sql); -// Assert.assertEquals("SHOW INDEX IN db.tb1", output); -// -// sql = "SHOW index in tb1 in db"; -// lexer = new SQLLexer(sql); -// parser = new DALParser(lexer, new SQLExprParser(lexer)); -// show = (DALShowStatement) parser.show(); -// parser.match(Token.EOF); -// output = output2MySQL(show, sql); -// Assert.assertEquals("SHOW INDEX IN db.tb1", output); -// -// sql = "SHOW indexes from tb1"; -// lexer = new SQLLexer(sql); -// parser = new DALParser(lexer, new SQLExprParser(lexer)); -// show = (DALShowStatement) parser.show(); -// parser.match(Token.EOF); -// output = output2MySQL(show, sql); -// Assert.assertEquals("SHOW INDEXES IN tb1", output); -// -// sql = "SHOW keys in tb1"; -// lexer = new SQLLexer(sql); -// parser = new DALParser(lexer, new SQLExprParser(lexer)); -// show = (DALShowStatement) parser.show(); -// parser.match(Token.EOF); -// output = output2MySQL(show, sql); -// Assert.assertEquals("SHOW KEYS IN tb1", output); -// // sql = "SHOW master status"; // lexer = new SQLLexer(sql); // parser = new DALParser(lexer, new SQLExprParser(lexer));