Skip to content
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

Remove alias of sort key when PagingQueryProvider has sort key with alias and group by clause. #4743

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 2006-2024 the original author or authors.
* Copyright 2006-2025 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 @@ -32,6 +32,7 @@
* @author Michael Minella
* @author Mahmoud Ben Hassine
* @author Taeik Lim
* @author Kyeonghoon Lee
* @since 2.0
*/
public abstract class SqlPagingQueryUtils {
Expand Down Expand Up @@ -108,8 +109,8 @@ public static String generateLimitGroupedSqlQuery(AbstractSqlPagingQueryProvider
buildGroupByClause(provider, sql);
sql.append(") AS MAIN_QRY ");
sql.append("WHERE ");
buildSortConditions(provider, sql);
sql.append(" ORDER BY ").append(buildSortClause(provider));
buildSortConditionsWithoutTableAliases(provider, sql);
sql.append(" ORDER BY ").append(buildSortClause(provider.getSortKeysWithoutAliases()));
sql.append(" ").append(limitClause);

return sql.toString();
Expand Down Expand Up @@ -233,7 +234,7 @@ public static String generateRowNumSqlQuery(AbstractSqlPagingQueryProvider provi

/**
* Generates ORDER BY attributes based on the sort keys.
* @param provider the {@link AbstractSqlPagingQueryProvider} to be used for used for
* @param provider the {@link AbstractSqlPagingQueryProvider} to be used for
* pagination.
* @return a String that can be appended to an ORDER BY clause.
*/
Expand Down Expand Up @@ -269,6 +270,19 @@ public static String buildSortClause(Map<String, Order> sortKeys) {
return builder.toString();
}

/**
* Appends the where conditions required to query for the subsequent pages, without
* using table aliases in sort keys.
* @param provider the {@link AbstractSqlPagingQueryProvider} to be used for
* pagination.
* @param sql {@link StringBuilder} containing the sql to be used for the query.
*/
public static void buildSortConditionsWithoutTableAliases(AbstractSqlPagingQueryProvider provider,
StringBuilder sql) {
List<Map.Entry<String, Order>> keys = new ArrayList<>(provider.getSortKeysWithoutAliases().entrySet());
buildDetailSortConditions(keys, provider, sql);
}

/**
* Appends the where conditions required to query for the subsequent pages.
* @param provider the {@link AbstractSqlPagingQueryProvider} to be used for
Expand All @@ -277,6 +291,11 @@ public static String buildSortClause(Map<String, Order> sortKeys) {
*/
public static void buildSortConditions(AbstractSqlPagingQueryProvider provider, StringBuilder sql) {
List<Map.Entry<String, Order>> keys = new ArrayList<>(provider.getSortKeys().entrySet());
buildDetailSortConditions(keys, provider, sql);
}

private static void buildDetailSortConditions(List<Map.Entry<String, Order>> keys,
AbstractSqlPagingQueryProvider provider, StringBuilder sql) {
List<String> clauses = new ArrayList<>();

for (int i = 0; i < keys.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2025 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 All @@ -17,6 +17,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.LinkedHashMap;

import org.junit.jupiter.api.Test;

Expand All @@ -27,6 +28,7 @@
/**
* @author Thomas Risberg
* @author Michael Minella
* @author Kyeonghoon Lee
*/
class MySqlPagingQueryProviderTests extends AbstractSqlPagingQueryProviderTests {

Expand Down Expand Up @@ -68,6 +70,22 @@ void testGenerateRemainingPagesQueryWithGroupBy() {
assertEquals(sql, s);
}

@Test
void testGenerateRemainingPagesQueryWithGroupByWithAlias() {
pagingQueryProvider.setSelectClause("SELECT f.id, f.name, f.age");
pagingQueryProvider.setFromClause("FROM foo f");
pagingQueryProvider.setWhereClause("f.bar = 1");
pagingQueryProvider.setGroupClause("dep");
Map<String, Order> sortKeys = new LinkedHashMap<>();
sortKeys.put("f.id", Order.ASCENDING);
pagingQueryProvider.setSortKeys(sortKeys);

String sql = "SELECT * FROM (SELECT f.id, f.name, f.age FROM foo f WHERE f.bar = 1 GROUP BY dep) AS MAIN_QRY WHERE ((id > ?)) ORDER BY id ASC LIMIT "
+ pageSize;
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals(sql, s);
}

@Test
void testFirstPageSqlWithAliases() {
Map<String, Order> sorts = new HashMap<>();
Expand Down