mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
backport sql parser improved from fastsql
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class OdpsSelectQueryBlock extends SQLSelectQueryBlock {
|
||||
public OdpsSelectQueryBlock(){
|
||||
dbType = JdbcConstants.ODPS;
|
||||
|
||||
distributeBy = new ArrayList<SQLExpr>();
|
||||
distributeBy = new ArrayList<SQLSelectOrderByItem>();
|
||||
sortBy = new ArrayList<SQLSelectOrderByItem>(2);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.alibaba.druid.sql.dialect.odps.parser;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderingSpecification;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
|
||||
@@ -29,6 +30,7 @@ import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectListCache;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.util.FnvHash;
|
||||
|
||||
public class OdpsSelectParser extends SQLSelectParser {
|
||||
public OdpsSelectParser(SQLExprParser exprParser){
|
||||
@@ -43,24 +45,24 @@ public class OdpsSelectParser extends SQLSelectParser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLSelectQuery query() {
|
||||
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
SQLSelectQuery select = query();
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return queryRest(select);
|
||||
return queryRest(select, acceptUnion);
|
||||
}
|
||||
|
||||
OdpsSelectQueryBlock queryBlock = new OdpsSelectQueryBlock();
|
||||
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
queryBlock.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
|
||||
accept(Token.SELECT);
|
||||
|
||||
|
||||
if (lexer.token() == Token.HINT) {
|
||||
this.exprParser.parseHints(queryBlock.getHints());
|
||||
}
|
||||
@@ -89,36 +91,51 @@ public class OdpsSelectParser extends SQLSelectParser {
|
||||
parseGroupBy(queryBlock);
|
||||
|
||||
queryBlock.setOrderBy(this.exprParser.parseOrderBy());
|
||||
|
||||
|
||||
if (lexer.token() == Token.DISTRIBUTE) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
this.exprParser.exprList(queryBlock.getDistributeBy(), queryBlock);
|
||||
|
||||
if (lexer.identifierEquals("SORT")) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
for (;;) {
|
||||
SQLExpr expr = this.expr();
|
||||
|
||||
SQLSelectOrderByItem sortByItem = new SQLSelectOrderByItem(expr);
|
||||
|
||||
if (lexer.token() == Token.ASC) {
|
||||
sortByItem.setType(SQLOrderingSpecification.ASC);
|
||||
lexer.nextToken();
|
||||
} else if (lexer.token() == Token.DESC) {
|
||||
sortByItem.setType(SQLOrderingSpecification.DESC);
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
queryBlock.getSortBy().add(sortByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
for (;;) {
|
||||
SQLSelectOrderByItem distributeByItem = this.exprParser.parseSelectOrderByItem();
|
||||
queryBlock.addDistributeBy(distributeByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.SORT)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
for (;;) {
|
||||
SQLSelectOrderByItem sortByItem = this.exprParser.parseSelectOrderByItem();
|
||||
queryBlock.addSortBy(sortByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.CLUSTER)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
for (;;) {
|
||||
SQLSelectOrderByItem clusterByItem = this.exprParser.parseSelectOrderByItem();
|
||||
queryBlock.addClusterBy(clusterByItem);
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -128,7 +145,7 @@ public class OdpsSelectParser extends SQLSelectParser {
|
||||
queryBlock.setLimit(new SQLLimit(this.expr()));
|
||||
}
|
||||
|
||||
return queryRest(queryBlock);
|
||||
return queryRest(queryBlock, acceptUnion);
|
||||
}
|
||||
|
||||
public SQLTableSource parseTableSource() {
|
||||
|
||||
@@ -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,12 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.postgresql.parser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.ast.SQLParameter;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
@@ -29,12 +24,9 @@ import com.alibaba.druid.sql.ast.statement.SQLTableSource;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGFunctionTableSource;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.IntoOption;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGValuesQuery;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLExprParser;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectListCache;
|
||||
import com.alibaba.druid.sql.parser.SQLSelectParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
import com.alibaba.druid.sql.parser.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PGSelectParser extends SQLSelectParser {
|
||||
|
||||
@@ -55,14 +47,9 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLSelectQuery query() {
|
||||
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
|
||||
if (lexer.token() == Token.VALUES) {
|
||||
lexer.nextToken();
|
||||
accept(Token.LPAREN);
|
||||
PGValuesQuery valuesQuery = new PGValuesQuery();
|
||||
this.exprParser.exprList(valuesQuery.getValues(), valuesQuery);
|
||||
accept(Token.RPAREN);
|
||||
return queryRest(valuesQuery);
|
||||
return valuesQuery(acceptUnion);
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
@@ -74,11 +61,15 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return queryRest(select);
|
||||
return queryRest(select, acceptUnion);
|
||||
}
|
||||
|
||||
PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
|
||||
|
||||
if (lexer.hasComment() && lexer.isKeepComments()) {
|
||||
queryBlock.addBeforeComment(lexer.readAndResetComments());
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.SELECT) {
|
||||
lexer.nextToken();
|
||||
|
||||
@@ -244,7 +235,7 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
queryBlock.setForClause(forClause);
|
||||
}
|
||||
|
||||
return queryRest(queryBlock);
|
||||
return queryRest(queryBlock, acceptUnion);
|
||||
}
|
||||
|
||||
protected SQLTableSource parseTableSourceRest(SQLTableSource tableSource) {
|
||||
@@ -264,7 +255,7 @@ public class PGSelectParser extends SQLSelectParser {
|
||||
if (alias != null) {
|
||||
functionTableSource.setAlias(alias);
|
||||
}
|
||||
|
||||
|
||||
lexer.nextToken();
|
||||
parserParameters(functionTableSource.getParameters());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
+33
-5
@@ -30,6 +30,7 @@ import com.alibaba.druid.sql.ast.SQLParameter;
|
||||
import com.alibaba.druid.sql.ast.SQLPartitionBy;
|
||||
import com.alibaba.druid.sql.ast.SQLSetQuantifier;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.SQLWindow;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBetweenExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryExpr;
|
||||
@@ -247,8 +248,16 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean visit(PGSelectQueryBlock x) {
|
||||
if ((!isParameterized()) && isPrettyFormat() && x.hasBeforeComment()) {
|
||||
printlnComments(x.getBeforeCommentsDirect());
|
||||
}
|
||||
|
||||
final boolean bracket = x.isBracket();
|
||||
if (bracket) {
|
||||
print('(');
|
||||
}
|
||||
|
||||
print0(ucase ? "SELECT " : "select ");
|
||||
|
||||
if (SQLSetQuantifier.ALL == x.getDistionOption()) {
|
||||
@@ -258,7 +267,9 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
if (x.getDistinctOn() != null && x.getDistinctOn().size() > 0) {
|
||||
print0(ucase ? "ON " : "on ");
|
||||
print0("(");
|
||||
printAndAccept(x.getDistinctOn(), ", ");
|
||||
print0(") ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,10 +292,16 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
x.getFrom().accept(this);
|
||||
}
|
||||
|
||||
if (x.getWhere() != null) {
|
||||
SQLExpr where = x.getWhere();
|
||||
if (where != null) {
|
||||
println();
|
||||
print0(ucase ? "WHERE " : "where ");
|
||||
x.getWhere().accept(this);
|
||||
where.accept(this);
|
||||
|
||||
if (where.hasAfterComment() && isPrettyFormat()) {
|
||||
print(' ');
|
||||
printlnComment(x.getWhere().getAfterCommentsDirect());
|
||||
}
|
||||
}
|
||||
|
||||
if (x.getGroupBy() != null) {
|
||||
@@ -292,9 +309,16 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
x.getGroupBy().accept(this);
|
||||
}
|
||||
|
||||
if (x.getWindow() != null) {
|
||||
// if (x.getWindow() != null) {
|
||||
// println();
|
||||
// x.getWindow().accept(this);
|
||||
// }
|
||||
|
||||
final List<SQLWindow> windows = x.getWindows();
|
||||
if (windows != null && windows.size() > 0) {
|
||||
println();
|
||||
x.getWindow().accept(this);
|
||||
print0(ucase ? "WINDOW " : "window ");
|
||||
printAndAccept(windows, ", ");
|
||||
}
|
||||
|
||||
if (x.getOrderBy() != null) {
|
||||
@@ -317,6 +341,10 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
x.getForClause().accept(this);
|
||||
}
|
||||
|
||||
if (bracket) {
|
||||
print(')');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLDataTypeImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLDataTypeRefExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
@@ -120,6 +121,8 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
protected long[] aggregateFunctionHashCodes = AGGREGATE_FUNCTIONS_CODES;
|
||||
|
||||
protected boolean allowIdentifierMethod = true;
|
||||
|
||||
public SQLExprParser(String sql){
|
||||
super(sql);
|
||||
}
|
||||
@@ -402,6 +405,12 @@ public class SQLExprParser extends SQLParser {
|
||||
} else if (JdbcConstants.MYSQL.equalsIgnoreCase(dbType) && ident.startsWith("0x") && (ident.length() % 2) == 0) {
|
||||
sqlExpr = new SQLHexExpr(ident.substring(2));
|
||||
} else {
|
||||
if (ident.charAt(0) == '`') {
|
||||
if (lexer.isEnabled(SQLParserFeature.IgnoreNameQuotes)) {
|
||||
ident = ident.substring(1, ident.length() - 1);
|
||||
}
|
||||
hash_lower = FnvHash.hashCode64(ident);
|
||||
}
|
||||
sqlExpr = new SQLIdentifierExpr(ident, hash_lower);
|
||||
}
|
||||
break;
|
||||
@@ -1058,6 +1067,10 @@ public class SQLExprParser extends SQLParser {
|
||||
if (lexer.token == Token.DISTINCT) {
|
||||
lexer.nextToken();
|
||||
distinct = true;
|
||||
|
||||
if (lexer.token == Token.RPAREN || lexer.token == Token.COMMA) {
|
||||
throw new ParserException(lexer.info());
|
||||
}
|
||||
}
|
||||
|
||||
String methodName = null;
|
||||
@@ -1072,43 +1085,87 @@ public class SQLExprParser extends SQLParser {
|
||||
methodName = identifierExpr.getName();
|
||||
hash_lower = identifierExpr.nameHashCode64();
|
||||
|
||||
if (hash_lower == FnvHash.Constants.TRIM) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.LEADING)) {
|
||||
trimOption = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.BOTH)) {
|
||||
trimOption = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.TRAILING)) {
|
||||
trimOption = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
if (allowIdentifierMethod) {
|
||||
if (hash_lower == FnvHash.Constants.TRIM) {
|
||||
if (lexer.identifierEquals(FnvHash.Constants.LEADING)) {
|
||||
trimOption = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.BOTH)) {
|
||||
trimOption = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.TRAILING)) {
|
||||
trimOption = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
}
|
||||
} else if (hash_lower == FnvHash.Constants.MATCH
|
||||
&& (JdbcConstants.MYSQL.equals(dbType) || JdbcConstants.ALIYUN_ADS.equals(dbType))) {
|
||||
return parseMatch();
|
||||
} else if (hash_lower == FnvHash.Constants.EXTRACT
|
||||
&& JdbcConstants.MYSQL.equals(dbType)) {
|
||||
return parseExtract();
|
||||
} else if (hash_lower == FnvHash.Constants.POSITION
|
||||
&& JdbcConstants.MYSQL.equals(dbType)) {
|
||||
return parsePosition();
|
||||
}else if (hash_lower == FnvHash.Constants.INT4 && JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
PGTypeCastExpr castExpr = new PGTypeCastExpr();
|
||||
castExpr.setExpr(this.expr());
|
||||
castExpr.setDataType(new SQLDataTypeImpl(methodName));
|
||||
accept(Token.RPAREN);
|
||||
return castExpr;
|
||||
} else if (hash_lower == FnvHash.Constants.VARBIT && JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
PGTypeCastExpr castExpr = new PGTypeCastExpr();
|
||||
SQLExpr len = this.primary();
|
||||
castExpr.setDataType(new SQLDataTypeImpl(methodName, len));
|
||||
accept(Token.RPAREN);
|
||||
castExpr.setExpr(this.expr());
|
||||
return castExpr;
|
||||
} else if (hash_lower == FnvHash.Constants.CONVERT && JdbcConstants.MYSQL.equals(dbType)) {
|
||||
methodInvokeExpr = new SQLMethodInvokeExpr(methodName, hash_lower);
|
||||
SQLExpr arg0 = this.expr();
|
||||
// Fix for using.
|
||||
Object exprUsing = arg0.getAttributes().get("USING");
|
||||
if (exprUsing instanceof String) {
|
||||
String charset = (String) exprUsing;
|
||||
methodInvokeExpr.setUsing(new SQLIdentifierExpr(charset));
|
||||
arg0.getAttributes().remove("USING");
|
||||
}
|
||||
methodInvokeExpr.addArgument(arg0);
|
||||
|
||||
if (lexer.token == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
SQLDataType dataType = this.parseDataType();
|
||||
SQLDataTypeRefExpr dataTypeRefExpr = new SQLDataTypeRefExpr(dataType);
|
||||
methodInvokeExpr.addArgument(dataTypeRefExpr);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.USING || lexer.identifierEquals(FnvHash.Constants.USING)) {
|
||||
lexer.nextToken();
|
||||
SQLExpr using;
|
||||
if (lexer.token == Token.STAR) {
|
||||
lexer.nextToken();
|
||||
using = new SQLAllColumnExpr();
|
||||
} else if (lexer.token == Token.BINARY) {
|
||||
using = new SQLIdentifierExpr(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
} else {
|
||||
using = this.primary();
|
||||
}
|
||||
methodInvokeExpr.setUsing(using);
|
||||
}
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return primaryRest(methodInvokeExpr);
|
||||
}
|
||||
} else if (hash_lower == FnvHash.Constants.MATCH
|
||||
&& JdbcConstants.MYSQL.equals(dbType)) {
|
||||
return parseMatch();
|
||||
} else if (hash_lower == FnvHash.Constants.EXTRACT
|
||||
&& JdbcConstants.MYSQL.equals(dbType)) {
|
||||
return parseExtract();
|
||||
} else if (hash_lower == FnvHash.Constants.POSITION
|
||||
&& JdbcConstants.MYSQL.equals(dbType)) {
|
||||
return parsePosition();
|
||||
} else if (hash_lower == FnvHash.Constants.INT4 && JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
PGTypeCastExpr castExpr = new PGTypeCastExpr();
|
||||
castExpr.setExpr(this.expr());
|
||||
castExpr.setDataType(new SQLDataTypeImpl(methodName));
|
||||
accept(Token.RPAREN);
|
||||
return castExpr;
|
||||
} else if (hash_lower == FnvHash.Constants.VARBIT && JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
PGTypeCastExpr castExpr = new PGTypeCastExpr();
|
||||
SQLExpr len = this.primary();
|
||||
castExpr.setDataType(new SQLDataTypeImpl(methodName, len));
|
||||
accept(Token.RPAREN);
|
||||
castExpr.setExpr(this.expr());
|
||||
return castExpr;
|
||||
}
|
||||
aggMethodName = getAggreateFunction(hash_lower);
|
||||
|
||||
if (distinct) {
|
||||
aggMethodName = methodName;
|
||||
} else {
|
||||
aggMethodName = getAggreateFunction(hash_lower);
|
||||
}
|
||||
} else if (expr instanceof SQLPropertyExpr) {
|
||||
methodName = expr.toString();
|
||||
methodName = ((SQLPropertyExpr) expr).getSimpleName();
|
||||
aggMethodName = SQLUtils.normalize(methodName);
|
||||
hash_lower = FnvHash.fnv1a_64_lower(aggMethodName);
|
||||
aggMethodName = getAggreateFunction(hash_lower);
|
||||
@@ -1118,10 +1175,13 @@ public class SQLExprParser extends SQLParser {
|
||||
methodName = "DEFAULT";
|
||||
} else if (expr instanceof SQLCharExpr) {
|
||||
methodName = ((SQLCharExpr) expr).getText();
|
||||
if (isAggreateFunction(methodName)) {
|
||||
aggMethodName = methodName;
|
||||
}
|
||||
}
|
||||
|
||||
if (aggMethodName != null) {
|
||||
SQLAggregateExpr aggregateExpr = parseAggregateExpr(aggMethodName);
|
||||
SQLAggregateExpr aggregateExpr = parseAggregateExpr(methodName);
|
||||
if (distinct) {
|
||||
aggregateExpr.setOption(SQLAggregateOption.DISTINCT);
|
||||
}
|
||||
@@ -1140,12 +1200,12 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
Token token = lexer.token;
|
||||
if (token != Token.RPAREN && token != Token.FROM) {
|
||||
exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
|
||||
exprList(methodInvokeExpr.getArguments(), methodInvokeExpr);
|
||||
}
|
||||
|
||||
if (hash_lower == FnvHash.Constants.EXIST
|
||||
&& methodInvokeExpr.getParameters().size() == 1
|
||||
&& methodInvokeExpr.getParameters().get(0) instanceof SQLQueryExpr) {
|
||||
&& methodInvokeExpr.getArguments().size() == 1
|
||||
&& methodInvokeExpr.getArguments().get(0) instanceof SQLQueryExpr) {
|
||||
throw new ParserException("exists syntax error.");
|
||||
}
|
||||
|
||||
@@ -1176,13 +1236,42 @@ public class SQLExprParser extends SQLParser {
|
||||
methodInvokeExpr.setUsing(using);
|
||||
}
|
||||
|
||||
// mysql
|
||||
if (hash_lower == FnvHash.Constants.WEIGHT_STRING) {
|
||||
if (lexer.token == Token.AS) {
|
||||
lexer.nextToken();
|
||||
SQLDataType as = this.parseDataType();
|
||||
methodInvokeExpr.putAttribute("as", as);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.LEVEL)) {
|
||||
lexer.nextToken();
|
||||
List<SQLSelectOrderByItem> levels = new ArrayList<SQLSelectOrderByItem>();
|
||||
for (;;) {
|
||||
SQLSelectOrderByItem level = this.parseSelectOrderByItem();
|
||||
levels.add(level);
|
||||
if (lexer.token == Token.COMMA) {
|
||||
lexer.nextToken();
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
methodInvokeExpr.putAttribute("levels", levels);
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.REVERSE)) {
|
||||
lexer.nextToken();
|
||||
methodInvokeExpr.putAttribute("reverse", true);
|
||||
}
|
||||
}
|
||||
|
||||
SQLAggregateExpr aggregateExpr = null;
|
||||
if (lexer.token == Token.ORDER) {
|
||||
lexer.nextToken();
|
||||
accept(Token.BY);
|
||||
|
||||
aggregateExpr = new SQLAggregateExpr(methodName);
|
||||
aggregateExpr.getArguments().addAll(methodInvokeExpr.getParameters());
|
||||
aggregateExpr.getArguments().addAll(methodInvokeExpr.getArguments());
|
||||
|
||||
SQLOrderBy orderBy = new SQLOrderBy();
|
||||
this.orderBy(orderBy.getItems(), orderBy);
|
||||
@@ -1191,10 +1280,28 @@ public class SQLExprParser extends SQLParser {
|
||||
|
||||
accept(Token.RPAREN);
|
||||
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.FILTER)) {
|
||||
if (aggregateExpr == null) {
|
||||
Lexer.SavePoint mark = lexer.mark();
|
||||
lexer.nextToken();
|
||||
Token nextToken = lexer.token;
|
||||
lexer.reset(mark);
|
||||
|
||||
if (nextToken == Token.LPAREN) {
|
||||
aggregateExpr = new SQLAggregateExpr(methodName);
|
||||
aggregateExpr.getArguments().addAll(methodInvokeExpr.getArguments());
|
||||
filter(aggregateExpr);
|
||||
}
|
||||
} else {
|
||||
filter(aggregateExpr);
|
||||
}
|
||||
}
|
||||
|
||||
if (lexer.token == Token.OVER) {
|
||||
if (aggregateExpr == null) {
|
||||
aggregateExpr = new SQLAggregateExpr(methodName);
|
||||
aggregateExpr.getArguments().addAll(methodInvokeExpr.getParameters());
|
||||
aggregateExpr.getArguments().addAll(methodInvokeExpr.getArguments());
|
||||
}
|
||||
over(aggregateExpr);
|
||||
}
|
||||
@@ -1203,6 +1310,10 @@ public class SQLExprParser extends SQLParser {
|
||||
return primaryRest(aggregateExpr);
|
||||
}
|
||||
|
||||
if (lexer.token == Token.LPAREN) {
|
||||
return methodInvokeExpr;
|
||||
}
|
||||
|
||||
return primaryRest(methodInvokeExpr);
|
||||
|
||||
//throw new ParserException("not support token:" + lexer.token + ", " + lexer.info());
|
||||
|
||||
@@ -25,6 +25,8 @@ public enum SQLParserFeature {
|
||||
KeepComments,
|
||||
SkipComments,
|
||||
StrictForWall,
|
||||
EnableMultiUnion,
|
||||
IgnoreNameQuotes,
|
||||
|
||||
PipesAsConcat, // for mysql
|
||||
;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1917,6 +1917,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
orderBy.accept(this);
|
||||
}
|
||||
|
||||
final SQLLimit limit = x.getLimit();
|
||||
if (limit != null) {
|
||||
println();
|
||||
limit.accept(this);
|
||||
}
|
||||
|
||||
if (x.getHintsSize() > 0) {
|
||||
printAndAccept(x.getHints(), "");
|
||||
}
|
||||
@@ -2881,6 +2887,34 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
@Override
|
||||
public boolean visit(SQLUnionQuery x) {
|
||||
SQLUnionOperator operator = x.getOperator();
|
||||
|
||||
List<SQLSelectQuery> relations = x.getRelations();
|
||||
if (relations.size() > 2) {
|
||||
for (int i = 0; i < relations.size(); i++) {
|
||||
if (i != 0) {
|
||||
println();
|
||||
print0(ucase ? operator.name : operator.name_lcase);
|
||||
println();
|
||||
}
|
||||
|
||||
SQLSelectQuery item = relations.get(i);
|
||||
item.accept(this);
|
||||
}
|
||||
|
||||
SQLOrderBy orderBy = x.getOrderBy();
|
||||
if (orderBy != null) {
|
||||
println();
|
||||
orderBy.accept(this);
|
||||
}
|
||||
|
||||
SQLLimit limit = x.getLimit();
|
||||
if (limit != null) {
|
||||
println();
|
||||
limit.accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
SQLSelectQuery left = x.getLeft();
|
||||
SQLSelectQuery right = x.getRight();
|
||||
|
||||
@@ -2891,10 +2925,18 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
&& left instanceof SQLUnionQuery
|
||||
&& ((SQLUnionQuery) left).getOperator() == operator
|
||||
&& !right.isBracket()
|
||||
&& x.getLimit() == null
|
||||
&& orderBy == null) {
|
||||
|
||||
SQLUnionQuery leftUnion = (SQLUnionQuery) left;
|
||||
|
||||
if (right instanceof SQLSelectQueryBlock) {
|
||||
SQLSelectQueryBlock rightQueryBlock = (SQLSelectQueryBlock) right;
|
||||
if (rightQueryBlock.getOrderBy() != null || rightQueryBlock.getLimit() != null) {
|
||||
rightQueryBlock.setBracket(true);
|
||||
}
|
||||
}
|
||||
|
||||
List<SQLSelectQuery> rights = new ArrayList<SQLSelectQuery>();
|
||||
rights.add(right);
|
||||
|
||||
@@ -2902,6 +2944,13 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
SQLSelectQuery leftLeft = leftUnion.getLeft();
|
||||
SQLSelectQuery leftRight = leftUnion.getRight();
|
||||
|
||||
if (leftRight instanceof SQLSelectQueryBlock) {
|
||||
SQLSelectQueryBlock leftRightQueryBlock = (SQLSelectQueryBlock) leftRight;
|
||||
if (leftRightQueryBlock.getOrderBy() != null || leftRightQueryBlock.getLimit() != null) {
|
||||
leftRightQueryBlock.setBracket(true);
|
||||
}
|
||||
}
|
||||
|
||||
if ((!leftUnion.isBracket())
|
||||
&& leftUnion.getOrderBy() == null
|
||||
&& (!leftLeft.isBracket())
|
||||
@@ -2939,24 +2988,30 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
for (;;) {
|
||||
if (left.getClass() == SQLUnionQuery.class) {
|
||||
SQLUnionQuery leftUnion = (SQLUnionQuery) left;
|
||||
SQLSelectQuery leftLeft = leftUnion.getLeft();
|
||||
SQLSelectQuery leftRigt = leftUnion.getRight();
|
||||
if ((!leftUnion.isBracket())
|
||||
&& leftUnion.getRight() instanceof SQLSelectQueryBlock
|
||||
&& leftUnion.getLeft() != null
|
||||
&& leftUnion.getOrderBy() == null)
|
||||
{
|
||||
if (leftLeft.getClass() == SQLUnionQuery.class) {
|
||||
visit((SQLUnionQuery) leftLeft);
|
||||
} else {
|
||||
printQuery(leftLeft);
|
||||
}
|
||||
println();
|
||||
print0(ucase ? leftUnion.getOperator().name : leftUnion.getOperator().name_lcase);
|
||||
println();
|
||||
leftRigt.accept(this);
|
||||
} else {
|
||||
if (leftUnion.getRelations().size() > 2) {
|
||||
visit(leftUnion);
|
||||
} else {
|
||||
SQLSelectQuery leftLeft = leftUnion.getLeft();
|
||||
SQLSelectQuery leftRigt = leftUnion.getRight();
|
||||
if ((!leftUnion.isBracket())
|
||||
&& leftUnion.getRight() instanceof SQLSelectQueryBlock
|
||||
&& leftUnion.getLeft() != null
|
||||
&& leftUnion.getLimit() == null
|
||||
&& leftUnion.getOrderBy() == null) {
|
||||
if (leftLeft.getClass() == SQLUnionQuery.class) {
|
||||
visit((SQLUnionQuery) leftLeft);
|
||||
}
|
||||
else {
|
||||
printQuery(leftLeft);
|
||||
}
|
||||
println();
|
||||
print0(ucase ? leftUnion.getOperator().name : leftUnion.getOperator().name_lcase);
|
||||
println();
|
||||
leftRigt.accept(this);
|
||||
}
|
||||
else {
|
||||
visit(leftUnion);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
left.accept(this);
|
||||
@@ -2974,8 +3029,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
println();
|
||||
|
||||
boolean needParen = false;
|
||||
if (orderBy != null
|
||||
&& (!right.isBracket()) && right instanceof SQLSelectQueryBlock) {
|
||||
if ((!right.isBracket())
|
||||
&& right instanceof SQLSelectQueryBlock) {
|
||||
SQLSelectQueryBlock rightQuery = (SQLSelectQueryBlock) right;
|
||||
if (rightQuery.getOrderBy() != null || rightQuery.getLimit() != null) {
|
||||
needParen = true;
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.alibaba.druid.sql.ast.SQLArgument;
|
||||
import com.alibaba.druid.sql.ast.SQLArrayDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLDataTypeRefExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLDeclareItem;
|
||||
import com.alibaba.druid.sql.ast.SQLKeep;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
@@ -905,4 +906,10 @@ public interface SQLASTVisitor {
|
||||
boolean visit(SQLDumpStatement x);
|
||||
void endVisit(SQLDumpStatement x);
|
||||
|
||||
boolean visit(SQLValuesQuery x);
|
||||
void endVisit(SQLValuesQuery x);
|
||||
|
||||
boolean visit(SQLDataTypeRefExpr x);
|
||||
void endVisit(SQLDataTypeRefExpr x);
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.alibaba.druid.sql.ast.SQLArgument;
|
||||
import com.alibaba.druid.sql.ast.SQLArrayDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLDataTypeRefExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLDeclareItem;
|
||||
import com.alibaba.druid.sql.ast.SQLKeep;
|
||||
import com.alibaba.druid.sql.ast.SQLLimit;
|
||||
@@ -85,6 +86,9 @@ import com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLMergeStatement.MergeInsertClause;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLMergeStatement.MergeUpdateClause;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
protected int features;
|
||||
|
||||
@@ -412,6 +416,58 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLUnionQuery x) {
|
||||
SQLUnionOperator operator = x.getOperator();
|
||||
List<SQLSelectQuery> relations = x.getRelations();
|
||||
if (relations.size() > 2) {
|
||||
for (SQLSelectQuery relation : x.getRelations()) {
|
||||
relation.accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
SQLSelectQuery left = x.getLeft();
|
||||
SQLSelectQuery right = x.getRight();
|
||||
|
||||
boolean bracket = x.isBracket() && !(x.getParent() instanceof SQLUnionQueryTableSource);
|
||||
|
||||
if ((!bracket)
|
||||
&& left instanceof SQLUnionQuery
|
||||
&& ((SQLUnionQuery) left).getOperator() == operator
|
||||
&& !right.isBracket()
|
||||
&& x.getOrderBy() == null) {
|
||||
|
||||
SQLUnionQuery leftUnion = (SQLUnionQuery) left;
|
||||
|
||||
List<SQLSelectQuery> rights = new ArrayList<SQLSelectQuery>();
|
||||
rights.add(right);
|
||||
|
||||
for (; ; ) {
|
||||
SQLSelectQuery leftLeft = leftUnion.getLeft();
|
||||
SQLSelectQuery leftRight = leftUnion.getRight();
|
||||
|
||||
if ((!leftUnion.isBracket())
|
||||
&& leftUnion.getOrderBy() == null
|
||||
&& (!leftLeft.isBracket())
|
||||
&& (!leftRight.isBracket())
|
||||
&& leftLeft instanceof SQLUnionQuery
|
||||
&& ((SQLUnionQuery) leftLeft).getOperator() == operator) {
|
||||
rights.add(leftRight);
|
||||
leftUnion = (SQLUnionQuery) leftLeft;
|
||||
continue;
|
||||
} else {
|
||||
rights.add(leftRight);
|
||||
rights.add(leftLeft);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = rights.size() - 1; i >= 0; i--) {
|
||||
SQLSelectQuery item = rights.get(i);
|
||||
item.accept(this);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2068,6 +2124,26 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLValuesQuery x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLValuesQuery x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLDataTypeRefExpr x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDataTypeRefExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public final boolean isEnabled(VisitorFeature feature) {
|
||||
return VisitorFeature.isEnabled(this.features, feature);
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ import com.alibaba.druid.sql.ast.SQLPartitionValue;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.SQLSubPartition;
|
||||
import com.alibaba.druid.sql.ast.SQLSubPartitionByHash;
|
||||
import com.alibaba.druid.sql.ast.SQLWindow;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLArrayExpr;
|
||||
@@ -1003,7 +1004,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectQueryBlock x) {
|
||||
if (x.getFrom() == null) {
|
||||
SQLTableSource from = x.getFrom();
|
||||
|
||||
if (from == null) {
|
||||
for (SQLSelectItem selectItem : x.getSelectList()) {
|
||||
statExpr(
|
||||
selectItem.getExpr());
|
||||
@@ -1018,7 +1021,6 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
SQLTableSource from = x.getFrom();
|
||||
if (from != null) {
|
||||
from.accept(this); // 提前执行,获得aliasMap
|
||||
}
|
||||
@@ -1039,8 +1041,12 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
for (SQLSelectItem selectItem : x.getSelectList()) {
|
||||
statExpr(
|
||||
selectItem.getExpr());
|
||||
if (selectItem.getClass() == SQLSelectItem.class) {
|
||||
statExpr(
|
||||
selectItem.getExpr());
|
||||
} else {
|
||||
selectItem.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
SQLExpr where = x.getWhere();
|
||||
@@ -1065,6 +1071,13 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
List<SQLWindow> windows = x.getWindows();
|
||||
if (windows != null && windows.size() > 0) {
|
||||
for (SQLWindow window : windows) {
|
||||
window.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
SQLOrderBy orderBy = x.getOrderBy();
|
||||
if (orderBy != null) {
|
||||
this.visit(orderBy);
|
||||
@@ -1075,17 +1088,17 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
statExpr(first);
|
||||
}
|
||||
|
||||
List<SQLExpr> distributeBy = x.getDistributeBy();
|
||||
List<SQLSelectOrderByItem> distributeBy = x.getDistributeBy();
|
||||
if (distributeBy != null) {
|
||||
for (SQLExpr expr : distributeBy) {
|
||||
statExpr(expr);
|
||||
for (SQLSelectOrderByItem item : distributeBy) {
|
||||
statExpr(item.getExpr());
|
||||
}
|
||||
}
|
||||
|
||||
List<SQLSelectOrderByItem> sortBy = x.getSortBy();
|
||||
if (sortBy != null) {
|
||||
for (SQLSelectOrderByItem orderByItem : sortBy) {
|
||||
visit(orderBy);
|
||||
statExpr(orderByItem.getExpr());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user