-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Differentiate between JPQL and native queries in count query derivation #2777
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,10 +18,23 @@ | |
import static jakarta.persistence.metamodel.Attribute.PersistentAttributeType.*; | ||
import static java.util.regex.Pattern.*; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.persistence.criteria.*; | ||
import jakarta.persistence.metamodel.*; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.OneToOne; | ||
import jakarta.persistence.Parameter; | ||
import jakarta.persistence.Query; | ||
import jakarta.persistence.criteria.CriteriaBuilder; | ||
import jakarta.persistence.criteria.Expression; | ||
import jakarta.persistence.criteria.Fetch; | ||
import jakarta.persistence.criteria.From; | ||
import jakarta.persistence.criteria.Join; | ||
import jakarta.persistence.criteria.JoinType; | ||
import jakarta.persistence.metamodel.Attribute; | ||
import jakarta.persistence.metamodel.Attribute.PersistentAttributeType; | ||
import jakarta.persistence.metamodel.Bindable; | ||
import jakarta.persistence.metamodel.ManagedType; | ||
import jakarta.persistence.metamodel.PluralAttribute; | ||
import jakarta.persistence.metamodel.SingularAttribute; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.AnnotatedElement; | ||
|
@@ -570,6 +583,19 @@ public static String createCountQueryFor(String originalQuery) { | |
*/ | ||
@Deprecated | ||
public static String createCountQueryFor(String originalQuery, @Nullable String countProjection) { | ||
return createCountQueryFor(originalQuery, countProjection, false); | ||
} | ||
|
||
/** | ||
* Creates a count projected query from the given original query. | ||
* | ||
* @param originalQuery must not be {@literal null}. | ||
* @param countProjection may be {@literal null}. | ||
* @param nativeQuery whether the underlying query is a native query. | ||
* @return a query String to be used a count query for pagination. Guaranteed to be not {@literal null}. | ||
* @since 2.7.8 | ||
*/ | ||
static String createCountQueryFor(String originalQuery, @Nullable String countProjection, boolean nativeQuery) { | ||
|
||
Assert.hasText(originalQuery, "OriginalQuery must not be null or empty"); | ||
|
||
|
@@ -591,9 +617,14 @@ public static String createCountQueryFor(String originalQuery, @Nullable String | |
|
||
String replacement = useVariable ? SIMPLE_COUNT_VALUE : complexCountValue; | ||
|
||
String alias = QueryUtils.detectAlias(originalQuery); | ||
if ("*".equals(variable) && alias != null) { | ||
replacement = alias; | ||
if (nativeQuery && (variable.contains(",") || "*".equals(variable))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if it is a native query and the variable is null 😬 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you run into a bug or you can reproduce such a case, please file a bug report. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. already present #2812 |
||
replacement = "1"; | ||
} else { | ||
|
||
String alias = QueryUtils.detectAlias(originalQuery); | ||
if (("*".equals(variable) && alias != null)) { | ||
replacement = alias; | ||
} | ||
} | ||
|
||
countQuery = matcher.replaceFirst(String.format(COUNT_REPLACEMENT_TEMPLATE, replacement)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2023 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.jpa.repository.query; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* TCK Tests for {@link DefaultQueryEnhancer}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
public class DefaultQueryEnhancerUnitTests extends QueryEnhancerTckTests { | ||
|
||
@Override | ||
QueryEnhancer createQueryEnhancer(DeclaredQuery declaredQuery) { | ||
return new DefaultQueryEnhancer(declaredQuery); | ||
} | ||
|
||
@Override | ||
@Test // GH-2511, GH-2773 | ||
@Disabled("Not properly supported by QueryUtils") | ||
void shouldDeriveNativeCountQueryWithVariable(String query, String expected) {} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer two separate methods
createCountQueryForNative
andcreateCountQueryForJpql
plus extracting the common parts into a new method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My gut feeling is that we should rethink
QueryUtils
entirely and eventually eliminate the most methods (or create delegates). Therefore, I would want to refrain from introducing additional methods for the time being unless new methods heavily improve the code.