修复 多表联查时,逻辑删除字段和租户字段,只过滤主表,未过滤子表; close #I7EV67

This commit is contained in:
开源海哥 2023-06-20 12:50:12 +08:00
parent bc259bbcc9
commit 60bc26c105
6 changed files with 153 additions and 16 deletions

View File

@ -423,9 +423,9 @@ public class EntitySqlProvider {
if (CollectionUtil.isNotEmpty(queryTables)) { if (CollectionUtil.isNotEmpty(queryTables)) {
tableInfos = new ArrayList<>(); tableInfos = new ArrayList<>();
for (QueryTable queryTable : queryTables) { for (QueryTable queryTable : queryTables) {
String tableName = queryTable.getName(); String tableNameWithSchema = queryTable.getNameWithSchema();
if (StringUtil.isNotBlank(tableName)) { if (StringUtil.isNotBlank(tableNameWithSchema)) {
TableInfo tableInfo = TableInfoFactory.ofTableName(tableName); TableInfo tableInfo = TableInfoFactory.ofTableName(tableNameWithSchema);
if (tableInfo != null) { if (tableInfo != null) {
tableInfos.add(tableInfo); tableInfos.add(tableInfo);
} }

View File

@ -71,6 +71,10 @@ public class QueryTable implements CloneSupport<QueryTable> {
this.name = name; this.name = name;
} }
public String getNameWithSchema() {
return StringUtil.isNotBlank(schema) ? schema + "." + name : name;
}
public QueryTable as(String alias) { public QueryTable as(String alias) {
this.alias = alias; this.alias = alias;
@ -84,8 +88,7 @@ public class QueryTable implements CloneSupport<QueryTable> {
if (StringUtil.isNotBlank(alias) if (StringUtil.isNotBlank(alias)
&& StringUtil.isNotBlank(table.alias) && StringUtil.isNotBlank(table.alias)
&& (Objects.equals(alias, table.alias))) { && (Objects.equals(alias, table.alias))) {
return false; return false;
} }
return Objects.equals(name, table.name); return Objects.equals(name, table.name);
} }

View File

@ -128,6 +128,10 @@ public class TableInfo {
return tableName; return tableName;
} }
public String getTableNameWithSchema() {
return StringUtil.isNotBlank(schema) ? schema + "." + tableName : tableName;
}
public String getWrapSchemaAndTableName(IDialect dialect) { public String getWrapSchemaAndTableName(IDialect dialect) {
if (StringUtil.isNotBlank(schema)) { if (StringUtil.isNotBlank(schema)) {
return dialect.wrap(dialect.getRealSchema(schema)) + "." + dialect.wrap(dialect.getRealTable(tableName)); return dialect.wrap(dialect.getRealSchema(schema)) + "." + dialect.wrap(dialect.getRealTable(tableName));
@ -171,6 +175,7 @@ public class TableInfo {
public String getLogicDeleteColumn() { public String getLogicDeleteColumn() {
return logicDeleteColumn; return logicDeleteColumn;
} }
public void setLogicDeleteColumn(String logicDeleteColumn) { public void setLogicDeleteColumn(String logicDeleteColumn) {
this.logicDeleteColumn = logicDeleteColumn; this.logicDeleteColumn = logicDeleteColumn;
} }
@ -627,6 +632,7 @@ public class TableInfo {
} }
private static final String APPEND_CONDITIONS_FLAG = "appendConditions"; private static final String APPEND_CONDITIONS_FLAG = "appendConditions";
private static final String APPEND_JOIN_FLAG = "appendJoins";
public void appendConditions(Object entity, QueryWrapper queryWrapper) { public void appendConditions(Object entity, QueryWrapper queryWrapper) {
@ -670,9 +676,7 @@ public class TableInfo {
//逻辑删除 //逻辑删除
if (StringUtil.isNotBlank(getLogicDeleteColumnOrSkip())) { if (StringUtil.isNotBlank(getLogicDeleteColumnOrSkip())) {
// queryWrapper.and(QueryCondition.create(schema, tableName, logicDeleteColumn, SqlConsts.EQUALS LogicDeleteManager.getProcessor().buildQueryCondition(queryWrapper, this);
// , FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
LogicDeleteManager.getProcessor().buildQueryCondition(queryWrapper,this);
} }
//多租户 //多租户
@ -693,6 +697,31 @@ public class TableInfo {
} }
} }
//join
if (!Boolean.TRUE.equals(CPI.getContext(queryWrapper, APPEND_JOIN_FLAG))) {
List<Join> joins = CPI.getJoins(queryWrapper);
if (CollectionUtil.isNotEmpty(joins)) {
for (Join join : joins) {
QueryTable joinQueryTable = CPI.getJoinQueryTable(join);
if (joinQueryTable instanceof SelectQueryTable) {
QueryWrapper childQuery = ((SelectQueryTable) joinQueryTable).getQueryWrapper();
doAppendConditions(entity, childQuery);
} else {
String nameWithSchema = joinQueryTable.getNameWithSchema();
if (StringUtil.isNotBlank(nameWithSchema)) {
TableInfo tableInfo = TableInfoFactory.ofTableName(nameWithSchema);
if (tableInfo != null) {
CPI.putContext(queryWrapper, APPEND_CONDITIONS_FLAG, Boolean.FALSE);
CPI.putContext(queryWrapper, APPEND_JOIN_FLAG, Boolean.TRUE);
tableInfo.appendConditions(entity, queryWrapper);
}
}
}
}
}
}
//union //union
List<UnionWrapper> unions = CPI.getUnions(queryWrapper); List<UnionWrapper> unions = CPI.getUnions(queryWrapper);
if (CollectionUtil.isNotEmpty(unions)) { if (CollectionUtil.isNotEmpty(unions)) {
@ -708,9 +737,17 @@ public class TableInfo {
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper); List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
if (queryTables != null && !queryTables.isEmpty()) { if (queryTables != null && !queryTables.isEmpty()) {
for (QueryTable queryTable : queryTables) { for (QueryTable queryTable : queryTables) {
TableInfo tableInfo = TableInfoFactory.ofTableName(queryTable.getName()); if (queryTable instanceof SelectQueryTable) {
if (tableInfo != null) { QueryWrapper childQuery = ((SelectQueryTable) queryTable).getQueryWrapper();
tableInfo.appendConditions(entity, queryWrapper); doAppendConditions(entity, childQuery);
} else {
String nameWithSchema = queryTable.getNameWithSchema();
if (StringUtil.isNotBlank(nameWithSchema)) {
TableInfo tableInfo = TableInfoFactory.ofTableName(nameWithSchema);
if (tableInfo != null) {
tableInfo.appendConditions(entity, queryWrapper);
}
}
} }
} }
} }

View File

@ -101,7 +101,7 @@ public class TableInfoFactory {
public static TableInfo ofEntityClass(Class<?> entityClass) { public static TableInfo ofEntityClass(Class<?> entityClass) {
return MapUtil.computeIfAbsent(entityTableMap, entityClass, aClass -> { return MapUtil.computeIfAbsent(entityTableMap, entityClass, aClass -> {
TableInfo tableInfo = createTableInfo(entityClass); TableInfo tableInfo = createTableInfo(entityClass);
tableInfoMap.put(tableInfo.getTableName(), tableInfo); tableInfoMap.put(tableInfo.getTableNameWithSchema(), tableInfo);
return tableInfo; return tableInfo;
}); });
} }

View File

@ -55,10 +55,10 @@ public class AccountSqlTester {
TableManager.setDynamicTableProcessor(new DynamicTableProcessor() { TableManager.setDynamicTableProcessor(new DynamicTableProcessor() {
@Override @Override
public String process(String tableName) { public String process(String tableName) {
return tableName+"_01"; return tableName + "_01";
} }
}); });
TableManager.setDynamicTableProcessor(original -> original+"_01"); TableManager.setDynamicTableProcessor(original -> original + "_01");
System.out.println(query.toSQL()); System.out.println(query.toSQL());
} }
@ -72,8 +72,8 @@ public class AccountSqlTester {
.where(ACCOUNT01.ID.ge(100)) .where(ACCOUNT01.ID.ge(100))
.and(ACCOUNT.SEX.eq(1)); .and(ACCOUNT.SEX.eq(1));
TableManager.setDynamicTableProcessor(original -> original+"_01"); TableManager.setDynamicTableProcessor(original -> original + "_01");
TableManager.setDynamicTableProcessor(original -> original+"_01"); TableManager.setDynamicTableProcessor(original -> original + "_01");
System.out.println(query.toSQL()); System.out.println(query.toSQL());
} }
@ -263,6 +263,8 @@ public class AccountSqlTester {
System.out.println(sql); System.out.println(sql);
} }
//https://gitee.com/mybatis-flex/mybatis-flex/issues/I7EAY9
@Test @Test
public void testGroup_I7EAY9() { public void testGroup_I7EAY9() {
QueryWrapper query = QueryWrapper.create() QueryWrapper query = QueryWrapper.create()
@ -330,6 +332,10 @@ public class AccountSqlTester {
System.out.println(sql); System.out.println(sql);
} }
@Test @Test
public void testOrderBySql() { public void testOrderBySql() {
QueryWrapper queryWrapper = QueryWrapper.create() QueryWrapper queryWrapper = QueryWrapper.create()

View File

@ -0,0 +1,91 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.test;
import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.audit.ConsoleMessageCollector;
import com.mybatisflex.core.audit.MessageCollector;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.mapper.ArticleMapper;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
import java.util.List;
import static com.mybatisflex.core.query.QueryMethods.raw;
import static com.mybatisflex.core.query.QueryMethods.select;
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
import static com.mybatisflex.test.table.ArticleTableDef.ARTICLE;
/**
* test https://gitee.com/mybatis-flex/mybatis-flex/issues/I7EV67
*/
public class JoinWithDeleteColumnTestStarter {
public static void main(String[] args) {
DataSource dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.addScript("data.sql")
.build();
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
.setDataSource(dataSource)
.addMapper(AccountMapper.class)
.addMapper(MyAccountMapper.class)
.addMapper(ArticleMapper.class)
.start();
//开启审计功能
AuditManager.setAuditEnable(true);
//设置 SQL 审计收集器
MessageCollector collector = new ConsoleMessageCollector();
AuditManager.setMessageCollector(collector);
AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class);
QueryWrapper query1 = QueryWrapper.create()
.select()
.from(ACCOUNT)
.leftJoin(ARTICLE).as("a").on(
ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID)
)
.where(ACCOUNT.AGE.ge(10));
List<AccountDTO> accountDTOS1 = accountMapper.selectListByQueryAs(query1, AccountDTO.class);
System.out.println(accountDTOS1);
System.out.println(">>>>>>>>>");
QueryWrapper query2 = QueryWrapper.create()
.select()
.from(ACCOUNT)
.leftJoin(
select().from(ARTICLE).where(ARTICLE.ID.ge(100))
).as("a").on(
ACCOUNT.ID.eq(raw("a.id"))
)
.where(ACCOUNT.AGE.ge(10));
List<AccountDTO> accountDTOS2 = accountMapper.selectListByQueryAs(query2, AccountDTO.class);
System.out.println(accountDTOS2);
}
}