mysql parser show

This commit is contained in:
温少
2012-05-16 11:28:20 +08:00
parent 7b8d158ba5
commit d2240b3513
7 changed files with 127 additions and 26 deletions
@@ -0,0 +1,24 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowProcessListStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private boolean full = false;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
public boolean isFull() {
return full;
}
public void setFull(boolean full) {
this.full = full;
}
}
@@ -87,6 +87,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStateme
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
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;
@@ -410,6 +411,14 @@ public class MySqlStatementParser extends SQLStatementParser {
if (lexer.token() == Token.FULL) {
lexer.nextToken();
if (identifierEquals("PROCESSLIST")) {
lexer.nextToken();
MySqlShowProcessListStatement stmt = new MySqlShowProcessListStatement();
stmt.setFull(true);
return stmt;
}
acceptIdentifier("COLUMNS");
MySqlShowColumnsStatement stmt = parseShowColumns();
@@ -686,12 +695,12 @@ public class MySqlStatementParser extends SQLStatementParser {
if (identifierEquals("EVENTS")) {
lexer.nextToken();
MySqlShowEventsStatement stmt = new MySqlShowEventsStatement();
if (lexer.token() == Token.FROM || lexer.token() == Token.IN) {
lexer.nextToken();
stmt.setSchema(this.exprParser.name());
}
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
stmt.setLike(this.exprParser.expr());
@@ -703,20 +712,20 @@ public class MySqlStatementParser extends SQLStatementParser {
}
return stmt;
}
if (identifierEquals("FUNCTION")) {
lexer.nextToken();
if (identifierEquals("CODE")) {
lexer.nextToken();
MySqlShowFunctionCodeStatement stmt = new MySqlShowFunctionCodeStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
acceptIdentifier("STATUS");
MySqlShowFunctionStatusStatement stmt = new MySqlShowFunctionStatusStatement();
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
stmt.setLike(this.exprParser.expr());
@@ -728,8 +737,8 @@ public class MySqlStatementParser extends SQLStatementParser {
}
return stmt;
}
//MySqlShowFunctionStatusStatement
// MySqlShowFunctionStatusStatement
if (identifierEquals("ENGINE")) {
lexer.nextToken();
@@ -753,23 +762,23 @@ public class MySqlStatementParser extends SQLStatementParser {
MySqlShowEnginesStatement stmt = new MySqlShowEnginesStatement();
return stmt;
}
if (identifierEquals("GRANTS")) {
lexer.nextToken();
MySqlShowGrantsStatement stmt = new MySqlShowGrantsStatement();
if (lexer.token() == Token.FOR) {
lexer.nextToken();
stmt.setUser(this.exprParser.expr());
}
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();
@@ -781,14 +790,14 @@ public class MySqlStatementParser extends SQLStatementParser {
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();
@@ -800,20 +809,20 @@ public class MySqlStatementParser extends SQLStatementParser {
stmt.setDatabase(database);
}
}
return stmt;
}
if (identifierEquals("OPEN")) {
lexer.nextToken();
acceptIdentifier("TABLES");
MySqlShowOpenTablesStatement stmt = new MySqlShowOpenTablesStatement();
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());
@@ -825,32 +834,32 @@ public class MySqlStatementParser extends SQLStatementParser {
}
return stmt;
}
if (identifierEquals("PLUGINS")) {
lexer.nextToken();
MySqlShowPluginsStatement stmt = new MySqlShowPluginsStatement();
return stmt;
}
if (identifierEquals("PRIVILEGES")) {
lexer.nextToken();
MySqlShowPrivilegesStatement stmt = new MySqlShowPrivilegesStatement();
return stmt;
}
if (identifierEquals("PROCEDURE")) {
lexer.nextToken();
if (identifierEquals("CODE")) {
lexer.nextToken();
MySqlShowProcedureCodeStatement stmt = new MySqlShowProcedureCodeStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
acceptIdentifier("STATUS");
MySqlShowProcedureStatusStatement stmt = new MySqlShowProcedureStatusStatement();
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
stmt.setLike(this.exprParser.expr());
@@ -863,6 +872,12 @@ public class MySqlStatementParser extends SQLStatementParser {
return stmt;
}
if (identifierEquals("PROCESSLIST")) {
lexer.nextToken();
MySqlShowProcessListStatement stmt = new MySqlShowProcessListStatement();
return stmt;
}
throw new ParserException("TODO " + lexer.stringVal());
}
@@ -78,6 +78,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStateme
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
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;
@@ -371,4 +372,8 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlShowProcedureStatusStatement x);
void endVisit(MySqlShowProcedureStatusStatement x);
boolean visit(MySqlShowProcessListStatement x);
void endVisit(MySqlShowProcessListStatement x);
}
@@ -65,6 +65,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStateme
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
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;
@@ -805,4 +806,14 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
public void endVisit(MySqlShowProcedureStatusStatement x) {
}
@Override
public boolean visit(MySqlShowProcessListStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowProcessListStatement x) {
}
}
@@ -97,6 +97,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStateme
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
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;
@@ -1963,4 +1964,19 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlShowProcedureStatusStatement x) {
}
@Override
public boolean visit(MySqlShowProcessListStatement x) {
if (x.isFull()) {
print("SHOW FULL PROCESSLIST");
} else {
print("SHOW PROCESSLIST");
}
return false;
}
@Override
public void endVisit(MySqlShowProcessListStatement x) {
}
}
@@ -74,6 +74,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStateme
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
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;
@@ -868,4 +869,14 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlShowProcedureStatusStatement x) {
}
@Override
public boolean visit(MySqlShowProcessListStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowProcessListStatement x) {
}
}
@@ -43,6 +43,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPluginsStateme
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureCodeStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcedureStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProcessListStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.Token;
@@ -810,6 +811,24 @@ public class DALParserTest extends TestCase {
Assert.assertEquals("SHOW PROCEDURE STATUS", output);
}
public void test_show_processList() throws Exception {
String sql = "SHOW processlist ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowProcessListStatement show = (MySqlShowProcessListStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW PROCESSLIST", output);
}
public void test_show_processList_1() throws Exception {
String sql = "SHOW full processlist ";
MySqlStatementParser parser = new MySqlStatementParser(sql);
MySqlShowProcessListStatement show = (MySqlShowProcessListStatement) parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(show);
Assert.assertEquals("SHOW FULL PROCESSLIST", output);
}
//
// public void testShow() throws Exception {