Skip to content

Commit

Permalink
Merge pull request #29 from yy1098029419/bugfix/distinct_20200818
Browse files Browse the repository at this point in the history
修改countSql在distinct情况下生成的sql不正确的问题
  • Loading branch information
blinkfox authored Aug 18, 2020
2 parents 67b5181 + 6fc6880 commit e1d67c4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/java/com/blinkfox/fenix/jpa/FenixJpaQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ public class FenixJpaQuery extends AbstractJpaQuery {
* 用来匹配 SQL 中的 DISTINCT 条件.
*/
private static final String REGX_SELECT_FROM_DISTINCT =
"((?i)select)([\\s\\S]*?)((?i)distinct)\\s+([^,\\s]+)\\s*(,|\\s)([\\s\\S]*?)((?i)from)";
"((?i)select)([\\s\\S]*?)((?i)distinct)\\s+([\\s\\S]*?)((?i)from)";

/**
* 匹配 SQL 中别名的正则.
*/
private static final String REGX_SQL_ALIAS = "\\s+((?i)as)\\s+[^,\\s]+";

/**
* select from 正则表达式 Pattern.
Expand Down Expand Up @@ -389,7 +394,7 @@ private String getCountSqlByQueryInfo(FenixQueryInfo fenixQueryInfo) {
if (!matcher.find()) {
return countSql;
}
String distinctColumn = matcher.group(4);
String distinctColumn = matcher.group(4).replaceAll(REGX_SQL_ALIAS,"");
return countSql.replaceFirst("count\\(\\*\\)", String.format("count(distinct %s)", distinctColumn));
}

Expand Down

0 comments on commit e1d67c4

Please sign in to comment.