|
@@ -32,6 +32,7 @@ import net.sf.jsqlparser.statement.Statement;
|
|
|
import net.sf.jsqlparser.statement.select.PlainSelect;
|
|
|
import net.sf.jsqlparser.statement.select.Select;
|
|
|
import net.sf.jsqlparser.statement.select.SelectBody;
|
|
|
+import net.sf.jsqlparser.statement.select.SubSelect;
|
|
|
|
|
|
@Intercepts({
|
|
|
@Signature(type = StatementHandler.class, method = "prepare", args = {
|
|
@@ -49,7 +50,7 @@ public class TenantSqlInterceptor extends BaseInterceptor implements Interceptor
|
|
|
// 获取原始 SQL 和 MappedStatement
|
|
|
String originalSql = (String) metaObject.getValue("delegate.boundSql.sql");
|
|
|
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
|
|
|
- Pair<Method,Boolean> mapperMethod = getMapperMethod(mappedStatement);
|
|
|
+ Pair<Method, Boolean> mapperMethod = getMapperMethod(mappedStatement);
|
|
|
// 判断是否需要添加租户条件
|
|
|
if (!ObjectUtils.allNotNull(mapperMethod.getKey(), getLoginUser().getUser().getTenantId())) {
|
|
|
return invocation.proceed();
|
|
@@ -69,18 +70,18 @@ public class TenantSqlInterceptor extends BaseInterceptor implements Interceptor
|
|
|
EqualsTo tenantCondition = new EqualsTo();
|
|
|
tenantCondition.setLeftExpression(handleColumn(mapperMethod.getKey()));
|
|
|
tenantCondition.setRightExpression(new LongValue(getLoginUser().getUser().getTenantId()));
|
|
|
- // *_COUNT 结尾的查询方法添加租户条件
|
|
|
- if (mapperMethod.getValue()) {
|
|
|
- SelectBody subSelect = ((SubSelect)plainSelect.getFromItem()).getSelectBody();
|
|
|
-
|
|
|
- if (subSelect instanceof PlainSelect) {
|
|
|
- PlainSelect subPlainSelect = (PlainSelect) subSelect;
|
|
|
- // 添加到原有WHERE条件
|
|
|
- if (subPlainSelect.getWhere() != null) {
|
|
|
- subPlainSelect.setWhere(new AndExpression(subPlainSelect.getWhere(), tenantCondition));
|
|
|
- } else {
|
|
|
- subPlainSelect.setWhere(tenantCondition);
|
|
|
- }
|
|
|
+ // *_COUNT 结尾的查询方法且from组合查询子句添加租户条件
|
|
|
+ // SELECT count(0) FROM (SELECT * FROM flow_task AS t LEFT JOIN flow_user uu ON uu.associated = t.id)
|
|
|
+ // table_count
|
|
|
+ if (mapperMethod.getValue() && plainSelect.getFromItem() instanceof SubSelect) {
|
|
|
+ SelectBody subSelect = ((SubSelect) plainSelect.getFromItem()).getSelectBody();
|
|
|
+ PlainSelect subPlainSelect = (PlainSelect) subSelect;
|
|
|
+ // 添加到原有WHERE条件
|
|
|
+ if (subPlainSelect.getWhere() != null) {
|
|
|
+ subPlainSelect.setWhere(new AndExpression(subPlainSelect.getWhere(), tenantCondition));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ subPlainSelect.setWhere(tenantCondition);
|
|
|
}
|
|
|
// 生成修改后的 SQL
|
|
|
String modifiedSql = plainSelect.toString();
|