Skip to content

Commit dc3fb76

Browse files
authored
Merge pull request #409 from jeffgbutler/gh-408
Add missing groupBy and orderBy Methods
2 parents 3b8f288 + 295e6ab commit dc3fb76

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -251,10 +251,18 @@ public UnionBuilder unionAll() {
251251
}
252252

253253
public SelectDSL<R> orderBy(SortSpecification...columns) {
254+
return orderBy(Arrays.asList(columns));
255+
}
256+
257+
public SelectDSL<R> orderBy(Collection<SortSpecification> columns) {
254258
return QueryExpressionDSL.this.orderBy(columns);
255259
}
256260

257261
public GroupByFinisher groupBy(BasicColumn...columns) {
262+
return groupBy(Arrays.asList(columns));
263+
}
264+
265+
public GroupByFinisher groupBy(Collection<BasicColumn> columns) {
258266
return QueryExpressionDSL.this.groupBy(columns);
259267
}
260268

@@ -410,6 +418,10 @@ public JoinSpecificationStarter fullJoin(Buildable<SelectModel> joinTable, Strin
410418
}
411419

412420
public GroupByFinisher groupBy(BasicColumn...columns) {
421+
return groupBy(Arrays.asList(columns));
422+
}
423+
424+
public GroupByFinisher groupBy(Collection<BasicColumn> columns) {
413425
return QueryExpressionDSL.this.groupBy(columns);
414426
}
415427

@@ -422,6 +434,10 @@ public UnionBuilder unionAll() {
422434
}
423435

424436
public SelectDSL<R> orderBy(SortSpecification...columns) {
437+
return orderBy(Arrays.asList(columns));
438+
}
439+
440+
public SelectDSL<R> orderBy(Collection<SortSpecification> columns) {
425441
return QueryExpressionDSL.this.orderBy(columns);
426442
}
427443

@@ -440,6 +456,10 @@ public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
440456

441457
public class GroupByFinisher implements Buildable<R> {
442458
public SelectDSL<R> orderBy(SortSpecification...columns) {
459+
return orderBy(Arrays.asList(columns));
460+
}
461+
462+
public SelectDSL<R> orderBy(Collection<SortSpecification> columns) {
443463
return QueryExpressionDSL.this.orderBy(columns);
444464
}
445465

src/test/java/examples/simple/PersonMapperTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
2626
import org.junit.jupiter.api.BeforeEach;
2727
import org.junit.jupiter.api.Test;
28+
import org.mybatis.dynamic.sql.SortSpecification;
2829
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
2930
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
3031
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
@@ -38,6 +39,7 @@
3839
import java.sql.Connection;
3940
import java.sql.DriverManager;
4041
import java.util.ArrayList;
42+
import java.util.Collection;
4143
import java.util.Date;
4244
import java.util.List;
4345
import java.util.Objects;
@@ -251,6 +253,25 @@ void testFirstNameIn() {
251253
}
252254
}
253255

256+
@Test
257+
void testOrderByCollection() {
258+
Collection<SortSpecification> orderByColumns = new ArrayList<>();
259+
orderByColumns.add(firstName);
260+
261+
try (SqlSession session = sqlSessionFactory.openSession()) {
262+
PersonMapper mapper = session.getMapper(PersonMapper.class);
263+
264+
List<PersonRecord> rows = mapper.select(c -> c
265+
.where(firstName, isIn("Fred", "Barney"))
266+
.orderBy(orderByColumns)
267+
);
268+
269+
assertThat(rows).hasSize(2);
270+
assertThat(rows.get(0).getLastName().getName()).isEqualTo("Rubble");
271+
assertThat(rows.get(1).getLastName().getName()).isEqualTo("Flintstone");
272+
}
273+
}
274+
254275
@Test
255276
void testDelete() {
256277
try (SqlSession session = sqlSessionFactory.openSession()) {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.mybatis.dynamic.sql.SqlBuilder.*;
2222

2323
import java.sql.JDBCType;
24+
import java.util.ArrayList;
2425
import java.util.Collection;
2526
import java.util.Collections;
2627
import java.util.Date;
@@ -29,6 +30,7 @@
2930

3031
import org.junit.jupiter.api.Test;
3132
import org.mybatis.dynamic.sql.Callback;
33+
import org.mybatis.dynamic.sql.SortSpecification;
3234
import org.mybatis.dynamic.sql.SqlColumn;
3335
import org.mybatis.dynamic.sql.SqlTable;
3436
import org.mybatis.dynamic.sql.render.RenderingStrategies;
@@ -176,6 +178,25 @@ void testOrderByMultipleColumns() {
176178
);
177179
}
178180

181+
@Test
182+
void testOrderByMultipleColumnsWithCollection() {
183+
Collection<SortSpecification> orderByColumns = new ArrayList<>();
184+
orderByColumns.add(column2.descending());
185+
orderByColumns.add(column1);
186+
187+
SelectStatementProvider selectStatement = select(column1.as("A_COLUMN1"), column2)
188+
.from(table, "a")
189+
.orderBy(orderByColumns)
190+
.build()
191+
.render(RenderingStrategies.MYBATIS3);
192+
193+
String expectedFullStatement = "select a.column1 as A_COLUMN1, a.column2 "
194+
+ "from foo a "
195+
+ "order by column2 DESC, column1";
196+
197+
assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedFullStatement);
198+
}
199+
179200
@Test
180201
void testDistinct() {
181202
Date d = new Date();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy