From 95bdee2eed5f3c44debc5e405cd44d9b5aa06d03 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 13 Feb 2024 10:58:15 +0100 Subject: [PATCH] Polishing. Remove unused parameter from DeclaredQuery. Reformat code. See: #3293 Original pull request: #3339 --- .../query/AbstractStringBasedJpaQuery.java | 17 +++++++++-------- .../jpa/repository/query/DeclaredQuery.java | 3 +-- .../repository/query/EmptyDeclaredQuery.java | 8 ++------ .../data/jpa/repository/query/NamedQuery.java | 6 +++--- .../data/jpa/repository/query/StringQuery.java | 10 ++-------- 5 files changed, 17 insertions(+), 27 deletions(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java index 135faddd8..92f2c17cf 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java @@ -17,6 +17,7 @@ package org.springframework.data.jpa.repository.query; import jakarta.persistence.EntityManager; import jakarta.persistence.Query; + import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.QueryRewriter; @@ -80,16 +81,17 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery { this.countQuery = Lazy.of(() -> { - if(StringUtils.hasText(countQueryString)) { + if (StringUtils.hasText(countQueryString)) { return new ExpressionBasedStringQuery(countQueryString, method.getEntityInformation(), parser, method.isNativeQuery()); } - return query.deriveCountQuery(null, method.getCountQueryProjection()); + + return query.deriveCountQuery(method.getCountQueryProjection()); }); this.countParameterBinder = Lazy.of(() -> { - return this.createCountBinder(this.countQuery.get()); + return this.createBinder(this.countQuery.get()); }); this.parser = parser; @@ -118,13 +120,12 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery { @Override protected ParameterBinder createBinder() { - - return ParameterBinderFactory.createQueryAwareBinder(getQueryMethod().getParameters(), query, parser, - evaluationContextProvider); + return createBinder(query); } - protected ParameterBinder createCountBinder(DeclaredQuery countQuery) { - return ParameterBinderFactory.createQueryAwareBinder(getQueryMethod().getParameters(), countQuery, parser, evaluationContextProvider); + protected ParameterBinder createBinder(DeclaredQuery query) { + return ParameterBinderFactory.createQueryAwareBinder(getQueryMethod().getParameters(), query, parser, + evaluationContextProvider); } @Override diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/DeclaredQuery.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/DeclaredQuery.java index dc5b1fb63..4e5442440 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/DeclaredQuery.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/DeclaredQuery.java @@ -80,11 +80,10 @@ interface DeclaredQuery { * expected from the original query, either derived from the query wrapped by this instance or from the information * passed as arguments. * - * @param countQuery an optional query string to be used if present. * @param countQueryProjection an optional return type for the query. * @return a new {@literal DeclaredQuery} instance. */ - DeclaredQuery deriveCountQuery(@Nullable String countQuery, @Nullable String countQueryProjection); + DeclaredQuery deriveCountQuery(@Nullable String countQueryProjection); /** * @return whether paging is implemented in the query itself, e.g. using SpEL expressions. diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EmptyDeclaredQuery.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EmptyDeclaredQuery.java index 804ea3391..67f9f9b3e 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EmptyDeclaredQuery.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EmptyDeclaredQuery.java @@ -19,7 +19,6 @@ import java.util.Collections; import java.util.List; import org.springframework.lang.Nullable; -import org.springframework.util.Assert; /** * NULL-Object pattern implementation for {@link DeclaredQuery}. @@ -65,11 +64,8 @@ class EmptyDeclaredQuery implements DeclaredQuery { } @Override - public DeclaredQuery deriveCountQuery(@Nullable String countQuery, @Nullable String countQueryProjection) { - - Assert.hasText(countQuery, "CountQuery must not be empty"); - - return DeclaredQuery.of(countQuery, false); + public DeclaredQuery deriveCountQuery(@Nullable String countQueryProjection) { + return EMPTY_QUERY; } @Override diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NamedQuery.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NamedQuery.java index 3decf6681..ea85d518d 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NamedQuery.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/NamedQuery.java @@ -98,7 +98,7 @@ final class NamedQuery extends AbstractJpaQuery { /** * Returns whether the named query with the given name exists. * - * @param em must not be {@literal null}. + * @param em must not be {@literal null}. * @param queryName must not be {@literal null}. */ static boolean hasNamedQuery(EntityManager em, String queryName) { @@ -127,7 +127,7 @@ final class NamedQuery extends AbstractJpaQuery { * Looks up a named query for the given {@link org.springframework.data.repository.query.QueryMethod}. * * @param method must not be {@literal null}. - * @param em must not be {@literal null}. + * @param em must not be {@literal null}. */ @Nullable public static RepositoryQuery lookupFrom(JpaQueryMethod method, EntityManager em) { @@ -190,7 +190,7 @@ final class NamedQuery extends AbstractJpaQuery { } else { - String countQueryString = declaredQuery.deriveCountQuery(null, countProjection).getQueryString(); + String countQueryString = declaredQuery.deriveCountQuery(countProjection).getQueryString(); cacheKey = countQueryString; countQuery = em.createQuery(countQueryString, Long.class); } diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java index bb7fb3c2a..5bea73422 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java @@ -72,7 +72,6 @@ class StringQuery implements DeclaredQuery { * * @param query must not be {@literal null} or empty. */ - @SuppressWarnings("deprecation") StringQuery(String query, boolean isNative) { Assert.hasText(query, "Query must not be null or empty"); @@ -109,16 +108,12 @@ class StringQuery implements DeclaredQuery { } @Override - public DeclaredQuery deriveCountQuery(@Nullable String countQuery, @Nullable String countQueryProjection) { - - if(StringUtils.hasText(countQuery)) { - return new StringQuery(countQuery, this.isNative); - } + public DeclaredQuery deriveCountQuery(@Nullable String countQueryProjection) { StringQuery stringQuery = new StringQuery(this.queryEnhancer.createCountQueryFor(countQueryProjection), // this.isNative); - if(this.hasParameterBindings() && !this.getParameterBindings().equals(stringQuery.getParameterBindings())) { + if (this.hasParameterBindings() && !this.getParameterBindings().equals(stringQuery.getParameterBindings())) { stringQuery.getParameterBindings().clear(); stringQuery.getParameterBindings().addAll(this.bindings); } @@ -289,7 +284,6 @@ class StringQuery implements DeclaredQuery { parameterIndex = expressionParameterIndex; } - BindingIdentifier queryParameter; if (parameterIndex != null) { queryParameter = BindingIdentifier.of(parameterIndex);