mysql parser show

This commit is contained in:
温少
2012-05-16 10:19:40 +08:00
parent 70e2b3211d
commit a047887354
8 changed files with 311 additions and 49 deletions
@@ -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);
}
}
@@ -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);
}
}
@@ -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());
}
@@ -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);
}
@@ -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) {
}
}
@@ -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) {
}
}
@@ -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) {
}
}
@@ -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));