Fix order by clauses for functions with positional and named arguments.

Related: #2045.

Closes #425.
This commit is contained in:
Greg L. Turnquist
2022-05-19 17:04:30 -05:00
parent a85284880b
commit b2f891f53c
3 changed files with 28 additions and 2 deletions

View File

@@ -77,6 +77,7 @@ import org.springframework.util.StringUtils;
* @author Diego Krupitza
* @author Jędrzej Biedrzycki
* @author Darin Manica
* @author Simon Paradies
*/
public abstract class QueryUtils {
@@ -182,7 +183,7 @@ public abstract class QueryUtils {
builder = new StringBuilder();
// any function call including parameters within the brackets
builder.append("\\w+\\s*\\([\\w\\.,\\s'=]+\\)");
builder.append("\\w+\\s*\\([\\w\\.,\\s'=:\\\\?]+\\)");
// the potential alias
builder.append("\\s+[as|AS]+\\s+(([\\w\\.]+))");

View File

@@ -94,7 +94,10 @@ import org.springframework.transaction.annotation.Transactional;
* @author Andrey Kovalev
* @author Sander Krabbenborg
* @author Jesse Wouters
* @author Greg Turnquist
* @author Greg Turnquist <<<<<<< HEAD =======
* @author Diego Krupitza
* @author Daniel Shuy
* @author Simon Paradies >>>>>>> 0cef764d (Fix order by clauses for functions with positional and named arguments.)
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:application-context.xml")
@@ -2656,6 +2659,16 @@ public class UserRepositoryTests {
assertThat(repository.findAllInterfaceProjectedBy()).hasSize(4);
}
@Test // GH-2045, GH-425
public void correctlyBuildSortClauseWhenSortingByFunctionAliasAndFunctionContainsPositionalParameters() {
repository.findAllAndSortByFunctionResultPositionalParameter("prefix", "suffix", Sort.by("idWithPrefixAndSuffix"));
}
@Test // GH-2045, GH-425
public void correctlyBuildSortClauseWhenSortingByFunctionAliasAndFunctionContainsNamedParameters() {
repository.findAllAndSortByFunctionResultNamedParameter("prefix", "suffix", Sort.by("idWithPrefixAndSuffix"));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -54,6 +54,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Andrey Kovalev
* @author JyotirmoyVS
* @author Greg Turnquist
* @author Simon Paradies
*/
public interface UserRepository
extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User>, UserRepositoryCustom {
@@ -625,6 +626,17 @@ public interface UserRepository
// GH-2408
List<NameOnly> findAllInterfaceProjectedBy();
// GH-2045, GH-425
@Query("select concat(?1,u.id,?2) as idWithPrefixAndSuffix from #{#entityName} u")
List<String> findAllAndSortByFunctionResultPositionalParameter(
@Param("positionalParameter1") String positionalParameter1,
@Param("positionalParameter2") String positionalParameter2, Sort sort);
// GH-2045, GH-425
@Query("select concat(:namedParameter1,u.id,:namedParameter2) as idWithPrefixAndSuffix from #{#entityName} u")
List<String> findAllAndSortByFunctionResultNamedParameter(@Param("namedParameter1") String namedParameter1,
@Param("namedParameter2") String namedParameter2, Sort sort);
interface RolesAndFirstname {
String getFirstname();