improved sql parser.

This commit is contained in:
wenshao
2017-08-18 10:16:53 +08:00
parent 97ed996c05
commit 3b0b090989
28 changed files with 789 additions and 478 deletions
@@ -15,10 +15,7 @@
*/
package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLReplaceable;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -28,6 +25,8 @@ public class SQLDeleteStatement extends SQLStatementImpl implements SQLReplaceab
protected SQLExpr where;
protected SQLTableSource from;
protected boolean only = false;
public SQLDeleteStatement(){
}
@@ -36,6 +35,33 @@ public class SQLDeleteStatement extends SQLStatementImpl implements SQLReplaceab
super (dbType);
}
protected void cloneTo(SQLDeleteStatement x) {
if (headHints != null) {
for (SQLCommentHint h : headHints) {
SQLCommentHint h2 = h.clone();
h2.setParent(x);
x.headHints.add(h2);
}
}
if (tableSource != null) {
x.setTableSource(tableSource.clone());
}
if (where != null) {
x.setWhere(where.clone());
}
if (from != null) {
x.setFrom(from.clone());
}
x.only = only;
}
public SQLDeleteStatement clone() {
SQLDeleteStatement x = new SQLDeleteStatement();
cloneTo(x);
return x;
}
public SQLTableSource getTableSource() {
return tableSource;
}
@@ -131,4 +157,12 @@ public class SQLDeleteStatement extends SQLStatementImpl implements SQLReplaceab
}
return false;
}
public boolean isOnly() {
return only;
}
public void setOnly(boolean only) {
this.only = only;
}
}
@@ -239,4 +239,20 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
return null;
}
public SQLTableSource findTableSource(String alias) {
if (alias == null) {
return null;
}
if (SQLUtils.nameEquals(alias, computeAlias())) {
return this;
}
if (containsAlias(alias)) {
return this;
}
return null;
}
}
@@ -327,17 +327,58 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
}
}
public boolean contains(SQLTableSource tableSource, SQLExpr conditionn) {
public boolean contains(SQLTableSource tableSource, SQLExpr condition) {
if (right.equals(tableSource)) {
if (this.condition == conditionn) {
if (this.condition == condition) {
return true;
}
return this.condition != null && this.condition.equals(conditionn);
return this.condition != null && this.condition.equals(condition);
}
if (left instanceof SQLJoinTableSource) {
return ((SQLJoinTableSource) left).contains(tableSource, conditionn);
SQLJoinTableSource joinLeft = (SQLJoinTableSource) left;
if (tableSource instanceof SQLJoinTableSource) {
SQLJoinTableSource join = (SQLJoinTableSource) tableSource;
if (join.right.equals(right) && this.condition.equals(condition) && joinLeft.right.equals(join.left)) {
return true;
}
}
return joinLeft.contains(tableSource, condition);
}
return false;
}
public boolean contains(SQLTableSource tableSource, SQLExpr condition, JoinType joinType) {
if (right.equals(tableSource)) {
if (this.condition == condition) {
return true;
}
return this.condition != null && this.condition.equals(condition) && this.joinType == joinType;
}
if (left instanceof SQLJoinTableSource) {
SQLJoinTableSource joinLeft = (SQLJoinTableSource) left;
if (tableSource instanceof SQLJoinTableSource) {
SQLJoinTableSource join = (SQLJoinTableSource) tableSource;
if (join.right.equals(right)
&& this.condition != null && this.condition.equals(join.condition)
&& joinLeft.right.equals(join.left)
&& this.joinType == join.joinType
&& joinLeft.condition != null && joinLeft.condition.equals(condition)
&& joinLeft.joinType == joinType) {
return true;
}
}
return joinLeft.contains(tableSource, condition, joinType);
}
return false;
@@ -476,4 +517,20 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
return joined;
}
public SQLTableSource findTableSource(String alias) {
if (alias == null) {
return null;
}
if (SQLUtils.nameEquals(alias, computeAlias())) {
return this;
}
SQLTableSource result = left.findTableSource(alias);
if (result != null) {
return result;
}
return right.findTableSource(alias);
}
}
@@ -93,6 +93,9 @@ public class SQLSelect extends SQLObjectImpl {
}
public void setOrderBy(SQLOrderBy orderBy) {
if (orderBy != null) {
orderBy.setParent(this);
}
this.orderBy = orderBy;
}
@@ -20,6 +20,7 @@ import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.ast.SQLReplaceable;
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -178,6 +179,10 @@ public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
return true;
}
if (expr instanceof SQLAllColumnExpr) {
return true;
}
if (expr instanceof SQLIdentifierExpr) {
String ident = ((SQLIdentifierExpr) expr).getName();
return alias_normalized.equalsIgnoreCase(SQLUtils.normalize(ident));
@@ -185,6 +190,10 @@ public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
if (expr instanceof SQLPropertyExpr) {
String ident = ((SQLPropertyExpr) expr).getName();
if ("*".equals(ident)) {
return true;
}
return alias_normalized.equalsIgnoreCase(SQLUtils.normalize(ident));
}
@@ -435,7 +435,10 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
}
public SQLTableSource findTableSource(String alias) {
return findTableSource(from, alias);
if (from == null) {
return null;
}
return from.findTableSource(alias);
}
public SQLTableSource findTableSourceWithColumn(String column) {
@@ -445,32 +448,6 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return from.findTableSourceWithColumn(column);
}
private static SQLTableSource findTableSource(SQLTableSource from, String alias) {
if (from == null || alias == null) {
return null;
}
if (alias.equalsIgnoreCase(from.computeAlias())) {
return from;
}
if (from instanceof SQLJoinTableSource) {
SQLJoinTableSource join = (SQLJoinTableSource) from;
SQLTableSource result = findTableSource(join.getLeft(), alias);
if (result != null) {
return result;
}
return findTableSource(join.getRight(), alias);
}
if (from instanceof SQLExprTableSource && from.containsAlias(alias)) {
return from;
}
return null;
}
@Override
public boolean replace(SQLExpr expr, SQLExpr target) {
if (where == expr) {
@@ -25,8 +25,6 @@ public class SQLSelectStatement extends SQLStatementImpl {
protected SQLSelect select;
private List<SQLCommentHint> headHints;
public SQLSelectStatement(){
}
@@ -66,14 +64,6 @@ public class SQLSelectStatement extends SQLStatementImpl {
visitor.endVisit(this);
}
public List<SQLCommentHint> getHeadHintsDirect() {
return headHints;
}
public void setHeadHints(List<SQLCommentHint> headHints) {
this.headHints = headHints;
}
public SQLSelectStatement clone() {
SQLSelectStatement x = new SQLSelectStatement();
if (select != null) {
@@ -77,4 +77,21 @@ public class SQLSubqueryTableSource extends SQLTableSourceImpl {
}
return x;
}
public SQLTableSource findTableSourceWithColumn(String columnName) {
if (select == null) {
return null;
}
SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock();
if (queryBlock == null) {
return null;
}
if (queryBlock.findSelectItem(columnName) != null) {
return this;
}
return null;
}
}
@@ -39,4 +39,6 @@ public interface SQLTableSource extends SQLObject {
SQLColumnDefinition findColumn(String columnName);
SQLTableSource findTableSourceWithColumn(String columnName);
SQLTableSource findTableSource(String alias);
}
@@ -102,4 +102,16 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
public SQLTableSource findTableSourceWithColumn(String columnName) {
return null;
}
public SQLTableSource findTableSource(String alias) {
if (alias == null) {
return null;
}
if (SQLUtils.nameEquals(alias, computeAlias())) {
return this;
}
return null;
}
}