mysql parser show statements

This commit is contained in:
温少
2012-05-16 07:35:28 +08:00
parent bc21aba00b
commit 836d8946f8
9 changed files with 439 additions and 123 deletions
@@ -0,0 +1,39 @@
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 MySqlShowEngineStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private SQLExpr name;
private Option option;
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
}
visitor.endVisit(this);
}
public SQLExpr getName() {
return name;
}
public void setName(SQLExpr name) {
this.name = name;
}
public Option getOption() {
return option;
}
public void setOption(Option option) {
this.option = option;
}
public static enum Option {
STATUS, MUTEX
}
}
@@ -0,0 +1,23 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowEnginesStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private boolean storage = false;
public void accept0(MySqlASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
public boolean isStorage() {
return storage;
}
public void setStorage(boolean storage) {
this.storage = storage;
}
}
@@ -0,0 +1,50 @@
/*
* 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.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlShowErrorsStatement extends MySqlStatementImpl {
private static final long serialVersionUID = 1L;
private boolean count = false;
private Limit limit;
public boolean isCount() {
return count;
}
public void setCount(boolean count) {
this.count = count;
}
public Limit getLimit() {
return limit;
}
public void setLimit(Limit limit) {
this.limit = limit;
}
public void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, limit);
}
visitor.endVisit(this);
}
}
@@ -70,6 +70,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableSta
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
@@ -466,10 +469,48 @@ public class MySqlStatementParser extends SQLStatementParser {
accept(Token.LPAREN);
accept(Token.STAR);
accept(Token.RPAREN);
acceptIdentifier("WARNINGS");
MySqlShowWarningsStatement stmt = new MySqlShowWarningsStatement();
stmt.setCount(true);
if (identifierEquals("ERRORS")) {
lexer.nextToken();
MySqlShowErrorsStatement stmt = new MySqlShowErrorsStatement();
stmt.setCount(true);
return stmt;
} else {
acceptIdentifier("WARNINGS");
MySqlShowWarningsStatement stmt = new MySqlShowWarningsStatement();
stmt.setCount(true);
return stmt;
}
}
if (identifierEquals("ERRORS")) {
lexer.nextToken();
MySqlShowErrorsStatement stmt = new MySqlShowErrorsStatement();
if (lexer.token() == Token.LIMIT) {
lexer.nextToken();
MySqlSelectQueryBlock.Limit limit = new MySqlSelectQueryBlock.Limit();
SQLExpr temp = this.exprParser.expr();
if (lexer.token() == (Token.COMMA)) {
limit.setOffset(temp);
lexer.nextToken();
limit.setRowCount(exprParser.expr());
} else if (identifierEquals("OFFSET")) {
limit.setRowCount(temp);
lexer.nextToken();
limit.setOffset(exprParser.expr());
} else {
limit.setRowCount(temp);
}
stmt.setLimit(limit);
}
return stmt;
}
@@ -603,69 +644,92 @@ public class MySqlStatementParser extends SQLStatementParser {
lexer.nextToken();
return new MySqlShowContributorsStatement();
}
if (lexer.token() == Token.CREATE) {
lexer.nextToken();
if (identifierEquals("DATABASE")) {
lexer.nextToken();
MySqlShowCreateDatabaseStatement stmt = new MySqlShowCreateDatabaseStatement();
stmt.setDatabase(this.exprParser.name());
return stmt;
}
if (identifierEquals("EVENT")) {
lexer.nextToken();
MySqlShowCreateEventStatement stmt = new MySqlShowCreateEventStatement();
stmt.setEventName(this.exprParser.name());
return stmt;
}
if (identifierEquals("FUNCTION")) {
lexer.nextToken();
MySqlShowCreateFunctionStatement stmt = new MySqlShowCreateFunctionStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
if (identifierEquals("PROCEDURE")) {
lexer.nextToken();
MySqlShowCreateProcedureStatement stmt = new MySqlShowCreateProcedureStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
if (lexer.token() == Token.TABLE) {
lexer.nextToken();
MySqlShowCreateTableStatement stmt = new MySqlShowCreateTableStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
if (lexer.token() == Token.VIEW) {
lexer.nextToken();
MySqlShowCreateViewStatement stmt = new MySqlShowCreateViewStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
if (identifierEquals("TRIGGER")) {
lexer.nextToken();
MySqlShowCreateTriggerStatement stmt = new MySqlShowCreateTriggerStatement();
stmt.setName(this.exprParser.name());
return stmt;
}
throw new ParserException("TODO " + lexer.stringVal());
}
if (identifierEquals("ENGINE")) {
lexer.nextToken();
MySqlShowEngineStatement stmt = new MySqlShowEngineStatement();
stmt.setName(this.exprParser.name());
stmt.setOption(MySqlShowEngineStatement.Option.valueOf(lexer.stringVal().toUpperCase()));
lexer.nextToken();
return stmt;
}
if (identifierEquals("STORAGE")) {
lexer.nextToken();
acceptIdentifier("ENGINES");
MySqlShowEnginesStatement stmt = new MySqlShowEnginesStatement();
stmt.setStorage(true);
return stmt;
}
if (identifierEquals("ENGINES")) {
lexer.nextToken();
MySqlShowEnginesStatement stmt = new MySqlShowEnginesStatement();
return stmt;
}
throw new ParserException("TODO " + lexer.stringVal());
}
@@ -61,6 +61,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableSta
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
@@ -291,4 +294,16 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlShowCreateViewStatement x);
void endVisit(MySqlShowCreateViewStatement x);
boolean visit(MySqlShowEngineStatement x);
void endVisit(MySqlShowEngineStatement x);
boolean visit(MySqlShowEnginesStatement x);
void endVisit(MySqlShowEnginesStatement x);
boolean visit(MySqlShowErrorsStatement x);
void endVisit(MySqlShowErrorsStatement x);
}
@@ -48,6 +48,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableSta
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
@@ -630,4 +633,34 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
public void endVisit(MySqlShowCreateViewStatement x) {
}
@Override
public boolean visit(MySqlShowEngineStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowEngineStatement x) {
}
@Override
public boolean visit(MySqlShowEnginesStatement x) {
return true;
}
@Override
public void endVisit(MySqlShowEnginesStatement x) {
}
@Override
public boolean visit(MySqlShowErrorsStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowErrorsStatement x) {
}
}
@@ -80,6 +80,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableSta
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
@@ -1677,4 +1680,52 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlShowCreateViewStatement x) {
}
@Override
public boolean visit(MySqlShowEngineStatement x) {
print("SHOW ENGINE ");
x.getName().accept(this);
print(' ');
print(x.getOption().name());
return false;
}
@Override
public void endVisit(MySqlShowEngineStatement x) {
}
@Override
public boolean visit(MySqlShowEnginesStatement x) {
if (x.isStorage()) {
print("SHOW STORAGE ENGINES");
} else {
print("SHOW ENGINES");
}
return false;
}
@Override
public void endVisit(MySqlShowEnginesStatement x) {
}
@Override
public boolean visit(MySqlShowErrorsStatement x) {
if (x.isCount()) {
print("SHOW COUNT(*) ERRORS");
} else {
print("SHOW ERRORS");
if (x.getLimit() != null) {
print(' ');
x.getLimit().accept(this);
}
}
return false;
}
@Override
public void endVisit(MySqlShowErrorsStatement x) {
}
}
@@ -57,6 +57,9 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTableSta
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateTriggerStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowCreateViewStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowDatabasesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEngineStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowEnginesStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowErrorsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowMasterLogsStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowStatusStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlShowTablesStatement;
@@ -692,4 +695,34 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
public void endVisit(MySqlShowCreateViewStatement x) {
}
@Override
public boolean visit(MySqlShowEngineStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowEngineStatement x) {
}
@Override
public boolean visit(MySqlShowEnginesStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowEnginesStatement x) {
}
@Override
public boolean visit(MySqlShowErrorsStatement x) {
return false;
}
@Override
public void endVisit(MySqlShowErrorsStatement x) {
}
}