Skip to content

Commit 2742daf

Browse files
committed
Add convenience methods for different type of count queries
1 parent e4e8632 commit 2742daf

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,33 @@
107107
public interface SqlBuilder {
108108

109109
// statements
110+
111+
/**
112+
* Renders as select count(distinct column) from table...
113+
*
114+
* @param table
115+
* @return CountDSL for adding an optional where clause
116+
*/
117+
static CountDSL<SelectModel> countDistinctFrom(BasicColumn column, SqlTable table) {
118+
return CountDSL.countDistinct(column).from(table);
119+
}
120+
121+
/**
122+
* Renders as select count(column) from table...
123+
*
124+
* @param table
125+
* @return CountDSL for adding an optional where clause
126+
*/
127+
static CountDSL<SelectModel> countFrom(BasicColumn column, SqlTable table) {
128+
return CountDSL.count(column).from(table);
129+
}
130+
131+
/**
132+
* Renders as select count(*) from table...
133+
*
134+
* @param table
135+
* @return CountDSL for adding an optional where clause
136+
*/
110137
static CountDSL<SelectModel> countFrom(SqlTable table) {
111138
return CountDSL.countFrom(table);
112139
}

src/main/java/org/mybatis/dynamic/sql/select/CountDSL.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
import java.util.Objects;
1919
import java.util.function.Function;
2020

21+
import org.mybatis.dynamic.sql.BasicColumn;
2122
import org.mybatis.dynamic.sql.BindableColumn;
2223
import org.mybatis.dynamic.sql.SqlBuilder;
2324
import org.mybatis.dynamic.sql.SqlCriterion;
@@ -41,9 +42,11 @@ public class CountDSL<R> extends AbstractQueryExpressionDSL<CountDSL<R>, R> impl
4142

4243
private Function<SelectModel, R> adapterFunction;
4344
private CountWhereBuilder whereBuilder = new CountWhereBuilder();
45+
private BasicColumn countColumn;
4446

45-
private CountDSL(SqlTable table, Function<SelectModel, R> adapterFunction) {
47+
private CountDSL(BasicColumn countColumn, SqlTable table, Function<SelectModel, R> adapterFunction) {
4648
super(table);
49+
this.countColumn = Objects.requireNonNull(countColumn);
4750
this.adapterFunction = Objects.requireNonNull(adapterFunction);
4851
}
4952

@@ -68,7 +71,7 @@ public R build() {
6871

6972
private SelectModel buildModel() {
7073
QueryExpressionModel.Builder b = new QueryExpressionModel.Builder()
71-
.withSelectColumn(SqlBuilder.count())
74+
.withSelectColumn(countColumn)
7275
.withTable(table())
7376
.withTableAliases(tableAliases)
7477
.withWhereModel(whereBuilder.buildWhereModel());
@@ -85,14 +88,44 @@ public static CountDSL<SelectModel> countFrom(SqlTable table) {
8588
}
8689

8790
public static <R> CountDSL<R> countFrom(Function<SelectModel, R> adapterFunction, SqlTable table) {
88-
return new CountDSL<>(table, adapterFunction);
91+
return new CountDSL<>(SqlBuilder.count(), table, adapterFunction);
92+
}
93+
94+
public static FromGatherer<SelectModel> count(BasicColumn column) {
95+
return count(Function.identity(), column);
96+
}
97+
98+
public static <R> FromGatherer<R> count(Function<SelectModel, R> adapterFunction, BasicColumn column) {
99+
return new FromGatherer<>(adapterFunction, SqlBuilder.count(column));
100+
}
101+
102+
public static FromGatherer<SelectModel> countDistinct(BasicColumn column) {
103+
return countDistinct(Function.identity(), column);
104+
}
105+
106+
public static <R> FromGatherer<R> countDistinct(Function<SelectModel, R> adapterFunction, BasicColumn column) {
107+
return new FromGatherer<>(adapterFunction, SqlBuilder.countDistinct(column));
89108
}
90109

91110
@Override
92111
protected CountDSL<R> getThis() {
93112
return this;
94113
}
95114

115+
public static class FromGatherer<R> {
116+
private BasicColumn column;
117+
private Function<SelectModel, R> adapterFunction;
118+
119+
public FromGatherer(Function<SelectModel, R> adapterFunction, BasicColumn column) {
120+
this.adapterFunction = adapterFunction;
121+
this.column = column;
122+
}
123+
124+
public CountDSL<R> from(SqlTable table) {
125+
return new CountDSL<> (column, table, adapterFunction);
126+
}
127+
}
128+
96129
public class CountWhereBuilder extends AbstractWhereDSL<CountWhereBuilder>
97130
implements Buildable<R> {
98131
private <T> CountWhereBuilder() {}

src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@
5252
public class MyBatis3Utils {
5353
private MyBatis3Utils() {}
5454

55+
public static long count(ToLongFunction<SelectStatementProvider> mapper, BasicColumn column, SqlTable table,
56+
CountDSLCompleter completer) {
57+
return mapper.applyAsLong(count(column, table, completer));
58+
}
59+
60+
public static SelectStatementProvider count(BasicColumn column, SqlTable table, CountDSLCompleter completer) {
61+
return countFrom(SqlBuilder.countFrom(column, table), completer);
62+
}
63+
64+
public static long countDistinct(ToLongFunction<SelectStatementProvider> mapper, BasicColumn column, SqlTable table,
65+
CountDSLCompleter completer) {
66+
return mapper.applyAsLong(countDistinct(column, table, completer));
67+
}
68+
69+
public static SelectStatementProvider countDistinct(BasicColumn column, SqlTable table, CountDSLCompleter completer) {
70+
return countFrom(SqlBuilder.countDistinctFrom(column, table), completer);
71+
}
72+
5573
public static SelectStatementProvider countFrom(SqlTable table, CountDSLCompleter completer) {
5674
return countFrom(SqlBuilder.countFrom(table), completer);
5775
}

src/test/java/examples/simple/PersonMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ default long count(CountDSLCompleter completer) {
100100
return MyBatis3Utils.countFrom(this::count, person, completer);
101101
}
102102

103+
default long count(BasicColumn column, CountDSLCompleter completer) {
104+
return MyBatis3Utils.count(this::count, column, person, completer);
105+
}
106+
107+
default long countDistinct(BasicColumn column, CountDSLCompleter completer) {
108+
return MyBatis3Utils.countDistinct(this::count, column, person, completer);
109+
}
110+
103111
default int delete(DeleteDSLCompleter completer) {
104112
return MyBatis3Utils.deleteFrom(this::delete, person, completer);
105113
}

src/test/java/examples/simple/PersonMapperTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,26 @@ void testCountAll() {
473473
}
474474
}
475475

476+
@Test
477+
void testCountLastName() {
478+
try (SqlSession session = sqlSessionFactory.openSession()) {
479+
PersonMapper mapper = session.getMapper(PersonMapper.class);
480+
long rows = mapper.count(lastName, CountDSLCompleter.allRows());
481+
482+
assertThat(rows).isEqualTo(6L);
483+
}
484+
}
485+
486+
@Test
487+
void testCountDistinctLastName() {
488+
try (SqlSession session = sqlSessionFactory.openSession()) {
489+
PersonMapper mapper = session.getMapper(PersonMapper.class);
490+
long rows = mapper.countDistinct(lastName, CountDSLCompleter.allRows());
491+
492+
assertThat(rows).isEqualTo(2L);
493+
}
494+
}
495+
476496
@Test
477497
void testTypeHandledLike() {
478498
try (SqlSession session = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)