File tree Expand file tree Collapse file tree 4 files changed +73
-0
lines changed
main/java/org/mybatis/dynamic/sql
test/java/examples/groupby Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
This log will detail notable changes to MyBatis Dynamic SQL. Full details are available on the GitHub milestone pages.
4
4
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
+
5
14
## Release 1.1.2 - July 5, 2019
6
15
7
16
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+ )
Original file line number Diff line number Diff line change 31
31
import org .mybatis .dynamic .sql .select .aggregate .Avg ;
32
32
import org .mybatis .dynamic .sql .select .aggregate .Count ;
33
33
import org .mybatis .dynamic .sql .select .aggregate .CountAll ;
34
+ import org .mybatis .dynamic .sql .select .aggregate .CountDistinct ;
34
35
import org .mybatis .dynamic .sql .select .aggregate .Max ;
35
36
import org .mybatis .dynamic .sql .select .aggregate .Min ;
36
37
import org .mybatis .dynamic .sql .select .aggregate .Sum ;
@@ -200,6 +201,10 @@ static Count count(BasicColumn column) {
200
201
return Count .of (column );
201
202
}
202
203
204
+ static CountDistinct countDistinct (BasicColumn column ) {
205
+ return CountDistinct .of (column );
206
+ }
207
+
203
208
static Max max (BasicColumn column ) {
204
209
return Max .of (column );
205
210
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -312,4 +312,24 @@ public void testFetchFirstOnlyAfterGroupBy() {
312
312
assertThat (row .get ("COUNT" )).isEqualTo (4L );
313
313
}
314
314
}
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
+ }
315
335
}
You can’t perform that action at this time.
0 commit comments