refactor sql parser.

This commit is contained in:
wenshao
2017-08-30 16:51:50 +08:00
parent aa260c9ee3
commit 99fe9112b4
75 changed files with 994 additions and 974 deletions
@@ -26,7 +26,7 @@ public class SQLOver extends SQLObjectImpl {
protected SQLOrderBy orderBy;
// for db2
protected SQLExpr of;
protected SQLName of;
protected SQLExpr windowing;
protected WindowingType windowingType = WindowingType.ROWS;
@@ -71,11 +71,11 @@ public class SQLOver extends SQLObjectImpl {
this.orderBy = orderBy;
}
public SQLExpr getOf() {
public SQLName getOf() {
return of;
}
public void setOf(SQLExpr of) {
public void setOf(SQLName of) {
if (of != null) {
of.setParent(this);
}
@@ -133,7 +133,9 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.arguments);
acceptChild(visitor, this.keep);
acceptChild(visitor, this.over);
acceptChild(visitor, this.withinGroup);
}
visitor.endVisit(this);
@@ -22,12 +22,12 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FnvHash;
public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
protected String name;
private transient String lowerName;
private transient long hashCode64;
protected String name;
private String lowerName;
private long hashCode64;
private transient SQLObject resolvedColumn;
private transient SQLObject resolvedOwnerObject;
private SQLObject resolvedColumn;
private SQLObject resolvedOwnerObject;
public SQLIdentifierExpr(){
@@ -125,6 +125,10 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
return null;
}
public SQLObject getResolvedColumnObject() {
return resolvedColumn;
}
public void setResolvedColumn(SQLColumnDefinition resolvedColumn) {
this.resolvedColumn = resolvedColumn;
}
@@ -29,7 +29,7 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
private transient long hashCode64;
private transient SQLColumnDefinition resolvedColumn;
private transient SQLObject resolvedTableSource;
private transient SQLObject resolvedOwnerObject;
public SQLPropertyExpr(String owner, String name){
this(new SQLIdentifierExpr(owner), name);
@@ -119,7 +119,7 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
} else if (owner == null){
hash = FnvHash.BASIC;
} else {
hash = FnvHash.fnv_64_lower(owner.toString());
hash = FnvHash.fnv1a_64_lower(owner.toString());
hash ^= '.';
hash *= FnvHash.PRIME;
@@ -170,7 +170,7 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
x.hashCode64 = hashCode64;
x.resolvedColumn = resolvedColumn;
x.resolvedTableSource = resolvedTableSource;
x.resolvedOwnerObject = resolvedOwnerObject;
return x;
}
@@ -214,41 +214,45 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
}
public SQLTableSource getResolvedTableSource() {
if (resolvedTableSource instanceof SQLTableSource) {
return (SQLTableSource) resolvedTableSource;
if (resolvedOwnerObject instanceof SQLTableSource) {
return (SQLTableSource) resolvedOwnerObject;
}
return null;
}
public void setResolvedTableSource(SQLTableSource resolvedTableSource) {
this.resolvedTableSource = resolvedTableSource;
this.resolvedOwnerObject = resolvedTableSource;
}
public void setResolvedProcedure(SQLCreateProcedureStatement stmt) {
this.resolvedTableSource = stmt;
this.resolvedOwnerObject = stmt;
}
public void setResolvedOwnerObject(SQLObject resolvedOwnerObject) {
this.resolvedTableSource = resolvedOwnerObject;
this.resolvedOwnerObject = resolvedOwnerObject;
}
public SQLCreateProcedureStatement getResolvedProcudure() {
if (this.resolvedTableSource instanceof SQLCreateProcedureStatement) {
return (SQLCreateProcedureStatement) this.resolvedTableSource;
if (this.resolvedOwnerObject instanceof SQLCreateProcedureStatement) {
return (SQLCreateProcedureStatement) this.resolvedOwnerObject;
}
return null;
}
public SQLObject getResolvedOwnerObject() {
return resolvedOwnerObject;
}
public SQLDataType computeDataType() {
if (resolvedColumn != null) {
return resolvedColumn.getDataType();
}
if (resolvedTableSource != null
&& resolvedTableSource instanceof SQLSubqueryTableSource) {
SQLSelect select = ((SQLSubqueryTableSource) resolvedTableSource).getSelect();
if (resolvedOwnerObject != null
&& resolvedOwnerObject instanceof SQLSubqueryTableSource) {
SQLSelect select = ((SQLSubqueryTableSource) resolvedOwnerObject).getSelect();
SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock();
if (queryBlock == null) {
return null;
@@ -32,7 +32,6 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
protected SQLExpr expr;
private List<SQLName> partitions;
private SchemaObject schemaObject;
public SQLExprTableSource(){
@@ -50,7 +50,10 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
protected SQLLimit limit;
// for oracle
protected List<SQLExpr> forUpdateOf;
protected List<SQLExpr> forUpdateOf;
protected List<SQLExpr> distributeBy;
protected List<SQLSelectOrderByItem> sortBy;
public SQLSelectQueryBlock(){
@@ -308,6 +311,14 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
this.noCycle = noCycle;
}
public List<SQLExpr> getDistributeBy() {
return distributeBy;
}
public List<SQLSelectOrderByItem> getSortBy() {
return sortBy;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -500,7 +511,7 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return null;
}
public boolean selectItemHashAllColumn() {
public boolean selectItemHasAllColumn() {
for (SQLSelectItem item : this.selectList) {
SQLExpr expr = item.getExpr();
@@ -512,7 +523,7 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
SQLSelect subSelect = ((SQLSubqueryTableSource) from).select;
SQLSelectQueryBlock queryBlock = subSelect.getQueryBlock();
if (queryBlock != null) {
return queryBlock.selectItemHashAllColumn();
return queryBlock.selectItemHasAllColumn();
}
}
return true;
@@ -522,6 +533,23 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return false;
}
public SQLSelectItem findAllColumnSelectItem() {
SQLSelectItem allColumnItem = null;
for (SQLSelectItem item : this.selectList) {
SQLExpr expr = item.getExpr();
boolean allColumn = expr instanceof SQLAllColumnExpr
|| (expr instanceof SQLPropertyExpr && ((SQLPropertyExpr) expr).getName().equals("*"));
if (allColumnItem != null) {
return null; // duplicateAllColumn
}
allColumnItem = item;
}
return allColumnItem;
}
public SQLColumnDefinition findColumn(String columnName) {
if (from == null) {
return null;
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLUpdateStatement extends SQLStatementImpl implements SQLReplaceable {
protected SQLWithSubqueryClause with; // for pg
protected final List<SQLUpdateSetItem> items = new ArrayList<SQLUpdateSetItem>();
protected SQLExpr where;
@@ -161,4 +162,15 @@ public class SQLUpdateStatement extends SQLStatementImpl implements SQLReplaceab
}
this.orderBy = orderBy;
}
public SQLWithSubqueryClause getWith() {
return with;
}
public void setWith(SQLWithSubqueryClause with) {
if (with != null) {
with.setParent(this);
}
this.with = with;
}
}
@@ -132,6 +132,15 @@ public class SQLWithSubqueryClause extends SQLObjectImpl {
return this;
}
}
if (subQuery != null) {
SQLSelectQueryBlock queryBlock = subQuery.getFirstQueryBlock();
if (queryBlock != null) {
if (queryBlock.findSelectItem(columnNameHash) != null) {
return this;
}
}
}
return null;
}
}