refactor sql parser.

This commit is contained in:
wenshao
2017-06-02 15:47:57 +08:00
parent 82d691365a
commit 4ea81bca69
78 changed files with 965 additions and 653 deletions
@@ -42,4 +42,8 @@ public class SQLCommentHint extends SQLObjectImpl implements SQLHint {
visitor.visit(this);
visitor.endVisit(this);
}
public SQLCommentHint clone() {
return new SQLCommentHint(text);
}
}
@@ -50,6 +50,19 @@ public class SQLKeep extends SQLObjectImpl {
this.orderBy = orderBy;
}
public SQLKeep clone() {
SQLKeep x = new SQLKeep();
x.denseRank = denseRank;
if (orderBy != null) {
x.setOrderBy(orderBy.clone());
}
return x;
}
public static enum DenseRank {
FIRST, //
LAST
@@ -70,4 +70,8 @@ public class SQLLimit extends SQLObjectImpl {
visitor.endVisit(this);
}
public SQLLimit clone() {
throw new UnsupportedOperationException();
}
}
@@ -52,4 +52,6 @@ public interface SQLObject {
boolean hasAfterComment();
void output(StringBuffer buf);
SQLObject clone();
}
@@ -20,6 +20,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.sql.dialect.oracle.ast.OracleSQLObject;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public abstract class SQLObjectImpl implements SQLObject {
@@ -206,4 +207,8 @@ public abstract class SQLObjectImpl implements SQLObject {
return !comments.isEmpty();
}
public SQLObject clone() {
throw new UnsupportedOperationException();
}
}
@@ -96,4 +96,8 @@ public class SQLOrderBy extends SQLObjectImpl {
protected SQLSelectOrderByItem createItem() {
return new SQLSelectOrderByItem();
}
public SQLOrderBy clone() {
throw new UnsupportedOperationException();
}
}
@@ -208,8 +208,45 @@ public class SQLOver extends SQLObjectImpl {
return result;
}
public SQLExpr clone() {
throw new UnsupportedOperationException();
public void cloneTo(SQLOver x) {
for (SQLExpr item : partitionBy) {
SQLExpr item1 = item.clone();
item1.setParent(x);
x.partitionBy.add(item);
}
if (orderBy != null) {
x.setOrderBy(orderBy.clone());
}
if (of != null) {
x.setOf(of.clone());
}
if (windowing != null) {
x.setWindowing(windowing.clone());
}
x.windowingType = windowingType;
x.windowingPreceding = windowingPreceding;
x.windowingFollowing = windowingFollowing;
if (windowingBetweenBegin != null) {
x.setWindowingBetweenBegin(windowingBetweenBegin.clone());
}
x.windowingBetweenBeginPreceding = windowingBetweenBeginPreceding;
x.windowingBetweenBeginFollowing = windowingBetweenBeginFollowing;
if (windowingBetweenEnd != null) {
x.setWindowingBetweenEnd(windowingBetweenEnd.clone());
}
x.windowingBetweenEndPreceding = windowingBetweenEndPreceding;
x.windowingBetweenEndFollowing = windowingBetweenEndFollowing;
}
public SQLOver clone() {
SQLOver x = new SQLOver();
cloneTo(x);
return x;
}
public static enum WindowingType {
@@ -176,4 +176,29 @@ public class SQLAggregateExpr extends SQLExprImpl implements Serializable {
return true;
}
public SQLAggregateExpr clone() {
SQLAggregateExpr x = new SQLAggregateExpr(methodName);
x.option = option;
for (SQLExpr arg : arguments) {
x.addArgument(arg.clone());
}
if (keep != null) {
x.setKeep(keep.clone());
}
if (over != null) {
x.setOver(over.clone());
}
if (withinGroup != null) {
x.setWithinGroup(withinGroup.clone());
}
x.ignoreNulls = ignoreNulls;
return x;
}
}
@@ -144,4 +144,24 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements Serializable {
result = 31 * result + (from != null ? from.hashCode() : 0);
return result;
}
public SQLMethodInvokeExpr clone() {
SQLMethodInvokeExpr x = new SQLMethodInvokeExpr();
x.methodName = methodName;
if (owner != null) {
x.setOwner(owner.clone());
}
for (SQLExpr param : parameters) {
x.addParameter(param.clone());
}
if (from != null) {
x.setFrom(from.clone());
}
return x;
}
}
@@ -26,6 +26,10 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
private SQLExpr owner;
private String name;
public SQLPropertyExpr(String owner, String name){
this(new SQLIdentifierExpr(owner), name);
}
public SQLPropertyExpr(SQLExpr owner, String name){
setOwner(owner);
this.name = name;
@@ -107,4 +107,43 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
result = 31 * result + (partitions != null ? partitions.hashCode() : 0);
return result;
}
public String computeAlias() {
String alias = this.getAlias();
if (alias == null) {
if (expr instanceof SQLName) {
alias =((SQLName) expr).getSimpleName();
}
}
if (alias.length() > 2) {
char c0 = alias.charAt(0);
char x0 = alias.charAt(alias.length() - 1);
if ((c0 == '"' && x0 == '"') || (c0 == '`' && x0 == '`')) {
alias = alias.substring(1, alias.length() - 1);
}
}
return alias;
}
public SQLExprTableSource clone() {
SQLExprTableSource x = new SQLExprTableSource();
cloneTo(x);
return x;
}
public void cloneTo(SQLExprTableSource x) {
if (expr != null) {
x.expr = expr.clone();
}
if (partitions != null) {
for (SQLName p : partitions) {
SQLName p1 = p.clone();
x.addPartition(p1);
}
}
}
}
@@ -40,6 +40,13 @@ public class SQLJoinTableSource extends SQLTableSourceImpl {
}
public SQLJoinTableSource(SQLTableSource left, JoinType joinType, SQLTableSource right, SQLExpr condition){
this.setLeft(left);
this.setJoinType(joinType);
this.setRight(right);
this.setCondition(condition);
}
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.left);
@@ -19,11 +19,7 @@ import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
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.sql.ast.SQLOrderBy;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLSelect extends SQLObjectImpl {
@@ -32,7 +28,16 @@ public class SQLSelect extends SQLObjectImpl {
protected SQLSelectQuery query;
protected SQLOrderBy orderBy;
protected List<SQLHint> hints;
protected List<SQLHint> hints;
protected SQLObject restriction;
protected boolean forBrowse;
protected List<String> forXmlOptions = new ArrayList<String>(4);
protected SQLExpr xmlPath;
protected SQLExpr rowCount;
protected SQLExpr offset;
public SQLSelect(){
@@ -86,8 +91,11 @@ public class SQLSelect extends SQLObjectImpl {
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.query);
acceptChild(visitor, this.restriction);
acceptChild(visitor, this.orderBy);
acceptChild(visitor, this.hints);
acceptChild(visitor, this.offset);
acceptChild(visitor, this.rowCount);
}
visitor.endVisit(this);
@@ -135,13 +143,85 @@ public class SQLSelect extends SQLObjectImpl {
}
public SQLSelect clone() {
SQLSelect clone = new SQLSelect();
SQLSelect x = new SQLSelect();
clone.withSubQuery = this.withSubQuery;
clone.query = this.query;
clone.orderBy = this.orderBy;
clone.hints = this.hints;
x.withSubQuery = this.withSubQuery;
x.query = this.query;
if (orderBy != null) {
x.setOrderBy(this.orderBy);
}
if (restriction != null) {
x.setRestriction(restriction.clone());
}
return clone;
if (this.hints != null) {
for (SQLHint hint : this.hints) {
x.hints.add(hint);
}
}
return x;
}
public SQLObject getRestriction() {
return this.restriction;
}
public void setRestriction(SQLObject restriction) {
if (restriction != null) {
restriction.setParent(this);
}
this.restriction = restriction;
}
public boolean isForBrowse() {
return forBrowse;
}
public void setForBrowse(boolean forBrowse) {
this.forBrowse = forBrowse;
}
public List<String> getForXmlOptions() {
return forXmlOptions;
}
public void setForXmlOptions(List<String> forXmlOptions) {
this.forXmlOptions = forXmlOptions;
}
public SQLExpr getRowCount() {
return rowCount;
}
public void setRowCount(SQLExpr rowCount) {
if (rowCount != null) {
rowCount.setParent(this);
}
this.rowCount = rowCount;
}
public SQLExpr getOffset() {
return offset;
}
public void setOffset(SQLExpr offset) {
if (offset != null) {
offset.setParent(this);
}
this.offset = offset;
}
public SQLExpr getXmlPath() {
return xmlPath;
}
public void setXmlPath(SQLExpr xmlPath) {
if (xmlPath != null) {
xmlPath.setParent(this);
}
this.xmlPath = xmlPath;
}
}
@@ -81,4 +81,8 @@ public class SQLSelectGroupByClause extends SQLObjectImpl {
this.items.add(sqlExpr);
}
}
public SQLSelectGroupByClause clone() {
throw new UnsupportedOperationException();
}
}
@@ -144,4 +144,13 @@ public class SQLSelectItem extends SQLObjectImpl {
this.connectByRoot = connectByRoot;
}
public SQLSelectItem clone() {
SQLSelectItem x = new SQLSelectItem();
x.alias = alias;
if (expr != null) {
x.expr = expr.clone();
}
x.connectByRoot = connectByRoot;
return x;
}
}
@@ -130,6 +130,14 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
item.setParent(this);
}
public void addSelectItem(SQLExpr expr) {
this.addSelectItem(new SQLSelectItem(expr));
}
public void addSelectItem(SQLExpr expr, String alias) {
this.addSelectItem(new SQLSelectItem(expr, alias));
}
public SQLTableSource getFrom() {
return this.from;
}
@@ -308,4 +316,64 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return true;
}
public SQLSelectQueryBlock clone() {
SQLSelectQueryBlock x = new SQLSelectQueryBlock();
clone(x);
return x;
}
public void clone(SQLSelectQueryBlock x) {
x.distionOption = distionOption;
for (SQLSelectItem item : this.selectList) {
x.addSelectItem(item.clone());
}
if (from != null) {
x.setFrom(from.clone());
}
if (into != null) {
x.setInto(into.clone());
}
if (where != null) {
x.setWhere(where.clone());
}
if (startWith != null) {
x.setStartWith(startWith.clone());
}
if (connectBy != null) {
x.setConnectBy(connectBy.clone());
}
x.prior = prior;
x.noCycle = noCycle;
if (orderBySiblings != null) {
x.setOrderBySiblings(orderBySiblings.clone());
}
if (groupBy != null) {
x.setGroupBy(groupBy.clone());
}
if (orderBy != null) {
x.setOrderBy(orderBy.clone());
}
x.parenthesized = parenthesized;
x.forUpdate = forUpdate;
x.noWait = noWait;
if (waitTime != null) {
x.setWaitTime(waitTime.clone());
}
if (limit != null) {
x.setLimit(limit.clone());
}
}
}
@@ -38,6 +38,10 @@ public class SQLSubqueryTableSource extends SQLTableSourceImpl {
this.setSelect(select);
}
public SQLSubqueryTableSource(SQLSelectQuery query){
this(new SQLSelect(query));
}
public SQLSelect getSelect() {
return this.select;
}
@@ -62,4 +66,12 @@ public class SQLSubqueryTableSource extends SQLTableSourceImpl {
this.select.output(buf);
buf.append(")");
}
public SQLSubqueryTableSource clone() {
SQLSubqueryTableSource x = new SQLSubqueryTableSource();
if (select != null) {
x.select = select.clone();
}
return x;
}
}
@@ -27,4 +27,6 @@ public interface SQLTableSource extends SQLObject {
void setAlias(String alias);
List<SQLHint> getHints();
SQLTableSource clone();
}
@@ -62,4 +62,9 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
public void setHints(List<SQLHint> hints) {
this.hints = hints;
}
public SQLTableSource clone() {
throw new UnsupportedOperationException();
}
}