Search in sources :

Example 11 with SQLBinaryOpExpr

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;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) ArrayList(java.util.ArrayList) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) ArrayList(java.util.ArrayList) List(java.util.List) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 12 with SQLBinaryOpExpr

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());
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 13 with SQLBinaryOpExpr

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;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 14 with SQLBinaryOpExpr

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);
    }
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 15 with SQLBinaryOpExpr

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;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) Item(com.alibaba.druid.sql.ast.expr.SQLCaseExpr.Item) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLNotExpr(com.alibaba.druid.sql.ast.expr.SQLNotExpr) SQLBetweenExpr(com.alibaba.druid.sql.ast.expr.SQLBetweenExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLUnaryExpr(com.alibaba.druid.sql.ast.expr.SQLUnaryExpr) SQLCaseExpr(com.alibaba.druid.sql.ast.expr.SQLCaseExpr)

Aggregations

SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)46 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)30 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)10 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)9 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)8 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)8 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)7 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)7 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)6 SQLObject (com.alibaba.druid.sql.ast.SQLObject)5 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)5 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)5 SQLBinaryOperator (com.alibaba.druid.sql.ast.expr.SQLBinaryOperator)4 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)4 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)4 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 SQLOver (com.alibaba.druid.sql.ast.SQLOver)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3