improved sql parser.

This commit is contained in:
wenshao
2017-06-05 20:27:08 +08:00
parent bea1d70af2
commit d9a0cae840
14 changed files with 431 additions and 9 deletions
@@ -1921,7 +1921,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
value.setTransactionHistogram(this.getTransactionHistogram().toArrayAndReset());
value.setConnectionHoldTimeHistogram(this.getDataSourceStat().getConnectionHoldHistogram().toArrayAndReset());
value.removeAbandoned = this.isRemoveAbandoned();
value.setRemoveAbandoned(this.isRemoveAbandoned());
value.setClobOpenCount(this.getDataSourceStat().getClobOpenCountAndReset());
value.setBlobOpenCount(this.getDataSourceStat().getBlobOpenCountAndReset());
@@ -777,6 +777,10 @@ public class SQLUtils {
}
public static String normalize(String name) {
if (name == null) {
return null;
}
if (name.length() > 2) {
char c0 = name.charAt(0);
char x0 = name.charAt(name.length() - 1);
@@ -859,5 +863,49 @@ public class SQLUtils {
return caseExpr;
}
public static boolean replaceInParent(SQLExpr expr, SQLExpr target) {
if (expr == null) {
return false;
}
SQLObject parent = expr.getParent();
if (parent instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryParent = (SQLBinaryOpExpr) expr.getParent();
if (binaryParent.getLeft() == expr) {
binaryParent.setLeft(target);
return true;
} else if (binaryParent.getRight() == expr) {
binaryParent.setRight(target);
return true;
} else {
return false;
}
}
if (parent instanceof SQLSelectOrderByItem){
SQLSelectOrderByItem orderByItem = (SQLSelectOrderByItem) parent;
if (orderByItem.getExpr() == expr) {
orderByItem.setExpr(target);
return true;
}
return false;
}
if (parent instanceof SQLMethodInvokeExpr) {
SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) parent;
List<SQLExpr> parameters = methodInvokeExpr.getParameters();
for (int i = 0; i < parameters.size(); ++i) {
if (parameters.get(i) == expr) {
parameters.set(i, target);
return true;
}
}
return false;
}
return false;
}
}
@@ -23,6 +23,7 @@ import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.Utils;
public class SQLBinaryOpExpr extends SQLExprImpl implements Serializable {
@@ -147,6 +148,25 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements Serializable {
return true;
}
public boolean equalsIgoreOrder(SQLBinaryOpExpr other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (operator != other.operator) {
return false;
}
return (Utils.equals(this.left, other.left)
&& Utils.equals(this.right, other.right))
|| (Utils.equals(this.left, other.right)
&& Utils.equals(this.right, other.left));
}
public SQLBinaryOpExpr clone() {
return new SQLBinaryOpExpr(left, operator, right, dbType);
}
@@ -155,7 +175,7 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements Serializable {
return SQLUtils.toSQLString(this, getDbType());
}
public static SQLExpr combine(List<SQLExpr> items, SQLBinaryOperator op) {
public static SQLExpr combine(List<? extends SQLExpr> items, SQLBinaryOperator op) {
if (items == null || op == null) {
return null;
}
@@ -183,7 +183,6 @@ public class SQLSelect extends SQLObjectImpl {
public boolean isSimple() {
return withSubQuery == null
&& orderBy == null
&& (hints == null || hints.size() == 0)
&& restriction == null
&& (!forBrowse)
@@ -272,6 +272,9 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
acceptChild(visitor, this.startWith);
acceptChild(visitor, this.connectBy);
acceptChild(visitor, this.groupBy);
acceptChild(visitor, this.orderBy);
acceptChild(visitor, this.waitTime);
acceptChild(visitor, this.limit);
}
visitor.endVisit(this);
}
@@ -386,4 +389,30 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
public void setBracket(boolean bracket) {
this.bracket = bracket;
}
public SQLTableSource findTableSource(String alias) {
return findTableSource(from, alias);
}
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);
}
return null;
}
}
@@ -135,6 +135,9 @@ public class OracleSelectQueryBlock extends SQLSelectQueryBlock {
acceptChild(visitor, this.startWith);
acceptChild(visitor, this.connectBy);
acceptChild(visitor, this.groupBy);
acceptChild(visitor, this.orderBy);
acceptChild(visitor, this.waitTime);
acceptChild(visitor, this.limit);
acceptChild(visitor, this.modelClause);
acceptChild(visitor, this.forUpdateOf);
}
@@ -163,8 +163,10 @@ public class ParameterizedOutputVisitorUtils {
SQLObject parent = x.getParent();
if (left instanceof SQLLiteralExpr && right instanceof SQLLiteralExpr) {
if (x.getOperator() == SQLBinaryOperator.Equality //
|| x.getOperator() == SQLBinaryOperator.NotEqual) {
SQLBinaryOperator op = x.getOperator();
if (op == SQLBinaryOperator.Equality
|| op == SQLBinaryOperator.NotEqual
|| op == SQLBinaryOperator.LessThanOrGreater) {
if((left instanceof SQLIntegerExpr) && (right instanceof SQLIntegerExpr) ) {
if (((SQLIntegerExpr) left).getNumber().intValue() < 100) {
left.putAttribute(ATTR_PARAMS_SKIP, true);
@@ -38,6 +38,7 @@ import com.alibaba.druid.sql.dialect.oracle.ast.OracleSegmentAttributes;
import com.alibaba.druid.sql.ast.statement.SQLDeclareStatement;
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCreatePackageStatement;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.JdbcUtils;
public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements ParameterizedVisitor, PrintableVisitor {
@@ -347,7 +348,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
if (parameters != null
&& parameters.size() > 0
&& x.getOperator() == SQLBinaryOperator.Equality
&& operator == SQLBinaryOperator.Equality
&& x.getRight() instanceof SQLVariantRefExpr
) {
SQLVariantRefExpr right = (SQLVariantRefExpr) x.getRight();
@@ -866,8 +867,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
if (parent instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) parent;
SQLExpr left = binaryOpExpr.getLeft();
SQLBinaryOperator op = binaryOpExpr.getOperator();
if (left instanceof SQLIdentifierExpr
&& binaryOpExpr.getOperator() == SQLBinaryOperator.Equality) {
&& op == SQLBinaryOperator.Equality) {
String name = ((SQLIdentifierExpr) left).getName();
if ("rownum".equals(name)) {
print(1);
@@ -900,7 +902,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
printMethodOwner(owner);
}
printFunctionName(x.getMethodName());
String function = x.getMethodName();
printFunctionName(function);
print('(');
List<SQLExpr> parameters = x.getParameters();
@@ -909,6 +912,18 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
print0(", ");
}
SQLExpr param = x.getParameters().get(i);
if (parameterized) {
if (JdbcUtils.ORACLE.equalsIgnoreCase(dbType)) {
if (size == 2 && i == 1 && param instanceof SQLCharExpr) {
if ("TO_CHAR".equalsIgnoreCase(function)
|| "TO_DATE".equalsIgnoreCase(function)) {
param.putAttribute(ParameterizedOutputVisitorUtils.ATTR_PARAMS_SKIP, true);
}
}
}
}
param.accept(this);
}
@@ -330,5 +330,8 @@ public class Utils {
b[off + 1] = (byte) (val >>> 48);
b[off + 0] = (byte) (val >>> 56);
}
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
}