use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.
the class ExportParameterVisitorUtils method exportParameter.
public static SQLExpr exportParameter(final List<Object> parameters, final SQLExpr param) {
Object value = null;
boolean replace = false;
if (param instanceof SQLCharExpr) {
value = ((SQLCharExpr) param).getText();
replace = true;
}
if (param instanceof SQLBooleanExpr) {
value = ((SQLBooleanExpr) param).getValue();
replace = true;
}
if (param instanceof SQLNumericLiteralExpr) {
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("?");
}
return param;
}
use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.
the class ParameterizedOutputVisitorUtils method mergeEqual.
private static boolean mergeEqual(SQLExpr a, SQLExpr b) {
if (!(a instanceof SQLBinaryOpExpr)) {
return false;
}
if (!(b instanceof SQLBinaryOpExpr)) {
return false;
}
SQLBinaryOpExpr binaryA = (SQLBinaryOpExpr) a;
SQLBinaryOpExpr binaryB = (SQLBinaryOpExpr) b;
if (binaryA.getOperator() != SQLBinaryOperator.Equality) {
return false;
}
if (binaryB.getOperator() != SQLBinaryOperator.Equality) {
return false;
}
if (!(binaryA.getRight() instanceof SQLLiteralExpr || binaryA.getRight() instanceof SQLVariantRefExpr)) {
return false;
}
if (!(binaryB.getRight() instanceof SQLLiteralExpr || binaryB.getRight() instanceof SQLVariantRefExpr)) {
return false;
}
return binaryA.getLeft().toString().equals(binaryB.getLeft().toString());
}
use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.
the class ParameterizedOutputVisitorUtils method merge.
public static SQLBinaryOpExpr merge(ParameterizedVisitor v, SQLBinaryOpExpr x) {
SQLExpr left = x.getLeft();
SQLExpr right = x.getRight();
SQLObject parent = x.getParent();
if (left instanceof SQLLiteralExpr && right instanceof SQLLiteralExpr) {
if (//
x.getOperator() == SQLBinaryOperator.Equality || x.getOperator() == SQLBinaryOperator.NotEqual) {
if ((left instanceof SQLIntegerExpr) && (right instanceof SQLIntegerExpr)) {
if (((SQLIntegerExpr) left).getNumber().intValue() < 100) {
left.putAttribute(ATTR_PARAMS_SKIP, true);
}
if (((SQLIntegerExpr) right).getNumber().intValue() < 100) {
right.putAttribute(ATTR_PARAMS_SKIP, true);
}
} else {
left.putAttribute(ATTR_PARAMS_SKIP, true);
right.putAttribute(ATTR_PARAMS_SKIP, true);
}
}
return x;
}
for (; ; ) {
if (x.getRight() instanceof SQLBinaryOpExpr) {
if (x.getLeft() instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr leftBinaryExpr = (SQLBinaryOpExpr) x.getLeft();
if (leftBinaryExpr.getRight().equals(x.getRight())) {
x = leftBinaryExpr;
v.incrementReplaceCunt();
continue;
}
}
SQLExpr mergedRight = merge(v, (SQLBinaryOpExpr) x.getRight());
if (mergedRight != x.getRight()) {
x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), mergedRight);
v.incrementReplaceCunt();
}
x.setParent(parent);
}
break;
}
if (x.getLeft() instanceof SQLBinaryOpExpr) {
SQLExpr mergedLeft = merge(v, (SQLBinaryOpExpr) x.getLeft());
if (mergedLeft != x.getLeft()) {
SQLBinaryOpExpr tmp = new SQLBinaryOpExpr(mergedLeft, x.getOperator(), x.getRight());
tmp.setParent(parent);
x = tmp;
v.incrementReplaceCunt();
}
}
// ID = ? OR ID = ? => ID = ?
if (x.getOperator() == SQLBinaryOperator.BooleanOr) {
if ((x.getLeft() instanceof SQLBinaryOpExpr) && (x.getRight() instanceof SQLBinaryOpExpr)) {
SQLBinaryOpExpr leftBinary = (SQLBinaryOpExpr) x.getLeft();
SQLBinaryOpExpr rightBinary = (SQLBinaryOpExpr) x.getRight();
if (mergeEqual(leftBinary, rightBinary)) {
v.incrementReplaceCunt();
leftBinary.setParent(x.getParent());
putMergedArribute(leftBinary, rightBinary);
return leftBinary;
}
if (//
isLiteralExpr(leftBinary.getLeft()) && leftBinary.getOperator() == SQLBinaryOperator.BooleanOr) {
if (mergeEqual(leftBinary.getRight(), x.getRight())) {
v.incrementReplaceCunt();
putMergedArribute(leftBinary, rightBinary);
return leftBinary;
}
}
}
}
return x;
}
use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.
the class WallVisitorUtils method checkJoinConditionForMultiTenant.
@Deprecated
public static void checkJoinConditionForMultiTenant(WallVisitor visitor, SQLJoinTableSource join, boolean checkLeft, StatementType statementType) {
String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
if (tenantTablePattern == null || tenantTablePattern.length() == 0) {
return;
}
SQLExpr condition = join.getCondition();
SQLTableSource right = join.getRight();
if (right instanceof SQLExprTableSource) {
SQLExpr tableExpr = ((SQLExprTableSource) right).getExpr();
if (tableExpr instanceof SQLIdentifierExpr) {
String tableName = ((SQLIdentifierExpr) tableExpr).getName();
if (ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
String alias = right.getAlias();
if (alias == null) {
alias = tableName;
}
SQLBinaryOpExpr tenantCondition = createTenantCondition(visitor, alias, statementType, tableName);
if (condition == null) {
condition = tenantCondition;
} else {
condition = new SQLBinaryOpExpr(tenantCondition, SQLBinaryOperator.BooleanAnd, condition);
}
}
}
}
if (condition != join.getCondition()) {
join.setCondition(condition);
visitor.setSqlModified(true);
}
}
use of com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr in project druid by alibaba.
the class WallVisitorUtils method getValue.
public static Object getValue(WallVisitor visitor, SQLExpr x) {
if (x != null && x.getAttributes().containsKey(EVAL_VALUE)) {
return getValueFromAttributes(visitor, x);
}
if (x instanceof SQLBinaryOpExpr) {
return getValue(visitor, (SQLBinaryOpExpr) x);
}
if (x instanceof SQLBooleanExpr) {
return ((SQLBooleanExpr) x).getValue();
}
if (x instanceof SQLNumericLiteralExpr) {
return ((SQLNumericLiteralExpr) x).getNumber();
}
if (x instanceof SQLCharExpr) {
return ((SQLCharExpr) x).getText();
}
if (x instanceof SQLNCharExpr) {
return ((SQLNCharExpr) x).getText();
}
if (x instanceof SQLNotExpr) {
Object result = getValue(visitor, ((SQLNotExpr) x).getExpr());
if (result instanceof Boolean) {
return !((Boolean) result).booleanValue();
}
}
if (x instanceof SQLQueryExpr) {
if (isSimpleCountTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
return Integer.valueOf(1);
}
if (isSimpleCaseTableSource(visitor, ((SQLQueryExpr) x).getSubQuery())) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) ((SQLQueryExpr) x).getSubQuery().getQuery();
SQLCaseExpr caseExpr = (SQLCaseExpr) queryBlock.getSelectList().get(0).getExpr();
Object result = getValue(caseExpr);
if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
boolean leftIsName = false;
if (x.getParent() instanceof SQLBinaryOpExpr) {
SQLExpr left = ((SQLBinaryOpExpr) x.getParent()).getLeft();
if (left instanceof SQLName) {
leftIsName = true;
}
}
if (!leftIsName && result != null) {
addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
}
}
return result;
}
}
String dbType = null;
if (visitor != null) {
dbType = visitor.getDbType();
}
if (//
x instanceof SQLMethodInvokeExpr || //
x instanceof SQLBetweenExpr || //
x instanceof SQLInListExpr || //
x instanceof SQLUnaryExpr) {
return eval(visitor, dbType, x, Collections.emptyList());
}
if (x instanceof SQLCaseExpr) {
if (visitor != null && !visitor.getConfig().isCaseConditionConstAllow()) {
SQLCaseExpr caseExpr = (SQLCaseExpr) x;
boolean leftIsName = false;
if (caseExpr.getParent() instanceof SQLBinaryOpExpr) {
SQLExpr left = ((SQLBinaryOpExpr) caseExpr.getParent()).getLeft();
if (left instanceof SQLName) {
leftIsName = true;
}
}
if (!leftIsName && caseExpr.getValueExpr() == null && caseExpr.getItems().size() > 0) {
SQLCaseExpr.Item item = caseExpr.getItems().get(0);
Object conditionVal = getValue(visitor, item.getConditionExpr());
Object itemVal = getValue(visitor, item.getValueExpr());
if (conditionVal instanceof Boolean && itemVal != null) {
addViolation(visitor, ErrorCode.CONST_CASE_CONDITION, "const case condition", caseExpr);
}
}
}
return eval(visitor, dbType, x, Collections.emptyList());
}
return null;
}
Aggregations