Search in sources :

Example 1 with StatementType

use of com.alibaba.druid.wall.WallConfig.TenantCallBack.StatementType in project druid by alibaba.

the class WallVisitorUtils method checkConditionForMultiTenant.

@Deprecated
public static void checkConditionForMultiTenant(WallVisitor visitor, SQLExpr x, SQLObject parent) {
    String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
    if (tenantTablePattern == null || tenantTablePattern.length() == 0) {
        return;
    }
    if (parent == null) {
        throw new IllegalStateException("parent is null");
    }
    String alias = null;
    SQLTableSource tableSource;
    StatementType statementType = null;
    if (parent instanceof SQLDeleteStatement) {
        tableSource = ((SQLDeleteStatement) parent).getTableSource();
        statementType = StatementType.DELETE;
    } else if (parent instanceof SQLUpdateStatement) {
        tableSource = ((SQLUpdateStatement) parent).getTableSource();
        statementType = StatementType.UPDATE;
    } else if (parent instanceof SQLSelectQueryBlock) {
        tableSource = ((SQLSelectQueryBlock) parent).getFrom();
        statementType = StatementType.SELECT;
    } else {
        throw new IllegalStateException("not support parent : " + parent.getClass());
    }
    String matchTableName = null;
    if (tableSource instanceof SQLExprTableSource) {
        SQLExpr tableExpr = ((SQLExprTableSource) tableSource).getExpr();
        if (tableExpr instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) tableExpr).getName();
            if (ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
                matchTableName = tableName;
                alias = tableSource.getAlias();
            }
        }
    } else if (tableSource instanceof SQLJoinTableSource) {
        SQLJoinTableSource join = (SQLJoinTableSource) tableSource;
        if (join.getLeft() instanceof SQLExprTableSource) {
            SQLExpr tableExpr = ((SQLExprTableSource) join.getLeft()).getExpr();
            if (tableExpr instanceof SQLIdentifierExpr) {
                String tableName = ((SQLIdentifierExpr) tableExpr).getName();
                if (ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
                    matchTableName = tableName;
                    alias = join.getLeft().getAlias();
                }
            }
            checkJoinConditionForMultiTenant(visitor, join, false, statementType);
        } else {
            checkJoinConditionForMultiTenant(visitor, join, true, statementType);
        }
    }
    if (matchTableName == null) {
        return;
    }
    SQLBinaryOpExpr tenantCondition = createTenantCondition(visitor, alias, statementType, matchTableName);
    SQLExpr condition;
    if (x == null) {
        condition = tenantCondition;
    } else {
        condition = new SQLBinaryOpExpr(tenantCondition, SQLBinaryOperator.BooleanAnd, x);
    }
    if (parent instanceof SQLDeleteStatement) {
        SQLDeleteStatement deleteStmt = (SQLDeleteStatement) parent;
        deleteStmt.setWhere(condition);
        visitor.setSqlModified(true);
    } else if (parent instanceof SQLUpdateStatement) {
        SQLUpdateStatement updateStmt = (SQLUpdateStatement) parent;
        updateStmt.setWhere(condition);
        visitor.setSqlModified(true);
    } else if (parent instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) parent;
        queryBlock.setWhere(condition);
        visitor.setSqlModified(true);
    }
}
Also used : StatementType(com.alibaba.druid.wall.WallConfig.TenantCallBack.StatementType) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 StatementType (com.alibaba.druid.wall.WallConfig.TenantCallBack.StatementType)1