Skip to content

Commit 880b742

Browse files
authored
Merge pull request #14 from tnicolas29200/groupByWithouOrderBy
Adding the groupBy function to the whereExpression
2 parents 8770f10 + 567de3b commit 880b742

File tree

8 files changed

+43
-7
lines changed

8 files changed

+43
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ buildNumber.properties
1111
/.classpath
1212
/.project
1313
/.vscode/
14+
/bin/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -216,6 +216,13 @@ public SelectDSL<R> orderBy(SortSpecification...columns) {
216216
return selectDSL;
217217
}
218218

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.

src/main/java/org/mybatis/dynamic/sql/select/render/SelectRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.

src/main/java/org/mybatis/dynamic/sql/select/render/SelectStatementProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.

src/test/java/examples/generated/always/spring/SpringTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -91,6 +91,7 @@ public void testSelect() {
9191
.render(RenderingStrategy.SPRING_NAMED_PARAMETER);
9292

9393
SqlParameterSource namedParameters = new MapSqlParameterSource(selectStatement.getParameters());
94+
9495
List<GeneratedAlwaysRecord> records = template.query(selectStatement.getSelectStatement(), namedParameters,
9596
new RowMapper<GeneratedAlwaysRecord>(){
9697
@Override
@@ -112,6 +113,7 @@ public GeneratedAlwaysRecord mapRow(ResultSet rs, int rowNum) throws SQLExceptio
112113

113114
assertThat(records.get(1).getId()).isEqualTo(5);
114115
assertThat(records.get(2).getId()).isEqualTo(4);
116+
115117
}
116118

117119
@Test
@@ -151,6 +153,7 @@ public void testInsert() {
151153

152154
assertThat(rows).isEqualTo(1);
153155
assertThat(generatedKey).isEqualTo("Bob Jones");
156+
154157
}
155158

156159
@Test
@@ -198,6 +201,7 @@ public void testUpdate() {
198201
int rows = template.update(updateStatement.getUpdateStatement(), parameterSource);
199202

200203
assertThat(rows).isEqualTo(2);
204+
201205
}
202206

203207
@AfterEach

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 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.
@@ -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 a.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)