mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
+2
-6
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObject;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
@@ -28,21 +29,16 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import oracle.sql.SQLUtil;
|
||||
|
||||
public class MySqlSelectQueryBlock extends SQLSelectQueryBlock implements MySqlObject {
|
||||
|
||||
private boolean hignPriority;
|
||||
private boolean straightJoin;
|
||||
|
||||
private boolean smallResult;
|
||||
private boolean bigResult;
|
||||
private boolean bufferResult;
|
||||
private Boolean cache;
|
||||
private boolean calcFoundRows;
|
||||
|
||||
private SQLName procedureName;
|
||||
private List<SQLExpr> procedureArgumentList;
|
||||
|
||||
private boolean lockInShareMode = false;
|
||||
|
||||
private boolean lockInShareMode;
|
||||
private List<SQLCommentHint> hints;
|
||||
|
||||
public MySqlSelectQueryBlock(){
|
||||
|
||||
@@ -31,10 +31,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlOutFileExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateTableSource;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.sql.parser.*;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
import java.util.List;
|
||||
@@ -48,6 +45,10 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
super(exprParser);
|
||||
}
|
||||
|
||||
public MySqlSelectParser(SQLExprParser exprParser, SQLSelectListCache selectListCache){
|
||||
super(exprParser, selectListCache);
|
||||
}
|
||||
|
||||
public MySqlSelectParser(String sql){
|
||||
this(new MySqlExprParser(sql));
|
||||
}
|
||||
@@ -93,6 +94,12 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
queryBlock.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
if (selectListCache != null) {
|
||||
selectListCache.match(lexer, queryBlock);
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
lexer.nextToken();
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
public SQLStatement parseSelect() {
|
||||
MySqlSelectParser selectParser = new MySqlSelectParser(this.exprParser);
|
||||
MySqlSelectParser selectParser = createSQLSelectParser();
|
||||
|
||||
SQLSelect select = selectParser.select();
|
||||
|
||||
@@ -120,7 +120,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
public SQLUpdateStatement parseUpdateStatement() {
|
||||
return new MySqlSelectParser(this.exprParser).parseUpdateStatment();
|
||||
return new MySqlSelectParser(this.exprParser, selectListCache).parseUpdateStatment();
|
||||
}
|
||||
|
||||
protected MySqlUpdateStatement createUpdateStatement() {
|
||||
@@ -2262,8 +2262,8 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return insertStatement;
|
||||
}
|
||||
|
||||
public SQLSelectParser createSQLSelectParser() {
|
||||
return new MySqlSelectParser(this.exprParser);
|
||||
public MySqlSelectParser createSQLSelectParser() {
|
||||
return new MySqlSelectParser(this.exprParser, selectListCache);
|
||||
}
|
||||
|
||||
public SQLStatement parseSet() {
|
||||
|
||||
@@ -116,62 +116,68 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
printlnComments(x.getBeforeCommentsDirect());
|
||||
}
|
||||
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
String cachedSelectList = x.getCachedSelectList();
|
||||
|
||||
for (int i = 0, size = x.getHintsSize(); i < size; ++i) {
|
||||
SQLCommentHint hint = x.getHints().get(i);
|
||||
hint.accept(this);
|
||||
print(' ');
|
||||
}
|
||||
if (cachedSelectList != null) {
|
||||
print0(cachedSelectList);
|
||||
} else {
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
|
||||
final int distionOption = x.getDistionOption();
|
||||
if (SQLSetQuantifier.ALL == distionOption) {
|
||||
print0(ucase ? "ALL " : "all ");
|
||||
} else if (SQLSetQuantifier.DISTINCT == distionOption) {
|
||||
print0(ucase ? "DISTINCT " : "distinct ");
|
||||
} else if (SQLSetQuantifier.DISTINCTROW == distionOption) {
|
||||
print0(ucase ? "DISTINCTROW " : "distinctrow ");
|
||||
}
|
||||
|
||||
if (x.isHignPriority()) {
|
||||
print0(ucase ? "HIGH_PRIORITY " : "high_priority ");
|
||||
}
|
||||
|
||||
if (x.isStraightJoin()) {
|
||||
print0(ucase ? "STRAIGHT_JOIN " : "straight_join ");
|
||||
}
|
||||
|
||||
if (x.isSmallResult()) {
|
||||
print0(ucase ? "SQL_SMALL_RESULT " : "sql_small_result ");
|
||||
}
|
||||
|
||||
if (x.isBigResult()) {
|
||||
print0(ucase ? "SQL_BIG_RESULT " : "sql_big_result ");
|
||||
}
|
||||
|
||||
if (x.isBufferResult()) {
|
||||
print0(ucase ? "SQL_BUFFER_RESULT " : "sql_buffer_result ");
|
||||
}
|
||||
|
||||
if (x.getCache() != null) {
|
||||
if (x.getCache().booleanValue()) {
|
||||
print0(ucase ? "SQL_CACHE " : "sql_cache ");
|
||||
} else {
|
||||
print0(ucase ? "SQL_NO_CACHE " : "sql_no_cache ");
|
||||
for (int i = 0, size = x.getHintsSize(); i < size; ++i) {
|
||||
SQLCommentHint hint = x.getHints().get(i);
|
||||
hint.accept(this);
|
||||
print(' ');
|
||||
}
|
||||
}
|
||||
|
||||
if (x.isCalcFoundRows()) {
|
||||
print0(ucase ? "SQL_CALC_FOUND_ROWS " : "sql_calc_found_rows ");
|
||||
}
|
||||
final int distionOption = x.getDistionOption();
|
||||
if (SQLSetQuantifier.ALL == distionOption) {
|
||||
print0(ucase ? "ALL " : "all ");
|
||||
} else if (SQLSetQuantifier.DISTINCT == distionOption) {
|
||||
print0(ucase ? "DISTINCT " : "distinct ");
|
||||
} else if (SQLSetQuantifier.DISTINCTROW == distionOption) {
|
||||
print0(ucase ? "DISTINCTROW " : "distinctrow ");
|
||||
}
|
||||
|
||||
printSelectList(x.getSelectList());
|
||||
if (x.isHignPriority()) {
|
||||
print0(ucase ? "HIGH_PRIORITY " : "high_priority ");
|
||||
}
|
||||
|
||||
SQLExprTableSource into = x.getInto();
|
||||
if (into != null) {
|
||||
println();
|
||||
print0(ucase ? "INTO " : "into ");
|
||||
printTableSource(into);
|
||||
if (x.isStraightJoin()) {
|
||||
print0(ucase ? "STRAIGHT_JOIN " : "straight_join ");
|
||||
}
|
||||
|
||||
if (x.isSmallResult()) {
|
||||
print0(ucase ? "SQL_SMALL_RESULT " : "sql_small_result ");
|
||||
}
|
||||
|
||||
if (x.isBigResult()) {
|
||||
print0(ucase ? "SQL_BIG_RESULT " : "sql_big_result ");
|
||||
}
|
||||
|
||||
if (x.isBufferResult()) {
|
||||
print0(ucase ? "SQL_BUFFER_RESULT " : "sql_buffer_result ");
|
||||
}
|
||||
|
||||
if (x.getCache() != null) {
|
||||
if (x.getCache().booleanValue()) {
|
||||
print0(ucase ? "SQL_CACHE " : "sql_cache ");
|
||||
} else {
|
||||
print0(ucase ? "SQL_NO_CACHE " : "sql_no_cache ");
|
||||
}
|
||||
}
|
||||
|
||||
if (x.isCalcFoundRows()) {
|
||||
print0(ucase ? "SQL_CALC_FOUND_ROWS " : "sql_calc_found_rows ");
|
||||
}
|
||||
|
||||
printSelectList(x.getSelectList());
|
||||
|
||||
SQLExprTableSource into = x.getInto();
|
||||
if (into != null) {
|
||||
println();
|
||||
print0(ucase ? "INTO " : "into ");
|
||||
printTableSource(into);
|
||||
}
|
||||
}
|
||||
|
||||
SQLTableSource from = x.getFrom();
|
||||
|
||||
Reference in New Issue
Block a user