mysql parser show

This commit is contained in:
温少
2012-05-16 12:37:10 +08:00
parent d2240b3513
commit 009a426ce9
8 changed files with 264 additions and 24 deletions
@@ -0,0 +1,60 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import java.util.ArrayList;
import java.util.List;
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 MySqlShowProfileStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private List<Type> types = new ArrayList<Type>();
private SQLExpr forQuery;
private Limit limit;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
public List<Type> getTypes() {
return types;
}
public void setTypes(List<Type> types) {
this.types = types;
}
public SQLExpr getForQuery() {
return forQuery;
}
public void setForQuery(SQLExpr forQuery) {
this.forQuery = forQuery;
}
public Limit getLimit() {
return limit;
}
public void setLimit(Limit limit) {
this.limit = limit;
}
public static enum Type {
ALL("ALL"), BLOCK_IO("BLOCK IO"), CONTEXT_SWITCHES("CONTEXT SWITCHES"), CPU("CPU"), IPC("IPC"),
MEMORY("MEMORY"), PAGE_FAULTS("PAGE FAULTS"), SOURCE("SOURCE"), SWAPS("SWAPS");
public final String name;
Type(String name){
this.name = name;
}
}
}
@@ -0,0 +1,13 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowProfilesStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
}
@@ -88,6 +88,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStat
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.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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;
@@ -877,6 +879,69 @@ 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);
lexer.nextToken();
} else if (identifierEquals("BLOCK")) {
lexer.nextToken();
acceptIdentifier("IO");
stmt.getTypes().add(MySqlShowProfileStatement.Type.BLOCK_IO);
} else if (identifierEquals("CONTEXT")) {
lexer.nextToken();
acceptIdentifier("SWITCHES");
stmt.getTypes().add(MySqlShowProfileStatement.Type.CONTEXT_SWITCHES);
} else if (identifierEquals("CPU")) {
lexer.nextToken();
stmt.getTypes().add(MySqlShowProfileStatement.Type.CPU);
} else if (identifierEquals("IPC")) {
lexer.nextToken();
stmt.getTypes().add(MySqlShowProfileStatement.Type.IPC);
} else if (identifierEquals("MEMORY")) {
lexer.nextToken();
stmt.getTypes().add(MySqlShowProfileStatement.Type.MEMORY);
} else if (identifierEquals("PAGE")) {
lexer.nextToken();
acceptIdentifier("FAULTS");
stmt.getTypes().add(MySqlShowProfileStatement.Type.PAGE_FAULTS);
} else if (identifierEquals("SOURCE")) {
lexer.nextToken();
stmt.getTypes().add(MySqlShowProfileStatement.Type.SOURCE);
} else if (identifierEquals("SWAPS")) {
lexer.nextToken();
stmt.getTypes().add(MySqlShowProfileStatement.Type.SWAPS);
} 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;
}
throw new ParserException("TODO " + lexer.stringVal());
}
@@ -79,6 +79,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStat
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.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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;
@@ -376,4 +378,12 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlShowProcessListStatement x);
void endVisit(MySqlShowProcessListStatement x);
boolean visit(MySqlShowProfileStatement x);
void endVisit(MySqlShowProfileStatement x);
boolean visit(MySqlShowProfilesStatement x);
void endVisit(MySqlShowProfilesStatement x);
}
@@ -66,6 +66,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStat
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.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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;
@@ -816,4 +818,24 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
public void endVisit(MySqlShowProcessListStatement x) {
}
@Override
public boolean visit(MySqlShowProfileStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowProfileStatement x) {
}
@Override
public boolean visit(MySqlShowProfilesStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowProfilesStatement x) {
}
}
@@ -98,6 +98,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStat
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.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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;
@@ -1979,4 +1981,44 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlShowProcessListStatement x) {
}
@Override
public boolean visit(MySqlShowProfileStatement x) {
print("SHOW PROFILE");
for (int i = 0; i < x.getTypes().size(); ++i) {
if (i == 0) {
print(' ');
} else {
print(", ");
}
print(x.getTypes().get(i).name);
}
if (x.getForQuery() != null) {
print(" FOR QUERY ");
x.getForQuery().accept(this);
}
if (x.getLimit() != null) {
print(' ');
x.getLimit().accept(this);
}
return false;
}
@Override
public void endVisit(MySqlShowProfileStatement x) {
}
@Override
public boolean visit(MySqlShowProfilesStatement x) {
print("SHOW PROFILES");
return false;
}
@Override
public void endVisit(MySqlShowProfilesStatement x) {
}
}
@@ -75,6 +75,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowPrivilegesStat
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.MySqlShowProfileStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowProfilesStatement;
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,4 +881,24 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlShowProcessListStatement x) {
}
@Override
public boolean visit(MySqlShowProfileStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowProfileStatement x) {
}
@Override
public boolean visit(MySqlShowProfilesStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowProfilesStatement x) {
}
}