bug fixed for sql SchemaStatVisitor

This commit is contained in:
wenshao
2017-02-17 00:03:38 +08:00
parent 29b4b3ea29
commit cd70ef8449
17 changed files with 593 additions and 56 deletions
@@ -15,9 +15,11 @@
*/
package com.alibaba.druid.sql.visitor;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.expr.SQLBetweenExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBooleanExpr;
@@ -86,21 +88,46 @@ public final class ExportParameterVisitorUtils {
}
public static SQLExpr exportParameter(final List<Object> parameters, final SQLExpr param) {
Object value = null;
boolean replace = false;
if (param instanceof SQLCharExpr) {
Object value = ((SQLCharExpr) param).getText();
parameters.add(value);
return new SQLVariantRefExpr("?");
value = ((SQLCharExpr) param).getText();
replace = true;
}
if (param instanceof SQLBooleanExpr) {
Object value = ((SQLBooleanExpr) param).getValue();
parameters.add(value);
return new SQLVariantRefExpr("?");
value = ((SQLBooleanExpr) param).getValue();
replace = true;
}
if (param instanceof SQLNumericLiteralExpr) {
Object value = ((SQLNumericLiteralExpr) param).getNumber();
value = ((SQLNumericLiteralExpr) param).getNumber();
replace = true;
}
if (replace) {
SQLObject parent = param.getParent();
if (parent != null) {
List<SQLObject> mergedList = (List<SQLObject>) parent.getAttribute(ParameterizedOutputVisitorUtils.ATTR_MERGED);
if (mergedList != null) {
List<Object> mergedListParams = new ArrayList<Object>(mergedList.size() + 1);
for (int i = 0; i < mergedList.size(); ++i) {
SQLObject item = mergedList.get(i);
if (item instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpItem = (SQLBinaryOpExpr) item;
exportParameter(mergedListParams, binaryOpItem.getRight());
}
}
if (mergedListParams.size() > 0) {
mergedListParams.add(0, value);
value = mergedListParams;
}
}
}
parameters.add(value);
return new SQLVariantRefExpr("?");
}
@@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.visitor;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.ast.SQLDataType;
@@ -23,10 +24,8 @@ import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
@@ -154,6 +153,16 @@ public class ParameterizedOutputVisitorUtils {
return false;
}
public final static String ATTR_MERGED = "parameterized.mergedList";
static void putMergedArribute(SQLObject object, SQLObject item) {
List<SQLObject> mergedList = (List<SQLObject>) object.getAttribute(ATTR_MERGED);
if (mergedList == null) {
mergedList = new ArrayList<SQLObject>();
object.putAttribute(ATTR_MERGED, mergedList);
}
mergedList.add(item);
}
public static SQLBinaryOpExpr merge(ParameterizedVisitor v, SQLBinaryOpExpr x) {
SQLExpr left = x.getLeft();
SQLExpr right = x.getRight();
@@ -201,10 +210,11 @@ public class ParameterizedOutputVisitorUtils {
if (x.getLeft() instanceof SQLBinaryOpExpr) {
SQLExpr mergedLeft = merge(v, (SQLBinaryOpExpr) x.getLeft());
if (mergedLeft != x.getLeft()) {
x = new SQLBinaryOpExpr(mergedLeft, x.getOperator(), x.getRight());
SQLBinaryOpExpr tmp = new SQLBinaryOpExpr(mergedLeft, x.getOperator(), x.getRight());
tmp.setParent(parent);
x = tmp;
v.incrementReplaceCunt();
}
x.setParent(parent);
}
// ID = ? OR ID = ? => ID = ?
@@ -215,6 +225,8 @@ public class ParameterizedOutputVisitorUtils {
if (mergeEqual(leftBinary, rightBinary)) {
v.incrementReplaceCunt();
leftBinary.setParent(x.getParent());
putMergedArribute(leftBinary, rightBinary);
return leftBinary;
}
@@ -222,6 +234,7 @@ public class ParameterizedOutputVisitorUtils {
&& leftBinary.getOperator() == SQLBinaryOperator.BooleanOr) {
if (mergeEqual(leftBinary.getRight(), x.getRight())) {
v.incrementReplaceCunt();
putMergedArribute(leftBinary, rightBinary);
return leftBinary;
}
}
@@ -318,6 +318,25 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
x = ParameterizedOutputVisitorUtils.merge(this, x);
}
if (parameters != null
&& parameters.size() > 0
&& x.getOperator() == SQLBinaryOperator.Equality
&& x.getRight() instanceof SQLVariantRefExpr
) {
SQLVariantRefExpr right = (SQLVariantRefExpr) x.getRight();
int index = right.getIndex();
if (index >= 0 && index < parameters.size()) {
Object param = parameters.get(index);
if (param instanceof Collection) {
x.getLeft().accept(this);
print0(" IN (");
right.accept(this);
print(')');
return false;
}
}
}
SQLObject parent = x.getParent();
boolean isRoot = parent instanceof SQLSelectQueryBlock;
boolean relational = x.getOperator() == SQLBinaryOperator.BooleanAnd
@@ -1259,7 +1278,23 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
Object param = parameters.get(index);
if (param instanceof Collection && x.getParent() instanceof SQLInListExpr) {
SQLObject parent = x.getParent();
boolean in;
if (parent instanceof SQLInListExpr) {
in = true;
} else if (parent instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) parent;
if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality) {
in = true;
} else {
in = false;
}
} else {
in = false;
}
if (in && param instanceof Collection) {
boolean first = true;
for (Object item : (Collection) param) {
if (!first) {
@@ -62,6 +62,7 @@ import com.alibaba.druid.stat.TableStat.Condition;
import com.alibaba.druid.stat.TableStat.Mode;
import com.alibaba.druid.stat.TableStat.Relationship;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.StringUtils;
public class SchemaStatVisitor extends SQLASTVisitorAdapter {
@@ -518,6 +519,10 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
for (SQLExpr item : valueExprs) {
Column valueColumn = getColumn(item);
if (valueColumn != null) {
continue;
}
Object value = SQLEvalVisitorUtils.eval(getDbType(), item, parameters, false);
condition.getValues().add(value);
}
@@ -541,6 +546,10 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
}
if (expr instanceof SQLCastExpr) {
expr = ((SQLCastExpr) expr).getExpr();
}
if (expr instanceof SQLPropertyExpr) {
SQLExpr owner = ((SQLPropertyExpr) expr).getOwner();
String column = ((SQLPropertyExpr) expr).getName();
@@ -854,6 +863,8 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
if (selectList != null) {
boolean allColumn = false;
String allColumnOwner = null;
for (SQLSelectItem item : selectList) {
if (!item.getClass().equals(SQLSelectItem.class)) {
continue;
@@ -885,7 +896,52 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
}
}
if (itemExpr instanceof SQLAllColumnExpr) {
allColumn = true;
} else if (itemExpr instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) itemExpr;
if (propertyExpr.getName().equals("*")) {
SQLExpr owner = propertyExpr.getOwner();
if (owner instanceof SQLIdentifierExpr) {
allColumnOwner = ((SQLIdentifierExpr) owner).getName();
allColumn = true;
}
}
}
}
if (allColumn) {
SQLTableSource from = queryBlock.getFrom();
String tableName = getTable(from, allColumnOwner);
if (tableName != null) {
return new Column(tableName, alias);
}
}
}
return null;
}
private String getTable(SQLTableSource from, String tableAlias) {
if (from instanceof SQLExprTableSource) {
boolean aliasEq = StringUtils.equals(from.getAlias(), tableAlias);
SQLExpr tableSourceExpr = ((SQLExprTableSource) from).getExpr();
if (tableSourceExpr instanceof SQLName) {
String tableName = ((SQLName) tableSourceExpr).toString();
if (tableAlias == null || aliasEq) {
return tableName;
}
}
} else if (from instanceof SQLJoinTableSource) {
SQLJoinTableSource joinTableSource = (SQLJoinTableSource) from;
String leftMatchTableName = getTable(joinTableSource.getLeft(), tableAlias);
if (leftMatchTableName != null) {
return leftMatchTableName;
}
return getTable(joinTableSource.getRight(), tableAlias);
}
return null;