This commit is contained in:
温少
2012-03-14 15:53:14 +08:00
parent 6b7958dbfd
commit 6f2aad48bc
22 changed files with 347 additions and 285 deletions
@@ -37,7 +37,7 @@ public class PGSQLStatementParser extends SQLStatementParser {
public PGSelectParser createSQLSelectParser() {
return new PGSelectParser(this.lexer);
}
public SQLUpdateStatement parseUpdateStatement() throws ParserException {
accept(Token.UPDATE);
@@ -68,10 +68,10 @@ public class PGSQLStatementParser extends SQLStatementParser {
lexer.nextToken();
udpateStatement.setWhere(this.exprParser.expr());
}
if (lexer.token() == Token.RETURNING) {
lexer.nextToken();
for (;;) {
udpateStatement.getReturning().add(this.exprParser.expr());
if (lexer.token() == Token.COMMA) {
@@ -86,17 +86,20 @@ public class PGSQLStatementParser extends SQLStatementParser {
}
public PGInsertStatement parseInsert() {
accept(Token.INSERT);
accept(Token.INTO);
PGInsertStatement stmt = new PGInsertStatement();
SQLName tableName = this.exprParser.name();
stmt.setTableName(tableName);
if (lexer.token() == Token.IDENTIFIER) {
stmt.setAlias(lexer.stringVal());
if (lexer.token() == Token.INSERT) {
lexer.nextToken();
accept(Token.INTO);
SQLName tableName = this.exprParser.name();
stmt.setTableName(tableName);
if (lexer.token() == Token.IDENTIFIER) {
stmt.setAlias(lexer.stringVal());
lexer.nextToken();
}
}
if (lexer.token() == (Token.LPAREN)) {
@@ -124,7 +127,7 @@ public class PGSQLStatementParser extends SQLStatementParser {
SQLQueryExpr queryExpr = (SQLQueryExpr) this.createExprParser().expr();
stmt.setQuery(queryExpr.getSubQuery());
}
if (lexer.token() == Token.RETURNING) {
lexer.nextToken();
SQLExpr returning = this.exprParser.expr();
@@ -242,7 +245,7 @@ public class PGSQLStatementParser extends SQLStatementParser {
return false;
}
public PGWithClause parseWithClause() {
lexer.nextToken();
@@ -269,7 +272,7 @@ public class PGSQLStatementParser extends SQLStatementParser {
private PGWithQuery withQuery() {
PGWithQuery withQuery = new PGWithQuery();
withQuery.setName(this.createExprParser().expr());
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
@@ -305,17 +308,17 @@ public class PGSQLStatementParser extends SQLStatementParser {
throw new ParserException("syntax error, support token '" + lexer.token() + "'");
}
withQuery.setQuery(query);
accept(Token.RPAREN);
}
return withQuery;
}
public PGSelectStatement parseSelect() throws ParserException {
return new PGSelectStatement(createSQLSelectParser().select());
}
public SQLStatement parseWith() {
PGWithClause with = this.parseWithClause();
if (lexer.token() == Token.INSERT) {
@@ -13,187 +13,188 @@ import com.alibaba.druid.sql.parser.Token;
public class PGSelectParser extends SQLSelectParser {
public PGSelectParser(Lexer lexer) {
super(lexer);
}
public PGSelectParser(Lexer lexer){
super(lexer);
}
public PGSelectParser(String sql) throws ParserException {
this(new PGLexer(sql));
this.lexer.nextToken();
}
public PGSelectParser(String sql) throws ParserException{
this(new PGLexer(sql));
this.lexer.nextToken();
}
@Override
public SQLSelectQuery query() throws ParserException {
PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
@Override
public SQLSelectQuery query() throws ParserException {
PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
accept(Token.SELECT);
if (lexer.token() == Token.SELECT) {
lexer.nextToken();
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
if (lexer.token() == Token.ON) {
lexer.nextToken();
if (lexer.token() == Token.ON) {
lexer.nextToken();
for (;;) {
SQLExpr expr = this.createExprParser().expr();
queryBlock.getDistinctOn().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
}
} else if (lexer.token() == Token.ALL) {
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
lexer.nextToken();
}
parseSelectList(queryBlock);
if (lexer.token() == Token.INTO) {
IntoClause into = new IntoClause();
lexer.nextToken();
if (lexer.token() == Token.TEMPORARY) {
lexer.nextToken();
into.setOption(IntoClause.Option.TEMPORARY);
} else if (lexer.token() == Token.TEMP) {
lexer.nextToken();
into.setOption(IntoClause.Option.TEMP);
} else if (lexer.token() == Token.UNLOGGED) {
lexer.nextToken();
into.setOption(IntoClause.Option.UNLOGGED);
}
if (lexer.token() == Token.TABLE) {
lexer.nextToken();
}
SQLExpr name = this.createExprParser().name();
into.setTable(name);
queryBlock.setInto(into);
}
for (;;) {
SQLExpr expr = this.createExprParser().expr();
queryBlock.getDistinctOn().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
}
} else if (lexer.token() == Token.ALL) {
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
lexer.nextToken();
}
parseFrom(queryBlock);
parseSelectList(queryBlock);
parseWhere(queryBlock);
if (lexer.token() == Token.INTO) {
IntoClause into = new IntoClause();
lexer.nextToken();
parseGroupBy(queryBlock);
if (lexer.token() == Token.TEMPORARY) {
lexer.nextToken();
into.setOption(IntoClause.Option.TEMPORARY);
} else if (lexer.token() == Token.TEMP) {
lexer.nextToken();
into.setOption(IntoClause.Option.TEMP);
} else if (lexer.token() == Token.UNLOGGED) {
lexer.nextToken();
into.setOption(IntoClause.Option.UNLOGGED);
}
if (lexer.token() == Token.WINDOW) {
lexer.nextToken();
PGSelectQueryBlock.WindowClause window = new PGSelectQueryBlock.WindowClause();
window.setName(this.expr());
accept(Token.AS);
if (lexer.token() == Token.TABLE) {
lexer.nextToken();
}
for (;;) {
SQLExpr expr = this.createExprParser().expr();
window.getDefinition().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
queryBlock.setWindow(window);
}
SQLExpr name = this.createExprParser().name();
into.setTable(name);
queryBlock.setOrderBy(this.createExprParser().parseOrderBy());
queryBlock.setInto(into);
}
}
if (lexer.token() == Token.LIMIT) {
lexer.nextToken();
if (lexer.token() == Token.ALL) {
queryBlock.setLimit(new SQLIdentifierExpr("ALL"));
lexer.nextToken();
} else {
SQLExpr limit = expr();
queryBlock.setLimit(limit);
}
}
parseFrom(queryBlock);
if (lexer.token() == Token.OFFSET) {
lexer.nextToken();
SQLExpr offset = expr();
queryBlock.setOffset(offset);
parseWhere(queryBlock);
if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ROW' or 'ROWS'");
}
}
parseGroupBy(queryBlock);
if (lexer.token() == Token.FETCH) {
lexer.nextToken();
PGSelectQueryBlock.FetchClause fetch = new PGSelectQueryBlock.FetchClause();
if (lexer.token() == Token.WINDOW) {
lexer.nextToken();
PGSelectQueryBlock.WindowClause window = new PGSelectQueryBlock.WindowClause();
window.setName(this.expr());
accept(Token.AS);
if (lexer.token() == Token.FIRST) {
fetch.setOption(PGSelectQueryBlock.FetchClause.Option.FIRST);
} else if (lexer.token() == Token.NEXT) {
fetch.setOption(PGSelectQueryBlock.FetchClause.Option.NEXT);
} else {
throw new ParserException("expect 'FIRST' or 'NEXT'");
}
for (;;) {
SQLExpr expr = this.createExprParser().expr();
window.getDefinition().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
queryBlock.setWindow(window);
}
SQLExpr count = expr();
fetch.setCount(count);
queryBlock.setOrderBy(this.createExprParser().parseOrderBy());
if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ROW' or 'ROWS'");
}
if (lexer.token() == Token.LIMIT) {
lexer.nextToken();
if (lexer.token() == Token.ALL) {
queryBlock.setLimit(new SQLIdentifierExpr("ALL"));
lexer.nextToken();
} else {
SQLExpr limit = expr();
queryBlock.setLimit(limit);
}
}
if (lexer.token() == Token.ONLY) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ONLY'");
}
queryBlock.setFetch(fetch);
}
if (lexer.token() == Token.OFFSET) {
lexer.nextToken();
SQLExpr offset = expr();
queryBlock.setOffset(offset);
if (lexer.token() == Token.FOR) {
lexer.nextToken();
if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ROW' or 'ROWS'");
}
}
PGSelectQueryBlock.ForClause forClause = new PGSelectQueryBlock.ForClause();
if (lexer.token() == Token.FETCH) {
lexer.nextToken();
PGSelectQueryBlock.FetchClause fetch = new PGSelectQueryBlock.FetchClause();
if (lexer.token() == Token.UPDATE) {
forClause.setOption(PGSelectQueryBlock.ForClause.Option.UPDATE);
} else if (lexer.token() == Token.SHARE) {
forClause.setOption(PGSelectQueryBlock.ForClause.Option.SHARE);
} else {
throw new ParserException("expect 'FIRST' or 'NEXT'");
}
if (lexer.token() == Token.FIRST) {
fetch.setOption(PGSelectQueryBlock.FetchClause.Option.FIRST);
} else if (lexer.token() == Token.NEXT) {
fetch.setOption(PGSelectQueryBlock.FetchClause.Option.NEXT);
} else {
throw new ParserException("expect 'FIRST' or 'NEXT'");
}
accept(Token.OF);
SQLExpr count = expr();
fetch.setCount(count);
for (;;) {
SQLExpr expr = this.createExprParser().expr();
forClause.getOf().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ROW' or 'ROWS'");
}
if (lexer.token() == Token.NOWAIT) {
lexer.nextToken();
forClause.setNoWait(true);
}
queryBlock.setForClause(forClause);
}
return queryRest(queryBlock);
}
if (lexer.token() == Token.ONLY) {
lexer.nextToken();
} else {
throw new ParserException("expect 'ONLY'");
}
queryBlock.setFetch(fetch);
}
if (lexer.token() == Token.FOR) {
lexer.nextToken();
PGSelectQueryBlock.ForClause forClause = new PGSelectQueryBlock.ForClause();
if (lexer.token() == Token.UPDATE) {
forClause.setOption(PGSelectQueryBlock.ForClause.Option.UPDATE);
} else if (lexer.token() == Token.SHARE) {
forClause.setOption(PGSelectQueryBlock.ForClause.Option.SHARE);
} else {
throw new ParserException("expect 'FIRST' or 'NEXT'");
}
accept(Token.OF);
for (;;) {
SQLExpr expr = this.createExprParser().expr();
forClause.getOf().add(expr);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
} else {
break;
}
}
if (lexer.token() == Token.NOWAIT) {
lexer.nextToken();
forClause.setNoWait(true);
}
queryBlock.setForClause(forClause);
}
return queryRest(queryBlock);
}
}