mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser refactor
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package com.alibaba.druid.hbase.hbql.parser;
|
||||
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
|
||||
@@ -12,7 +11,7 @@ public class HBQLStatementParser extends SQLStatementParser {
|
||||
super(new SQLExprParser(sql));
|
||||
}
|
||||
|
||||
public HBQLStatementParser(Lexer lexer){
|
||||
super(new SQLExprParser(lexer));
|
||||
public HBQLStatementParser(SQLExprParser exprParser){
|
||||
super(exprParser);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.util.List;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
|
||||
import com.alibaba.druid.sql.parser.Lexer;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
@@ -13,8 +12,8 @@ import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public class HiveStatementParser extends SQLStatementParser {
|
||||
|
||||
public HiveStatementParser(Lexer lexer){
|
||||
super(new SQLExprParser(lexer));
|
||||
public HiveStatementParser(SQLExprParser exprParser){
|
||||
super(exprParser);
|
||||
}
|
||||
|
||||
public HiveStatementParser(String sql){
|
||||
|
||||
@@ -65,7 +65,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
lexer.nextToken();
|
||||
|
||||
if (lexer.token() == Token.HINT) {
|
||||
this.createExprParser().parseHints(queryBlock.getHints());
|
||||
this.exprParser.parseHints(queryBlock.getHints());
|
||||
}
|
||||
|
||||
if (lexer.token() == (Token.DISTINCT)) {
|
||||
@@ -174,7 +174,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
queryBlock.setInto(this.createExprParser().name());
|
||||
queryBlock.setInto(this.exprParser.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -185,7 +185,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
|
||||
parseGroupBy(queryBlock);
|
||||
|
||||
queryBlock.setOrderBy(this.createExprParser().parseOrderBy());
|
||||
queryBlock.setOrderBy(this.exprParser.parseOrderBy());
|
||||
|
||||
if (lexer.token() == Token.LIMIT) {
|
||||
queryBlock.setLimit(parseLimit());
|
||||
@@ -198,7 +198,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
|
||||
if (lexer.token() == Token.INTO) {
|
||||
lexer.nextToken();
|
||||
SQLExpr expr = this.createExprParser().name();
|
||||
SQLExpr expr = this.exprParser.name();
|
||||
queryBlock.setInto(expr);
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
|
||||
SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause();
|
||||
while (true) {
|
||||
groupBy.getItems().add(this.createExprParser().expr());
|
||||
groupBy.getItems().add(this.exprParser.expr());
|
||||
if (!(lexer.token() == (Token.COMMA))) break;
|
||||
lexer.nextToken();
|
||||
}
|
||||
@@ -246,18 +246,13 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
if (lexer.token() == Token.HAVING) {
|
||||
lexer.nextToken();
|
||||
|
||||
groupBy.setHaving(this.createExprParser().expr());
|
||||
groupBy.setHaving(this.exprParser.expr());
|
||||
}
|
||||
|
||||
queryBlock.setGroupBy(groupBy);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MySqlExprParser createExprParser() {
|
||||
return new MySqlExprParser(lexer);
|
||||
}
|
||||
|
||||
protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) throws ParserException {
|
||||
if (identifierEquals("USING")) {
|
||||
return tableSource;
|
||||
@@ -312,7 +307,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
}
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.createExprParser().names(hint.getIndexList());
|
||||
this.exprParser.names(hint.getIndexList());
|
||||
accept(Token.RPAREN);
|
||||
}
|
||||
|
||||
@@ -329,6 +324,6 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
}
|
||||
|
||||
public Limit parseLimit() {
|
||||
return this.createExprParser().parseLimit();
|
||||
return ((MySqlExprParser)this.exprParser) .parseLimit();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
|
||||
public SQLUnionQuery unionRest(SQLUnionQuery union) {
|
||||
if (lexer.token() == Token.ORDER) {
|
||||
SQLOrderBy orderBy = this.createExprParser().parseOrderBy();
|
||||
SQLOrderBy orderBy = this.exprParser.parseOrderBy();
|
||||
union.setOrderBy(orderBy);
|
||||
return unionRest(union);
|
||||
}
|
||||
@@ -193,7 +193,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLSelectGroupByClause groupBy = new SQLSelectGroupByClause();
|
||||
groupBy.setHaving(this.createExprParser().expr());
|
||||
groupBy.setHaving(this.expr());
|
||||
queryBlock.setGroupBy(groupBy);
|
||||
}
|
||||
}
|
||||
@@ -252,7 +252,7 @@ public class SQLSelectParser extends SQLParser {
|
||||
private void parseTableSourceQueryTableExpr(SQLExprTableSource tableReference) throws ParserException {
|
||||
if (lexer.token() == Token.LITERAL_ALIAS || lexer.token() == Token.IDENTIFIED
|
||||
|| lexer.token() == Token.LITERAL_CHARS) {
|
||||
tableReference.setExpr(this.createExprParser().name());
|
||||
tableReference.setExpr(this.exprParser.name());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -327,15 +327,11 @@ public class SQLSelectParser extends SQLParser {
|
||||
}
|
||||
|
||||
public SQLExpr expr() {
|
||||
return createExprParser().expr();
|
||||
}
|
||||
|
||||
protected SQLExprParser createExprParser() {
|
||||
return new SQLExprParser(lexer);
|
||||
return this.exprParser.expr();
|
||||
}
|
||||
|
||||
public SQLOrderBy parseOrderBy() throws ParserException {
|
||||
return createExprParser().parseOrderBy();
|
||||
return this.exprParser.parseOrderBy();
|
||||
}
|
||||
|
||||
public void acceptKeyword(String ident) {
|
||||
|
||||
@@ -50,14 +50,7 @@ public class SQLStatementParser extends SQLParser {
|
||||
protected SQLExprParser exprParser;
|
||||
|
||||
public SQLStatementParser(String sql){
|
||||
super(sql);
|
||||
|
||||
this.exprParser = new SQLExprParser(lexer);
|
||||
}
|
||||
|
||||
public SQLStatementParser(Lexer lexer){
|
||||
super(lexer);
|
||||
this.exprParser = new SQLExprParser(lexer);
|
||||
this (new SQLExprParser(sql));
|
||||
}
|
||||
|
||||
public SQLStatementParser(SQLExprParser exprParser){
|
||||
|
||||
Reference in New Issue
Block a user