Polishing.

Remove unused parameter from DeclaredQuery. Reformat code.

See: #3293
Original pull request: #3339
This commit is contained in:
Mark Paluch
2024-02-13 10:58:15 +01:00
parent 93385c793c
commit 764a3ed3d5
6 changed files with 20 additions and 29 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -446,8 +446,9 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
@Query("select u from User u where u.firstname = ?#{[0]} and u.firstname = ?1 and u.lastname like %?#{[1]}% and u.lastname like %?2%")
List<User> findByFirstnameAndLastnameWithSpelExpression(String firstname, String lastname);
@Query(value = "select * from SD_User", countQuery = "select count(1) from SD_User u where u.lastname = :#{#lastname}", nativeQuery = true)
Page<User> findByWithSpelParameterOnlyUsedForCountQuery(String lastname, Pageable page);
@Query(value = "select * from SD_User",
countQuery = "select count(1) from SD_User u where u.lastname = :#{#lastname}", nativeQuery = true)
Page<User> findByWithSpelParameterOnlyUsedForCountQuery(@Param("lastname") String lastname, Pageable page);
// DATAJPA-564
@Query("select u from User u where u.lastname like %:#{[0]}% and u.lastname like %:lastname%")