Skip to content

Commit 066174c

Browse files
author
nt
committed
Adding the groupBy function to the whereExpression
1 parent a3ffea4 commit 066174c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ public SelectDSL<R> orderBy(SortSpecification...columns) {
216216
return selectDSL;
217217
}
218218

219+
public GroupByFinisher groupBy(BasicColumn...columns) {
220+
groupByModel = GroupByModel.of(columns);
221+
selectDSL.addQueryExpression(buildModel());
222+
return new GroupByFinisher();
223+
}
224+
219225
@Override
220226
public R build() {
221227
whereModel = buildWhereModel();

src/test/java/org/mybatis/dynamic/sql/select/SelectStatementTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,28 @@ public void testNoWhere() {
236236
softly.assertThat(parameters.size()).isEqualTo(0);
237237
});
238238
}
239+
240+
@Test
241+
public void testGroupBySingleColumn() {
242+
Date d = new Date();
243+
244+
SelectStatementProvider selectStatement = select(column1.as("A_COLUMN1"), column2)
245+
.from(table, "a")
246+
.where(column1, isEqualTo(d))
247+
.groupBy(column2)
248+
.build()
249+
.render(RenderingStrategy.MYBATIS3);
250+
251+
SoftAssertions.assertSoftly(softly -> {
252+
String expectedFullStatement = "select a.column1 as A_COLUMN1, a.column2 "
253+
+ "from foo a "
254+
+ "where a.column1 = #{parameters.p1,jdbcType=DATE} "
255+
+ "group by column2 ";
256+
257+
softly.assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedFullStatement);
258+
259+
Map<String, Object> parameters = selectStatement.getParameters();
260+
softly.assertThat(parameters.get("p1")).isEqualTo(d);
261+
});
262+
}
239263
}

0 commit comments

Comments
 (0)