Skip to content

Commit 3ae221c

Browse files
committed
Documentation
1 parent 42af2c1 commit 3ae221c

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ If you have written your own set of functions to extend the library, you will no
1818
- Added the capability to specify a rendering strategy on a column to override the defaut rendering strategy for a statement. This will allow certain edge cases where a parameter marker needs to be formatted in a unique way (for example, "::jsonb" needs to be added to parameter markers for JSON fields in PostgreSQL) ([#200](https://github.com/mybatis/mybatis-dynamic-sql/issues/200))
1919
- Added the ability to write a function that will change the column data type ([#197](https://github.com/mybatis/mybatis-dynamic-sql/issues/197))
2020
- Added the `applyOperator` function to make it easy to use non-standard database operators in expressions ([#220](https://github.com/mybatis/mybatis-dynamic-sql/issues/220))
21+
- Added convenience methods for count(column) and count(distinct column)([#221](https://github.com/mybatis/mybatis-dynamic-sql/issues/221))
2122

2223
## Release 1.1.4 - November 23, 2019
2324

src/site/markdown/docs/kotlinMyBatis3.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ A count query is a specialized select - it returns a single column - typically a
8080

8181
Count method support enables the creation of methods that execute a count query allowing a user to specify a where clause at runtime, but abstracting away all other details.
8282

83-
To use this support, we envision creating two methods - one standard mapper method, and one extension method. The first method is the standard MyBatis Dynamic SQL method that will execute a select:
83+
To use this support, we envision creating several methods - one standard mapper method, and other extension methods. The first method is the standard MyBatis Dynamic SQL method that will execute a select:
8484

8585
```kotlin
8686
@Mapper
@@ -90,14 +90,20 @@ interface PersonMapper {
9090
}
9191
```
9292

93-
This is a standard method for MyBatis Dynamic SQL that executes a query and returns a `Long`. The second method should be an extension maethod. It will reuse the abstract method and supply everything needed to build the select statement except the where clause:
93+
This is a standard method for MyBatis Dynamic SQL that executes a query and returns a `Long`. The other methods should be extension methods. They will reuse the abstract method and supply everything needed to build the select statement except the where clause:
9494

9595
```kotlin
96-
fun PersonMapper.count(completer: CountCompleter) =
96+
fun PersonMapper.count(completer: CountCompleter) = // count(*)
9797
countFrom(this::count, Person, completer)
98+
99+
fun PersonMapper.count(column: BasicColumn, completer: CountCompleter) = // count(column)
100+
count(this::count, column, Person, completer)
101+
102+
fun PersonMapper.countDistinct(column: BasicColumn, completer: CountCompleter) = // count(distinct column)
103+
countDistinct(this::count, column, Person, completer)
98104
```
99105

100-
This method shows the use of `CountCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. This also shows use of the Kotlin `countFrom` method which is supplied by the library. That method will build and execute the select count statement with the supplied where clause. Clients can use the method as follows:
106+
These methods show the use of `CountCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. This also shows use of the Kotlin `countFrom`, `count`, and `countDistinct` methods which are supplied by the library. Those methods will build and execute the select count statements with the supplied where clause. Clients can use the methods as follows:
101107

102108
```kotlin
103109
val rows = mapper.count {

src/site/markdown/docs/kotlinSpring.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,34 @@ This object is a singleton containing the `SqlTable` and `SqlColumn` objects tha
3535

3636
A count query is a specialized select - it returns a single column - typically a long - and supports joins and a where clause.
3737

38+
The library supports three types of count statements:
39+
40+
1. `count(*)` - counts the number of rows that match a where clause
41+
1. `count(column)` - counts the number of non-null column values that match a where clause
42+
1. `count(distinct column)` - counts the number of unique column values that match a where clause
43+
3844
The DSL for count methods looks like this:
3945

4046
```kotlin
47+
// count(*)
4148
val countStatement = countFrom(Person) { // countStatement is a SelectStatementProvider
4249
where(id, isLessThan(4))
4350
}
51+
52+
// count(column)
53+
val countStatement = countColumn(lastName).from(Person) { // countStatement is a SelectStatementProvider
54+
allRows()
55+
}
56+
57+
// count(distinct column)
58+
val countStatement = countDistinctColumn(lastName).from(Person) { // countStatement is a SelectStatementProvider
59+
allRows()
60+
}
4461
```
4562

46-
This code creates a `SelectStatementProvider` that can be executed with an extension method for `NamedParameterJdbcTemplate` like this:
63+
Note the somewhat awkward method names `countColumn`, and `countDistinctColumn`. The methods are named this way to avoid a name collision with other methods in the `SqlBuilder`. This awkwardness can be avoided by using the one step method shown below.
64+
65+
These methods create a `SelectStatementProvider` that can be executed with an extension method for `NamedParameterJdbcTemplate` like this:
4766

4867
```kotlin
4968
val template: NamedParameterJdbcTemplate = getTemplate() // not shown
@@ -56,6 +75,14 @@ This is the two step execution process. This can be combined into a single step
5675
val rows = template.countFrom(Person) {
5776
where(id, isLessThan(4))
5877
}
78+
79+
val rows = template.count(lastName).from(Person) {
80+
where(id, isLessThan(4))
81+
}
82+
83+
val rows = template.countDistinct(lastName).from(Person) {
84+
where(id, isLessThan(4))
85+
}
5986
```
6087

6188
There is also an extention method that can be used to count all rows in a table:

src/site/markdown/docs/mybatis3.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,36 @@ With version 1.1.3, specialized interfaces and utilities were added that can fur
99

1010
The goal of count method support is to enable the creation of methods that execute a count query allowing a user to specify a where clause at runtime, but abstracting away all other details.
1111

12-
To use this support, we envision creating two methods on a MyBatis mapper interface. The first method is the standard MyBatis Dynamic SQL method that will execute a select:
12+
To use this support, we envision creating several methods on a MyBatis mapper interface. The first method is the standard MyBatis Dynamic SQL method that will execute a select:
1313

1414
```java
1515
@SelectProvider(type=SqlProviderAdapter.class, method="select")
1616
long count(SelectStatementProvider selectStatement);
1717
```
1818

19-
This is a standard method for MyBatis Dynamic SQL that executes a query and returns a `long`. The second method will reuse this method and supply everything needed to build the select statement except the where clause:
19+
This is a standard method for MyBatis Dynamic SQL that executes a query and returns a `long`. The other methods will reuse this method and supply everything needed to build the select statement except the where clause. There are several varients of count queries that may be useful:
20+
21+
1. `count(*)` - counts the number of rows that match a where clause
22+
1. `count(column)` - counts the number of non-null column values that match a where clause
23+
1. `count(distinct column)` - counts the number of unique column values that match a where clause
24+
25+
Corresponding mapper methods are as follows:
2026

2127
```java
22-
default long count(CountDSLCompleter completer) {
28+
default long count(CountDSLCompleter completer) { // count(*)
2329
return MyBatis3Utils.countFrom(this::count, person, completer);
2430
}
31+
32+
default long count(BasicColumn column, CountDSLCompleter completer) { // count(column)
33+
return MyBatis3Utils.count(this::count, column, person, completer);
34+
}
35+
36+
default long countDistinct(BasicColumn column, CountDSLCompleter completer) { // count(distinct column)
37+
return MyBatis3Utils.countDistinct(this::count, column, person, completer);
38+
}
2539
```
2640

27-
This method shows the use of `CountDSLCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows:
41+
These methods show the use of `CountDSLCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows:
2842

2943
```java
3044
long rows = mapper.count(c ->

0 commit comments

Comments
 (0)