mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor sql parser group by
This commit is contained in:
@@ -34,9 +34,9 @@ import com.alibaba.druid.sql.ast.statement.SQLUnionOperator;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUnionQueryTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class SQLSelectParser extends SQLParser {
|
||||
|
||||
protected SQLExprParser exprParser;
|
||||
|
||||
public SQLSelectParser(String sql){
|
||||
@@ -48,7 +48,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
}
|
||||
|
||||
public SQLSelectParser(SQLExprParser exprParser){
|
||||
super(exprParser.getLexer());
|
||||
super(exprParser.getLexer(), exprParser.getDbType());
|
||||
this.exprParser = exprParser;
|
||||
}
|
||||
|
||||
@@ -257,26 +257,42 @@ public class SQLSelectParser extends SQLParser {
|
||||
queryBlock.setWhere(where);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected void parseGroupBy(SQLSelectQueryBlock queryBlock) {
|
||||
if (lexer.token() == Token.GROUP) {
|
||||
if (lexer.token() == (Token.GROUP)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause();
|
||||
while (true) {
|
||||
groupBy.addItem(expr());
|
||||
if (lexer.token() != Token.COMMA) {
|
||||
for (;;) {
|
||||
SQLExpr item = this.exprParser.expr();
|
||||
|
||||
item.setParent(groupBy);
|
||||
groupBy.addItem(item);
|
||||
|
||||
if (!(lexer.token() == (Token.COMMA))) {
|
||||
break;
|
||||
}
|
||||
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.HAVING) {
|
||||
if (lexer.token() == (Token.HAVING)) {
|
||||
lexer.nextToken();
|
||||
|
||||
groupBy.setHaving(expr());
|
||||
SQLExpr having = this.exprParser.expr();
|
||||
groupBy.setHaving(having);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.WITH) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("ROLLUP");
|
||||
|
||||
groupBy.setRollUp(true);
|
||||
}
|
||||
|
||||
if(JdbcConstants.MYSQL.equals(getDbType()) && lexer.token() == Token.DESC) {
|
||||
lexer.nextToken(); // skip
|
||||
}
|
||||
|
||||
queryBlock.setGroupBy(groupBy);
|
||||
@@ -284,7 +300,37 @@ public class SQLSelectParser extends SQLParser {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause();
|
||||
groupBy.setHaving(this.expr());
|
||||
groupBy.setHaving(this.exprParser.expr());
|
||||
|
||||
if (lexer.token() == (Token.GROUP)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
for (;;) {
|
||||
SQLExpr item = this.exprParser.expr();
|
||||
|
||||
item.setParent(groupBy);
|
||||
groupBy.addItem(item);
|
||||
|
||||
if (!(lexer.token() == (Token.COMMA))) {
|
||||
break;
|
||||
}
|
||||
|
||||
lexer.nextToken();
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.WITH) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier("ROLLUP");
|
||||
|
||||
groupBy.setRollUp(true);
|
||||
}
|
||||
|
||||
if(JdbcConstants.MYSQL.equals(getDbType()) && lexer.token() == Token.DESC) {
|
||||
lexer.nextToken(); // skip
|
||||
}
|
||||
|
||||
queryBlock.setGroupBy(groupBy);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user