mysql parser show

This commit is contained in:
温少
2012-05-16 12:58:32 +08:00
parent 009a426ce9
commit 35ee96c6ed
9 changed files with 303 additions and 7 deletions
@@ -0,0 +1,62 @@
/*
* 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.ast.statement.MySqlSelectQueryBlock.Limit;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowRelayLogEventsStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private SQLExpr logName;
private SQLExpr from;
private Limit limit;
public SQLExpr getLogName() {
return logName;
}
public void setLogName(SQLExpr logName) {
this.logName = logName;
}
public SQLExpr getFrom() {
return from;
}
public void setFrom(SQLExpr from) {
this.from = from;
}
public Limit getLimit() {
return limit;
}
public void setLimit(Limit limit) {
this.limit = limit;
}
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, logName);
acceptChild(visitor, from);
acceptChild(visitor, limit);
}
visitor.endVisit(this);
}
}
@@ -0,0 +1,13 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowSlaveHostsStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
}
@@ -0,0 +1,13 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowSlaveStatusStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
}
@@ -90,6 +90,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatu
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -879,17 +882,17 @@ public class MySqlStatementParser extends SQLStatementParser {
MySqlShowProcessListStatement stmt = new MySqlShowProcessListStatement();
return stmt;
}
if (identifierEquals("PROFILES")) {
lexer.nextToken();
MySqlShowProfilesStatement stmt = new MySqlShowProfilesStatement();
return stmt;
}
if (identifierEquals("PROFILE")) {
lexer.nextToken();
MySqlShowProfileStatement stmt = new MySqlShowProfileStatement();
for (;;) {
if (lexer.token() == Token.ALL) {
stmt.getTypes().add(MySqlShowProfileStatement.Type.ALL);
@@ -924,25 +927,58 @@ public class MySqlStatementParser extends SQLStatementParser {
} else {
break;
}
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
break;
}
if (lexer.token() == Token.FOR) {
lexer.nextToken();
acceptIdentifier("QUERY");
stmt.setForQuery(this.exprParser.primary());
}
stmt.setLimit(this.parseLimit());
return stmt;
}
if (identifierEquals("RELAYLOG")) {
lexer.nextToken();
acceptIdentifier("EVENTS");
MySqlShowRelayLogEventsStatement stmt = new MySqlShowRelayLogEventsStatement();
if (lexer.token() == Token.IN) {
lexer.nextToken();
stmt.setLogName(this.exprParser.primary());
}
if (lexer.token() == Token.FROM) {
lexer.nextToken();
stmt.setFrom(this.exprParser.primary());
}
stmt.setLimit(this.parseLimit());
return stmt;
}
if (identifierEquals("SLAVE")) {
lexer.nextToken();
if (identifierEquals("STATUS")) {
lexer.nextToken();
return new MySqlShowSlaveStatusStatement();
} else {
acceptIdentifier("HOSTS");
MySqlShowSlaveHostsStatement stmt = new MySqlShowSlaveHostsStatement();
return stmt;
}
}
// MySqlShowSlaveHostsStatement
throw new ParserException("TODO " + lexer.stringVal());
}
@@ -81,6 +81,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatu
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -386,4 +389,16 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlShowProfilesStatement x);
void endVisit(MySqlShowProfilesStatement x);
boolean visit(MySqlShowRelayLogEventsStatement x);
void endVisit(MySqlShowRelayLogEventsStatement x);
boolean visit(MySqlShowSlaveHostsStatement x);
void endVisit(MySqlShowSlaveHostsStatement x);
boolean visit(MySqlShowSlaveStatusStatement x);
void endVisit(MySqlShowSlaveStatusStatement x);
}
@@ -68,6 +68,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatu
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -838,4 +841,34 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
public void endVisit(MySqlShowProfilesStatement x) {
}
@Override
public boolean visit(MySqlShowRelayLogEventsStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowRelayLogEventsStatement x) {
}
@Override
public boolean visit(MySqlShowSlaveHostsStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowSlaveHostsStatement x) {
}
@Override
public boolean visit(MySqlShowSlaveStatusStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowSlaveStatusStatement x) {
}
}
@@ -100,6 +100,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatu
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -2021,4 +2024,53 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlShowProfilesStatement x) {
}
@Override
public boolean visit(MySqlShowRelayLogEventsStatement x) {
print("SHOW RELAYLOG EVENTS");
if (x.getLogName() != null) {
print(" IN ");
x.getLogName().accept(this);
}
if (x.getFrom() != null) {
print(" FROM ");
x.getFrom().accept(this);
}
if (x.getLimit() != null) {
print(' ');
x.getLimit().accept(this);
}
return false;
}
@Override
public void endVisit(MySqlShowRelayLogEventsStatement x) {
}
@Override
public boolean visit(MySqlShowSlaveHostsStatement x) {
print("SHOW SLAVE HOSTS");
return false;
}
@Override
public void endVisit(MySqlShowSlaveHostsStatement x) {
}
@Override
public boolean visit(MySqlShowSlaveStatusStatement x) {
print("SHOW SLAVE STATUS");
return false;
}
@Override
public void endVisit(MySqlShowSlaveStatusStatement x) {
}
}
@@ -77,6 +77,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatu
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -901,4 +904,34 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlShowProfilesStatement x) {
}
@Override
public boolean visit(MySqlShowRelayLogEventsStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowRelayLogEventsStatement x) {
}
@Override
public boolean visit(MySqlShowSlaveHostsStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowSlaveHostsStatement x) {
}
@Override
public boolean visit(MySqlShowSlaveStatusStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowSlaveStatusStatement x) {
}
}
@@ -46,6 +46,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatu
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.Token;
@@ -860,6 +863,42 @@ public class DALParserTest extends TestCase {
Assert.assertEquals("SHOW PROFILE ALL, BLOCK IO, CONTEXT SWITCHES, CPU, IPC, MEMORY, PAGE FAULTS, SOURCE, SWAPS FOR QUERY 2 LIMIT 2, 1", output);
}
public void test_show_relayLogEvents() throws Exception {
String sql = "SHOW RELAYLOG EVENTS";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowRelayLogEventsStatement show = (MySqlShowRelayLogEventsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW RELAYLOG EVENTS", output);
}
public void test_show_relayLogEvents_1() throws Exception {
String sql = "SHOW RELAYLOG EVENTS IN 'x' from 3 limit 5,6";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowRelayLogEventsStatement show = (MySqlShowRelayLogEventsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW RELAYLOG EVENTS IN 'x' FROM 3 LIMIT 5, 6", output);
}
public void test_show_slaveHosts() throws Exception {
String sql = "SHOW SLAVE HOSTS";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowSlaveHostsStatement show = (MySqlShowSlaveHostsStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW SLAVE HOSTS", output);
}
public void test_show_slaveStatus() throws Exception {
String sql = "SHOW SLAVE Status";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowSlaveStatusStatement show = (MySqlShowSlaveStatusStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW SLAVE STATUS", output);
}
//
// public void testShow() throws Exception {
//