Skip to content

Commit aa903aa

Browse files
committed
Rework the SortSpecification to avoid some weirdness in the DSL
When a column is renamed, or when using a calculated column or an aggregate, it makes much more syntactic sense to just reuse the name rather that using a fake column with an alias.
1 parent 9526c4e commit aa903aa

File tree

7 files changed

+76
-25
lines changed

7 files changed

+76
-25
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*/
2424
public interface SortSpecification {
25-
SortSpecification as(String alias);
25+
SortSpecification descending();
2626
String aliasOrName();
2727
boolean isDescending();
2828
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
2626
import org.mybatis.dynamic.sql.select.SelectDSL;
2727
import org.mybatis.dynamic.sql.select.SelectModel;
28+
import org.mybatis.dynamic.sql.select.SimpleSortSpecification;
2829
import org.mybatis.dynamic.sql.select.aggregate.Avg;
2930
import org.mybatis.dynamic.sql.select.aggregate.Count;
3031
import org.mybatis.dynamic.sql.select.aggregate.CountAll;
@@ -310,4 +311,9 @@ static IsInCaseInsensitive isInCaseInsensitive(String...values) {
310311
static IsNotInCaseInsensitive isNotInCaseInsensitive(String...values) {
311312
return IsNotInCaseInsensitive.of(Arrays.asList(values));
312313
}
314+
315+
// order by support
316+
static SortSpecification sortColumn(String name) {
317+
return SimpleSortSpecification.of(name);
318+
}
313319
}

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

+4-16
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,11 @@ public Optional<String> typeHandler() {
6565
return typeHandler;
6666
}
6767

68+
@Override
6869
public SortSpecification descending() {
69-
return new SortSpecification() {
70-
@Override
71-
public SortSpecification as(String alias) {
72-
return SqlColumn.this.as(alias);
73-
}
74-
75-
@Override
76-
public boolean isDescending() {
77-
return true;
78-
}
79-
80-
@Override
81-
public String aliasOrName() {
82-
return SqlColumn.this.aliasOrName();
83-
}
84-
};
70+
SqlColumn<T> column = new SqlColumn<>(this);
71+
column.isDescending = true;
72+
return column;
8573
}
8674

8775
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2016-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select;
17+
18+
import java.util.Objects;
19+
20+
import org.mybatis.dynamic.sql.SortSpecification;
21+
22+
/**
23+
* This class is used for an order by phrase where there is no suitable column name
24+
* to use (for example a calculated column or an aggregate column).
25+
*
26+
* @author Jeff Butler
27+
*/
28+
public class SimpleSortSpecification implements SortSpecification {
29+
30+
private String name;
31+
private boolean isDescending;
32+
33+
private SimpleSortSpecification(String name) {
34+
this.name = Objects.requireNonNull(name);
35+
}
36+
37+
@Override
38+
public SortSpecification descending() {
39+
SimpleSortSpecification answer = new SimpleSortSpecification(name);
40+
answer.isDescending = true;
41+
return answer;
42+
}
43+
44+
@Override
45+
public String aliasOrName() {
46+
return name;
47+
}
48+
49+
@Override
50+
public boolean isDescending() {
51+
return isDescending;
52+
}
53+
54+
public static SimpleSortSpecification of(String name) {
55+
return new SimpleSortSpecification(name);
56+
}
57+
}

src/test/java/examples/animal/data/AnimalDataTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public void testUnionSelectWithTableAndColumnAliases() {
291291
.select(id.as("animalId"), animalName, bodyWeight, brainWeight)
292292
.from(animalData, "b")
293293
.where(id, isGreaterThan(40))
294-
.orderBy(id.as("animalId"))
294+
.orderBy(sortColumn("animalId"))
295295
.build()
296296
.render(RenderingStrategy.MYBATIS3);
297297

src/test/java/examples/groupby/GroupByTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,22 @@ public void testBasicGroupByOrderByWithCalculatedColumnAndTableAlias() {
163163
SelectStatement selectStatement = select(substring(gender, 1, 1).as("ShortGender"), avg(age).as("AverageAge"))
164164
.from(person, "a")
165165
.groupBy(substring(gender, 1, 1))
166-
.orderBy(gender.as("ShortGender"))
166+
.orderBy(sortColumn("ShortGender").descending())
167167
.build()
168168
.render(RenderingStrategy.MYBATIS3);
169169

170-
String expected = "select substring(a.gender, 1, 1) as ShortGender, avg(a.age) as AverageAge from Person a group by substring(a.gender, 1, 1) order by ShortGender";
170+
String expected = "select substring(a.gender, 1, 1) as ShortGender, avg(a.age) as AverageAge from Person a group by substring(a.gender, 1, 1) order by ShortGender DESC";
171171
assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
172172

173173
List<Map<String, Object>> rows = mapper.generalSelect(selectStatement);
174174
assertThat(rows.size()).isEqualTo(2);
175175
Map<String, Object> row = rows.get(0);
176-
assertThat(row.get("SHORTGENDER")).isEqualTo("F");
177-
assertThat(row.get("AVERAGEAGE")).isEqualTo(27);
178-
179-
row = rows.get(1);
180176
assertThat(row.get("SHORTGENDER")).isEqualTo("M");
181177
assertThat(row.get("AVERAGEAGE")).isEqualTo(25);
178+
179+
row = rows.get(1);
180+
assertThat(row.get("SHORTGENDER")).isEqualTo("F");
181+
assertThat(row.get("AVERAGEAGE")).isEqualTo(27);
182182
} finally {
183183
session.close();
184184
}

src/test/java/examples/joins/JoinMapperTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public void testFullJoin() {
439439
SelectStatement selectStatement = select(orderLine.orderId, orderLine.quantity, orderLine.itemId.as("ol_itemid"), itemMaster.itemId.as("im_itemid"), itemMaster.description)
440440
.from(itemMaster, "im")
441441
.fullJoin(orderLine, "ol").on(itemMaster.itemId, equalTo(orderLine.itemId))
442-
.orderBy(itemMaster.itemId.as("im_itemid"))
442+
.orderBy(sortColumn("im_itemid"))
443443
.build()
444444
.render(RenderingStrategy.MYBATIS3);
445445

0 commit comments

Comments
 (0)