refactor sql parser.

This commit is contained in:
wenshao
2017-08-28 01:26:21 +08:00
parent a983b2685b
commit 833a9b5a65
91 changed files with 2964 additions and 1368 deletions
@@ -28,6 +28,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex;
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;
@@ -216,13 +217,20 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
}
public SQLColumnDefinition findColumn(String columName) {
columName = SQLUtils.normalize(columName);
if (columName == null) {
return null;
}
long hash = FNVUtils.fnv_64_lower_normalized(columName);
return findColumn(hash);
}
public SQLColumnDefinition findColumn(long columName_hash) {
for (SQLTableElement element : tableElementList) {
if (element instanceof SQLColumnDefinition) {
SQLColumnDefinition column = (SQLColumnDefinition) element;
String name = column.computeAlias();
if (columName.equalsIgnoreCase(name)) {
SQLName columnName = column.getName();
if (columnName != null && columnName.name_hash_lower() == columName_hash) {
return column;
}
}
@@ -27,6 +27,7 @@ import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.repository.SchemaObject;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FNVUtils;
public class SQLExprTableSource extends SQLTableSourceImpl {
@@ -211,7 +212,29 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
return false;
}
public boolean containsAlias(long aliasHash) {
if (this.alias_hash() == aliasHash) {
return true;
}
if (expr instanceof SQLName) {
long exprNameHash = ((SQLName) expr).name_hash_lower();
return exprNameHash == alias_hash;
}
return false;
}
public SQLColumnDefinition findColumn(String columnName) {
if (columnName == null) {
return null;
}
long hash = FNVUtils.fnv_64_lower_normalized(columnName);
return findColumn(hash);
}
public SQLColumnDefinition findColumn(long columnNameHash) {
if (schemaObject == null) {
return null;
}
@@ -219,12 +242,21 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
SQLStatement stmt = schemaObject.getStatement();
if (stmt instanceof SQLCreateTableStatement) {
SQLCreateTableStatement createTableStmt = (SQLCreateTableStatement) stmt;
return createTableStmt.findColumn(columnName);
return createTableStmt.findColumn(columnNameHash);
}
return null;
}
public SQLTableSource findTableSourceWithColumn(String columnName) {
if (columnName == null) {
return null;
}
long hash = FNVUtils.fnv_64_lower_normalized(columnName);
return findTableSourceWithColumn(hash);
}
public SQLTableSource findTableSourceWithColumn(long columnName_hash) {
if (schemaObject == null) {
return null;
}
@@ -232,7 +264,7 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
SQLStatement stmt = schemaObject.getStatement();
if (stmt instanceof SQLCreateTableStatement) {
SQLCreateTableStatement createTableStmt = (SQLCreateTableStatement) stmt;
if (createTableStmt.findColumn(columnName) != null) {
if (createTableStmt.findColumn(columnName_hash) != null) {
return this;
}
}
@@ -240,17 +272,20 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
return null;
}
public SQLTableSource findTableSource(String alias) {
if (alias == null) {
public SQLTableSource findTableSource(long alias_hash) {
if (alias_hash == 0) {
return null;
}
if (SQLUtils.nameEquals(alias, computeAlias())) {
if (alias_hash() == alias_hash) {
return this;
}
if (containsAlias(alias)) {
return this;
if (expr instanceof SQLName) {
long exprNameHash = ((SQLName) expr).name_hash_lower();
if (exprNameHash == alias_hash) {
return this;
}
}
return null;
@@ -28,6 +28,7 @@ import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FNVUtils;
public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplaceable {
@@ -416,15 +417,20 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
}
public SQLColumnDefinition findColumn(String columnName) {
long hash = FNVUtils.fnv_64_lower_normalized(columnName);
return findColumn(hash);
}
public SQLColumnDefinition findColumn(long columnNameHash) {
if (left != null) {
SQLColumnDefinition column = left.findColumn(columnName);
SQLColumnDefinition column = left.findColumn(columnNameHash);
if (column != null) {
return column;
}
}
if (right != null) {
return right.findColumn(columnName);
return right.findColumn(columnNameHash);
}
return null;
@@ -432,15 +438,20 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
@Override
public SQLTableSource findTableSourceWithColumn(String columnName) {
long hash = FNVUtils.fnv_64_lower_normalized(columnName);
return findTableSourceWithColumn(hash);
}
public SQLTableSource findTableSourceWithColumn(long columnNameHash) {
if (left != null) {
SQLTableSource tableSource = left.findTableSourceWithColumn(columnName);
SQLTableSource tableSource = left.findTableSourceWithColumn(columnNameHash);
if (tableSource != null) {
return tableSource;
}
}
if (right != null) {
return right.findTableSourceWithColumn(columnName);
return right.findTableSourceWithColumn(columnNameHash);
}
return null;
@@ -517,20 +528,20 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
return joined;
}
public SQLTableSource findTableSource(String alias) {
if (alias == null) {
public SQLTableSource findTableSource(long alias_hash) {
if (alias_hash == 0) {
return null;
}
if (SQLUtils.nameEquals(alias, computeAlias())) {
if (alias_hash() == alias_hash) {
return this;
}
SQLTableSource result = left.findTableSource(alias);
SQLTableSource result = left.findTableSource(alias_hash);
if (result != null) {
return result;
}
return right.findTableSource(alias);
return right.findTableSource(alias_hash);
}
}
@@ -21,13 +21,17 @@ import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FNVUtils;
public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
protected SQLExpr expr;
protected String alias;
protected boolean connectByRoot = false;
protected transient long alias_hash;
public SQLSelectItem(){
}
@@ -170,24 +174,39 @@ public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
return false;
}
String alias_normalized = SQLUtils.normalize(alias);
long hash = FNVUtils.fnv_64_lower_normalized(alias);
return match(hash);
}
if (alias_normalized.equalsIgnoreCase(this.alias)) {
public long alias_hash() {
if (this.alias_hash == 0) {
this.alias_hash = FNVUtils.fnv_64_lower_normalized(alias);
}
return alias_hash;
}
public boolean match(long alias_hash) {
if (alias == null) {
return false;
}
long hash = alias_hash();
if (hash == alias_hash) {
return true;
}
if (expr instanceof SQLAllColumnExpr) {
SQLTableSource resolvedTableSource = ((SQLAllColumnExpr) expr).getResolvedTableSource();
if (resolvedTableSource != null
&& resolvedTableSource.findColumn(alias) != null) {
&& resolvedTableSource.findColumn(alias_hash) != null) {
return true;
}
return false;
}
if (expr instanceof SQLIdentifierExpr) {
String ident = ((SQLIdentifierExpr) expr).getName();
return alias_normalized.equalsIgnoreCase(SQLUtils.normalize(ident));
return ((SQLIdentifierExpr) expr).name_hash_lower() == alias_hash;
}
if (expr instanceof SQLPropertyExpr) {
@@ -195,13 +214,13 @@ public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
if ("*".equals(ident)) {
SQLTableSource resolvedTableSource = ((SQLPropertyExpr) expr).getResolvedTableSource();
if (resolvedTableSource != null
&& resolvedTableSource.findColumn(alias) != null) {
&& resolvedTableSource.findColumn(alias_hash) != null) {
return true;
}
return false;
}
return alias_normalized.equalsIgnoreCase(SQLUtils.normalize(ident));
return ((SQLPropertyExpr) expr).name_hash_lower() == alias_hash;
}
return false;
@@ -25,6 +25,7 @@ import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.repository.SchemaObject;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FNVUtils;
public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery, SQLReplaceable {
private boolean bracket = false;
@@ -448,6 +449,13 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return from.findTableSourceWithColumn(column);
}
public SQLTableSource findTableSourceWithColumn(long columnHash) {
if (from == null) {
return null;
}
return from.findTableSourceWithColumn(columnHash);
}
@Override
public boolean replace(SQLExpr expr, SQLExpr target) {
if (where == expr) {
@@ -462,10 +470,13 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return null;
}
String ident_normalized = SQLUtils.normalize(ident);
long hash = FNVUtils.fnv_64_lower_normalized(ident);
return findSelectItem(hash);
}
public SQLSelectItem findSelectItem(long identHash) {
for (SQLSelectItem item : this.selectList) {
if (item.match(ident_normalized)) {
if (item.match(identHash)) {
return item;
}
}
@@ -478,7 +489,8 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return null;
}
return from.findColumn(columnName);
long hash = FNVUtils.fnv_64_lower_normalized(columnName);
return from.findColumn(hash);
}
public void addCondition(SQLExpr expr) {
@@ -94,4 +94,21 @@ public class SQLSubqueryTableSource extends SQLTableSourceImpl {
return null;
}
public SQLTableSource findTableSourceWithColumn(long columnNameHash) {
if (select == null) {
return null;
}
SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock();
if (queryBlock == null) {
return null;
}
if (queryBlock.findSelectItem(columnNameHash) != null) {
return this;
}
return null;
}
}
@@ -38,7 +38,11 @@ public interface SQLTableSource extends SQLObject {
void setFlashback(SQLExpr flashback);
SQLColumnDefinition findColumn(String columnName);
SQLColumnDefinition findColumn(long columnNameHash);
SQLTableSource findTableSourceWithColumn(String columnName);
SQLTableSource findTableSourceWithColumn(long columnName_hash);
SQLTableSource findTableSource(String alias);
SQLTableSource findTableSource(long alias_hash);
}
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLHint;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.util.FNVUtils;
public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTableSource {
@@ -31,12 +32,13 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
protected SQLExpr flashback;
protected long alias_hash;
public SQLTableSourceImpl(){
}
public SQLTableSourceImpl(String alias){
this.alias = alias;
}
@@ -46,6 +48,7 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
public void setAlias(String alias) {
this.alias = alias;
this.alias_hash = 0L;
}
public int getHintsSize() {
@@ -86,7 +89,6 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
this.flashback = flashback;
}
public boolean containsAlias(String alias) {
if (SQLUtils.nameEquals(this.alias, alias)) {
return true;
@@ -95,23 +97,40 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
return false;
}
public long alias_hash() {
if (alias_hash == 0
&& alias != null) {
alias_hash = FNVUtils.fnv_64_lower_normalized(alias);
}
return alias_hash;
}
public SQLColumnDefinition findColumn(String columnName) {
return null;
}
public SQLColumnDefinition findColumn(long columnNameHash) {
return null;
}
public SQLTableSource findTableSourceWithColumn(String columnName) {
return null;
}
public SQLTableSource findTableSource(String alias) {
if (alias == null) {
return null;
}
public SQLTableSource findTableSourceWithColumn(long columnNameHash) {
return null;
}
if (SQLUtils.nameEquals(alias, computeAlias())) {
public SQLTableSource findTableSource(String alias) {
long hash = FNVUtils.fnv_64_lower_normalized(alias);
return findTableSource(hash);
}
public SQLTableSource findTableSource(long alias_hash) {
long hash = this.alias_hash();
if (hash != 0 && hash == alias_hash) {
return this;
}
return null;
}
}
@@ -133,6 +133,7 @@ public class SQLUpdateStatement extends SQLStatementImpl implements SQLReplaceab
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, tableSource);
acceptChild(visitor, from);
acceptChild(visitor, items);
acceptChild(visitor, where);
}
@@ -20,6 +20,7 @@ import java.util.List;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -28,6 +29,19 @@ public class SQLWithSubqueryClause extends SQLObjectImpl {
private Boolean recursive;
private final List<Entry> entries = new ArrayList<Entry>();
public SQLWithSubqueryClause clone() {
SQLWithSubqueryClause x = new SQLWithSubqueryClause();
x.recursive = recursive;
for (Entry entry : entries) {
Entry entry2 = entry.clone();
entry2.setParent(x);
x.entries.add(entry2);
}
return x;
}
public List<Entry> getEntries() {
return entries;
}
@@ -55,41 +69,76 @@ public class SQLWithSubqueryClause extends SQLObjectImpl {
visitor.endVisit(this);
}
public static class Entry extends SQLObjectImpl {
public static class Entry extends SQLTableSourceImpl {
protected SQLIdentifierExpr name;
protected final List<SQLName> columns = new ArrayList<SQLName>();
protected SQLSelect subQuery;
protected SQLStatement returningStatement;
public Entry clone() {
Entry x = new Entry();
for (SQLName column : columns) {
SQLName column2 = column.clone();
column2.setParent(x);
x.columns.add(column2);
}
if (subQuery != null) {
x.setSubQuery(subQuery.clone());
}
return x;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, name);
acceptChild(visitor, columns);
acceptChild(visitor, subQuery);
acceptChild(visitor, returningStatement);
}
visitor.endVisit(this);
}
public SQLIdentifierExpr getName() {
return name;
}
public void setName(SQLIdentifierExpr name) {
this.name = name;
}
public SQLSelect getSubQuery() {
return subQuery;
}
public void setSubQuery(SQLSelect subQuery) {
if (subQuery != null) {
subQuery.setParent(this);
}
this.subQuery = subQuery;
}
public SQLStatement getReturningStatement() {
return returningStatement;
}
public void setReturningStatement(SQLStatement returningStatement) {
if (returningStatement != null) {
returningStatement.setParent(this);
}
this.returningStatement = returningStatement;
}
public List<SQLName> getColumns() {
return columns;
}
}
public Entry findEntry(long alias_hash) {
if (alias_hash == 0) {
return null;
}
for (Entry entry : entries) {
if (entry.alias_hash() == alias_hash) {
return entry;
}
}
return null;
}
}