Merge pull request #515 from cybzzz/main

fix: exists 条件中的表别名和父查询保持一致
This commit is contained in:
Michael Yang 2025-03-17 09:16:21 +08:00 committed by GitHub
commit eafe6d338f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 2 deletions

View File

@ -15,6 +15,7 @@
*/ */
package com.mybatisflex.core.dialect; package com.mybatisflex.core.dialect;
import com.mybatisflex.core.query.QueryTable;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row; import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfo;
@ -64,6 +65,10 @@ public interface IDialect {
String buildSelectSql(QueryWrapper queryWrapper); String buildSelectSql(QueryWrapper queryWrapper);
default String buildSelectSql(QueryWrapper queryWrapper, List<QueryTable> contextTables) {
return buildSelectSql(queryWrapper);
}
String buildNoSelectSql(QueryWrapper queryWrapper); String buildNoSelectSql(QueryWrapper queryWrapper);
String buildDeleteSql(QueryWrapper queryWrapper); String buildDeleteSql(QueryWrapper queryWrapper);

View File

@ -395,10 +395,16 @@ public class CommonsDialectImpl implements IDialect {
////////////build query sql/////// ////////////build query sql///////
@Override @Override
public String buildSelectSql(QueryWrapper queryWrapper) { public String buildSelectSql(QueryWrapper queryWrapper) {
return buildSelectSql(queryWrapper, Collections.emptyList());
}
@Override
public String buildSelectSql(QueryWrapper queryWrapper, List<QueryTable> contextTables) {
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper); List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
List<QueryTable> joinTables = CPI.getJoinTables(queryWrapper); List<QueryTable> joinTables = CPI.getJoinTables(queryWrapper);
List<QueryTable> allTables = CollectionUtil.merge(queryTables, joinTables); List<QueryTable> allTables = CollectionUtil.merge(queryTables, joinTables);
allTables = CollectionUtil.merge(allTables, contextTables);
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper); List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);

View File

@ -48,7 +48,7 @@ public class OperatorSelectCondition extends QueryCondition {
//检测是否生效 //检测是否生效
if (checkEffective()) { if (checkEffective()) {
String childSql = dialect.buildSelectSql(queryWrapper); String childSql = dialect.buildSelectSql(queryWrapper, queryTables);
if (StringUtil.hasText(childSql)) { if (StringUtil.hasText(childSql)) {
QueryCondition prevEffectiveCondition = getPrevEffectiveCondition(); QueryCondition prevEffectiveCondition = getPrevEffectiveCondition();
if (prevEffectiveCondition != null && this.connector != null) { if (prevEffectiveCondition != null && this.connector != null) {

View File

@ -400,7 +400,7 @@ public class AccountSqlTester {
Assert.assertEquals("SELECT * FROM `tb_account` " + Assert.assertEquals("SELECT * FROM `tb_account` " +
"WHERE `id` >= 100 " + "WHERE `id` >= 100 " +
"AND EXISTS (SELECT 1 AS `temp_one` FROM `tb_article` AS `a` WHERE `id` >= 100)" "AND EXISTS (SELECT 1 AS `temp_one` FROM `tb_article` AS `a` WHERE `a`.`id` >= 100)"
, query.toSQL()); , query.toSQL());
System.out.println(query.toSQL()); System.out.println(query.toSQL());