Skip to content

Commit 3963159

Browse files
committed
Add Kotlin support for new count functions
1 parent 2742daf commit 3963159

File tree

10 files changed

+222
-81
lines changed

10 files changed

+222
-81
lines changed

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,20 @@ public interface SqlBuilder {
110110

111111
/**
112112
* Renders as select count(distinct column) from table...
113-
*
114-
* @param table
115-
* @return CountDSL for adding an optional where clause
116113
*/
117-
static CountDSL<SelectModel> countDistinctFrom(BasicColumn column, SqlTable table) {
118-
return CountDSL.countDistinct(column).from(table);
114+
static CountDSL.FromGatherer<SelectModel> countDistinctColumn(BasicColumn column) {
115+
return CountDSL.countDistinct(column);
119116
}
120117

121118
/**
122119
* Renders as select count(column) from table...
123-
*
124-
* @param table
125-
* @return CountDSL for adding an optional where clause
126120
*/
127-
static CountDSL<SelectModel> countFrom(BasicColumn column, SqlTable table) {
128-
return CountDSL.count(column).from(table);
121+
static CountDSL.FromGatherer<SelectModel> countColumn(BasicColumn column) {
122+
return CountDSL.count(column);
129123
}
130124

131125
/**
132126
* Renders as select count(*) from table...
133-
*
134-
* @param table
135-
* @return CountDSL for adding an optional where clause
136127
*/
137128
static CountDSL<SelectModel> countFrom(SqlTable table) {
138129
return CountDSL.countFrom(table);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static long count(ToLongFunction<SelectStatementProvider> mapper, BasicCo
5858
}
5959

6060
public static SelectStatementProvider count(BasicColumn column, SqlTable table, CountDSLCompleter completer) {
61-
return countFrom(SqlBuilder.countFrom(column, table), completer);
61+
return countFrom(SqlBuilder.countColumn(column).from(table), completer);
6262
}
6363

6464
public static long countDistinct(ToLongFunction<SelectStatementProvider> mapper, BasicColumn column, SqlTable table,
@@ -67,7 +67,7 @@ public static long countDistinct(ToLongFunction<SelectStatementProvider> mapper,
6767
}
6868

6969
public static SelectStatementProvider countDistinct(BasicColumn column, SqlTable table, CountDSLCompleter completer) {
70-
return countFrom(SqlBuilder.countDistinctFrom(column, table), completer);
70+
return countFrom(SqlBuilder.countDistinctColumn(column).from(table), completer);
7171
}
7272

7373
public static SelectStatementProvider countFrom(SqlTable table, CountDSLCompleter completer) {

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/MapperSupportFunctions.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
2828
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider
2929
import org.mybatis.dynamic.sql.util.kotlin.*
3030

31+
fun count(mapper: (SelectStatementProvider) -> Long, column: BasicColumn, table: SqlTable, completer: CountCompleter) =
32+
mapper(SqlBuilder.countColumn(column).from(table, completer))
33+
34+
fun countDistinct(mapper: (SelectStatementProvider) -> Long, column: BasicColumn, table: SqlTable, completer: CountCompleter) =
35+
mapper(SqlBuilder.countDistinctColumn(column).from(table, completer))
36+
3137
fun countFrom(mapper: (SelectStatementProvider) -> Long, table: SqlTable, completer: CountCompleter) =
3238
mapper(countFrom(table, completer))
3339

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/ProviderBuilderFunctions.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider
2525
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
2626
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider
2727
import org.mybatis.dynamic.sql.render.RenderingStrategies
28+
import org.mybatis.dynamic.sql.select.CountDSL
2829
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
2930
import org.mybatis.dynamic.sql.select.SelectModel
3031
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
@@ -52,6 +53,15 @@ fun <T> MultiRowInsertDSL.IntoGatherer<T>.into(
5253
): MultiRowInsertStatementProvider<T> =
5354
completer(into(table)).build().render(RenderingStrategies.MYBATIS3)
5455

56+
fun CountDSL.FromGatherer<SelectModel>.from(
57+
table: SqlTable,
58+
completer: CountCompleter
59+
): SelectStatementProvider {
60+
val builder = KotlinCountBuilder(from(table))
61+
completer(builder)
62+
return builder.build().render(RenderingStrategies.MYBATIS3)
63+
}
64+
5565
fun QueryExpressionDSL.FromGatherer<SelectModel>.from(
5666
table: SqlTable,
5767
completer: SelectCompleter

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/NamedParameterJdbcTemplateExtensions.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.mybatis.dynamic.sql.SqlTable
2121
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider
2222
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider
2323
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
24+
import org.mybatis.dynamic.sql.select.CountDSL
2425
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
2526
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider
2627
import org.mybatis.dynamic.sql.util.kotlin.*
@@ -31,6 +32,12 @@ import java.sql.ResultSet
3132
fun NamedParameterJdbcTemplate.count(selectStatement: SelectStatementProvider) =
3233
queryForObject(selectStatement.selectStatement, selectStatement.parameters, Long::class.java)!!
3334

35+
fun NamedParameterJdbcTemplate.count(column: BasicColumn) =
36+
CountFromGatherer(column, this)
37+
38+
fun NamedParameterJdbcTemplate.countDistinct(column: BasicColumn) =
39+
CountDistinctFromGatherer(column, this)
40+
3441
fun NamedParameterJdbcTemplate.countFrom(table: SqlTable, completer: CountCompleter) =
3542
count(org.mybatis.dynamic.sql.util.kotlin.spring.countFrom(table, completer))
3643

@@ -79,6 +86,23 @@ fun NamedParameterJdbcTemplate.update(updateStatement: UpdateStatementProvider)
7986
fun NamedParameterJdbcTemplate.update(table: SqlTable, completer: UpdateCompleter) =
8087
update(org.mybatis.dynamic.sql.util.kotlin.spring.update(table, completer))
8188

89+
// support classes for count DSL
90+
class CountFromGatherer(
91+
private val column: BasicColumn,
92+
private val template: NamedParameterJdbcTemplate
93+
) {
94+
fun from(table: SqlTable, completer: CountCompleter) =
95+
template.count(CountDSL.count(column).from(table, completer))
96+
}
97+
98+
class CountDistinctFromGatherer(
99+
private val column: BasicColumn,
100+
private val template: NamedParameterJdbcTemplate
101+
) {
102+
fun from(table: SqlTable, completer: CountCompleter) =
103+
template.count(CountDSL.countDistinct(column).from(table, completer))
104+
}
105+
82106
// support classes for select DSL
83107
class SelectListFromGatherer(
84108
private val selectList: List<BasicColumn>,

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/spring/ProviderBuilderFunctions.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,37 @@ import org.mybatis.dynamic.sql.insert.InsertDSL
2323
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider
2424
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
2525
import org.mybatis.dynamic.sql.render.RenderingStrategies
26+
import org.mybatis.dynamic.sql.select.CountDSL
2627
import org.mybatis.dynamic.sql.select.QueryExpressionDSL
2728
import org.mybatis.dynamic.sql.select.SelectModel
2829
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
2930
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider
3031
import org.mybatis.dynamic.sql.util.kotlin.*
3132

32-
fun countFrom(table: SqlTable, completer: CountCompleter): SelectStatementProvider {
33-
val builder = KotlinCountBuilder(SqlBuilder.countFrom(table))
33+
fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteStatementProvider {
34+
val builder = KotlinDeleteBuilder(SqlBuilder.deleteFrom(table))
3435
completer(builder)
3536
return builder.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
3637
}
3738

38-
fun deleteFrom(table: SqlTable, completer: DeleteCompleter): DeleteStatementProvider {
39-
val builder = KotlinDeleteBuilder(SqlBuilder.deleteFrom(table))
39+
fun countFrom(table: SqlTable, completer: CountCompleter): SelectStatementProvider {
40+
val builder = KotlinCountBuilder(SqlBuilder.countFrom(table))
4041
completer(builder)
4142
return builder.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
4243
}
4344

4445
fun <T> InsertDSL.IntoGatherer<T>.into(table: SqlTable, completer: InsertCompleter<T>): InsertStatementProvider<T> =
4546
completer(into(table)).build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
4647

48+
fun CountDSL.FromGatherer<SelectModel>.from(
49+
table: SqlTable,
50+
completer: CountCompleter
51+
): SelectStatementProvider {
52+
val builder = KotlinCountBuilder(from(table))
53+
completer(builder)
54+
return builder.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER)
55+
}
56+
4757
fun QueryExpressionDSL.FromGatherer<SelectModel>.from(
4858
table: SqlTable,
4959
completer: SelectCompleter

src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperExtensions.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.Person.firstNa
2323
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.Person.id
2424
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.Person.lastName
2525
import examples.kotlin.mybatis3.canonical.PersonDynamicSqlSupport.Person.occupation
26+
import org.mybatis.dynamic.sql.BasicColumn
2627
import org.mybatis.dynamic.sql.SqlBuilder.isEqualTo
2728
import org.mybatis.dynamic.sql.util.kotlin.*
2829
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.*
2930

31+
fun PersonMapper.count(column: BasicColumn, completer: CountCompleter) =
32+
count(this::count, column, Person, completer)
33+
34+
fun PersonMapper.countDistinct(column: BasicColumn, completer: CountCompleter) =
35+
countDistinct(this::count, column, Person, completer)
36+
3037
fun PersonMapper.count(completer: CountCompleter) =
3138
countFrom(this::count, Person, completer)
3239

src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,28 @@ class PersonMapperTest {
483483
}
484484
}
485485

486+
@Test
487+
fun testCountLastName() {
488+
newSession().use { session ->
489+
val mapper = session.getMapper(PersonMapper::class.java)
490+
491+
val rows = mapper.count(lastName) { allRows() }
492+
493+
assertThat(rows).isEqualTo(6L)
494+
}
495+
}
496+
497+
@Test
498+
fun testCountDistinctLastName() {
499+
newSession().use { session ->
500+
val mapper = session.getMapper(PersonMapper::class.java)
501+
502+
val rows = mapper.countDistinct(lastName) { allRows() }
503+
504+
assertThat(rows).isEqualTo(2L)
505+
}
506+
}
507+
486508
@Test
487509
fun testTypeHandledLike() {
488510
newSession().use { session ->

src/test/kotlin/examples/kotlin/spring/canonical/CanonicalSpringKotlinTemplateDirectTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ class CanonicalSpringKotlinTemplateDirectTest {
6565
assertThat(rows).isEqualTo(6)
6666
}
6767

68+
@Test
69+
fun testCountLastName() {
70+
val rows = template.count(lastName).from(Person) {
71+
allRows()
72+
}
73+
74+
assertThat(rows).isEqualTo(6)
75+
}
76+
77+
@Test
78+
fun testCountDistinctLastName() {
79+
val rows = template.countDistinct(lastName).from(Person) {
80+
allRows()
81+
}
82+
83+
assertThat(rows).isEqualTo(2)
84+
}
85+
6886
@Test
6987
fun testAllRows() {
7088
val rows = template.deleteFrom(Person) {

0 commit comments

Comments
 (0)