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;
}
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);
}
}
}
}
Aggregations