bug fixed for SchemaStatVisitor

This commit is contained in:
温少
2012-02-22 14:09:55 +08:00
13 changed files with 1995 additions and 763 deletions
@@ -9,6 +9,8 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class PGSelectQueryBlock extends SQLSelectQueryBlock {
private static final long serialVersionUID = 1L;
@@ -17,8 +19,58 @@ public class PGSelectQueryBlock extends SQLSelectQueryBlock {
private List<SQLExpr> distinctOn = new ArrayList<SQLExpr>(2);
private SQLExpr limit;
private SQLExpr offset;
private WindowClause window;
private SQLOrderBy orderBy;
private FetchClause fetch;
private ForClause forClause;
@Override
protected void accept0(SQLASTVisitor visitor) {
accept0((PGASTVisitor) visitor);
}
protected void accept0(PGASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.with);
acceptChild(visitor, this.distinctOn);
acceptChild(visitor, this.selectList);
acceptChild(visitor, this.from);
acceptChild(visitor, this.where);
acceptChild(visitor, this.groupBy);
acceptChild(visitor, this.window);
acceptChild(visitor, this.orderBy);
acceptChild(visitor, this.limit);
acceptChild(visitor, this.offset);
acceptChild(visitor, this.fetch);
acceptChild(visitor, this.forClause);
}
visitor.endVisit(this);
}
public FetchClause getFetch() {
return fetch;
}
public void setFetch(FetchClause fetch) {
this.fetch = fetch;
}
public ForClause getForClause() {
return forClause;
}
public void setForClause(ForClause forClause) {
this.forClause = forClause;
}
public WindowClause getWindow() {
return window;
}
public void setWindow(WindowClause window) {
this.window = window;
}
public WithClause getWith() {
return with;
@@ -95,6 +95,7 @@ public class PGLexer extends Lexer {
map.put("SHARE", Token.SHARE);
map.put("NOWAIT", Token.NOWAIT);
map.put("RECURSIVE", Token.RECURSIVE);
map.put("WITH", Token.WITH);
DEFAULT_MYSQL_KEYWORDS = new Keywords(map);
}
@@ -1,8 +1,12 @@
package com.alibaba.druid.sql.dialect.postgresql.parser;
import java.util.List;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.parser.Token;
public class PGSQLStatementParser extends SQLStatementParser {
public PGSQLStatementParser(String sql) throws ParserException {
@@ -17,4 +21,14 @@ public class PGSQLStatementParser extends SQLStatementParser {
public PGSelectParser createSQLSelectParser() {
return new PGSelectParser(this.lexer);
}
public boolean parseStatementListDialect(List<SQLStatement> statementList) {
if (lexer.token() == Token.WITH) {
SQLStatement stmt = parseSelect();
statementList.add(stmt);
return true;
}
return false;
}
}
@@ -74,6 +74,8 @@ public class PGSelectParser extends SQLSelectParser {
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
lexer.nextToken();
}
parseSelectList(queryBlock);
parseFrom(queryBlock);
@@ -97,6 +99,7 @@ public class PGSelectParser extends SQLSelectParser {
break;
}
}
queryBlock.setWindow(window);
}
queryBlock.setOrderBy(this.createExprParser().parseOrderBy());
@@ -150,6 +153,8 @@ public class PGSelectParser extends SQLSelectParser {
} else {
throw new ParserException("expect 'ONLY'");
}
queryBlock.setFetch(fetch);
}
if (lexer.token() == Token.FOR) {
@@ -182,14 +187,17 @@ public class PGSelectParser extends SQLSelectParser {
lexer.nextToken();
forClause.setNoWait(true);
}
queryBlock.setForClause(forClause);
}
return queryBlock;
return queryRest(queryBlock);
}
private WithQuery withQuery() {
WithQuery withQuery = new WithQuery();
withQuery.setName(expr());
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
@@ -210,6 +218,8 @@ public class PGSelectParser extends SQLSelectParser {
accept(Token.AS);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLSelectQuery subQuery = query();
withQuery.setSubQuery(subQuery);
accept(Token.RPAREN);
@@ -0,0 +1,68 @@
package com.alibaba.druid.sql.dialect.postgresql.visitor;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.FetchClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.ForClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.WindowClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.WithClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.WithQuery;
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;
public class PGASTVisitorAdapter extends SQLASTVisitorAdapter implements
PGASTVisitor {
@Override
public void endVisit(WindowClause x) {
}
@Override
public boolean visit(WindowClause x) {
return true;
}
@Override
public void endVisit(FetchClause x) {
}
@Override
public boolean visit(FetchClause x) {
return true;
}
@Override
public void endVisit(ForClause x) {
}
@Override
public boolean visit(ForClause x) {
return true;
}
@Override
public void endVisit(WithQuery x) {
}
@Override
public boolean visit(WithQuery x) {
return true;
}
@Override
public void endVisit(WithClause x) {
}
@Override
public boolean visit(WithClause x) {
return true;
}
}
@@ -0,0 +1,213 @@
package com.alibaba.druid.sql.dialect.postgresql.visitor;
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.FetchClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.ForClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.WindowClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.WithClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.WithQuery;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
public class PGOutputVisitor extends SQLASTOutputVisitor implements
PGASTVisitor {
public PGOutputVisitor(Appendable appender) {
super(appender);
}
@Override
public void endVisit(WindowClause x) {
}
@Override
public boolean visit(WindowClause x) {
print("WINDOW ");
x.getName().accept(this);
print(" AS ");
for (int i = 0; i < x.getDefinition().size(); ++i) {
if (i != 0) {
println(", ");
}
print("(");
x.getDefinition().get(i).accept(this);
print(")");
}
return false;
}
@Override
public void endVisit(FetchClause x) {
}
@Override
public boolean visit(FetchClause x) {
print("FETCH ");
if (FetchClause.Option.FIRST.equals(x.getOption())) {
print("FIRST ");
} else if (FetchClause.Option.NEXT.equals(x.getOption())) {
print("NEXT ");
}
x.getCount().accept(this);
print(" ROWS ONLY");
return false;
}
@Override
public void endVisit(ForClause x) {
}
@Override
public boolean visit(ForClause x) {
print("FOR ");
if (ForClause.Option.UPDATE.equals(x.getOption())) {
print("UPDATE ");
} else if (ForClause.Option.SHARE.equals(x.getOption())) {
print("SHARE ");
}
if (x.getOf().size() > 0) {
for (int i = 0; i < x.getOf().size(); ++i) {
if (i != 0) {
println(", ");
}
x.getOf().get(i).accept(this);
}
}
if (x.isNoWait()) {
print(" NOWAIT");
}
return false;
}
@Override
public void endVisit(WithQuery x) {
}
@Override
public boolean visit(WithQuery x) {
x.getName().accept(this);
if (x.getColumns().size() > 0) {
print(" (");
printAndAccept(x.getColumns(), ", ");
print(")");
}
println();
print("AS");
println();
print("(");
incrementIndent();
println();
x.getSubQuery().accept(this);
decrementIndent();
println();
print(")");
return false;
}
@Override
public void endVisit(WithClause x) {
}
@Override
public boolean visit(WithClause x) {
print("WITH");
incrementIndent();
println();
printlnAndAccept(x.getWithQuery(), ", ");
decrementIndent();
return false;
}
public boolean visit(SQLSelectQueryBlock x) {
if (x instanceof PGSelectQueryBlock) {
return visit((PGSelectQueryBlock) x);
}
return super.visit(x);
}
public boolean visit(PGSelectQueryBlock x) {
if (x.getWith() != null) {
x.getWith().accept(this);
println();
}
print("SELECT ");
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
print("ALL ");
} else if (SQLSetQuantifier.DISTINCT == x.getDistionOption()) {
print("DISTINCT ");
if (x.getDistinctOn() != null) {
print("ON ");
printAndAccept(x.getDistinctOn(), ", ");
}
}
printSelectList(x.getSelectList());
if (x.getFrom() != null) {
println();
print("FROM ");
x.getFrom().accept(this);
}
if (x.getWhere() != null) {
println();
print("WHERE ");
x.getWhere().accept(this);
}
if (x.getGroupBy() != null) {
print(" ");
x.getGroupBy().accept(this);
}
if (x.getWindow() != null) {
println();
x.getWindow().accept(this);
}
if (x.getOrderBy() != null) {
println();
x.getOrderBy().accept(this);
}
if (x.getLimit() != null) {
println();
print("LIMIT ");
x.getLimit().accept(this);
}
if (x.getOffset() != null) {
println();
print("OFFSET ");
x.getOffset().accept(this);
print(" ROWS");
}
if (x.getFetch() != null) {
println();
x.getFetch().accept(this);
}
if (x.getForClause() != null) {
println();
x.getForClause().accept(this);
}
return false;
}
}
@@ -0,0 +1,66 @@
package com.alibaba.druid.sql.dialect.postgresql.visitor;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.FetchClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.ForClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.WindowClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.WithClause;
import com.alibaba.druid.sql.dialect.postgresql.ast.PGSelectQueryBlock.WithQuery;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
public class PGSchemaStatVisitor extends SchemaStatVisitor implements
PGASTVisitor {
@Override
public void endVisit(WindowClause x) {
}
@Override
public boolean visit(WindowClause x) {
return true;
}
@Override
public void endVisit(FetchClause x) {
}
@Override
public boolean visit(FetchClause x) {
return true;
}
@Override
public void endVisit(ForClause x) {
}
@Override
public boolean visit(ForClause x) {
return true;
}
@Override
public void endVisit(WithQuery x) {
}
@Override
public boolean visit(WithQuery x) {
return true;
}
@Override
public void endVisit(WithClause x) {
}
@Override
public boolean visit(WithClause x) {
return true;
}
}