mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
backport sql parser improved from fastsql
This commit is contained in:
@@ -35,7 +35,7 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
public OdpsSelectQueryBlock(){
|
||||
dbType = JdbcConstants.ODPS;
|
||||
|
||||
distributeBy = new ArrayList<SQLExpr>();
|
||||
distributeBy = new ArrayList<SQLSelectOrderByItem>();
|
||||
sortBy = new ArrayList<SQLSelectOrderByItem>(2);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.dialect.odps.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
|
||||
@@ -29,6 +30,7 @@ import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectListCache;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
public class OdpsSelectParser extends SQLSelectParser {
|
||||
public OdpsSelectParser(SQLExprParser exprParser){
|
||||
@@ -43,24 +45,24 @@ public class OdpsSelectParser extends SQLSelectParser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLSelectQuery query() {
|
||||
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLSelectQuery select = query();
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return queryRest(select);
|
||||
return queryRest(select, acceptUnion);
|
||||
}
|
||||
|
||||
OdpsSelectQueryBlock queryBlock = new OdpsSelectQueryBlock();
|
||||
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
queryBlock.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
|
||||
accept(Token.SELECT);
|
||||
|
||||
|
||||
if (lexer.token() == Token.HINT) {
|
||||
this.exprParser.parseHints(queryBlock.getHints());
|
||||
}
|
||||
@@ -89,36 +91,51 @@ public class OdpsSelectParser extends SQLSelectParser {
|
||||
parseGroupBy(queryBlock);
|
||||
|
||||
queryBlock.setOrderBy(this.exprParser.parseOrderBy());
|
||||
|
||||
|
||||
if (lexer.token() == Token.DISTRIBUTE) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
this.exprParser.exprList(queryBlock.getDistributeBy(), queryBlock);
|
||||
|
||||
if (lexer.identifierEquals("SORT")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
for (;;) {
|
||||
SQLExpr expr = this.expr();
|
||||
|
||||
SQLSelectOrderByItem sortByItem = new SQLSelectOrderByItem(expr);
|
||||
|
||||
if (lexer.token() == Token.ASC) {
|
||||
sortByItem.setType(SQLOrderingSpecification.ASC);
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.DESC) {
|
||||
sortByItem.setType(SQLOrderingSpecification.DESC);
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
queryBlock.getSortBy().add(sortByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
for (;;) {
|
||||
SQLSelectOrderByItem distributeByItem = this.exprParser.parseSelectOrderByItem();
|
||||
queryBlock.addDistributeBy(distributeByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.SORT)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
for (;;) {
|
||||
SQLSelectOrderByItem sortByItem = this.exprParser.parseSelectOrderByItem();
|
||||
queryBlock.addSortBy(sortByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.CLUSTER)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
for (;;) {
|
||||
SQLSelectOrderByItem clusterByItem = this.exprParser.parseSelectOrderByItem();
|
||||
queryBlock.addClusterBy(clusterByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -128,7 +145,7 @@ public class OdpsSelectParser extends SQLSelectParser {
|
||||
queryBlock.setLimit(new SQLLimit(this.expr()));
|
||||
}
|
||||
|
||||
return queryRest(queryBlock);
|
||||
return queryRest(queryBlock, acceptUnion);
|
||||
}
|
||||
|
||||
public SQLTableSource parseTableSource() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,12 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.parser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.ast.SQLParameter;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
@@ -29,12 +24,9 @@ import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGFunctionTableSource;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.IntoOption;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGValuesQuery;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectListCache;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.sql.parser.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PGSelectParser extends SQLSelectParser {
|
||||
|
||||
@@ -55,14 +47,9 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLSelectQuery query() {
|
||||
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
|
||||
if (lexer.token() == Token.VALUES) {
|
||||
lexer.nextToken();
|
||||
accept(Token.LPAREN);
|
||||
PGValuesQuery valuesQuery = new PGValuesQuery();
|
||||
this.exprParser.exprList(valuesQuery.getValues(), valuesQuery);
|
||||
accept(Token.RPAREN);
|
||||
return queryRest(valuesQuery);
|
||||
return valuesQuery(acceptUnion);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
@@ -74,11 +61,15 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return queryRest(select);
|
||||
return queryRest(select, acceptUnion);
|
||||
}
|
||||
|
||||
PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
queryBlock.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -244,7 +235,7 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
queryBlock.setForClause(forClause);
|
||||
}
|
||||
|
||||
return queryRest(queryBlock);
|
||||
return queryRest(queryBlock, acceptUnion);
|
||||
}
|
||||
|
||||
protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
|
||||
@@ -264,7 +255,7 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
if (alias != null) {
|
||||
functionTableSource.setAlias(alias);
|
||||
}
|
||||
|
||||
|
||||
lexer.nextToken();
|
||||
parserParameters(functionTableSource.getParameters());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
+33
-5
@@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.SQLParameter;
|
||||
import com.alibaba.druid.sql.ast.SQLPartitionBy;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.SQLWindow;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBetweenExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr;
|
||||
@@ -247,8 +248,16 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean visit(PGSelectQueryBlock x) {
|
||||
if ((!isParameterized()) && isPrettyFormat() && x.hasBeforeComment()) {
|
||||
printlnComments(x.getBeforeCommentsDirect());
|
||||
}
|
||||
|
||||
final boolean bracket = x.isBracket();
|
||||
if (bracket) {
|
||||
print('(');
|
||||
}
|
||||
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
|
||||
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
|
||||
@@ -258,7 +267,9 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
if (x.getDistinctOn() != null && x.getDistinctOn().size() > 0) {
|
||||
print0(ucase ? "ON " : "on ");
|
||||
print0("(");
|
||||
printAndAccept(x.getDistinctOn(), ", ");
|
||||
print0(") ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,10 +292,16 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
x.getFrom().accept(this);
|
||||
}
|
||||
|
||||
if (x.getWhere() != null) {
|
||||
SQLExpr where = x.getWhere();
|
||||
if (where != null) {
|
||||
println();
|
||||
print0(ucase ? "WHERE " : "where ");
|
||||
x.getWhere().accept(this);
|
||||
where.accept(this);
|
||||
|
||||
if (where.hasAfterComment() && isPrettyFormat()) {
|
||||
print(' ');
|
||||
printlnComment(x.getWhere().getAfterCommentsDirect());
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getGroupBy() != null) {
|
||||
@@ -292,9 +309,16 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
x.getGroupBy().accept(this);
|
||||
}
|
||||
|
||||
if (x.getWindow() != null) {
|
||||
// if (x.getWindow() != null) {
|
||||
// println();
|
||||
// x.getWindow().accept(this);
|
||||
// }
|
||||
|
||||
final List<SQLWindow> windows = x.getWindows();
|
||||
if (windows != null && windows.size() > 0) {
|
||||
println();
|
||||
x.getWindow().accept(this);
|
||||
print0(ucase ? "WINDOW " : "window ");
|
||||
printAndAccept(windows, ", ");
|
||||
}
|
||||
|
||||
if (x.getOrderBy() != null) {
|
||||
@@ -317,6 +341,10 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
x.getForClause().accept(this);
|
||||
}
|
||||
|
||||
if (bracket) {
|
||||
print(')');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user