Search in sources :

Example 6 with TenantCallBack

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

the class WallVisitorUtils method generateTenantValue.

private static SQLExpr generateTenantValue(WallVisitor visitor, String alias, StatementType statementType, String tableName) {
    SQLExpr value;
    TenantCallBack callBack = visitor.getConfig().getTenantCallBack();
    if (callBack != null) {
        WallProvider.setTenantValue(callBack.getTenantValue(statementType, tableName));
    }
    Object tenantValue = WallProvider.getTenantValue();
    if (tenantValue instanceof Number) {
        value = new SQLNumberExpr((Number) tenantValue);
    } else if (tenantValue instanceof String) {
        value = new SQLCharExpr((String) tenantValue);
    } else {
        throw new IllegalStateException("tenant value not support type " + tenantValue);
    }
    return value;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) TenantCallBack(com.alibaba.druid.wall.WallConfig.TenantCallBack) SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 7 with TenantCallBack

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

the class WallVisitorUtils method checkJoinSelectForMultiTenant.

private static void checkJoinSelectForMultiTenant(WallVisitor visitor, SQLJoinTableSource join, SQLSelectQueryBlock x) {
    TenantCallBack tenantCallBack = visitor.getConfig().getTenantCallBack();
    String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
    if (tenantCallBack == null && (tenantTablePattern == null || tenantTablePattern.length() == 0)) {
        return;
    }
    SQLTableSource right = join.getRight();
    if (right instanceof SQLExprTableSource) {
        SQLExpr tableExpr = ((SQLExprTableSource) right).getExpr();
        if (tableExpr instanceof SQLIdentifierExpr) {
            String tableName = ((SQLIdentifierExpr) tableExpr).getName();
            String alias = null;
            String tenantColumn = null;
            if (tenantCallBack != null) {
                tenantColumn = tenantCallBack.getTenantColumn(StatementType.SELECT, tableName);
            }
            if (StringUtils.isEmpty(tenantColumn) && ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
                tenantColumn = visitor.getConfig().getTenantColumn();
            }
            if (!StringUtils.isEmpty(tenantColumn)) {
                alias = right.getAlias();
                if (alias == null) {
                    alias = tableName;
                }
                SQLExpr item = null;
                if (alias != null) {
                    item = new SQLPropertyExpr(new SQLIdentifierExpr(alias), tenantColumn);
                } else {
                    item = new SQLIdentifierExpr(tenantColumn);
                }
                SQLSelectItem selectItem = new SQLSelectItem(item);
                x.getSelectList().add(selectItem);
                visitor.setSqlModified(true);
            }
        }
    }
}
Also used : TenantCallBack(com.alibaba.druid.wall.WallConfig.TenantCallBack) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

TenantCallBack (com.alibaba.druid.wall.WallConfig.TenantCallBack)7 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)4 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)1 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)1 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)1 SQLServerInsertStatement (com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1