Skip to content

Commit aff06dc

Browse files
author
nt
committed
Bug correction for the group by clause
1 parent 066174c commit aff06dc

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

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

Lines changed: 2 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.
@@ -22,6 +22,7 @@
2222
import java.util.stream.Stream;
2323

2424
import org.mybatis.dynamic.sql.BasicColumn;
25+
import org.mybatis.dynamic.sql.SqlColumn;
2526

2627
public class GroupByModel {
2728
private List<BasicColumn> columns = new ArrayList<>();

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

Lines changed: 4 additions & 2 deletions
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.
@@ -25,6 +25,7 @@
2525
import org.mybatis.dynamic.sql.BasicColumn;
2626
import org.mybatis.dynamic.sql.BindableColumn;
2727
import org.mybatis.dynamic.sql.SortSpecification;
28+
import org.mybatis.dynamic.sql.SqlColumn;
2829
import org.mybatis.dynamic.sql.SqlCriterion;
2930
import org.mybatis.dynamic.sql.SqlTable;
3031
import org.mybatis.dynamic.sql.VisitableCondition;
@@ -217,8 +218,9 @@ public SelectDSL<R> orderBy(SortSpecification...columns) {
217218
}
218219

219220
public GroupByFinisher groupBy(BasicColumn...columns) {
220-
groupByModel = GroupByModel.of(columns);
221+
whereModel = buildWhereModel();
221222
selectDSL.addQueryExpression(buildModel());
223+
selectDSL.setGroupByModel(GroupByModel.of(columns));
222224
return new GroupByFinisher();
223225
}
224226

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

Lines changed: 7 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.
@@ -37,6 +37,7 @@ public class SelectDSL<R> {
3737
private Function<SelectModel, R> adapterFunction;
3838
private List<QueryExpressionModel> queryExpressions = new ArrayList<>();
3939
private OrderByModel orderByModel;
40+
private GroupByModel groupByModel;
4041

4142
private SelectDSL(Function<SelectModel, R> adapterFunction) {
4243
this.adapterFunction = Objects.requireNonNull(adapterFunction);
@@ -96,9 +97,14 @@ void setOrderByModel(OrderByModel orderByModel) {
9697
this.orderByModel = orderByModel;
9798
}
9899

100+
void setGroupByModel(GroupByModel groupByModel) {
101+
this.groupByModel = groupByModel;
102+
}
103+
99104
public R build() {
100105
SelectModel selectModel = SelectModel.withQueryExpressions(queryExpressions)
101106
.withOrderByModel(orderByModel)
107+
.withGroupByModel(groupByModel)
102108
.build();
103109
return adapterFunction.apply(selectModel);
104110
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
public class SelectModel {
3030
private List<QueryExpressionModel> queryExpressions;
3131
private OrderByModel orderByModel;
32+
private GroupByModel groupByModel;
3233

3334
private SelectModel(Builder builder) {
3435
queryExpressions = Objects.requireNonNull(builder.queryExpressions);
3536
orderByModel = builder.orderByModel;
37+
groupByModel = builder.groupByModel;
3638
}
3739

3840
public <R> Stream<R> mapQueryExpressions(Function<QueryExpressionModel, R> mapper) {
@@ -43,6 +45,10 @@ public Optional<OrderByModel> orderByModel() {
4345
return Optional.ofNullable(orderByModel);
4446
}
4547

48+
public Optional<GroupByModel> groupByModel() {
49+
return Optional.ofNullable(groupByModel);
50+
}
51+
4652
public SelectStatementProvider render(RenderingStrategy renderingStrategy) {
4753
return SelectRenderer.withSelectModel(this)
4854
.withRenderingStrategy(renderingStrategy)
@@ -57,6 +63,7 @@ public static Builder withQueryExpressions(List<QueryExpressionModel> queryExpre
5763
public static class Builder {
5864
private List<QueryExpressionModel> queryExpressions = new ArrayList<>();
5965
private OrderByModel orderByModel;
66+
private GroupByModel groupByModel;
6067

6168
public Builder withQueryExpressions(List<QueryExpressionModel> queryExpressions) {
6269
this.queryExpressions.addAll(queryExpressions);
@@ -68,6 +75,11 @@ public Builder withOrderByModel(OrderByModel orderByModel) {
6875
return this;
6976
}
7077

78+
public Builder withGroupByModel(GroupByModel groupByModel) {
79+
this.groupByModel = groupByModel;
80+
return this;
81+
}
82+
7183
public SelectModel build() {
7284
return new SelectModel(this);
7385
}

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

Lines changed: 14 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.
@@ -19,8 +19,12 @@
1919
import java.util.Optional;
2020
import java.util.concurrent.atomic.AtomicInteger;
2121

22+
import org.mybatis.dynamic.sql.BasicColumn;
23+
import org.mybatis.dynamic.sql.BindableColumn;
2224
import org.mybatis.dynamic.sql.SortSpecification;
25+
import org.mybatis.dynamic.sql.SqlColumn;
2326
import org.mybatis.dynamic.sql.render.RenderingStrategy;
27+
import org.mybatis.dynamic.sql.select.GroupByModel;
2428
import org.mybatis.dynamic.sql.select.OrderByModel;
2529
import org.mybatis.dynamic.sql.select.QueryExpressionModel;
2630
import org.mybatis.dynamic.sql.select.SelectModel;
@@ -45,6 +49,7 @@ public SelectStatementProvider render() {
4549
return SelectStatementProvider.withQueryExpression(collector.queryExpression())
4650
.withParameters(collector.parameters())
4751
.withOrderByClause(selectModel.orderByModel().map(this::renderOrderBy))
52+
.withGroupByClause(selectModel.groupByModel().map(this::renderGroupBy))
4853
.build();
4954
}
5055

@@ -61,6 +66,10 @@ private String renderOrderBy(OrderByModel orderByModel) {
6166
.collect(CustomCollectors.joining(", ", "order by ", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
6267
}
6368

69+
private String renderGroupBy(GroupByModel groupByModel) {
70+
return groupByModel.mapColumns(this::groupByPhrase).collect(CustomCollectors.joining(", ", "group by ", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
71+
}
72+
6473
private String orderByPhrase(SortSpecification column) {
6574
String phrase = column.aliasOrName();
6675
if (column.isDescending()) {
@@ -69,6 +78,10 @@ private String orderByPhrase(SortSpecification column) {
6978
return phrase;
7079
}
7180

81+
private String groupByPhrase(BasicColumn column) {
82+
return ((SqlColumn) column).aliasOrName();
83+
}
84+
7285
public static Builder withSelectModel(SelectModel selectModel) {
7386
return new Builder().withSelectModel(selectModel);
7487
}

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

Lines changed: 10 additions & 2 deletions
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.
@@ -28,10 +28,12 @@ public class SelectStatementProvider {
2828
private String queryExpression;
2929
private Map<String, Object> parameters;
3030
private Optional<String> orderByClause;
31+
private Optional<String> groupByClause;
3132

3233
private SelectStatementProvider(Builder builder) {
3334
queryExpression = Objects.requireNonNull(builder.queryExpression);
3435
orderByClause = Objects.requireNonNull(builder.orderByClause);
36+
groupByClause = Objects.requireNonNull(builder.groupByClause);
3537
parameters = Collections.unmodifiableMap(Objects.requireNonNull(builder.parameters));
3638
}
3739

@@ -40,7 +42,7 @@ public Map<String, Object> getParameters() {
4042
}
4143

4244
public String getSelectStatement() {
43-
return queryExpression + StringUtilities.spaceBefore(orderByClause);
45+
return queryExpression + StringUtilities.spaceBefore(orderByClause) + StringUtilities.spaceBefore(groupByClause);
4446
}
4547

4648
public static Builder withQueryExpression(String queryExpression) {
@@ -50,6 +52,7 @@ public static Builder withQueryExpression(String queryExpression) {
5052
public static class Builder {
5153
private String queryExpression;
5254
private Optional<String> orderByClause = Optional.empty();
55+
private Optional<String> groupByClause = Optional.empty();
5356
private Map<String, Object> parameters = new HashMap<>();
5457

5558
public Builder withQueryExpression(String queryExpression) {
@@ -62,6 +65,11 @@ public Builder withOrderByClause(Optional<String> orderByClause) {
6265
return this;
6366
}
6467

68+
public Builder withGroupByClause(Optional<String> groupByClause) {
69+
this.groupByClause = groupByClause;
70+
return this;
71+
}
72+
6573
public Builder withParameters(Map<String, Object> parameters) {
6674
this.parameters.putAll(parameters);
6775
return this;

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

Lines changed: 2 additions & 2 deletions
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.
@@ -252,7 +252,7 @@ public void testGroupBySingleColumn() {
252252
String expectedFullStatement = "select a.column1 as A_COLUMN1, a.column2 "
253253
+ "from foo a "
254254
+ "where a.column1 = #{parameters.p1,jdbcType=DATE} "
255-
+ "group by column2 ";
255+
+ "group by column2";
256256

257257
softly.assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedFullStatement);
258258

0 commit comments

Comments
 (0)