From 16dd1c2cd6159a20db0850b9aea197e8659f1f2c Mon Sep 17 00:00:00 2001 From: Jeff Butler Date: Sun, 7 Jul 2019 09:54:05 -0400 Subject: [PATCH] Add support for count(distinct ...) Resolves #112 --- CHANGELOG.md | 9 +++++ .../org/mybatis/dynamic/sql/SqlBuilder.java | 5 +++ .../sql/select/aggregate/CountDistinct.java | 39 +++++++++++++++++++ .../java/examples/groupby/GroupByTest.java | 20 ++++++++++ 4 files changed, 73 insertions(+) create mode 100644 src/main/java/org/mybatis/dynamic/sql/select/aggregate/CountDistinct.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a8176a32..b9862eff8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ This log will detail notable changes to MyBatis Dynamic SQL. Full details are available on the GitHub milestone pages. +## Release 1.1.3 - Unreleased + +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+) + +### Added + +- Added support for `count(distinct ...)` [#112](https://github.com/mybatis/mybatis-dynamic-sql/issues/112) + + ## Release 1.1.2 - July 5, 2019 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+) diff --git a/src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java b/src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java index 1159f93fe..748548fcc 100644 --- a/src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java +++ b/src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java @@ -31,6 +31,7 @@ import org.mybatis.dynamic.sql.select.aggregate.Avg; import org.mybatis.dynamic.sql.select.aggregate.Count; import org.mybatis.dynamic.sql.select.aggregate.CountAll; +import org.mybatis.dynamic.sql.select.aggregate.CountDistinct; import org.mybatis.dynamic.sql.select.aggregate.Max; import org.mybatis.dynamic.sql.select.aggregate.Min; import org.mybatis.dynamic.sql.select.aggregate.Sum; @@ -200,6 +201,10 @@ static Count count(BasicColumn column) { return Count.of(column); } + static CountDistinct countDistinct(BasicColumn column) { + return CountDistinct.of(column); + } + static Max max(BasicColumn column) { return Max.of(column); } diff --git a/src/main/java/org/mybatis/dynamic/sql/select/aggregate/CountDistinct.java b/src/main/java/org/mybatis/dynamic/sql/select/aggregate/CountDistinct.java new file mode 100644 index 000000000..7435b4568 --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/select/aggregate/CountDistinct.java @@ -0,0 +1,39 @@ +/** + * Copyright 2016-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.dynamic.sql.select.aggregate; + +import org.mybatis.dynamic.sql.BasicColumn; + +public class CountDistinct extends AbstractAggregate { + + private CountDistinct(BasicColumn column) { + super(column); + } + + @Override + protected String render(String columnName) { + return "count(distinct " + columnName + ")"; //$NON-NLS-1$ //$NON-NLS-2$ + } + + @Override + protected CountDistinct copy() { + return new CountDistinct(column); + } + + public static CountDistinct of(BasicColumn column) { + return new CountDistinct(column); + } +} diff --git a/src/test/java/examples/groupby/GroupByTest.java b/src/test/java/examples/groupby/GroupByTest.java index 3a4fc4554..b78d11ff8 100644 --- a/src/test/java/examples/groupby/GroupByTest.java +++ b/src/test/java/examples/groupby/GroupByTest.java @@ -312,4 +312,24 @@ public void testFetchFirstOnlyAfterGroupBy() { assertThat(row.get("COUNT")).isEqualTo(4L); } } + + @Test + public void testCountDistinct() { + try (SqlSession session = sqlSessionFactory.openSession()) { + GroupByMapper mapper = session.getMapper(GroupByMapper.class); + + SelectStatementProvider selectStatement = select(countDistinct(lastName).as("count")) + .from(person) + .build() + .render(RenderingStrategy.MYBATIS3); + + String expected = "select count(distinct last_name) as count from Person"; + assertThat(selectStatement.getSelectStatement()).isEqualTo(expected); + + List> rows = mapper.generalSelect(selectStatement); + assertThat(rows.size()).isEqualTo(1); + Map row = rows.get(0); + assertThat(row.get("COUNT")).isEqualTo(2L); + } + } }