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) {
|
||||
|
||||
Reference in New Issue
Block a user