mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor sql parser.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user