mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -78,6 +78,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLSelectQuery select = query();
|
||||
select.setBracket(true);
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return queryRest(select);
|
||||
@@ -210,9 +211,12 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
SQLTableSource tableSource;
|
||||
if (lexer.token() == Token.SELECT || lexer.token() == Token.WITH) {
|
||||
SQLSelect select = select();
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
SQLSelectQuery query = queryRest(select.getQuery());
|
||||
if (query instanceof SQLUnionQuery) {
|
||||
select.getQuery().setBracket(true);
|
||||
tableSource = new SQLUnionQueryTableSource((SQLUnionQuery) query);
|
||||
} else {
|
||||
tableSource = new SQLSubqueryTableSource(select);
|
||||
|
||||
@@ -193,6 +193,11 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
public boolean visit(MySqlSelectQueryBlock x) {
|
||||
final boolean bracket = x.isBracket();
|
||||
if (bracket) {
|
||||
print('(');
|
||||
}
|
||||
|
||||
if ((!isParameterized()) && isPrettyFormat() && x.hasBeforeComment()) {
|
||||
printComment(x.getBeforeCommentsDirect(), "\n");
|
||||
}
|
||||
@@ -309,6 +314,10 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
print0(ucase ? "LOCK IN SHARE MODE" : "lock in share mode");
|
||||
}
|
||||
|
||||
if (bracket) {
|
||||
print(')');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -716,10 +725,10 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
public boolean visit(SQLMethodInvokeExpr x) {
|
||||
SQLExpr owner = x.getOwner();
|
||||
if ("SUBSTRING".equalsIgnoreCase(x.getMethodName())) {
|
||||
if (x.getOwner() != null) {
|
||||
x.getOwner().accept(this);
|
||||
print('.');
|
||||
if (owner != null) {
|
||||
printMethodOwner(owner);
|
||||
}
|
||||
print0(x.getMethodName());
|
||||
print('(');
|
||||
@@ -741,9 +750,8 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
if ("TRIM".equalsIgnoreCase(x.getMethodName())) {
|
||||
if (x.getOwner() != null) {
|
||||
x.getOwner().accept(this);
|
||||
print('.');
|
||||
if (owner != null) {
|
||||
printMethodOwner(owner);
|
||||
}
|
||||
print0(x.getMethodName());
|
||||
print('(');
|
||||
@@ -768,9 +776,8 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
}
|
||||
|
||||
if (("CONVERT".equalsIgnoreCase(x.getMethodName())) || "CHAR".equalsIgnoreCase(x.getMethodName())) {
|
||||
if (x.getOwner() != null) {
|
||||
x.getOwner().accept(this);
|
||||
print('.');
|
||||
if (owner != null) {
|
||||
printMethodOwner(owner);
|
||||
}
|
||||
print0(x.getMethodName());
|
||||
print('(');
|
||||
@@ -2569,37 +2576,37 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
|
||||
}
|
||||
|
||||
public boolean visit(SQLUnionQuery x) {
|
||||
print('(');
|
||||
x.getLeft().accept(this);
|
||||
print(')');
|
||||
println();
|
||||
print0(ucase ? x.getOperator().name : x.getOperator().name_lcase);
|
||||
println();
|
||||
|
||||
SQLSelectQuery right = x.getRight();
|
||||
boolean needParen = ! (right instanceof SQLUnionQuery);
|
||||
|
||||
if (needParen) {
|
||||
print('(');
|
||||
right.accept(this);
|
||||
print(')');
|
||||
} else {
|
||||
right.accept(this);
|
||||
}
|
||||
|
||||
if (x.getOrderBy() != null) {
|
||||
println();
|
||||
x.getOrderBy().accept(this);
|
||||
}
|
||||
|
||||
if (x.getLimit() != null) {
|
||||
println();
|
||||
x.getLimit().accept(this);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
// public boolean visit(SQLUnionQuery x) {
|
||||
// print('(');
|
||||
// x.getLeft().accept(this);
|
||||
// print(')');
|
||||
// println();
|
||||
// print0(ucase ? x.getOperator().name : x.getOperator().name_lcase);
|
||||
// println();
|
||||
//
|
||||
// SQLSelectQuery right = x.getRight();
|
||||
// boolean needParen = ! (right instanceof SQLUnionQuery);
|
||||
//
|
||||
// if (needParen) {
|
||||
// print('(');
|
||||
// right.accept(this);
|
||||
// print(')');
|
||||
// } else {
|
||||
// right.accept(this);
|
||||
// }
|
||||
//
|
||||
// if (x.getOrderBy() != null) {
|
||||
// println();
|
||||
// x.getOrderBy().accept(this);
|
||||
// }
|
||||
//
|
||||
// if (x.getLimit() != null) {
|
||||
// println();
|
||||
// x.getLimit().accept(this);
|
||||
// }
|
||||
//
|
||||
// return false;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlUseIndexHint x) {
|
||||
|
||||
@@ -19,11 +19,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLHint;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
@@ -242,32 +238,6 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLCaseExpr x) {
|
||||
incrementIndent();
|
||||
print0(ucase ? "CASE " : "case ");
|
||||
if (x.getValueExpr() != null) {
|
||||
x.getValueExpr().accept(this);
|
||||
println();
|
||||
}
|
||||
|
||||
for (int i = 0, size = x.getItems().size(); i < size; ++i) {
|
||||
println();
|
||||
x.getItems().get(i).accept(this);
|
||||
}
|
||||
|
||||
if (x.getElseExpr() != null) {
|
||||
println();
|
||||
print0(ucase ? "ELSE " : "else ");
|
||||
x.getElseExpr().accept(this);
|
||||
}
|
||||
|
||||
decrementIndent();
|
||||
println();
|
||||
print0(ucase ? "END" : "end");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected void printSelectList(List<SQLSelectItem> selectList) {
|
||||
incrementIndent();
|
||||
for (int i = 0, size = selectList.size(); i < size; ++i) {
|
||||
@@ -685,16 +655,9 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLMethodInvokeExpr x) {
|
||||
if (x.getOwner() != null) {
|
||||
x.getOwner().accept(this);
|
||||
print(':');
|
||||
}
|
||||
printFunctionName(x.getMethodName());
|
||||
print('(');
|
||||
printAndAccept(x.getParameters(), ", ");
|
||||
print(')');
|
||||
return false;
|
||||
protected void printMethodOwner(SQLExpr owner) {
|
||||
owner.accept(this);
|
||||
print(':');
|
||||
}
|
||||
|
||||
protected void printJoinType(JoinType joinType) {
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.alibaba.druid.sql.dialect.postgresql.ast.PGSQLObjectImpl;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
|
||||
|
||||
public class PGValuesQuery extends PGSQLObjectImpl implements SQLSelectQuery {
|
||||
private boolean bracket = false;
|
||||
|
||||
private List<SQLExpr> values = new ArrayList<SQLExpr>();
|
||||
|
||||
@@ -39,4 +40,13 @@ public class PGValuesQuery extends PGSQLObjectImpl implements SQLSelectQuery {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBracket() {
|
||||
return bracket;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBracket(boolean bracket) {
|
||||
this.bracket = bracket;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user