mysql parser show

This commit is contained in:
温少
2012-05-16 12:47:00 +08:00
parent 009a426ce9
commit 224020733a
7 changed files with 157 additions and 0 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);
}
}
@@ -90,6 +90,7 @@ 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.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -942,6 +943,26 @@ public class MySqlStatementParser extends SQLStatementParser {
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;
}
throw new ParserException("TODO " + lexer.stringVal());
}
@@ -81,6 +81,7 @@ 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.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -386,4 +387,8 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlShowProfilesStatement x);
void endVisit(MySqlShowProfilesStatement x);
boolean visit(MySqlShowRelayLogEventsStatement x);
void endVisit(MySqlShowRelayLogEventsStatement x);
}
@@ -68,6 +68,7 @@ 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.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -838,4 +839,14 @@ 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) {
}
}
@@ -100,6 +100,7 @@ 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.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -2021,4 +2022,31 @@ 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) {
}
}
@@ -77,6 +77,7 @@ 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.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowWarningsStatement;
@@ -901,4 +902,14 @@ 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) {
}
}
@@ -46,6 +46,7 @@ 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.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.Token;
@@ -860,6 +861,24 @@ 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 testShow() throws Exception {
//