diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/EntitySqlProvider.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/EntitySqlProvider.java index aca2e3f0..b9d58a68 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/EntitySqlProvider.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/provider/EntitySqlProvider.java @@ -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); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryTable.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryTable.java index ec5d4aab..202d61dc 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryTable.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryTable.java @@ -71,6 +71,10 @@ public class QueryTable implements CloneSupport { this.name = name; } + public String getNameWithSchema() { + return StringUtil.isNotBlank(schema) ? schema + "." + name : name; + } + public QueryTable as(String alias) { this.alias = alias; @@ -84,8 +88,7 @@ public class QueryTable implements CloneSupport { if (StringUtil.isNotBlank(alias) && StringUtil.isNotBlank(table.alias) && (Objects.equals(alias, table.alias))) { - return false; - + return false; } return Objects.equals(name, table.name); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 69be0562..7a85ae43 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -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 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 unions = CPI.getUnions(queryWrapper); if (CollectionUtil.isNotEmpty(unions)) { @@ -708,9 +737,17 @@ public class TableInfo { List queryTables = CPI.getQueryTables(queryWrapper); if (queryTables != null && !queryTables.isEmpty()) { for (QueryTable queryTable : queryTables) { - TableInfo tableInfo = TableInfoFactory.ofTableName(queryTable.getName()); - if (tableInfo != null) { - tableInfo.appendConditions(entity, queryWrapper); + 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); + } + } } } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java index c82a77e4..b2446216 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfoFactory.java @@ -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; }); } diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java index 1d2f8b3f..817b1862 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java @@ -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() diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/JoinWithDeleteColumnTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/JoinWithDeleteColumnTestStarter.java new file mode 100644 index 00000000..23366109 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/JoinWithDeleteColumnTestStarter.java @@ -0,0 +1,91 @@ +/** + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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 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 accountDTOS2 = accountMapper.selectListByQueryAs(query2, AccountDTO.class); + System.out.println(accountDTOS2); + + } +}