mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
mysql parser refactor.
This commit is contained in:
+8
-6
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.clause;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlStatementImpl;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
@@ -25,23 +27,23 @@ import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
public class MySqlCursorDeclareStatement extends MySqlStatementImpl{
|
||||
|
||||
//cursor name
|
||||
private String cursorName;
|
||||
private SQLName cursorName;
|
||||
//select statement
|
||||
private SQLSelectStatement select;
|
||||
private SQLSelect select;
|
||||
|
||||
public String getCursorName() {
|
||||
public SQLName getCursorName() {
|
||||
return cursorName;
|
||||
}
|
||||
|
||||
public void setCursorName(String cursorName) {
|
||||
public void setCursorName(SQLName cursorName) {
|
||||
this.cursorName = cursorName;
|
||||
}
|
||||
|
||||
public SQLSelectStatement getSelect() {
|
||||
public SQLSelect getSelect() {
|
||||
return select;
|
||||
}
|
||||
|
||||
public void setSelect(SQLSelectStatement select) {
|
||||
public void setSelect(SQLSelect select) {
|
||||
if (select != null) {
|
||||
select.setParent(this);
|
||||
}
|
||||
|
||||
-2
@@ -43,12 +43,10 @@ public class MySqlDeclareStatement extends MySqlStatementImpl {
|
||||
|
||||
@Override
|
||||
public void accept0(MySqlASTVisitor visitor) {
|
||||
// TODO Auto-generated method stub
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, varList);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,12 +26,17 @@ public class MySqlUserName extends MySqlExprImpl implements SQLName, Cloneable {
|
||||
|
||||
protected transient long userName_hash;
|
||||
|
||||
protected long hashCode64;
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
|
||||
hashCode64 = 0;
|
||||
userName_hash = 0;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
@@ -40,6 +45,9 @@ public class MySqlUserName extends MySqlExprImpl implements SQLName, Cloneable {
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
|
||||
hashCode64 = 0;
|
||||
userName_hash = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -87,4 +95,16 @@ public class MySqlUserName extends MySqlExprImpl implements SQLName, Cloneable {
|
||||
}
|
||||
return userName_hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long hashCode64() {
|
||||
if (hashCode64 == 0) {
|
||||
if (host != null) {
|
||||
hashCode64 = FNVUtils.fnv_64_lower(host, userName);
|
||||
} else {
|
||||
hashCode64 = name_hash_lower();
|
||||
}
|
||||
}
|
||||
return hashCode64;
|
||||
}
|
||||
}
|
||||
|
||||
+21
-14
@@ -48,7 +48,7 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
|
||||
private List<SQLCommentHint> optionHints = new ArrayList<SQLCommentHint>();
|
||||
|
||||
private SQLExprTableSource like;
|
||||
|
||||
|
||||
private SQLName tableGroup;
|
||||
|
||||
@@ -56,20 +56,7 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
super (JdbcConstants.MYSQL);
|
||||
}
|
||||
|
||||
public SQLExprTableSource getLike() {
|
||||
return like;
|
||||
}
|
||||
|
||||
public void setLike(SQLName like) {
|
||||
this.setLike(new SQLExprTableSource(like));
|
||||
}
|
||||
|
||||
public void setLike(SQLExprTableSource like) {
|
||||
if (like != null) {
|
||||
like.setParent(this);
|
||||
}
|
||||
this.like = like;
|
||||
}
|
||||
|
||||
public List<SQLCommentHint> getHints() {
|
||||
return hints;
|
||||
@@ -135,6 +122,9 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
}
|
||||
|
||||
public void setName(SQLName name) {
|
||||
if (name != null) {
|
||||
name.setParent(this);
|
||||
}
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@@ -143,6 +133,9 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
}
|
||||
|
||||
public void setStorage(SQLExpr storage) {
|
||||
if (storage != null) {
|
||||
storage.setParent(this);
|
||||
}
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
@@ -155,6 +148,20 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public TableSpaceOption clone() {
|
||||
TableSpaceOption x = new TableSpaceOption();
|
||||
|
||||
if (name != null) {
|
||||
x.setName(name.clone());
|
||||
}
|
||||
|
||||
if (storage != null) {
|
||||
x.setStorage(storage.clone());
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public List<SQLCommentHint> getOptionHints() {
|
||||
|
||||
-9
@@ -34,7 +34,6 @@ public class MySqlDeleteStatement extends SQLDeleteStatement {
|
||||
private boolean quick = false;
|
||||
private boolean ignore = false;
|
||||
|
||||
private SQLTableSource using;
|
||||
private SQLOrderBy orderBy;
|
||||
private SQLLimit limit;
|
||||
|
||||
@@ -104,14 +103,6 @@ public class MySqlDeleteStatement extends SQLDeleteStatement {
|
||||
this.ignore = ignore;
|
||||
}
|
||||
|
||||
public SQLTableSource getUsing() {
|
||||
return using;
|
||||
}
|
||||
|
||||
public void setUsing(SQLTableSource using) {
|
||||
this.using = using;
|
||||
}
|
||||
|
||||
public SQLOrderBy getOrderBy() {
|
||||
return orderBy;
|
||||
}
|
||||
|
||||
+5
@@ -23,6 +23,7 @@ import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class MySqlInsertStatement extends SQLInsertStatement {
|
||||
|
||||
@@ -34,6 +35,10 @@ public class MySqlInsertStatement extends SQLInsertStatement {
|
||||
|
||||
private final List<SQLExpr> duplicateKeyUpdate = new ArrayList<SQLExpr>();
|
||||
|
||||
public MySqlInsertStatement() {
|
||||
dbType = JdbcConstants.MYSQL;
|
||||
}
|
||||
|
||||
public void cloneTo(MySqlInsertStatement x) {
|
||||
super.cloneTo(x);
|
||||
x.lowPriority = lowPriority;
|
||||
|
||||
+5
@@ -18,12 +18,14 @@ package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
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;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import oracle.sql.SQLUtil;
|
||||
|
||||
public class MySqlSelectQueryBlock extends SQLSelectQueryBlock implements MySqlObject {
|
||||
|
||||
@@ -256,4 +258,7 @@ public class MySqlSelectQueryBlock extends SQLSelectQueryBlock implements MySqlO
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return SQLUtils.toMySqlString(this);
|
||||
}
|
||||
}
|
||||
|
||||
-9
@@ -24,8 +24,6 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
public class MySqlUpdateStatement extends SQLUpdateStatement implements MySqlStatement {
|
||||
|
||||
private SQLOrderBy orderBy;
|
||||
private SQLLimit limit;
|
||||
|
||||
private boolean lowPriority = false;
|
||||
@@ -121,12 +119,5 @@ public class MySqlUpdateStatement extends SQLUpdateStatement implements MySqlSta
|
||||
this.targetAffectRow = targetAffectRow;
|
||||
}
|
||||
|
||||
public SQLOrderBy getOrderBy() {
|
||||
return orderBy;
|
||||
}
|
||||
|
||||
public void setOrderBy(SQLOrderBy orderBy) {
|
||||
this.orderBy = orderBy;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -692,7 +692,8 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
statementList.add(this.parseWhile(label));
|
||||
} else if (lexer.token() == Token.BEGIN) {
|
||||
// parse begin-end statement with label
|
||||
statementList.add(this.parseBlock(label));
|
||||
SQLBlockStatement block = this.parseBlock(label);
|
||||
statementList.add(block);
|
||||
} else if (lexer.token() == Token.REPEAT) {
|
||||
// parse repeat statement with label
|
||||
statementList.add(this.parseRepeat(label));
|
||||
@@ -808,6 +809,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
|
||||
public SQLBlockStatement parseBlock() {
|
||||
SQLBlockStatement block = new SQLBlockStatement();
|
||||
block.setDbType(dbType);
|
||||
|
||||
accept(Token.BEGIN);
|
||||
this.parseStatementList(block.getStatementList(), -1, block);
|
||||
@@ -1821,7 +1823,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
if (lexer.token() == Token.VALUES || lexer.identifierEquals("VALUE")) {
|
||||
lexer.nextToken();
|
||||
|
||||
parseValueClause(stmt.getValuesList(), 0);
|
||||
parseValueClause(stmt.getValuesList(), 0, stmt);
|
||||
} else if (lexer.token() == Token.SELECT) {
|
||||
SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr();
|
||||
stmt.setQuery(queryExpr);
|
||||
@@ -2176,7 +2178,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
|
||||
if (lexer.token() == Token.VALUES || lexer.identifierEquals("VALUE")) {
|
||||
lexer.nextTokenLParen();
|
||||
parseValueClause(insertStatement.getValuesList(), columnSize);
|
||||
parseValueClause(insertStatement.getValuesList(), columnSize, insertStatement);
|
||||
} else if (lexer.token() == Token.SET) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -2225,7 +2227,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
return insertStatement;
|
||||
}
|
||||
|
||||
private void parseValueClause(List<ValuesClause> valueClauseList, int columnSize) {
|
||||
private void parseValueClause(List<ValuesClause> valueClauseList, int columnSize, SQLObject parent) {
|
||||
for (int i = 0; ; ++i) {
|
||||
int startPos = lexer.pos() - 1;
|
||||
|
||||
@@ -2281,6 +2283,7 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
}
|
||||
|
||||
SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause(valueExprList);
|
||||
values.setParent(parent);
|
||||
|
||||
if (lexer.isEnabled(SQLParserFeature.KeepInsertValueClauseOriginalString)) {
|
||||
int endPos = lexer.pos();
|
||||
@@ -4022,14 +4025,15 @@ public class MySqlStatementParser extends SQLStatementParser {
|
||||
MySqlCursorDeclareStatement stmt = new MySqlCursorDeclareStatement();
|
||||
accept(Token.DECLARE);
|
||||
|
||||
stmt.setCursorName(exprParser.name().getSimpleName());
|
||||
stmt.setCursorName(exprParser.name());
|
||||
|
||||
accept(Token.CURSOR);
|
||||
|
||||
accept(Token.FOR);
|
||||
|
||||
SQLSelectStatement selelctStmt = (SQLSelectStatement) parseSelect();
|
||||
stmt.setSelect(selelctStmt);
|
||||
//SQLSelectStatement selelctStmt = (SQLSelectStatement) parseSelect();
|
||||
SQLSelect select = this.createSQLSelectParser().select();
|
||||
stmt.setSelect(select);
|
||||
|
||||
accept(Token.SEMI);
|
||||
|
||||
|
||||
@@ -3275,7 +3275,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
@Override
|
||||
public boolean visit(MySqlCursorDeclareStatement x) {
|
||||
print0(ucase ? "DECLARE " : "declare ");
|
||||
print0(x.getCursorName());
|
||||
printExpr(x.getCursorName());
|
||||
print0(ucase ? " CURSOR FOR" : " cursor for");
|
||||
this.indentCount++;
|
||||
println();
|
||||
|
||||
+21
-3
@@ -65,7 +65,8 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectStatement x) {
|
||||
if (repository != null) {
|
||||
if (repository != null
|
||||
&& x.getParent() == null) {
|
||||
repository.resolve(x);
|
||||
}
|
||||
|
||||
@@ -82,7 +83,8 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
// DUAL
|
||||
public boolean visit(MySqlDeleteStatement x) {
|
||||
if (repository != null) {
|
||||
if (repository != null
|
||||
&& x.getParent() == null) {
|
||||
repository.resolve(x);
|
||||
}
|
||||
|
||||
@@ -122,6 +124,11 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlInsertStatement x) {
|
||||
if (repository != null
|
||||
&& x.getParent() == null) {
|
||||
repository.resolve(x);
|
||||
}
|
||||
|
||||
setMode(x, Mode.Insert);
|
||||
|
||||
setAliasMap();
|
||||
@@ -289,6 +296,11 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlReplaceStatement x) {
|
||||
if (repository != null
|
||||
&& x.getParent() == null) {
|
||||
repository.resolve(x);
|
||||
}
|
||||
|
||||
setMode(x, Mode.Replace);
|
||||
|
||||
setAliasMap();
|
||||
@@ -466,6 +478,11 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlExplainStatement x) {
|
||||
if (repository != null
|
||||
&& x.getParent() == null) {
|
||||
repository.resolve(x);
|
||||
}
|
||||
|
||||
SQLName tableName = x.getTableName();
|
||||
if (tableName != null) {
|
||||
String table = tableName.toString();
|
||||
@@ -1033,7 +1050,8 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlCreateTableStatement x) {
|
||||
if (repository != null) {
|
||||
if (repository != null
|
||||
&& x.getParent() == null) {
|
||||
repository.resolve(x);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user