From 4e7458eb0428dbaea02321960d41dd7e0a693349 Mon Sep 17 00:00:00 2001 From: muyuanjin <2874665803@qq.com> Date: Fri, 19 Aug 2022 11:19:25 +0800 Subject: [PATCH] =?UTF-8?q?Group=20By=20Having=20=E4=BC=98=E5=85=88?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=B7=B2=E6=9F=A5=E8=AF=A2=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=9A=84=E5=88=AB=E5=90=8D=E8=80=8C=E4=B8=8D=E6=98=AF=20@DbFie?= =?UTF-8?q?ld.value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../implement/DefaultSqlResolver.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/bean-searcher/src/main/java/com/ejlchina/searcher/implement/DefaultSqlResolver.java b/bean-searcher/src/main/java/com/ejlchina/searcher/implement/DefaultSqlResolver.java index 3564f872..1cdf9cc3 100644 --- a/bean-searcher/src/main/java/com/ejlchina/searcher/implement/DefaultSqlResolver.java +++ b/bean-searcher/src/main/java/com/ejlchina/searcher/implement/DefaultSqlResolver.java @@ -52,7 +52,7 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { } Map paraMap = searchParam.getParaMap(); SqlWrapper fieldSelectSqlWrapper = buildFieldSelectSql(beanMeta, fetchFields, paraMap); - SqlWrapper fromWhereSqlWrapper = buildFromWhereSql(beanMeta, searchParam.getParamsGroup(), paraMap); + SqlWrapper fromWhereSqlWrapper = buildFromWhereSql(beanMeta, searchParam, paraMap); String fieldSelectSql = fieldSelectSqlWrapper.getSql(); String fromWhereSql = fromWhereSqlWrapper.getSql(); @@ -134,7 +134,8 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { return sqlWrapper; } - protected SqlWrapper buildFromWhereSql(BeanMeta beanMeta, Group> paramsGroup, Map paraMap) { + protected SqlWrapper buildFromWhereSql(BeanMeta beanMeta, SearchParam searchParam, Map paraMap) { + Group> paramsGroup = searchParam.getParamsGroup(); SqlWrapper sqlWrapper = new SqlWrapper<>(); SqlWrapper tableSql = resolveTableSql(beanMeta.getTableSnippet(), paraMap); sqlWrapper.addParas(tableSql.getParas()); @@ -159,7 +160,7 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { } } if (hasWhereParams) { - useGroup(whereGroup, beanMeta, paraMap, builder, sqlWrapper.getParas()); + useGroup(whereGroup, beanMeta, searchParam, paraMap, builder, sqlWrapper.getParas(), false); } } if (groupBy != null) { @@ -181,7 +182,7 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { } } if (hasHavingParams) { - useGroup(havingGroup, beanMeta, paraMap, builder, sqlWrapper.getParas()); + useGroup(havingGroup, beanMeta, searchParam, paraMap, builder, sqlWrapper.getParas(), true); } } } @@ -189,8 +190,8 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { return sqlWrapper; } - protected void useGroup(Group> group, BeanMeta beanMeta, Map paraMap, - StringBuilder sqlBuilder, List paraReceiver) { + protected void useGroup(Group> group, BeanMeta beanMeta, SearchParam searchParam, Map paraMap, + StringBuilder sqlBuilder, List paraReceiver, boolean isHaving) { group.forEach(event -> { if (event.isGroupStart()) { sqlBuilder.append("("); @@ -220,7 +221,14 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { (name) -> { String field = name != null ? name : param.getName(); FieldMeta meta = beanMeta.requireFieldMeta(field); - return resolveDbFieldSql(meta.getFieldSql(), paraMap); + SqlSnippet sql = meta.getFieldSql(); + //如果是 group by having 且查询中存在该字段,则使用该字段别名 + if (isHaving && searchParam.getFetchFields().contains(field)) { + SqlSnippet sqlSnippet = new SqlSnippet(); + sqlSnippet.setSql(meta.getDbAlias()); + sql = sqlSnippet; + } + return resolveDbFieldSql(sql, paraMap); }, param.isIgnoreCase(), param.getValues() @@ -231,7 +239,6 @@ public class DefaultSqlResolver extends DialectWrapper implements SqlResolver { } }); } - protected SqlWrapper resolveGroupBy(BeanMeta beanMeta, Map paraMap) { String groupBy = beanMeta.getGroupBy(); if (StringUtils.isNotBlank(groupBy)) { -- Gitee