improved sql parser.

This commit is contained in:
wenshao
2017-09-17 17:11:26 +08:00
parent b54a987cc8
commit 3c98a1eee5
23 changed files with 955 additions and 31 deletions
@@ -26,11 +26,12 @@ import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.ast.SQLReplaceable;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.FnvHash;
public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable, Serializable {
private static final long serialVersionUID = 1L;
private String methodName;
private String name;
private SQLExpr owner;
private final List<SQLExpr> parameters = new ArrayList<SQLExpr>();
@@ -40,34 +41,50 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
private String trimOption;
private long nameHashCode64;
public SQLMethodInvokeExpr(){
}
public SQLMethodInvokeExpr(String methodName){
this.methodName = methodName;
this.name = methodName;
}
public SQLMethodInvokeExpr(String methodName, long nameHashCode64){
this.name = methodName;
this.nameHashCode64 = nameHashCode64;
}
public SQLMethodInvokeExpr(String methodName, SQLExpr owner){
this.methodName = methodName;
this.name = methodName;
setOwner(owner);
}
public SQLMethodInvokeExpr(String methodName, SQLExpr owner, SQLExpr... params){
this.methodName = methodName;
this.name = methodName;
setOwner(owner);
for (SQLExpr param : params) {
this.addParameter(param);
}
}
public long methodNameHashCode64() {
if (nameHashCode64 == 0
&& name != null) {
nameHashCode64 = FnvHash.hashCode64(name);
}
return nameHashCode64;
}
public String getMethodName() {
return this.methodName;
return this.name;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
this.name = methodName;
this.nameHashCode64 = 0L;
}
public SQLExpr getOwner() {
@@ -106,7 +123,7 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
buf.append(".");
}
buf.append(this.methodName);
buf.append(this.name);
buf.append("(");
for (int i = 0, size = this.parameters.size(); i < size; ++i) {
if (i != 0) {
@@ -144,7 +161,7 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
SQLMethodInvokeExpr that = (SQLMethodInvokeExpr) o;
if (methodName != null ? !methodName.equals(that.methodName) : that.methodName != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) 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;
return from != null ? from.equals(that.from) : that.from == null;
@@ -153,7 +170,7 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
@Override
public int hashCode() {
int result = methodName != null ? methodName.hashCode() : 0;
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (owner != null ? owner.hashCode() : 0);
result = 31 * result + (parameters != null ? parameters.hashCode() : 0);
result = 31 * result + (from != null ? from.hashCode() : 0);
@@ -163,7 +180,7 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
public SQLMethodInvokeExpr clone() {
SQLMethodInvokeExpr x = new SQLMethodInvokeExpr();
x.methodName = methodName;
x.name = name;
if (owner != null) {
x.setOwner(owner.clone());
@@ -204,7 +221,7 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
return false;
}
if (!SQLUtils.nameEquals(function, methodName)) {
if (!SQLUtils.nameEquals(function, name)) {
return false;
}
@@ -224,19 +241,19 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
}
public SQLDataType computeDataType() {
if (SQLUtils.nameEquals("to_date", methodName)
|| SQLUtils.nameEquals("add_months", methodName)) {
if (SQLUtils.nameEquals("to_date", name)
|| SQLUtils.nameEquals("add_months", name)) {
return SQLDateExpr.DEFAULT_DATA_TYPE;
}
if (parameters.size() == 1) {
if (SQLUtils.nameEquals("trunc", methodName)) {
if (SQLUtils.nameEquals("trunc", name)) {
return parameters.get(0).computeDataType();
}
} else if (parameters.size() == 2) {
SQLExpr param0 = parameters.get(0);
SQLExpr param1 = parameters.get(1);
if (SQLUtils.nameEquals("nvl", methodName) || SQLUtils.nameEquals("ifnull", methodName)) {
if (SQLUtils.nameEquals("nvl", name) || SQLUtils.nameEquals("ifnull", name)) {
SQLDataType dataType = param0.computeDataType();
if (dataType != null) {
return dataType;
@@ -24,7 +24,7 @@ import java.io.IOException;
import java.math.BigDecimal;
public class SQLNumberExpr extends SQLNumericLiteralExpr implements SQLValuableExpr {
private static SQLDataType defaultDataType = new SQLDataTypeImpl("number");
public final static SQLDataType defaultDataType = new SQLDataTypeImpl("number");
private Number number;