mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor sql parser about the desc and explain parts of the code
This commit is contained in:
+5
-15
@@ -33,9 +33,7 @@ public class MySqlExplainStatement extends SQLExplainStatement implements MySqlS
|
||||
private SQLName columnName;
|
||||
private SQLExpr wild;
|
||||
|
||||
private MySqlExplainType type;
|
||||
|
||||
private MySqlFormatName formatName;
|
||||
private String format;
|
||||
|
||||
private SQLExpr connectionId;
|
||||
|
||||
@@ -106,20 +104,12 @@ public class MySqlExplainStatement extends SQLExplainStatement implements MySqlS
|
||||
this.wild = wild;
|
||||
}
|
||||
|
||||
public MySqlExplainType getType() {
|
||||
return type;
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setType(MySqlExplainType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public MySqlFormatName getFormatName() {
|
||||
return formatName;
|
||||
}
|
||||
|
||||
public void setFormatName(MySqlFormatName formatName) {
|
||||
this.formatName = formatName;
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public SQLExpr getConnectionId() {
|
||||
|
||||
+29
-33
@@ -654,7 +654,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.EXPLAIN) {
|
||||
MySqlExplainStatement stmt = this.parseExplain();
|
||||
SQLStatement stmt = this.parseExplain();
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
}
|
||||
@@ -829,44 +829,40 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
|
||||
|
||||
private MySqlExplainStatement parseExplain(MySqlExplainStatement explain) {
|
||||
|
||||
if (lexer.token() == Token.HINT) {
|
||||
List<SQLCommentHint> hints = this.exprParser.parseHints();
|
||||
explain.setHints(hints);
|
||||
}
|
||||
// see https://dev.mysql.com/doc/refman/5.7/en/explain.html
|
||||
|
||||
boolean extended = lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase(EXTENDED);
|
||||
boolean partitions = lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase(PARTITIONS);
|
||||
boolean format = lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase(FORMAT);
|
||||
boolean type = extended || partitions || format;
|
||||
boolean forConnection = lexer.token() == Token.FOR;
|
||||
boolean table = lexer.token() == Token.IDENTIFIER && !type && !forConnection;
|
||||
boolean table = false;
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
String stringVal = lexer.stringVal();
|
||||
|
||||
// tbl_name [col_name | wild]
|
||||
if (table) {
|
||||
explain.setTableName(exprParser.name());
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
explain.setColumnName(exprParser.name());
|
||||
} else if (lexer.token() == Token.LITERAL_CHARS) {
|
||||
explain.setWild(exprParser.expr());
|
||||
}
|
||||
} else if (type) {
|
||||
// [explain_type]
|
||||
if (extended) {
|
||||
explain.setType(MySqlExplainType.EXTENDED);
|
||||
} else if (partitions) {
|
||||
explain.setType(MySqlExplainType.PARTITIONS);
|
||||
} else if (format) {
|
||||
explain.setType(MySqlExplainType.FORMAT);
|
||||
lexer.nextToken();
|
||||
accept(Token.EQ);
|
||||
if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase(TRADITIONAL)) {
|
||||
explain.setFormatName(MySqlFormatName.TRADITIONAL);
|
||||
} else if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase(JSON)) {
|
||||
explain.setFormatName(MySqlFormatName.JSON);
|
||||
}
|
||||
}
|
||||
if (stringVal.equalsIgnoreCase(EXTENDED)
|
||||
|| stringVal.equalsIgnoreCase(PARTITIONS)) {
|
||||
explain.setType(stringVal);
|
||||
lexer.nextToken();
|
||||
} else if (stringVal.equalsIgnoreCase(FORMAT)) {
|
||||
explain.setType(stringVal);
|
||||
lexer.nextToken();
|
||||
accept(Token.EQ);
|
||||
|
||||
lexer.nextToken();
|
||||
String format = lexer.stringVal();
|
||||
explain.setFormat(format);
|
||||
accept(Token.IDENTIFIER);
|
||||
} else {
|
||||
explain.setTableName(exprParser.name());
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
explain.setColumnName(exprParser.name());
|
||||
} else if (lexer.token() == Token.LITERAL_CHARS) {
|
||||
explain.setWild(exprParser.expr());
|
||||
}
|
||||
table = true;
|
||||
}
|
||||
}
|
||||
|
||||
// {explainable_stmt | FOR CONNECTION connection_id}
|
||||
if (lexer.token() == Token.FOR) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier(CONNECTION);
|
||||
|
||||
@@ -1723,12 +1723,13 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
} else {
|
||||
// [explain_type]
|
||||
if (x.getType() != null) {
|
||||
print0(ucase ? x.getType().name() : x.getType().name().toLowerCase());
|
||||
String type = x.getType();
|
||||
if (type != null) {
|
||||
print0(type);
|
||||
print(' ');
|
||||
if (x.getType() == MySqlExplainType.FORMAT) {
|
||||
if ("format".equalsIgnoreCase(type)) {
|
||||
print0("= ");
|
||||
print(ucase ? x.getFormatName().name() : x.getFormatName().name().toLowerCase());
|
||||
print0(x.getFormat());
|
||||
print(' ');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2017 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.odps.ast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDescribeStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLObjectType;
|
||||
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class OdpsDescStmt extends SQLDescribeStatement {
|
||||
|
||||
private SQLObjectType objectType;
|
||||
|
||||
private List<SQLExpr> partition = new ArrayList<SQLExpr>();
|
||||
|
||||
public OdpsDescStmt() {
|
||||
this.setDbType(JdbcConstants.ODPS);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
accept0((OdpsASTVisitor) visitor);
|
||||
}
|
||||
|
||||
protected void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, object);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLObjectType getObjectType() {
|
||||
return objectType;
|
||||
}
|
||||
|
||||
public void setObjectType(SQLObjectType objectType) {
|
||||
this.objectType = objectType;
|
||||
}
|
||||
|
||||
public List<SQLExpr> getPartition() {
|
||||
return partition;
|
||||
}
|
||||
|
||||
public void setPartition(List<SQLExpr> partition) {
|
||||
this.partition = partition;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,7 +33,6 @@ import com.alibaba.druid.sql.ast.statement.SQLShowTablesStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsAddStatisticStatement;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsAnalyzeTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsDescStmt;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsGrantStmt;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsInsert;
|
||||
import com.alibaba.druid.sql.dialect.odps.ast.OdpsInsertStatement;
|
||||
@@ -179,7 +178,7 @@ public class OdpsStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.DESC || identifierEquals("DESCRIBE")) {
|
||||
OdpsDescStmt stmt = parseDescribe();
|
||||
SQLStatement stmt = parseDescribe();
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
}
|
||||
@@ -589,42 +588,4 @@ public class OdpsStatementParser extends SQLStatementParser {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public OdpsDescStmt parseDescribe() {
|
||||
if (lexer.token() == Token.DESC || identifierEquals("DESCRIBE")) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
throw new ParserException("expect DESC, actual " + lexer.token());
|
||||
}
|
||||
|
||||
OdpsDescStmt stmt = new OdpsDescStmt();
|
||||
if (identifierEquals("ROLE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(SQLObjectType.ROLE);
|
||||
} else if (identifierEquals("PACKAGE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(SQLObjectType.PACKAGE);
|
||||
} else if (identifierEquals("INSTANCE")) {
|
||||
lexer.nextToken();
|
||||
stmt.setObjectType(SQLObjectType.INSTANCE);
|
||||
}
|
||||
stmt.setObject(this.exprParser.name());
|
||||
|
||||
if (lexer.token() == Token.PARTITION) {
|
||||
lexer.nextToken();
|
||||
this.accept(Token.LPAREN);
|
||||
for (;;) {
|
||||
stmt.getPartition().add(this.exprParser.expr());
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
if (lexer.token() == Token.RPAREN) {
|
||||
lexer.nextToken();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return stmt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,10 +104,6 @@ public interface OdpsASTVisitor extends SQLASTVisitor {
|
||||
|
||||
boolean visit(OdpsGrantStmt x);
|
||||
|
||||
void endVisit(OdpsDescStmt x);
|
||||
|
||||
boolean visit(OdpsDescStmt x);
|
||||
|
||||
void endVisit(OdpsLateralViewTableSource x);
|
||||
|
||||
boolean visit(OdpsLateralViewTableSource x);
|
||||
|
||||
@@ -231,16 +231,6 @@ public class OdpsASTVisitorAdapter extends SQLASTVisitorAdapter implements OdpsA
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsDescStmt x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OdpsDescStmt x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsLateralViewTableSource x) {
|
||||
|
||||
|
||||
@@ -815,31 +815,6 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsDescStmt x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OdpsDescStmt x) {
|
||||
print0(ucase ? "DESC " : "desc ");
|
||||
if (x.getObjectType() != null) {
|
||||
print0(x.getObjectType().name());
|
||||
print(' ');
|
||||
}
|
||||
|
||||
if(x.getObject() != null) {
|
||||
x.getObject().accept(this);
|
||||
}
|
||||
|
||||
if (x.getPartition().size() > 0) {
|
||||
print0(ucase ? " PARTITION (" : " partition (");
|
||||
printAndAccept(x.getPartition(), ", ");
|
||||
print(')');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsLateralViewTableSource x) {
|
||||
|
||||
|
||||
@@ -264,16 +264,6 @@ public class OdpsSchemaStatVisitor extends SchemaStatVisitor implements OdpsASTV
|
||||
return super.visit((SQLGrantStatement) x);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsDescStmt x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(OdpsDescStmt x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(OdpsLateralViewTableSource x) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user