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:
@@ -34,6 +34,10 @@ public class SQLBlockStatement extends SQLStatementImpl {
|
||||
|
||||
public SQLStatement exception;
|
||||
|
||||
public SQLBlockStatement() {
|
||||
|
||||
}
|
||||
|
||||
public List<SQLStatement> getStatementList() {
|
||||
return statementList;
|
||||
}
|
||||
@@ -110,4 +114,14 @@ public class SQLBlockStatement extends SQLStatementImpl {
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public SQLParameter findParameter(long hash) {
|
||||
for (SQLParameter param : this.parameters) {
|
||||
if (param.getName().name_hash_lower() == hash) {
|
||||
return param;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,6 +64,10 @@ public abstract class SQLConstraintImpl extends SQLObjectImpl implements SQLCons
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.setName(new SQLIdentifierExpr(name));
|
||||
}
|
||||
|
||||
public Boolean getEnable() {
|
||||
return enable;
|
||||
}
|
||||
@@ -107,7 +111,7 @@ public abstract class SQLConstraintImpl extends SQLObjectImpl implements SQLCons
|
||||
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) this.name;
|
||||
String columnName = identExpr.getName();
|
||||
columnName = SQLUtils.normalize(columnName, dbType);
|
||||
identExpr.setName(columnName);
|
||||
this.setName(columnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,4 +166,14 @@ public class SQLCreateProcedureStatement extends SQLStatementImpl implements SQL
|
||||
public void setModifiesSqlData(boolean modifiesSqlData) {
|
||||
this.modifiesSqlData = modifiesSqlData;
|
||||
}
|
||||
|
||||
public SQLParameter findParameter(long hash) {
|
||||
for (SQLParameter param : this.parameters) {
|
||||
if (param.getName().name_hash_lower() == hash) {
|
||||
return param;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.FNVUtils;
|
||||
import com.alibaba.druid.util.ListDG;
|
||||
import com.alibaba.druid.util.lang.Consumer;
|
||||
import oracle.sql.SQLUtil;
|
||||
|
||||
public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLStatement, SQLCreateStatement {
|
||||
|
||||
@@ -47,6 +48,8 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
|
||||
protected SQLExpr comment;
|
||||
|
||||
protected SQLExprTableSource like;
|
||||
|
||||
public SQLCreateTableStatement(){
|
||||
|
||||
}
|
||||
@@ -159,6 +162,21 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
this.select = select;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
@@ -569,13 +587,6 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
identExpr.setName(tableName);
|
||||
}
|
||||
|
||||
if (name instanceof SQLIdentifierExpr) {
|
||||
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) name;
|
||||
String tableName = identExpr.getName();
|
||||
tableName = SQLUtils.normalize(tableName);
|
||||
identExpr.setName(tableName);
|
||||
}
|
||||
|
||||
for (SQLTableElement element : this.tableElementList) {
|
||||
if (element instanceof SQLColumnDefinition) {
|
||||
SQLColumnDefinition column = (SQLColumnDefinition) element;
|
||||
@@ -901,8 +912,12 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
}
|
||||
|
||||
public SQLCreateTableStatement clone() {
|
||||
SQLCreateTableStatement x = new SQLCreateTableStatement();
|
||||
SQLCreateTableStatement x = new SQLCreateTableStatement(dbType);
|
||||
cloneTo(x);
|
||||
return x;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return SQLUtils.toSQLString(this, dbType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,11 +32,11 @@ public class SQLDeclareStatement extends SQLStatementImpl {
|
||||
|
||||
}
|
||||
|
||||
public SQLDeclareStatement(SQLExpr name, SQLDataType dataType) {
|
||||
public SQLDeclareStatement(SQLName name, SQLDataType dataType) {
|
||||
this.addItem(new SQLDeclareItem(name, dataType));
|
||||
}
|
||||
|
||||
public SQLDeclareStatement(SQLExpr name, SQLDataType dataType, SQLExpr value) {
|
||||
public SQLDeclareStatement(SQLName name, SQLDataType dataType, SQLExpr value) {
|
||||
this.addItem(new SQLDeclareItem(name, dataType, value));
|
||||
}
|
||||
|
||||
|
||||
@@ -20,10 +20,12 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLDeleteStatement extends SQLStatementImpl implements SQLReplaceable {
|
||||
protected SQLWithSubqueryClause with;
|
||||
|
||||
protected SQLTableSource tableSource;
|
||||
protected SQLExpr where;
|
||||
protected SQLTableSource from;
|
||||
protected SQLTableSource using;
|
||||
|
||||
protected boolean only = false;
|
||||
|
||||
@@ -44,6 +46,10 @@ public class SQLDeleteStatement extends SQLStatementImpl implements SQLReplaceab
|
||||
}
|
||||
}
|
||||
|
||||
if (with != null) {
|
||||
x.setWith(with.clone());
|
||||
}
|
||||
|
||||
if (tableSource != null) {
|
||||
x.setTableSource(tableSource.clone());
|
||||
}
|
||||
@@ -53,6 +59,9 @@ public class SQLDeleteStatement extends SQLStatementImpl implements SQLReplaceab
|
||||
if (from != null) {
|
||||
x.setFrom(from.clone());
|
||||
}
|
||||
if (using != null) {
|
||||
x.setUsing(using.clone());
|
||||
}
|
||||
x.only = only;
|
||||
}
|
||||
|
||||
@@ -131,6 +140,7 @@ public class SQLDeleteStatement extends SQLStatementImpl implements SQLReplaceab
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, with);
|
||||
acceptChild(visitor, tableSource);
|
||||
acceptChild(visitor, where);
|
||||
}
|
||||
@@ -165,4 +175,23 @@ public class SQLDeleteStatement extends SQLStatementImpl implements SQLReplaceab
|
||||
public void setOnly(boolean only) {
|
||||
this.only = only;
|
||||
}
|
||||
|
||||
public SQLTableSource getUsing() {
|
||||
return using;
|
||||
}
|
||||
|
||||
public void setUsing(SQLTableSource using) {
|
||||
this.using = using;
|
||||
}
|
||||
|
||||
public SQLWithSubqueryClause getWith() {
|
||||
return with;
|
||||
}
|
||||
|
||||
public void setWith(SQLWithSubqueryClause with) {
|
||||
if (with != null) {
|
||||
with.setParent(this);
|
||||
}
|
||||
this.with = with;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
|
||||
|
||||
protected SQLExpr expr;
|
||||
private List<SQLName> partitions;
|
||||
|
||||
private SchemaObject schemaObject;
|
||||
|
||||
public SQLExprTableSource(){
|
||||
@@ -257,15 +258,20 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
|
||||
}
|
||||
|
||||
public SQLTableSource findTableSourceWithColumn(long columnName_hash) {
|
||||
if (schemaObject == null) {
|
||||
return null;
|
||||
if (schemaObject != null) {
|
||||
SQLStatement stmt = schemaObject.getStatement();
|
||||
if (stmt instanceof SQLCreateTableStatement) {
|
||||
SQLCreateTableStatement createTableStmt = (SQLCreateTableStatement) stmt;
|
||||
if (createTableStmt.findColumn(columnName_hash) != null) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SQLStatement stmt = schemaObject.getStatement();
|
||||
if (stmt instanceof SQLCreateTableStatement) {
|
||||
SQLCreateTableStatement createTableStmt = (SQLCreateTableStatement) stmt;
|
||||
if (createTableStmt.findColumn(columnName_hash) != null) {
|
||||
return this;
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
SQLTableSource tableSource = ((SQLIdentifierExpr) expr).getResolvedTableSource();
|
||||
if (tableSource != null) {
|
||||
return tableSource.findTableSourceWithColumn(columnName_hash);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,6 +294,13 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
|
||||
}
|
||||
}
|
||||
|
||||
if (expr instanceof SQLPropertyExpr) {
|
||||
long hash = ((SQLPropertyExpr) expr).hashCode64();
|
||||
if (hash == alias_hash) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,17 @@ package com.alibaba.druid.sql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObjectImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import oracle.sql.SQLUtil;
|
||||
|
||||
public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
|
||||
private String dbType;
|
||||
protected SQLWithSubqueryClause with;
|
||||
|
||||
protected String dbType;
|
||||
|
||||
protected boolean upsert = false; // for phoenix
|
||||
|
||||
@@ -39,6 +43,10 @@ public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
|
||||
x.dbType = dbType;
|
||||
x.upsert = upsert;
|
||||
x.afterSemi = afterSemi;
|
||||
|
||||
if (with != null) {
|
||||
x.setWith(with.clone());
|
||||
}
|
||||
}
|
||||
|
||||
public SQLInsertStatement clone() {
|
||||
@@ -148,4 +156,20 @@ public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
|
||||
public void setAfterSemi(boolean afterSemi) {
|
||||
this.afterSemi = afterSemi;
|
||||
}
|
||||
|
||||
|
||||
public SQLWithSubqueryClause getWith() {
|
||||
return with;
|
||||
}
|
||||
|
||||
public void setWith(SQLWithSubqueryClause with) {
|
||||
if (with != null) {
|
||||
with.setParent(this);
|
||||
}
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return SQLUtils.toSQLString(this, dbType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,11 +50,7 @@ public class SQLMergeStatement extends SQLStatementImpl {
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
public void setAlias(String alias) {
|
||||
this.alias = alias;
|
||||
return into.getAlias();
|
||||
}
|
||||
|
||||
public SQLTableSource getInto() {
|
||||
@@ -171,8 +167,8 @@ public class SQLMergeStatement extends SQLStatementImpl {
|
||||
public void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, columns);
|
||||
acceptChild(visitor, columns);
|
||||
acceptChild(visitor, columns);
|
||||
acceptChild(visitor, values);
|
||||
acceptChild(visitor, where);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
@@ -186,10 +186,6 @@ public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
|
||||
}
|
||||
|
||||
public boolean match(long alias_hash) {
|
||||
if (alias == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
long hash = alias_hash();
|
||||
|
||||
if (hash == alias_hash) {
|
||||
|
||||
@@ -28,6 +28,8 @@ public class SQLSelectOrderByItem extends SQLObjectImpl implements SQLReplaceabl
|
||||
protected SQLOrderingSpecification type;
|
||||
protected NullsOrderType nullsOrderType;
|
||||
|
||||
protected transient SQLSelectItem resolvedSelectItem;
|
||||
|
||||
public SQLSelectOrderByItem(){
|
||||
|
||||
}
|
||||
@@ -140,4 +142,12 @@ public class SQLSelectOrderByItem extends SQLObjectImpl implements SQLReplaceabl
|
||||
x.nullsOrderType = nullsOrderType;
|
||||
return x;
|
||||
}
|
||||
|
||||
public SQLSelectItem getResolvedSelectItem() {
|
||||
return resolvedSelectItem;
|
||||
}
|
||||
|
||||
public void setResolvedSelectItem(SQLSelectItem resolvedSelectItem) {
|
||||
this.resolvedSelectItem = resolvedSelectItem;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,7 @@ import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.repository.SchemaObject;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.FNVUtils;
|
||||
@@ -52,6 +50,9 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
|
||||
protected SQLLimit limit;
|
||||
|
||||
// for oracle
|
||||
protected List<SQLExpr> forUpdateOf;
|
||||
|
||||
public SQLSelectQueryBlock(){
|
||||
|
||||
}
|
||||
@@ -313,6 +314,7 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.selectList);
|
||||
acceptChild(visitor, this.from);
|
||||
acceptChild(visitor, this.into);
|
||||
acceptChild(visitor, this.where);
|
||||
acceptChild(visitor, this.startWith);
|
||||
acceptChild(visitor, this.connectBy);
|
||||
@@ -370,6 +372,21 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
return x;
|
||||
}
|
||||
|
||||
public List<SQLExpr> getForUpdateOf() {
|
||||
if (forUpdateOf == null) {
|
||||
forUpdateOf = new ArrayList<SQLExpr>(1);
|
||||
}
|
||||
return forUpdateOf;
|
||||
}
|
||||
|
||||
public int getForUpdateOfSize() {
|
||||
if (forUpdateOf == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return forUpdateOf.size();
|
||||
}
|
||||
|
||||
public void cloneTo(SQLSelectQueryBlock x) {
|
||||
|
||||
x.distionOption = distionOption;
|
||||
@@ -484,6 +501,28 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean selectItemHashAllColumn() {
|
||||
for (SQLSelectItem item : this.selectList) {
|
||||
SQLExpr expr = item.getExpr();
|
||||
|
||||
boolean allColumn = expr instanceof SQLAllColumnExpr
|
||||
|| (expr instanceof SQLPropertyExpr && ((SQLPropertyExpr) expr).getName().equals("*"));
|
||||
|
||||
if (allColumn) {
|
||||
if (from instanceof SQLSubqueryTableSource) {
|
||||
SQLSelect subSelect = ((SQLSubqueryTableSource) from).select;
|
||||
SQLSelectQueryBlock queryBlock = subSelect.getQueryBlock();
|
||||
if (queryBlock != null) {
|
||||
return queryBlock.selectItemHashAllColumn();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public SQLColumnDefinition findColumn(String columnName) {
|
||||
if (from == null) {
|
||||
return null;
|
||||
|
||||
@@ -106,7 +106,12 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
|
||||
}
|
||||
|
||||
public SQLColumnDefinition findColumn(String columnName) {
|
||||
return null;
|
||||
if (columnName == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
long hash = FNVUtils.fnv_64_lower_normalized(alias);
|
||||
return findColumn(hash);
|
||||
}
|
||||
|
||||
public SQLColumnDefinition findColumn(long columnNameHash) {
|
||||
@@ -114,7 +119,12 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
|
||||
}
|
||||
|
||||
public SQLTableSource findTableSourceWithColumn(String columnName) {
|
||||
return null;
|
||||
if (columnName == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
long hash = FNVUtils.fnv_64_lower_normalized(alias);
|
||||
return findTableSourceWithColumn(hash);
|
||||
}
|
||||
|
||||
public SQLTableSource findTableSourceWithColumn(long columnNameHash) {
|
||||
|
||||
@@ -18,10 +18,7 @@ package com.alibaba.druid.sql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLReplaceable;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLUpdateStatement extends SQLStatementImpl implements SQLReplaceable {
|
||||
@@ -33,6 +30,9 @@ public class SQLUpdateStatement extends SQLStatementImpl implements SQLReplaceab
|
||||
protected SQLTableSource tableSource;
|
||||
protected List<SQLExpr> returning;
|
||||
|
||||
// for mysql
|
||||
protected SQLOrderBy orderBy;
|
||||
|
||||
public SQLUpdateStatement(){
|
||||
|
||||
}
|
||||
@@ -136,6 +136,7 @@ public class SQLUpdateStatement extends SQLStatementImpl implements SQLReplaceab
|
||||
acceptChild(visitor, from);
|
||||
acceptChild(visitor, items);
|
||||
acceptChild(visitor, where);
|
||||
acceptChild(visitor, orderBy);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
@@ -148,4 +149,16 @@ public class SQLUpdateStatement extends SQLStatementImpl implements SQLReplaceab
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public SQLOrderBy getOrderBy() {
|
||||
return orderBy;
|
||||
}
|
||||
|
||||
public void setOrderBy(SQLOrderBy orderBy) {
|
||||
if (orderBy != null) {
|
||||
orderBy.setParent(this);
|
||||
}
|
||||
this.orderBy = orderBy;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,6 +126,15 @@ public class SQLWithSubqueryClause extends SQLObjectImpl {
|
||||
public List<SQLName> getColumns() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public SQLTableSource findTableSourceWithColumn(long columnNameHash) {
|
||||
for (SQLName column : columns) {
|
||||
if (column.name_hash_lower() == columnNameHash) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Entry findEntry(long alias_hash) {
|
||||
|
||||
Reference in New Issue
Block a user