Merge remote-tracking branch 'gitee/main'

# Conflicts:
#	mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/QueryWrapperTest.java
This commit is contained in:
Michael Yang 2024-10-28 16:46:25 +08:00
commit cd23561a20
3 changed files with 26 additions and 14 deletions

View File

@ -978,15 +978,17 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
// 可以省略表的引用直接使用列名
// SELECT 1
// SELECT id FROM tb_user
if (queryTables == null || queryTables.isEmpty() || queryTables.size() == 1) {
if (queryTables == null || queryTables.isEmpty()) {
return null;
}
QueryTable consideredTable = queryTables.get(0);
// 列未指定表名仅以字符串的形式输入列名
// 以查询表中的第一个表为主
// SELECT tb_user.id FROM tb_user
if (selfTable == null) {
return queryTables.get(0);
return consideredTable;
}
// 当前表有别名以别名为主
@ -995,7 +997,14 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
return selfTable;
}
QueryTable consideredTable = selfTable;
// 当前表没有别名查询表只有一个
// 如果两个表是一样的则可以忽略表的引用
// 兼容子查询时子查询的查询表和父查询没有合并的问题
if (queryTables.size() == 1 && Objects.equals(selfTable.name, consideredTable.name)) {
return null;
}
consideredTable = selfTable;
// 当前表存在且没有别名
ListIterator<QueryTable> it = queryTables.listIterator(queryTables.size());

View File

@ -41,7 +41,7 @@ public class SelectQueryColumn extends QueryColumn implements HasParamsColumn {
@Override
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
return dialect.forSelectByQuery(queryWrapper);
return WrapperUtil.withBracket(dialect.forSelectByQuery(queryWrapper));
}
@Override

View File

@ -69,17 +69,20 @@ public class MapperUtil {
.select(count().as("total"))
.from(queryWrapper).as("t");
}
public static QueryWrapper rawCountQueryWrapper(QueryWrapper queryWrapper, List<QueryColumn> customCountColumns) {
return customCountColumns != null ? QueryWrapper.create()
.select(customCountColumns)
.from(queryWrapper).as("t") : rawCountQueryWrapper(queryWrapper);
}
/**
* 优化 COUNT 查询语句
*/
public static QueryWrapper optimizeCountQueryWrapper(QueryWrapper queryWrapper) {
return optimizeCountQueryWrapper(queryWrapper, Collections.singletonList(count().as("total")));
}
/**
* 优化 COUNT 查询语句
*/
@ -108,7 +111,7 @@ public class MapperUtil {
// 如果有 distinctgroup byhaving 等语句则不优化
// 这种一旦优化了就会造成 count 语句查询出来的值不对
if (hasDistinct(selectColumns) || hasGroupBy(groupByColumns) || havingCondition != null) {
return clone;
return rawCountQueryWrapper(clone);
}
// 判断能不能清除 join 语句
if (canClearJoins(clone)) {