You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,7 @@ If you have written your own set of functions to extend the library, you will no
18
18
- 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))
19
19
- Added the ability to write a function that will change the column data type ([#197](https://github.com/mybatis/mybatis-dynamic-sql/issues/197))
20
20
- 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))
Copy file name to clipboardExpand all lines: src/site/markdown/docs/kotlinMyBatis3.md
+10-4Lines changed: 10 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,7 +80,7 @@ A count query is a specialized select - it returns a single column - typically a
80
80
81
81
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.
82
82
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:
84
84
85
85
```kotlin
86
86
@Mapper
@@ -90,14 +90,20 @@ interface PersonMapper {
90
90
}
91
91
```
92
92
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:
94
94
95
95
```kotlin
96
-
fun PersonMapper.count(completer:CountCompleter) =
96
+
fun PersonMapper.count(completer:CountCompleter) =// count(*)
97
97
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)
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:
Copy file name to clipboardExpand all lines: src/site/markdown/docs/kotlinSpring.md
+28-1Lines changed: 28 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,15 +35,34 @@ This object is a singleton containing the `SqlTable` and `SqlColumn` objects tha
35
35
36
36
A count query is a specialized select - it returns a single column - typically a long - and supports joins and a where clause.
37
37
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
+
38
44
The DSL for count methods looks like this:
39
45
40
46
```kotlin
47
+
// count(*)
41
48
val countStatement = countFrom(Person) { // countStatement is a SelectStatementProvider
42
49
where(id, isLessThan(4))
43
50
}
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
+
}
44
61
```
45
62
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:
47
66
48
67
```kotlin
49
68
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
56
75
val rows = template.countFrom(Person) {
57
76
where(id, isLessThan(4))
58
77
}
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
+
}
59
86
```
60
87
61
88
There is also an extention method that can be used to count all rows in a table:
Copy file name to clipboardExpand all lines: src/site/markdown/docs/mybatis3.md
+18-4Lines changed: 18 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,22 +9,36 @@ With version 1.1.3, specialized interfaces and utilities were added that can fur
9
9
10
10
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.
11
11
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:
long count(SelectStatementProvider selectStatement);
17
17
```
18
18
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
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:
0 commit comments