Skip to content

Commit

Permalink
Merge pull request #409 from jeffgbutler/gh-408
Browse files Browse the repository at this point in the history
Add missing groupBy and orderBy Methods
  • Loading branch information
jeffgbutler committed Nov 2, 2021
2 parents 3b8f288 + 295e6ab commit dc3fb76
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -251,10 +251,18 @@ public UnionBuilder unionAll() {
}

public SelectDSL<R> orderBy(SortSpecification...columns) {
return orderBy(Arrays.asList(columns));
}

public SelectDSL<R> orderBy(Collection<SortSpecification> columns) {
return QueryExpressionDSL.this.orderBy(columns);
}

public GroupByFinisher groupBy(BasicColumn...columns) {
return groupBy(Arrays.asList(columns));
}

public GroupByFinisher groupBy(Collection<BasicColumn> columns) {
return QueryExpressionDSL.this.groupBy(columns);
}

Expand Down Expand Up @@ -410,6 +418,10 @@ public JoinSpecificationStarter fullJoin(Buildable<SelectModel> joinTable, Strin
}

public GroupByFinisher groupBy(BasicColumn...columns) {
return groupBy(Arrays.asList(columns));
}

public GroupByFinisher groupBy(Collection<BasicColumn> columns) {
return QueryExpressionDSL.this.groupBy(columns);
}

Expand All @@ -422,6 +434,10 @@ public UnionBuilder unionAll() {
}

public SelectDSL<R> orderBy(SortSpecification...columns) {
return orderBy(Arrays.asList(columns));
}

public SelectDSL<R> orderBy(Collection<SortSpecification> columns) {
return QueryExpressionDSL.this.orderBy(columns);
}

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

public class GroupByFinisher implements Buildable<R> {
public SelectDSL<R> orderBy(SortSpecification...columns) {
return orderBy(Arrays.asList(columns));
}

public SelectDSL<R> orderBy(Collection<SortSpecification> columns) {
return QueryExpressionDSL.this.orderBy(columns);
}

Expand Down
21 changes: 21 additions & 0 deletions src/test/java/examples/simple/PersonMapperTest.java
Expand Up @@ -25,6 +25,7 @@
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mybatis.dynamic.sql.SortSpecification;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
Expand All @@ -38,6 +39,7 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -251,6 +253,25 @@ void testFirstNameIn() {
}
}

@Test
void testOrderByCollection() {
Collection<SortSpecification> orderByColumns = new ArrayList<>();
orderByColumns.add(firstName);

try (SqlSession session = sqlSessionFactory.openSession()) {
PersonMapper mapper = session.getMapper(PersonMapper.class);

List<PersonRecord> rows = mapper.select(c -> c
.where(firstName, isIn("Fred", "Barney"))
.orderBy(orderByColumns)
);

assertThat(rows).hasSize(2);
assertThat(rows.get(0).getLastName().getName()).isEqualTo("Rubble");
assertThat(rows.get(1).getLastName().getName()).isEqualTo("Flintstone");
}
}

@Test
void testDelete() {
try (SqlSession session = sqlSessionFactory.openSession()) {
Expand Down
Expand Up @@ -21,6 +21,7 @@
import static org.mybatis.dynamic.sql.SqlBuilder.*;

import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
Expand All @@ -29,6 +30,7 @@

import org.junit.jupiter.api.Test;
import org.mybatis.dynamic.sql.Callback;
import org.mybatis.dynamic.sql.SortSpecification;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
Expand Down Expand Up @@ -176,6 +178,25 @@ void testOrderByMultipleColumns() {
);
}

@Test
void testOrderByMultipleColumnsWithCollection() {
Collection<SortSpecification> orderByColumns = new ArrayList<>();
orderByColumns.add(column2.descending());
orderByColumns.add(column1);

SelectStatementProvider selectStatement = select(column1.as("A_COLUMN1"), column2)
.from(table, "a")
.orderBy(orderByColumns)
.build()
.render(RenderingStrategies.MYBATIS3);

String expectedFullStatement = "select a.column1 as A_COLUMN1, a.column2 "
+ "from foo a "
+ "order by column2 DESC, column1";

assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedFullStatement);
}

@Test
void testDistinct() {
Date d = new Date();
Expand Down

0 comments on commit dc3fb76

Please sign in to comment.