mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser. for issue #1637
This commit is contained in:
@@ -15,13 +15,18 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SQLSelectStatement extends SQLStatementImpl {
|
||||
|
||||
protected SQLSelect select;
|
||||
|
||||
private List<SQLCommentHint> headHints;
|
||||
|
||||
public SQLSelectStatement(){
|
||||
|
||||
}
|
||||
@@ -60,4 +65,12 @@ public class SQLSelectStatement extends SQLStatementImpl {
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public List<SQLCommentHint> getHeadHintsDirect() {
|
||||
return headHints;
|
||||
}
|
||||
|
||||
public void setHeadHints(List<SQLCommentHint> headHints) {
|
||||
this.headHints = headHints;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -692,7 +692,29 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.HINT) {
|
||||
statementList.add(this.parseHint());
|
||||
List<SQLCommentHint> hints = this.exprParser.parseHints();
|
||||
|
||||
boolean tddlSelectHints = false;
|
||||
|
||||
if (hints.size() == 1 && statementList.size() == 0 && lexer.token() == Token.SELECT) {
|
||||
SQLCommentHint hint = hints.get(0);
|
||||
String hintText = hint.getText();
|
||||
if (hintText.startsWith("+TDDL")) {
|
||||
tddlSelectHints = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (tddlSelectHints) {
|
||||
SQLSelectStatement stmt = (SQLSelectStatement) this.parseStatement();
|
||||
stmt.setHeadHints(hints);
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
}
|
||||
|
||||
MySqlHintStatement stmt = new MySqlHintStatement();
|
||||
stmt.setHints(hints);
|
||||
|
||||
statementList.add(stmt);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1105,7 +1127,8 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
|
||||
if (identifierEquals("STORAGE")) {
|
||||
lexer.nextToken();
|
||||
acceptIdentifier(ENGINES);
|
||||
accept(Token.EQ);
|
||||
accept(Token.DEFAULT);
|
||||
MySqlShowEnginesStatement stmt = new MySqlShowEnginesStatement();
|
||||
stmt.setStorage(true);
|
||||
return stmt;
|
||||
@@ -2755,6 +2778,20 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
SQLTableSource tableSrc = this.createSQLSelectParser().parseTableSource();
|
||||
stmt.getTableOptions().put("UNION", tableSrc);
|
||||
accept(Token.RPAREN);
|
||||
} else if (identifierEquals("ROW_FORMAT")) {
|
||||
lexer.nextToken();
|
||||
if (lexer.token() == Token.EQ) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.DEFAULT || lexer.token() == Token.IDENTIFIER) {
|
||||
SQLIdentifierExpr rowFormat = new SQLIdentifierExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
stmt.getTableOptions().put("ROW_FORMAT", rowFormat);
|
||||
} else {
|
||||
throw new ParserException("illegal syntax.");
|
||||
}
|
||||
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -3025,13 +3062,6 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return (MySqlExprParser) exprParser;
|
||||
}
|
||||
|
||||
public MySqlHintStatement parseHint() {
|
||||
// accept(Token.HINT);
|
||||
MySqlHintStatement stmt = new MySqlHintStatement();
|
||||
stmt.setHints(this.exprParser.parseHints());
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse create procedure statement
|
||||
|
||||
@@ -864,36 +864,6 @@ public class OracleExprParser extends SQLExprParser {
|
||||
return new OracleSelectParser(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLSelectOrderByItem parseSelectOrderByItem() {
|
||||
SQLSelectOrderByItem item = new SQLSelectOrderByItem();
|
||||
|
||||
item.setExpr(expr());
|
||||
|
||||
if (lexer.token() == (Token.ASC)) {
|
||||
lexer.nextToken();
|
||||
item.setType(SQLOrderingSpecification.ASC);
|
||||
} else if (lexer.token() == (Token.DESC)) {
|
||||
lexer.nextToken();
|
||||
item.setType(SQLOrderingSpecification.DESC);
|
||||
}
|
||||
|
||||
if (identifierEquals("NULLS")) {
|
||||
lexer.nextToken();
|
||||
if (identifierEquals("FIRST")) {
|
||||
lexer.nextToken();
|
||||
item.setNullsOrderType(SQLSelectOrderByItem.NullsOrderType.NullsFirst);
|
||||
} else if (identifierEquals("LAST")) {
|
||||
lexer.nextToken();
|
||||
item.setNullsOrderType(SQLSelectOrderByItem.NullsOrderType.NullsLast);
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
protected SQLExpr parseInterval() {
|
||||
accept(Token.INTERVAL);
|
||||
|
||||
|
||||
@@ -1220,6 +1220,19 @@ public class SQLExprParser extends SQLParser {
|
||||
item.setType(SQLOrderingSpecification.DESC);
|
||||
}
|
||||
|
||||
if (identifierEquals("NULLS")) {
|
||||
lexer.nextToken();
|
||||
if (identifierEquals("FIRST")) {
|
||||
lexer.nextToken();
|
||||
item.setNullsOrderType(SQLSelectOrderByItem.NullsOrderType.NullsFirst);
|
||||
} else if (identifierEquals("LAST")) {
|
||||
lexer.nextToken();
|
||||
item.setNullsOrderType(SQLSelectOrderByItem.NullsOrderType.NullsLast);
|
||||
} else {
|
||||
throw new ParserException("TODO " + lexer.token());
|
||||
}
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
@@ -1192,6 +1192,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
print0(x.getCollate());
|
||||
}
|
||||
|
||||
if (x.getNullsOrderType() != null) {
|
||||
print(' ');
|
||||
print0(x.getNullsOrderType().toFormalString());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1261,6 +1266,14 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectStatement stmt) {
|
||||
List<SQLCommentHint> headHints = stmt.getHeadHintsDirect();
|
||||
if (headHints != null) {
|
||||
for (SQLCommentHint hint : headHints) {
|
||||
hint.accept(this);
|
||||
println();
|
||||
}
|
||||
}
|
||||
|
||||
SQLSelect select = stmt.getSelect();
|
||||
|
||||
select.accept(this);
|
||||
|
||||
Reference in New Issue
Block a user