Skip to content

Commit 2e2d564

Browse files
authored
Merge pull request #113 from jeffgbutler/master
Add support for count(distinct ...)
2 parents 86e3980 + 16dd1c2 commit 2e2d564

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
This log will detail notable changes to MyBatis Dynamic SQL. Full details are available on the GitHub milestone pages.
44

5+
## Release 1.1.3 - Unreleased
6+
7+
GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.2+](https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.3+)
8+
9+
### Added
10+
11+
- Added support for `count(distinct ...)` [#112](https://github.com/mybatis/mybatis-dynamic-sql/issues/112)
12+
13+
514
## Release 1.1.2 - July 5, 2019
615

716
GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.2+](https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.2+)

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.mybatis.dynamic.sql.select.aggregate.Avg;
3232
import org.mybatis.dynamic.sql.select.aggregate.Count;
3333
import org.mybatis.dynamic.sql.select.aggregate.CountAll;
34+
import org.mybatis.dynamic.sql.select.aggregate.CountDistinct;
3435
import org.mybatis.dynamic.sql.select.aggregate.Max;
3536
import org.mybatis.dynamic.sql.select.aggregate.Min;
3637
import org.mybatis.dynamic.sql.select.aggregate.Sum;
@@ -200,6 +201,10 @@ static Count count(BasicColumn column) {
200201
return Count.of(column);
201202
}
202203

204+
static CountDistinct countDistinct(BasicColumn column) {
205+
return CountDistinct.of(column);
206+
}
207+
203208
static Max max(BasicColumn column) {
204209
return Max.of(column);
205210
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2016-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select.aggregate;
17+
18+
import org.mybatis.dynamic.sql.BasicColumn;
19+
20+
public class CountDistinct extends AbstractAggregate<CountDistinct> {
21+
22+
private CountDistinct(BasicColumn column) {
23+
super(column);
24+
}
25+
26+
@Override
27+
protected String render(String columnName) {
28+
return "count(distinct " + columnName + ")"; //$NON-NLS-1$ //$NON-NLS-2$
29+
}
30+
31+
@Override
32+
protected CountDistinct copy() {
33+
return new CountDistinct(column);
34+
}
35+
36+
public static CountDistinct of(BasicColumn column) {
37+
return new CountDistinct(column);
38+
}
39+
}

src/test/java/examples/groupby/GroupByTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,24 @@ public void testFetchFirstOnlyAfterGroupBy() {
312312
assertThat(row.get("COUNT")).isEqualTo(4L);
313313
}
314314
}
315+
316+
@Test
317+
public void testCountDistinct() {
318+
try (SqlSession session = sqlSessionFactory.openSession()) {
319+
GroupByMapper mapper = session.getMapper(GroupByMapper.class);
320+
321+
SelectStatementProvider selectStatement = select(countDistinct(lastName).as("count"))
322+
.from(person)
323+
.build()
324+
.render(RenderingStrategy.MYBATIS3);
325+
326+
String expected = "select count(distinct last_name) as count from Person";
327+
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
328+
329+
List<Map<String, Object>> rows = mapper.generalSelect(selectStatement);
330+
assertThat(rows.size()).isEqualTo(1);
331+
Map<String, Object> row = rows.get(0);
332+
assertThat(row.get("COUNT")).isEqualTo(2L);
333+
}
334+
}
315335
}

0 commit comments

Comments
 (0)