mysql parser show

This commit is contained in:
温少
2012-05-16 13:14:38 +08:00
parent 2c97e4b300
commit 029ec5f034
7 changed files with 158 additions and 0 deletions
@@ -0,0 +1,61 @@
/*
* 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.SQLExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowTableStatusStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private SQLExpr database;
private SQLExpr like;
private SQLExpr where;
public SQLExpr getDatabase() {
return database;
}
public void setDatabase(SQLExpr database) {
this.database = database;
}
public SQLExpr getLike() {
return like;
}
public void setLike(SQLExpr like) {
this.like = like;
}
public SQLExpr getWhere() {
return where;
}
public void setWhere(SQLExpr where) {
this.where = where;
}
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, database);
acceptChild(visitor, like);
acceptChild(visitor, where);
}
visitor.endVisit(this);
}
}
@@ -94,6 +94,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEvents
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement;
@@ -997,6 +998,28 @@ public class MySqlStatementParser extends SQLStatementParser {
return stmt;
}
}
if (lexer.token() == Token.TABLE) {
lexer.nextToken();
acceptIdentifier("STATUS");
MySqlShowTableStatusStatement stmt = new MySqlShowTableStatusStatement();
if (lexer.token() == Token.FROM || lexer.token() == Token.IN) {
lexer.nextToken();
stmt.setDatabase(this.exprParser.name());
}
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
stmt.setLike(this.exprParser.expr());
}
if (lexer.token() == Token.WHERE) {
lexer.nextToken();
stmt.setWhere(this.exprParser.expr());
}
return stmt;
}
// MySqlShowSlaveHostsStatement
throw new ParserException("TODO " + lexer.stringVal());
@@ -85,6 +85,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEvents
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement;
@@ -401,4 +402,8 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlShowSlaveStatusStatement x);
void endVisit(MySqlShowSlaveStatusStatement x);
boolean visit(MySqlShowTableStatusStatement x);
void endVisit(MySqlShowTableStatusStatement x);
}
@@ -72,6 +72,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEvents
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement;
@@ -871,4 +872,14 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
public void endVisit(MySqlShowSlaveStatusStatement x) {
}
@Override
public boolean visit(MySqlShowTableStatusStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowTableStatusStatement x) {
}
}
@@ -104,6 +104,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEvents
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement;
@@ -2073,4 +2074,30 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlShowSlaveStatusStatement x) {
}
@Override
public boolean visit(MySqlShowTableStatusStatement x) {
print("SHOW TABLE STATUS");
if (x.getDatabase() != null) {
print(" FROM ");
x.getDatabase().accept(this);
}
if (x.getLike() != null) {
print(" LIKE ");
x.getLike().accept(this);
}
if (x.getWhere() != null) {
print(" WHERE ");
x.getWhere().accept(this);
}
return false;
}
@Override
public void endVisit(MySqlShowTableStatusStatement x) {
}
}
@@ -81,6 +81,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEvents
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStartTransactionStatement;
@@ -934,4 +935,14 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlShowSlaveStatusStatement x) {
}
@Override
public boolean visit(MySqlShowTableStatusStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowTableStatusStatement x) {
}
}
@@ -49,6 +49,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatem
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowRelayLogEventsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveHostsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowSlaveStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTableStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.Token;
@@ -899,6 +901,24 @@ public class DALParserTest extends TestCase {
Assert.assertEquals("SHOW SLAVE STATUS", output);
}
public void test_show_status() throws Exception {
String sql = "SHOW STATUS LIKE 'Key%'";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowStatusStatement show = (MySqlShowStatusStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW STATUS LIKE 'Key%'", output);
}
public void test_show_table_status() throws Exception {
String sql = "SHOW TABLE STATUS FROM mysql";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowTableStatusStatement show = (MySqlShowTableStatusStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW TABLE STATUS FROM mysql", output);
}
//
// public void testShow() throws Exception {
//