fix postgresql parenthesized bug

example:
1、(select * from a)
2、(select * from a) union all select * from b;
pg以括号开头的select语句会陷入死循环。
This commit is contained in:
huangtiande
2015-02-05 15:32:12 +08:00
parent 883d1fde6d
commit 947190c6a0
3 changed files with 48 additions and 1 deletions
@@ -31,6 +31,7 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
protected SQLExprTableSource into;
protected SQLExpr where;
protected SQLSelectGroupByClause groupBy;
protected boolean parenthesized = false;
public SQLSelectQueryBlock(){
@@ -90,7 +91,15 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
this.from = from;
}
@Override
public boolean isParenthesized() {
return parenthesized;
}
public void setParenthesized(boolean parenthesized) {
this.parenthesized = parenthesized;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.selectList);
@@ -105,6 +114,7 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (Boolean.valueOf(parenthesized).hashCode());
result = prime * result + distionOption;
result = prime * result + ((from == null) ? 0 : from.hashCode());
result = prime * result + ((groupBy == null) ? 0 : groupBy.hashCode());
@@ -120,6 +130,7 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
SQLSelectQueryBlock other = (SQLSelectQueryBlock) obj;
if (parenthesized ^ other.parenthesized) return false;
if (distionOption != other.distionOption) return false;
if (from == null) {
if (other.from != null) return false;
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.ast.SQLSetQuantifier;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.dialect.postgresql.ast.expr.PGParameter;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGFunctionTableSource;
@@ -58,6 +59,18 @@ public class PGSelectParser extends SQLSelectParser {
return queryRest(valuesQuery);
}
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLSelectQuery select = query();
if (select instanceof SQLSelectQueryBlock) {
((SQLSelectQueryBlock) select).setParenthesized(true);
}
accept(Token.RPAREN);
return queryRest(select);
}
PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
if (lexer.token() == Token.SELECT) {