mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser. bug fixed for issue #2446
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.db2.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
@@ -45,14 +46,14 @@ public class DB2SelectParser 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);
|
||||
}
|
||||
|
||||
accept(Token.SELECT);
|
||||
@@ -83,7 +84,7 @@ public class DB2SelectParser extends SQLSelectParser {
|
||||
parseHierachical(queryBlock);
|
||||
|
||||
parseGroupBy(queryBlock);
|
||||
|
||||
|
||||
if (lexer.token() == Token.ORDER) {
|
||||
SQLOrderBy orderBy = parseOrderBy();
|
||||
queryBlock.setOrderBy(orderBy);
|
||||
@@ -102,7 +103,7 @@ public class DB2SelectParser extends SQLSelectParser {
|
||||
accept(Token.ONLY);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.WITH) {
|
||||
lexer.nextToken();
|
||||
if (lexer.identifierEquals("RR")) {
|
||||
@@ -119,10 +120,10 @@ public class DB2SelectParser extends SQLSelectParser {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.FOR) {
|
||||
lexer.nextToken();
|
||||
|
||||
|
||||
if (lexer.token() == Token.UPDATE) {
|
||||
queryBlock.setForUpdate(true);
|
||||
lexer.nextToken();
|
||||
@@ -132,11 +133,11 @@ public class DB2SelectParser extends SQLSelectParser {
|
||||
queryBlock.setForReadOnly(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.OPTIMIZE) {
|
||||
lexer.nextToken();
|
||||
accept(Token.FOR);
|
||||
|
||||
|
||||
queryBlock.setOptimizeFor(this.expr());
|
||||
if (lexer.identifierEquals("ROW")) {
|
||||
lexer.nextToken();
|
||||
@@ -144,10 +145,10 @@ public class DB2SelectParser extends SQLSelectParser {
|
||||
acceptIdentifier("ROWS");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return queryRest(queryBlock);
|
||||
return queryRest(queryBlock, acceptUnion);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.hive.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectListCache;
|
||||
@@ -40,4 +42,32 @@ public class HiveSelectParser extends SQLSelectParser {
|
||||
return new HiveExprParser(lexer);
|
||||
}
|
||||
|
||||
protected void parseSortBy(SQLSelectQueryBlock queryBlock) {
|
||||
if (lexer.token() == Token.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.addSortBy(sortByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-20
@@ -39,7 +39,6 @@ public class MySqlSelectQueryBlock extends SQLSelectQueryBlock implements MySqlO
|
||||
private SQLName procedureName;
|
||||
private List<SQLExpr> procedureArgumentList;
|
||||
private boolean lockInShareMode;
|
||||
private List<SQLCommentHint> hints;
|
||||
private SQLName forcePartition; // for petadata
|
||||
|
||||
public MySqlSelectQueryBlock(){
|
||||
@@ -74,25 +73,6 @@ public class MySqlSelectQueryBlock extends SQLSelectQueryBlock implements MySqlO
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getHintsSize() {
|
||||
if (hints == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return hints.size();
|
||||
}
|
||||
|
||||
public List<SQLCommentHint> getHints() {
|
||||
if (hints == null) {
|
||||
hints = new ArrayList<SQLCommentHint>(2);
|
||||
}
|
||||
return hints;
|
||||
}
|
||||
|
||||
public void setHints(List<SQLCommentHint> hints) {
|
||||
this.hints = hints;
|
||||
}
|
||||
|
||||
public boolean isLockInShareMode() {
|
||||
return lockInShareMode;
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.dialect.mysql.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
|
||||
@@ -75,9 +76,9 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
queryBlock.setFrom(parseTableSource());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public SQLSelectQuery query() {
|
||||
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -85,10 +86,11 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
select.setBracket(true);
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return queryRest(select);
|
||||
return queryRest(select, acceptUnion);
|
||||
}
|
||||
|
||||
MySqlSelectQueryBlock queryBlock = new MySqlSelectQueryBlock();
|
||||
queryBlock.setParent(parent);
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
queryBlock.addBeforeComment(lexer.readAndResetComments());
|
||||
@@ -103,8 +105,12 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
lexer.nextTokenValue();
|
||||
|
||||
if (lexer.token() == Token.HINT) {
|
||||
this.exprParser.parseHints(queryBlock.getHints());
|
||||
for(;;) {
|
||||
if (lexer.token() == Token.HINT) {
|
||||
this.exprParser.parseHints(queryBlock.getHints());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Token token = lexer.token();
|
||||
@@ -167,7 +173,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
SQLName partition = this.exprParser.name();
|
||||
queryBlock.setForcePartition(partition);
|
||||
}
|
||||
|
||||
|
||||
parseInto(queryBlock);
|
||||
}
|
||||
|
||||
@@ -197,7 +203,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
accept(Token.UPDATE);
|
||||
|
||||
queryBlock.setForUpdate(true);
|
||||
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.NO_WAIT) || lexer.identifierEquals(FnvHash.Constants.NOWAIT)) {
|
||||
lexer.nextToken();
|
||||
queryBlock.setNoWait(true);
|
||||
@@ -216,7 +222,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
queryBlock.setLockInShareMode(true);
|
||||
}
|
||||
|
||||
return queryRest(queryBlock);
|
||||
return queryRest(queryBlock, acceptUnion);
|
||||
}
|
||||
|
||||
public SQLTableSource parseTableSource() {
|
||||
|
||||
@@ -34,8 +34,6 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
|
||||
private SQLOrderBy orderBy;
|
||||
|
||||
protected List<SQLHint> hints;
|
||||
|
||||
public OdpsSelectQueryBlock(){
|
||||
dbType = JdbcConstants.ODPS;
|
||||
|
||||
@@ -72,21 +70,6 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<SQLHint> getHintsDirect() {
|
||||
return hints;
|
||||
}
|
||||
|
||||
public List<SQLHint> getHints() {
|
||||
if (hints == null) {
|
||||
hints = new ArrayList<SQLHint>(2);
|
||||
}
|
||||
return hints;
|
||||
}
|
||||
|
||||
public void setHints(List<SQLHint> hints) {
|
||||
this.hints = hints;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor instanceof OdpsASTVisitor) {
|
||||
|
||||
@@ -455,7 +455,7 @@ public class OdpsOutputVisitor extends SQLASTOutputVisitor implements OdpsASTVis
|
||||
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
|
||||
List<SQLHint> hints = x.getHintsDirect();
|
||||
List<SQLCommentHint> hints = x.getHintsDirect();
|
||||
if (hints != null) {
|
||||
printAndAccept(hints, " ");
|
||||
print(' ');
|
||||
|
||||
-19
@@ -35,11 +35,7 @@ import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class OracleSelectQueryBlock extends SQLSelectQueryBlock implements OracleSQLObject {
|
||||
|
||||
private List<SQLCommentHint> hints;
|
||||
|
||||
private ModelClause modelClause;
|
||||
|
||||
|
||||
private boolean skipLocked = false;
|
||||
|
||||
public OracleSelectQueryBlock clone() {
|
||||
@@ -84,21 +80,6 @@ public class OracleSelectQueryBlock extends SQLSelectQueryBlock implements Oracl
|
||||
this.modelClause = modelClause;
|
||||
}
|
||||
|
||||
public List<SQLCommentHint> getHints() {
|
||||
if (hints == null) {
|
||||
hints = new ArrayList<SQLCommentHint>(1);
|
||||
}
|
||||
return this.hints;
|
||||
}
|
||||
|
||||
public int getHintsSize() {
|
||||
if (hints == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return hints.size();
|
||||
}
|
||||
|
||||
public boolean isSkipLocked() {
|
||||
return skipLocked;
|
||||
}
|
||||
|
||||
+4
-3
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.druid.sql.dialect.sqlserver.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
|
||||
@@ -115,14 +116,14 @@ public class SQLServerSelectParser extends SQLSelectParser {
|
||||
return select;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
SQLServerSelectQueryBlock queryBlock = new SQLServerSelectQueryBlock();
|
||||
@@ -167,7 +168,7 @@ public class SQLServerSelectParser extends SQLSelectParser {
|
||||
|
||||
parseFetchClause(queryBlock);
|
||||
|
||||
return queryRest(queryBlock);
|
||||
return queryRest(queryBlock, acceptUnion);
|
||||
}
|
||||
|
||||
protected SQLServerExprParser createExprParser() {
|
||||
|
||||
Reference in New Issue
Block a user