mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
修复 多表联查时,逻辑删除字段和租户字段,只过滤主表,未过滤子表; close #I7EV67
This commit is contained in:
parent
bc259bbcc9
commit
60bc26c105
@ -423,9 +423,9 @@ public class EntitySqlProvider {
|
||||
if (CollectionUtil.isNotEmpty(queryTables)) {
|
||||
tableInfos = new ArrayList<>();
|
||||
for (QueryTable queryTable : queryTables) {
|
||||
String tableName = queryTable.getName();
|
||||
if (StringUtil.isNotBlank(tableName)) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofTableName(tableName);
|
||||
String tableNameWithSchema = queryTable.getNameWithSchema();
|
||||
if (StringUtil.isNotBlank(tableNameWithSchema)) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofTableName(tableNameWithSchema);
|
||||
if (tableInfo != null) {
|
||||
tableInfos.add(tableInfo);
|
||||
}
|
||||
|
||||
@ -71,6 +71,10 @@ public class QueryTable implements CloneSupport<QueryTable> {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getNameWithSchema() {
|
||||
return StringUtil.isNotBlank(schema) ? schema + "." + name : name;
|
||||
}
|
||||
|
||||
|
||||
public QueryTable as(String alias) {
|
||||
this.alias = alias;
|
||||
@ -85,7 +89,6 @@ public class QueryTable implements CloneSupport<QueryTable> {
|
||||
&& StringUtil.isNotBlank(table.alias)
|
||||
&& (Objects.equals(alias, table.alias))) {
|
||||
return false;
|
||||
|
||||
}
|
||||
return Objects.equals(name, table.name);
|
||||
}
|
||||
|
||||
@ -128,6 +128,10 @@ public class TableInfo {
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public String getTableNameWithSchema() {
|
||||
return StringUtil.isNotBlank(schema) ? schema + "." + tableName : tableName;
|
||||
}
|
||||
|
||||
public String getWrapSchemaAndTableName(IDialect dialect) {
|
||||
if (StringUtil.isNotBlank(schema)) {
|
||||
return dialect.wrap(dialect.getRealSchema(schema)) + "." + dialect.wrap(dialect.getRealTable(tableName));
|
||||
@ -171,6 +175,7 @@ public class TableInfo {
|
||||
public String getLogicDeleteColumn() {
|
||||
return logicDeleteColumn;
|
||||
}
|
||||
|
||||
public void setLogicDeleteColumn(String logicDeleteColumn) {
|
||||
this.logicDeleteColumn = logicDeleteColumn;
|
||||
}
|
||||
@ -627,6 +632,7 @@ public class TableInfo {
|
||||
}
|
||||
|
||||
private static final String APPEND_CONDITIONS_FLAG = "appendConditions";
|
||||
private static final String APPEND_JOIN_FLAG = "appendJoins";
|
||||
|
||||
public void appendConditions(Object entity, QueryWrapper queryWrapper) {
|
||||
|
||||
@ -670,9 +676,7 @@ public class TableInfo {
|
||||
|
||||
//逻辑删除
|
||||
if (StringUtil.isNotBlank(getLogicDeleteColumnOrSkip())) {
|
||||
// queryWrapper.and(QueryCondition.create(schema, tableName, logicDeleteColumn, SqlConsts.EQUALS
|
||||
// , FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete()));
|
||||
LogicDeleteManager.getProcessor().buildQueryCondition(queryWrapper,this);
|
||||
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
|
||||
List<UnionWrapper> unions = CPI.getUnions(queryWrapper);
|
||||
if (CollectionUtil.isNotEmpty(unions)) {
|
||||
@ -708,13 +737,21 @@ public class TableInfo {
|
||||
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
|
||||
if (queryTables != null && !queryTables.isEmpty()) {
|
||||
for (QueryTable queryTable : queryTables) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofTableName(queryTable.getName());
|
||||
if (queryTable instanceof SelectQueryTable) {
|
||||
QueryWrapper childQuery = ((SelectQueryTable) queryTable).getQueryWrapper();
|
||||
doAppendConditions(entity, childQuery);
|
||||
} else {
|
||||
String nameWithSchema = queryTable.getNameWithSchema();
|
||||
if (StringUtil.isNotBlank(nameWithSchema)) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofTableName(nameWithSchema);
|
||||
if (tableInfo != null) {
|
||||
tableInfo.appendConditions(entity, queryWrapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getKeyProperties() {
|
||||
|
||||
@ -101,7 +101,7 @@ public class TableInfoFactory {
|
||||
public static TableInfo ofEntityClass(Class<?> entityClass) {
|
||||
return MapUtil.computeIfAbsent(entityTableMap, entityClass, aClass -> {
|
||||
TableInfo tableInfo = createTableInfo(entityClass);
|
||||
tableInfoMap.put(tableInfo.getTableName(), tableInfo);
|
||||
tableInfoMap.put(tableInfo.getTableNameWithSchema(), tableInfo);
|
||||
return tableInfo;
|
||||
});
|
||||
}
|
||||
|
||||
@ -55,10 +55,10 @@ public class AccountSqlTester {
|
||||
TableManager.setDynamicTableProcessor(new DynamicTableProcessor() {
|
||||
@Override
|
||||
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());
|
||||
}
|
||||
@ -72,8 +72,8 @@ public class AccountSqlTester {
|
||||
.where(ACCOUNT01.ID.ge(100))
|
||||
.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());
|
||||
}
|
||||
@ -263,6 +263,8 @@ public class AccountSqlTester {
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
|
||||
//https://gitee.com/mybatis-flex/mybatis-flex/issues/I7EAY9
|
||||
@Test
|
||||
public void testGroup_I7EAY9() {
|
||||
QueryWrapper query = QueryWrapper.create()
|
||||
@ -330,6 +332,10 @@ public class AccountSqlTester {
|
||||
System.out.println(sql);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testOrderBySql() {
|
||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||
|
||||
@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user