fixed:修复多租户的场景下 union 的子查询不添加租户 ID 的问题

This commit is contained in:
开源海哥 2023-04-14 18:09:03 +08:00
parent 8e5ab410f0
commit 48277d6c12
3 changed files with 24 additions and 9 deletions

View File

@ -24,10 +24,7 @@ import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.javassist.ModifyAttrsRecord; import com.mybatisflex.core.javassist.ModifyAttrsRecord;
import com.mybatisflex.core.mybatis.TypeHandlerObject; import com.mybatisflex.core.mybatis.TypeHandlerObject;
import com.mybatisflex.core.query.CPI; import com.mybatisflex.core.query.*;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.UnionWrapper;
import com.mybatisflex.core.row.Row; import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.tenant.TenantManager; import com.mybatisflex.core.tenant.TenantManager;
import com.mybatisflex.core.util.*; import com.mybatisflex.core.util.*;
@ -513,7 +510,14 @@ public class TableInfo {
List<UnionWrapper> unions = CPI.getUnions(queryWrapper); List<UnionWrapper> unions = CPI.getUnions(queryWrapper);
if (CollectionUtil.isNotEmpty(unions)) { if (CollectionUtil.isNotEmpty(unions)) {
for (UnionWrapper union : unions) { for (UnionWrapper union : unions) {
appendConditions(entity, union.getQueryWrapper()); QueryWrapper unionQueryWrapper = union.getQueryWrapper();
List<QueryTable> queryTables = CPI.getQueryTables(unionQueryWrapper);
for (QueryTable queryTable : queryTables) {
TableInfo tableInfo = TableInfoFactory.ofTableName(queryTable.getName());
if (tableInfo != null) {
tableInfo.appendConditions(entity, unionQueryWrapper);
}
}
} }
} }

View File

@ -63,6 +63,7 @@ public class TableInfoFactory {
private static final Map<Class<?>, TableInfo> mapperTableInfoMap = new ConcurrentHashMap<>(); private static final Map<Class<?>, TableInfo> mapperTableInfoMap = new ConcurrentHashMap<>();
private static final Map<Class<?>, TableInfo> entityTableMap = new ConcurrentHashMap<>(); private static final Map<Class<?>, TableInfo> entityTableMap = new ConcurrentHashMap<>();
private static final Map<String, TableInfo> tableInfoMap = new ConcurrentHashMap<>();
public static TableInfo ofMapperClass(Class<?> mapperClass) { public static TableInfo ofMapperClass(Class<?> mapperClass) {
@ -78,10 +79,20 @@ public class TableInfoFactory {
public static TableInfo ofEntityClass(Class<?> entityClass) { public static TableInfo ofEntityClass(Class<?> entityClass) {
return MapUtil.computeIfAbsent(entityTableMap, entityClass, key -> createTableInfo(entityClass)); return MapUtil.computeIfAbsent(entityTableMap, entityClass, aClass -> {
TableInfo tableInfo = createTableInfo(entityClass);
tableInfoMap.put(tableInfo.getTableName(), tableInfo);
return tableInfo;
});
} }
public static TableInfo ofTableName(String tableName){
return tableInfoMap.get(tableName);
}
private static Class<?> getEntityClass(Class<?> mapperClass) { private static Class<?> getEntityClass(Class<?> mapperClass) {
Type[] genericInterfaces = mapperClass.getGenericInterfaces(); Type[] genericInterfaces = mapperClass.getGenericInterfaces();
if (genericInterfaces.length == 1) { if (genericInterfaces.length == 1) {

View File

@ -27,6 +27,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource; import javax.sql.DataSource;
import static com.mybatisflex.core.query.QueryMethods.select;
import static com.mybatisflex.test.table.Tables.ACCOUNT; import static com.mybatisflex.test.table.Tables.ACCOUNT;
import static com.mybatisflex.test.table.Tables.TENANT_ACCOUNT; import static com.mybatisflex.test.table.Tables.TENANT_ACCOUNT;
@ -62,11 +63,10 @@ public class TenantTester {
mapper.selectListByQuery(QueryWrapper.create() mapper.selectListByQuery(QueryWrapper.create()
.select(TENANT_ACCOUNT.ALL_COLUMNS) .select(TENANT_ACCOUNT.ALL_COLUMNS)
.from(TENANT_ACCOUNT.as("a"), ACCOUNT.as("b")) .from(TENANT_ACCOUNT.as("c"), ACCOUNT.as("b"))
.where(TENANT_ACCOUNT.ID.eq(ACCOUNT.ID)) .where(TENANT_ACCOUNT.ID.eq(ACCOUNT.ID))
.and(TENANT_ACCOUNT.ID.eq(1)) .and(TENANT_ACCOUNT.ID.eq(1))
.unionAll(QueryWrapper.create() .unionAll(select(TENANT_ACCOUNT.ALL_COLUMNS).from(TENANT_ACCOUNT)
.select(TENANT_ACCOUNT.ALL_COLUMNS).from(TENANT_ACCOUNT)
.where(TENANT_ACCOUNT.ID.eq(2)) .where(TENANT_ACCOUNT.ID.eq(2))
) )
); );