backport sql parser improved from fastsql

This commit is contained in:
高铁
2019-10-05 01:50:37 +08:00
parent 20801d01ce
commit b79edab45f
110 changed files with 2766 additions and 838 deletions
@@ -37,6 +37,11 @@ public final class SQLOrderBy extends SQLObjectImpl {
addItem(item);
}
public SQLOrderBy(SQLExpr expr, SQLOrderingSpecification type){
SQLSelectOrderByItem item = new SQLSelectOrderByItem(expr, type);
addItem(item);
}
public void addItem(SQLSelectOrderByItem item) {
if (item != null) {
item.setParent(this);
@@ -44,6 +49,10 @@ public final class SQLOrderBy extends SQLObjectImpl {
this.items.add(item);
}
public void addItem(SQLExpr item) {
addItem(new SQLSelectOrderByItem(item));
}
public List<SQLSelectOrderByItem> getItems() {
return this.items;
}
@@ -1,5 +1,5 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,32 +15,22 @@
*/
package com.alibaba.druid.sql.ast.expr;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.ast.SQLKeep;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLOver;
import com.alibaba.druid.sql.ast.SQLReplaceable;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FnvHash;
public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLReplaceable {
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class SQLAggregateExpr extends SQLMethodInvokeExpr implements Serializable, SQLReplaceable {
private static final long serialVersionUID = 1L;
protected String methodName;
protected long methodNameHashCod64;
protected SQLAggregateOption option;
protected final List<SQLExpr> arguments = new ArrayList<SQLExpr>();
protected SQLKeep keep;
protected SQLExpr filter;
protected SQLOver over;
@@ -51,25 +41,21 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
public SQLAggregateExpr(String methodName){
this.methodName = methodName;
}
public SQLAggregateExpr(String methodName, SQLAggregateOption option){
this.methodName = methodName;
this.option = option;
}
public String getMethodName() {
return this.methodName;
}
public void setMethodName(String methodName) {
public SQLAggregateExpr(String methodName, SQLAggregateOption option, SQLExpr... arguments){
this.methodName = methodName;
}
public long methodNameHashCod64() {
if (methodNameHashCod64 == 0) {
methodNameHashCod64 = FnvHash.hashCode64(methodName);
this.option = option;
if (arguments != null) {
for (SQLExpr argument : arguments) {
if (argument != null) {
addArgument(argument);
}
}
}
return methodNameHashCod64;
}
public SQLOrderBy getWithinGroup() {
@@ -92,26 +78,19 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
this.option = option;
}
public List<SQLExpr> getArguments() {
return this.arguments;
}
public void addArgument(SQLExpr argument) {
if (argument != null) {
argument.setParent(this);
}
this.arguments.add(argument);
public boolean isDistinct() {
return option == SQLAggregateOption.DISTINCT;
}
public SQLOver getOver() {
return over;
}
public void setOver(SQLOver over) {
if (over != null) {
over.setParent(this);
public void setOver(SQLOver x) {
if (x != null) {
x.setParent(this);
}
this.over = over;
this.over = x;
}
public SQLName getOverRef() {
@@ -124,7 +103,7 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
}
this.overRef = x;
}
public SQLKeep getKeep() {
return keep;
}
@@ -135,7 +114,7 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
}
this.keep = keep;
}
public boolean isIgnoreNulls() {
return this.ignoreNulls != null && this.ignoreNulls;
}
@@ -156,11 +135,35 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.arguments);
acceptChild(visitor, this.keep);
acceptChild(visitor, this.over);
acceptChild(visitor, this.overRef);
acceptChild(visitor, this.withinGroup);
if (this.owner != null) {
this.owner.accept(visitor);
}
for (SQLExpr arg : this.arguments) {
if (arg != null) {
arg.accept(visitor);
}
}
if (this.keep != null) {
this.keep.accept(visitor);
}
if (this.filter != null) {
this.filter.accept(visitor);
}
if (this.over != null) {
this.over.accept(visitor);
}
if (this.overRef != null) {
this.overRef.accept(visitor);
}
if (this.withinGroup != null) {
this.withinGroup.accept(visitor);
}
}
visitor.endVisit(this);
@@ -182,17 +185,27 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
return children;
}
public SQLExpr getFilter() {
return filter;
}
public void setFilter(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this.filter = x;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
SQLAggregateExpr that = (SQLAggregateExpr) o;
if (methodNameHashCod64 != that.methodNameHashCod64) return false;
if (methodName != null ? !methodName.equals(that.methodName) : that.methodName != null) return false;
if (option != that.option) return false;
if (arguments != null ? !arguments.equals(that.arguments) : that.arguments != null) return false;
if (keep != null ? !keep.equals(that.keep) : that.keep != null) return false;
if (filter != null ? !filter.equals(that.filter) : that.filter != null) return false;
if (over != null ? !over.equals(that.over) : that.over != null) return false;
@@ -203,10 +216,8 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
@Override
public int hashCode() {
int result = methodName != null ? methodName.hashCode() : 0;
result = 31 * result + (int) (methodNameHashCod64 ^ (methodNameHashCod64 >>> 32));
int result = super.hashCode();
result = 31 * result + (option != null ? option.hashCode() : 0);
result = 31 * result + (arguments != null ? arguments.hashCode() : 0);
result = 31 * result + (keep != null ? keep.hashCode() : 0);
result = 31 * result + (filter != null ? filter.hashCode() : 0);
result = 31 * result + (over != null ? over.hashCode() : 0);
@@ -229,10 +240,6 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
x.setKeep(keep.clone());
}
if (filter != null) {
x.setFilter(filter.clone());
}
if (over != null) {
x.setOver(over.clone());
}
@@ -247,38 +254,34 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
x.ignoreNulls = ignoreNulls;
return x;
}
public SQLExpr getFilter() {
return filter;
}
public void setFilter(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this.filter = x;
}
public SQLDataType computeDataType() {
long hash = methodNameHashCod64();
if (hash == FnvHash.Constants.COUNT
|| hash == FnvHash.Constants.ROW_NUMBER) {
return SQLIntegerExpr.DEFAULT_DATA_TYPE;
}
if (arguments.size() > 0) {
SQLDataType dataType = arguments.get(0).computeDataType();
if (dataType != null) {
return dataType;
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof SQLObject) {
value = ((SQLObject) value).clone();
}
x.putAttribute(key, value);
}
}
if (hash == FnvHash.Constants.WM_CONCAT
|| hash == FnvHash.Constants.GROUP_CONCAT) {
return SQLCharExpr.DEFAULT_DATA_TYPE;
return x;
}
public SQLDataType computeDataType() {
if (resolvedReturnDataType != null) {
return resolvedReturnDataType;
}
long hash = methodNameHashCode64();
if (arguments.size() > 0) {
SQLDataType dataType = arguments.get(0)
.computeDataType();
if (dataType != null
&& (dataType.nameHashCode64() != FnvHash.Constants.BOOLEAN)) {
return dataType;
}
}
return null;
@@ -302,10 +305,9 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable, SQLRe
return true;
}
if (filter == expr) {
this.filter = target;
if (filter != null) {
filter = target;
target.setParent(this);
return true;
}
return false;
@@ -265,6 +265,21 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
return groupList;
}
public static List<SQLExpr> split(SQLExpr x, SQLBinaryOperator op) {
if (x instanceof SQLBinaryOpExprGroup) {
SQLBinaryOpExprGroup group = (SQLBinaryOpExprGroup) x;
if (group.getOperator() == op) {
return new ArrayList<SQLExpr>(group.getItems());
}
} else if (x instanceof SQLBinaryOpExpr) {
return split((SQLBinaryOpExpr) x, op);
}
List<SQLExpr> list = new ArrayList<SQLExpr>(1);
list.add(x);
return list;
}
public static void split(List<SQLExpr> outList, SQLExpr expr, SQLBinaryOperator op) {
if (expr == null) {
return;
@@ -649,4 +664,47 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
return binaryA.getLeft().toString().equals(binaryB.getLeft().toString());
}
public static boolean isOr(SQLExpr x) {
return x instanceof SQLBinaryOpExpr
&& ((SQLBinaryOpExpr) x).getOperator() == SQLBinaryOperator.BooleanOr;
}
public static SQLExpr or(List<? extends SQLExpr> list) {
if (list.size() == 0) {
return null;
}
SQLExpr first = list.get(0);
for (int i = 1; i < list.size(); i++) {
first = or(first, list.get(i));
}
return first;
}
public static SQLExpr or(SQLExpr a, SQLExpr b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
if (a instanceof SQLBinaryOpExprGroup) {
SQLBinaryOpExprGroup group = (SQLBinaryOpExprGroup) a;
if (group.getOperator() == SQLBinaryOperator.BooleanOr) {
group.add(b);
return group;
}
}
if (b instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr bb = (SQLBinaryOpExpr) b;
if (bb.operator == SQLBinaryOperator.BooleanOr) {
return or(or(a, bb.left), bb.right);
}
}
return new SQLBinaryOpExpr(a, SQLBinaryOperator.BooleanOr, b);
}
}
@@ -87,6 +87,16 @@ public final class SQLInListExpr extends SQLExprImpl implements Serializable {
this.targetList = targetList;
}
public void addTarget(SQLExpr x) {
x.setParent(this);
targetList.add(x);
}
public void addTarget(int index, SQLExpr x) {
x.setParent(this);
targetList.add(index, x);
}
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.expr);
@@ -1,5 +1,5 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,77 +15,118 @@
*/
package com.alibaba.druid.sql.ast.expr;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLReplaceable;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FnvHash;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable, Serializable {
private static final long serialVersionUID = 1L;
private String name;
private SQLExpr owner;
private final List<SQLExpr> parameters = new ArrayList<SQLExpr>();
private static final long serialVersionUID = 1L;
private SQLExpr from;
private SQLExpr using;
private SQLExpr _for;
private String trimOption;
private long nameHashCode64;
protected String methodName;
protected long methodNameHashCode64;
protected SQLExpr owner;
protected final List<SQLExpr> arguments = new ArrayList<SQLExpr>();
private SQLExpr from;
private SQLExpr using;
private SQLExpr _for;
private String trimOption;
protected transient SQLDataType resolvedReturnDataType;
public SQLMethodInvokeExpr(){
}
public SQLMethodInvokeExpr(String methodName){
this.name = methodName;
this.methodName = methodName;
}
public SQLMethodInvokeExpr(String methodName, long nameHashCode64){
this.name = methodName;
this.nameHashCode64 = nameHashCode64;
public SQLMethodInvokeExpr(String methodName, long methodNameHashCode64){
this.methodName = methodName;
this.methodNameHashCode64 = methodNameHashCode64;
}
public SQLMethodInvokeExpr(String methodName, SQLExpr owner){
this.name = methodName;
this.methodName = methodName;
setOwner(owner);
}
public SQLMethodInvokeExpr(String methodName, SQLExpr owner, SQLExpr... params){
this.name = methodName;
this.methodName = methodName;
setOwner(owner);
for (SQLExpr param : params) {
this.addParameter(param);
this.addArgument(param);
}
}
public SQLMethodInvokeExpr(String methodName, SQLExpr owner, List<SQLExpr> params) {
this.methodName = methodName;
setOwner(owner);
for (SQLExpr param : params) {
this.addArgument(param);
}
}
public long methodNameHashCode64() {
if (nameHashCode64 == 0
&& name != null) {
nameHashCode64 = FnvHash.hashCode64(name);
if (methodNameHashCode64 == 0
&& methodName != null) {
methodNameHashCode64 = FnvHash.hashCode64(methodName);
}
return nameHashCode64;
return methodNameHashCode64;
}
public String getMethodName() {
return this.name;
return this.methodName;
}
public void setMethodName(String methodName) {
this.name = methodName;
this.nameHashCode64 = 0L;
this.methodName = methodName;
this.methodNameHashCode64 = 0L;
}
/**
* instead of getArguments
* @deprecated
*/
public List<SQLExpr> getParameters() {
return this.arguments;
}
public List<SQLExpr> getArguments() {
return this.arguments;
}
public void setArgument(int i, SQLExpr arg) {
if (arg != null) {
arg.setParent(this);
}
this.arguments.set(i, arg);
}
/**
* deprecated, instead of addArgument
* @deprecated
*/
public void addParameter(SQLExpr param) {
if (param != null) {
param.setParent(this);
}
this.arguments.add(param);
}
public void addArgument(SQLExpr arg) {
if (arg != null) {
arg.setParent(this);
}
this.arguments.add(arg);
}
public SQLExpr getOwner() {
@@ -110,27 +151,7 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
this.from = x;
}
public List<SQLExpr> getArguments() {
return this.parameters;
}
public List<SQLExpr> getParameters() {
return this.parameters;
}
public void addParameter(SQLExpr param) {
if (param != null) {
param.setParent(this);
}
this.parameters.add(param);
}
public void addArgument(SQLExpr arg) {
if (arg != null) {
arg.setParent(this);
}
this.parameters.add(arg);
}
public void output(StringBuffer buf) {
if (this.owner != null) {
@@ -138,14 +159,14 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
buf.append(".");
}
buf.append(this.name);
buf.append(this.methodName);
buf.append("(");
for (int i = 0, size = this.parameters.size(); i < size; ++i) {
for (int i = 0, size = this.arguments.size(); i < size; ++i) {
if (i != 0) {
buf.append(", ");
}
this.parameters.get(i).output(buf);
this.arguments.get(i).output(buf);
}
buf.append(")");
}
@@ -153,11 +174,27 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.owner);
acceptChild(visitor, this.parameters);
acceptChild(visitor, this.from);
acceptChild(visitor, this.using);
acceptChild(visitor, this._for);
if (this.owner != null) {
this.owner.accept(visitor);
}
for (SQLExpr arg : this.arguments) {
if (arg != null) {
arg.accept(visitor);
}
}
if (this.from != null) {
this.from.accept(visitor);
}
if (this.using != null) {
this.using.accept(visitor);
}
if (this._for != null) {
this._for.accept(visitor);
}
}
visitor.endVisit(this);
@@ -165,22 +202,38 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
public List getChildren() {
if (this.owner == null) {
return this.parameters;
return this.arguments;
}
List<SQLObject> children = new ArrayList<SQLObject>();
children.add(owner);
children.addAll(this.parameters);
children.addAll(this.arguments);
return children;
}
protected void accept0(OracleASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.owner);
acceptChild(visitor, this.parameters);
acceptChild(visitor, this.from);
acceptChild(visitor, this.using);
acceptChild(visitor, this._for);
if (this.owner != null) {
this.owner.accept(visitor);
}
for (SQLExpr arg : this.arguments) {
if (arg != null) {
arg.accept(visitor);
}
}
if (this.from != null) {
this.from.accept(visitor);
}
if (this.using != null) {
this.using.accept(visitor);
}
if (this._for != null) {
this._for.accept(visitor);
}
}
visitor.endVisit(this);
@@ -193,18 +246,20 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
SQLMethodInvokeExpr that = (SQLMethodInvokeExpr) o;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (methodNameHashCode64() != that.methodNameHashCode64()) {
return false;
}
if (owner != null ? !owner.equals(that.owner) : that.owner != null) return false;
if (parameters != null ? !parameters.equals(that.parameters) : that.parameters != null) return false;
if (!arguments.equals(that.arguments)) return false;
return from != null ? from.equals(that.from) : that.from == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
int result = (int) (methodNameHashCode64() ^ (methodNameHashCode64() >>> 32));
result = 31 * result + (owner != null ? owner.hashCode() : 0);
result = 31 * result + (parameters != null ? parameters.hashCode() : 0);
result = 31 * result + arguments.hashCode();
result = 31 * result + (from != null ? from.hashCode() : 0);
return result;
}
@@ -212,14 +267,14 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
public SQLMethodInvokeExpr clone() {
SQLMethodInvokeExpr x = new SQLMethodInvokeExpr();
x.name = name;
x.methodName = methodName;
if (owner != null) {
x.setOwner(owner.clone());
}
for (SQLExpr param : parameters) {
x.addParameter(param.clone());
for (SQLExpr arg : arguments) {
x.addArgument(arg.clone());
}
if (from != null) {
@@ -230,6 +285,21 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
x.setUsing(using.clone());
}
if (trimOption != null) {
x.setTrimOption(trimOption);
}
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof SQLObject) {
value = ((SQLObject) value).clone();
}
x.putAttribute(key, value);
}
}
return x;
}
@@ -239,9 +309,9 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
return false;
}
for (int i = 0; i < parameters.size(); ++i) {
if (parameters.get(i) == expr) {
parameters.set(i, target);
for (int i = 0; i < arguments.size(); ++i) {
if (arguments.get(i) == expr) {
arguments.set(i, target);
target.setParent(this);
return true;
}
@@ -270,7 +340,7 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
return false;
}
if (!SQLUtils.nameEquals(function, name)) {
if (!SQLUtils.nameEquals(function, methodName)) {
return false;
}
@@ -290,19 +360,34 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
}
public SQLDataType computeDataType() {
if (SQLUtils.nameEquals("to_date", name)
|| SQLUtils.nameEquals("add_months", name)) {
return SQLDateExpr.DEFAULT_DATA_TYPE;
if (resolvedReturnDataType != null) {
return resolvedReturnDataType;
}
if (parameters.size() == 1) {
if (SQLUtils.nameEquals("trunc", name)) {
return parameters.get(0).computeDataType();
long nameHash = this.methodNameHashCode64();
if (nameHash == FnvHash.Constants.BIT_COUNT
|| nameHash == FnvHash.Constants.ROW_NUMBER) {
return resolvedReturnDataType = new SQLDataTypeImpl("BIGINT");
}
if (arguments.size() == 1) {
if (nameHash == FnvHash.Constants.TRUNC) {
return resolvedReturnDataType = arguments.get(0).computeDataType();
}
} else if (parameters.size() == 2) {
SQLExpr param0 = parameters.get(0);
SQLExpr param1 = parameters.get(1);
if (SQLUtils.nameEquals("nvl", name) || SQLUtils.nameEquals("ifnull", name)) {
} else if (arguments.size() == 2) {
SQLExpr param0 = arguments.get(0);
SQLExpr param1 = arguments.get(1);
if (nameHash == FnvHash.Constants.ROUND) {
SQLDataType dataType = param0.computeDataType();
if (dataType != null) {
return dataType;
}
} else if (nameHash == FnvHash.Constants.NVL
|| nameHash == FnvHash.Constants.IFNULL
|| nameHash == FnvHash.Constants.ISNULL
|| nameHash == FnvHash.Constants.COALESCE) {
SQLDataType dataType = param0.computeDataType();
if (dataType != null) {
return dataType;
@@ -310,7 +395,35 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
return param1.computeDataType();
}
}
// if (nameHash == FnvHash.Constants.ROUND) {
// return resolvedReturnDataType = SQLDecimalExpr.DATA_TYPE;
// }
if (nameHash == FnvHash.Constants.YEAR
|| nameHash == FnvHash.Constants.MONTH
|| nameHash == FnvHash.Constants.DAY
|| nameHash == FnvHash.Constants.HOUR
|| nameHash == FnvHash.Constants.MINUTE
|| nameHash == FnvHash.Constants.SECOND
|| nameHash == FnvHash.Constants.PERIOD_ADD
|| nameHash == FnvHash.Constants.PERIOD_DIFF
) {
return resolvedReturnDataType = new SQLDataTypeImpl("INT");
}
if (nameHash == FnvHash.Constants.GROUPING) {
return resolvedReturnDataType = new SQLDataTypeImpl("INT");
}
if (nameHash == FnvHash.Constants.TIME) {
return resolvedReturnDataType = new SQLDataTypeImpl("VARCHAR");
}
return null;
}
@@ -343,4 +456,12 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
public void setTrimOption(String trimOption) {
this.trimOption = trimOption;
}
public SQLDataType getResolvedReturnDataType() {
return resolvedReturnDataType;
}
public void setResolvedReturnDataType(SQLDataType resolvedReturnDataType) {
this.resolvedReturnDataType = resolvedReturnDataType;
}
}
@@ -21,6 +21,7 @@ import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLReplaceable;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
@@ -35,6 +36,8 @@ public class SQLExprTableSource extends SQLTableSourceImpl implements SQLReplace
private List<SQLName> partitions;
private SchemaObject schemaObject;
protected List<SQLName> columns;
public SQLExprTableSource(){
}
@@ -309,4 +312,54 @@ public class SQLExprTableSource extends SQLTableSourceImpl implements SQLReplace
}
return false;
}
public SQLObject resolveColum(long columnNameHash) {
if (schemaObject != null) {
SQLStatement stmt = schemaObject.getStatement();
if (stmt instanceof SQLCreateTableStatement) {
SQLCreateTableStatement createTableStmt = (SQLCreateTableStatement) stmt;
return createTableStmt.findColumn(columnNameHash);
}
}
SQLObject resolvedOwnerObject = null;
if (expr instanceof SQLIdentifierExpr) {
resolvedOwnerObject = ((SQLIdentifierExpr) expr).getResolvedOwnerObject();
}
if (resolvedOwnerObject == null) {
return resolvedOwnerObject;
}
if (resolvedOwnerObject instanceof SQLWithSubqueryClause.Entry) {
final SQLSelect subQuery = ((SQLWithSubqueryClause.Entry) resolvedOwnerObject)
.getSubQuery();
if (subQuery == null) {
return null;
}
final SQLSelectQueryBlock firstQueryBlock = subQuery.getFirstQueryBlock();
if (firstQueryBlock == null) {
return null;
}
SQLSelectItem selectItem = firstQueryBlock.findSelectItem(columnNameHash);
if (selectItem != null) {
return selectItem;
}
}
return null;
}
public List<SQLName> getColumns() {
if (columns == null) {
columns = new ArrayList<SQLName>(2);
}
return columns;
}
public List<SQLName> getColumnsDirect() {
return columns;
}
}
@@ -20,6 +20,7 @@ import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLReplaceable;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
@@ -125,6 +126,29 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
this.condition = condition;
}
public void addCondition(SQLExpr condition) {
if (this.condition == null) {
this.condition = condition;
setImplicitJoinToCross();
return;
}
this.condition = SQLBinaryOpExpr.and(this.condition, condition);
}
public void setImplicitJoinToCross() {
if (joinType == JoinType.COMMA) {
joinType = JoinType.CROSS_JOIN;
}
if (left instanceof SQLJoinTableSource) {
((SQLJoinTableSource) left).setImplicitJoinToCross();
}
if (right instanceof SQLJoinTableSource) {
((SQLJoinTableSource) right).setImplicitJoinToCross();
}
}
public void addConditionn(SQLExpr condition) {
this.condition = SQLBinaryOpExpr.and(this.condition, condition);
}
@@ -207,6 +231,7 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
JOIN("JOIN"), //
INNER_JOIN("INNER JOIN"), //
CROSS_JOIN("CROSS JOIN"), //
NATURAL_CROSS_JOIN("NATURAL CROSS JOIN"), //
NATURAL_JOIN("NATURAL JOIN"), //
NATURAL_INNER_JOIN("NATURAL INNER JOIN"), //
LEFT_OUTER_JOIN("LEFT JOIN"), //
@@ -517,4 +542,19 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
return null;
}
public SQLObject resolveColum(long columnNameHash) {
if (left != null) {
SQLObject column = left.resolveColum(columnNameHash);
if (column != null) {
return column;
}
}
if (right != null) {
return right.resolveColum(columnNameHash);
}
return null;
}
}
@@ -25,6 +25,7 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLLateralViewTableSource extends SQLTableSourceImpl {
private SQLTableSource tableSource;
private boolean outer;
private SQLMethodInvokeExpr method;
@@ -101,4 +102,12 @@ public class SQLLateralViewTableSource extends SQLTableSourceImpl {
}
return null;
}
public boolean isOuter() {
return outer;
}
public void setOuter(boolean outer) {
this.outer = outer;
}
}
@@ -21,6 +21,7 @@ import java.util.List;
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.SQLLimit;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.SQLOrderBy;
@@ -36,6 +37,7 @@ public class SQLSelect extends SQLObjectImpl {
protected SQLWithSubqueryClause withSubQuery;
protected SQLSelectQuery query;
protected SQLOrderBy orderBy;
protected SQLLimit limit;
protected List<SQLHint> hints;
@@ -123,31 +125,65 @@ public class SQLSelect extends SQLObjectImpl {
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((orderBy == null) ? 0 : orderBy.hashCode());
result = prime * result + ((query == null) ? 0 : query.hashCode());
result = prime * result + ((withSubQuery == null) ? 0 : withSubQuery.hashCode());
return result;
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SQLSelect sqlSelect = (SQLSelect) o;
if (forBrowse != sqlSelect.forBrowse) {
return false;
}
if (withSubQuery != null ? !withSubQuery.equals(sqlSelect.withSubQuery) : sqlSelect.withSubQuery != null) {
return false;
}
if (query != null ? !query.equals(sqlSelect.query) : sqlSelect.query != null) {
return false;
}
if (orderBy != null ? !orderBy.equals(sqlSelect.orderBy) : sqlSelect.orderBy != null) {
return false;
}
if (limit != null ? !limit.equals(sqlSelect.limit) : sqlSelect.limit != null) {
return false;
}
if (hints != null ? !hints.equals(sqlSelect.hints) : sqlSelect.hints != null) {
return false;
}
if (restriction != null ? !restriction.equals(sqlSelect.restriction) : sqlSelect.restriction != null) {
return false;
}
if (forXmlOptions != null ? !forXmlOptions.equals(sqlSelect.forXmlOptions) : sqlSelect.forXmlOptions != null) {
return false;
}
if (xmlPath != null ? !xmlPath.equals(sqlSelect.xmlPath) : sqlSelect.xmlPath != null) {
return false;
}
if (rowCount != null ? !rowCount.equals(sqlSelect.rowCount) : sqlSelect.rowCount != null) {
return false;
}
return offset != null ? offset.equals(sqlSelect.offset) : sqlSelect.offset == null;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
SQLSelect other = (SQLSelect) obj;
if (orderBy == null) {
if (other.orderBy != null) return false;
} else if (!orderBy.equals(other.orderBy)) return false;
if (query == null) {
if (other.query != null) return false;
} else if (!query.equals(other.query)) return false;
if (withSubQuery == null) {
if (other.withSubQuery != null) return false;
} else if (!withSubQuery.equals(other.withSubQuery)) return false;
return true;
public int hashCode()
{
int result = withSubQuery != null ? withSubQuery.hashCode() : 0;
result = 31 * result + (query != null ? query.hashCode() : 0);
result = 31 * result + (orderBy != null ? orderBy.hashCode() : 0);
result = 31 * result + (limit != null ? limit.hashCode() : 0);
result = 31 * result + (hints != null ? hints.hashCode() : 0);
result = 31 * result + (restriction != null ? restriction.hashCode() : 0);
result = 31 * result + (forBrowse ? 1 : 0);
result = 31 * result + (forXmlOptions != null ? forXmlOptions.hashCode() : 0);
result = 31 * result + (xmlPath != null ? xmlPath.hashCode() : 0);
result = 31 * result + (rowCount != null ? rowCount.hashCode() : 0);
result = 31 * result + (offset != null ? offset.hashCode() : 0);
return result;
}
public void output(StringBuffer buf) {
@@ -349,4 +385,15 @@ public class SQLSelect extends SQLObjectImpl {
return false;
}
public SQLLimit getLimit() {
return limit;
}
public void setLimit(SQLLimit x) {
if (x != null) {
x.setParent(this);
}
this.limit = x;
}
}
@@ -20,6 +20,7 @@ import java.util.List;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLSelectGroupByClause extends SQLObjectImpl {
@@ -28,6 +29,8 @@ public class SQLSelectGroupByClause extends SQLObjectImpl {
private SQLExpr having;
private boolean withRollUp = false;
private boolean withCube = false;
private boolean distinct = false;
private boolean paren = false;
public SQLSelectGroupByClause(){
@@ -96,4 +99,58 @@ public class SQLSelectGroupByClause extends SQLObjectImpl {
x.withCube = withCube;
return x;
}
public void addHaving(SQLExpr condition) {
if (condition == null) {
return;
}
if (having == null) {
having = condition;
} else {
having = SQLBinaryOpExpr.and(having, condition);
}
}
public boolean isDistinct() {
return distinct;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isParen() {
return paren;
}
public void setParen(boolean paren) {
this.paren = paren;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SQLSelectGroupByClause group = (SQLSelectGroupByClause) o;
if (withRollUp != group.withRollUp) return false;
if (withCube != group.withCube) return false;
if (distinct != group.distinct) return false;
if (paren != group.paren) return false;
if (!items.equals(group.items)) return false;
return having != null ? having.equals(group.having) : group.having == null;
}
@Override
public int hashCode() {
int result = items != null ? items.hashCode() : 0;
result = 31 * result + (having != null ? having.hashCode() : 0);
result = 31 * result + (withRollUp ? 1 : 0);
result = 31 * result + (withCube ? 1 : 0);
result = 31 * result + (distinct ? 1 : 0);
result = 31 * result + (paren ? 1 : 0);
return result;
}
}
@@ -38,6 +38,11 @@ public final class SQLSelectOrderByItem extends SQLObjectImpl implements SQLRepl
this.setExpr(expr);
}
public SQLSelectOrderByItem(SQLExpr expr, SQLOrderingSpecification type){
this.setExpr(expr);
this.type = type;
}
public SQLExpr getExpr() {
return this.expr;
}
File diff suppressed because it is too large Load Diff
@@ -15,11 +15,16 @@
*/
package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.ArrayList;
import java.util.List;
public class SQLSubqueryTableSource extends SQLTableSourceImpl {
protected SQLSelect select;
protected List<SQLName> columns = new ArrayList<SQLName>();
public SQLSubqueryTableSource(){
@@ -57,6 +62,7 @@ public class SQLSubqueryTableSource extends SQLTableSourceImpl {
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, select);
acceptChild(visitor, columns);
}
visitor.endVisit(this);
}
@@ -73,6 +79,12 @@ public class SQLSubqueryTableSource extends SQLTableSourceImpl {
if (select != null) {
x.select = select.clone();
}
for (SQLName column : columns) {
SQLName c2 = column.clone();
c2.setParent(x);
x.columns.add(c2);
}
}
public SQLSubqueryTableSource clone() {
@@ -114,4 +126,28 @@ public class SQLSubqueryTableSource extends SQLTableSourceImpl {
return null;
}
public List<SQLName> getColumns() {
return columns;
}
public void addColumn(SQLName column) {
column.setParent(this);
this.columns.add(column);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SQLSubqueryTableSource that = (SQLSubqueryTableSource) o;
return select != null ? select.equals(that.select) : that.select == null;
}
@Override
public int hashCode() {
return select != null ? select.hashCode() : 0;
}
}
@@ -38,6 +38,8 @@ public interface SQLTableSource extends SQLObject {
SQLExpr getFlashback();
void setFlashback(SQLExpr flashback);
SQLObject resolveColum(long columnNameHash);
SQLColumnDefinition findColumn(String columnName);
SQLColumnDefinition findColumn(long columnNameHash);
@@ -21,6 +21,7 @@ import java.util.List;
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.SQLObject;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.util.FnvHash;
@@ -139,4 +140,8 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
}
return null;
}
public SQLObject resolveColum(long columnNameHash) {
return findColumn(columnNameHash);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,19 +15,20 @@
*/
package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLDbTypedObject;
import com.alibaba.druid.sql.ast.SQLLimit;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery {
import java.util.ArrayList;
import java.util.List;
public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery, SQLDbTypedObject {
private boolean bracket = false;
private SQLSelectQuery left;
private SQLSelectQuery right;
private List<SQLSelectQuery> relations = new ArrayList<SQLSelectQuery>();
private SQLUnionOperator operator = SQLUnionOperator.UNION;
private SQLOrderBy orderBy;
@@ -52,26 +53,67 @@ public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery {
this.setRight(right);
}
public List<SQLSelectQuery> getRelations() {
return relations;
}
public void addRelation(SQLSelectQuery relation) {
if (relation != null) {
relation.setParent(this);
}
relations.add(relation);
}
public SQLSelectQuery getLeft() {
return left;
if (relations.size() == 0) {
return null;
}
return relations.get(0);
}
public void setLeft(SQLSelectQuery left) {
if (left != null) {
left.setParent(this);
}
this.left = left;
if (relations.size() == 0) {
relations.add(left);
} else if (relations.size() <= 2) {
relations.set(0, left);
} else {
throw new UnsupportedOperationException("multi-union");
}
}
public SQLSelectQuery getRight() {
return right;
if (relations.size() < 2) {
return null;
}
if (relations.size() == 2) {
return relations.get(1);
}
throw new UnsupportedOperationException("multi-union");
}
public void setRight(SQLSelectQuery right) {
if (right != null) {
right.setParent(this);
}
this.right = right;
if (relations.size() == 0) {
relations.add(null);
}
if (relations.size() == 1) {
relations.add(right);
} else if (relations.size() == 2) {
relations.set(1, right);
} else {
throw new UnsupportedOperationException("multi-union");
}
}
public SQLOrderBy getOrderBy() {
@@ -88,10 +130,17 @@ public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery {
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, left);
acceptChild(visitor, right);
acceptChild(visitor, orderBy);
acceptChild(visitor, limit);
for (SQLSelectQuery relation : relations) {
relation.accept(visitor);
}
if (orderBy != null) {
orderBy.accept(visitor);
}
if (limit != null) {
limit.accept(visitor);
}
}
visitor.endVisit(this);
}
@@ -120,12 +169,13 @@ public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery {
SQLUnionQuery x = new SQLUnionQuery();
x.bracket = bracket;
if (left != null) {
x.setLeft(left.clone());
}
if (right != null) {
x.setRight(right.clone());
for (SQLSelectQuery relation : relations) {
SQLSelectQuery r = relation.clone();
r.setParent(x);
x.relations.add(r);
}
x.operator = operator;
if (orderBy != null) {
@@ -142,6 +192,8 @@ public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery {
}
public SQLSelectQueryBlock getFirstQueryBlock() {
SQLSelectQuery left = getLeft();
if (left instanceof SQLSelectQueryBlock) {
return (SQLSelectQueryBlock) left;
}
@@ -161,8 +213,44 @@ public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery {
this.dbType = dbType;
}
public void output(StringBuffer buf) {
SQLASTOutputVisitor visitor = SQLUtils.createOutputVisitor(buf, dbType);
this.accept(visitor);
public boolean replace(SQLSelectQuery cmp, SQLSelectQuery target) {
for (int i = 0; i < relations.size(); i++) {
SQLSelectQuery r = relations.get(i);
if (r == cmp) {
if (cmp != null) {
cmp.setParent(this);
}
relations.set(i, cmp);
return true;
}
}
return false;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SQLUnionQuery that = (SQLUnionQuery) o;
if (bracket != that.bracket) return false;
if (relations != null ? !relations.equals(that.relations) : that.relations != null) return false;
if (operator != that.operator) return false;
if (orderBy != null ? !orderBy.equals(that.orderBy) : that.orderBy != null) return false;
if (limit != null ? !limit.equals(that.limit) : that.limit != null) return false;
return dbType == that.dbType;
}
@Override
public int hashCode() {
int result = (bracket ? 1 : 0);
result = 31 * result + (relations != null ? relations.hashCode() : 0);
result = 31 * result + (operator != null ? operator.hashCode() : 0);
result = 31 * result + (orderBy != null ? orderBy.hashCode() : 0);
result = 31 * result + (limit != null ? limit.hashCode() : 0);
result = 31 * result + (dbType != null ? dbType.hashCode() : 0);
return result;
}
}