Skip to content

Add missing groupBy and orderBy Methods #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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