Skip to content

Commit debdeba

Browse files
committed
Post-Rebase
1 parent 0ee6c66 commit debdeba

34 files changed

+324
-506
lines changed

spring-data-jpa/src/jmh/java/org/springframework/data/jpa/repository/query/HqlParserBenchmarks.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ OR TREAT(p AS SmallProject).name LIKE 'Persist%'
5555
OR p.description LIKE "cost overrun"
5656
""";
5757

58-
query = DeclaredQuery.of(s, false);
59-
enhancer = QueryEnhancerFactory.forQuery(query);
58+
query = DeclaredQuery.ofJpql(s);
59+
enhancer = QueryEnhancerFactory.forQuery(query).create(query);
6060
}
6161
}
6262

spring-data-jpa/src/jmh/java/org/springframework/data/jpa/repository/query/JSqlParserQueryEnhancerBenchmarks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void doSetup() throws IOException {
5656
select SOME_COLUMN from SOME_OTHER_TABLE where REPORTING_DATE = :REPORTING_DATE
5757
union select SOME_COLUMN from SOME_OTHER_OTHER_TABLE""";
5858

59-
enhancer = new JSqlParserQueryEnhancer(DeclaredQuery.of(s, true));
59+
enhancer = new JSqlParserQueryEnhancer(DeclaredQuery.ofNative(s));
6060
}
6161
}
6262

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/AbstractStringBasedJpaQuery.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
*/
4949
abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
5050

51-
private final IntrospectedQuery query;
51+
private final EntityQuery query;
5252
private final Lazy<IntrospectedQuery> countQuery;
5353
private final ValueExpressionDelegate valueExpressionDelegate;
5454
private final QueryParameterSetter.QueryMetadataCache metadataCache = new QueryParameterSetter.QueryMetadataCache();
@@ -65,33 +65,27 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
6565
* @param em must not be {@literal null}.
6666
* @param queryString must not be {@literal null}.
6767
* @param countQueryString must not be {@literal null}.
68-
* @param queryRewriter must not be {@literal null}.
69-
* @param valueExpressionDelegate must not be {@literal null}.
7068
* @param queryConfiguration must not be {@literal null}.
7169
*/
7270
public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, String queryString,
73-
@Nullable String countQueryString, QueryRewriter queryRewriter,
74-
ValueExpressionDelegate valueExpressionDelegate, JpaQueryConfiguration queryConfiguration) {
71+
@Nullable String countQueryString, JpaQueryConfiguration queryConfiguration) {
7572

7673
super(method, em);
7774

7875
Assert.hasText(queryString, "Query string must not be null or empty");
79-
Assert.notNull(valueExpressionDelegate, "ValueExpressionDelegate must not be null");
80-
Assert.notNull(queryRewriter, "QueryRewriter must not be null");
76+
Assert.notNull(queryConfiguration, "JpaQueryConfiguration must not be null");
8177

82-
this.valueExpressionDelegate = valueExpressionDelegate;
78+
this.valueExpressionDelegate = queryConfiguration.getValueExpressionDelegate();
8379
this.valueExpressionContextProvider = valueExpressionDelegate.createValueContextProvider(method.getParameters());
8480
this.query = ExpressionBasedStringQuery.create(queryString, method, queryConfiguration);
8581

8682
this.countQuery = Lazy.of(() -> {
8783

8884
if (StringUtils.hasText(countQueryString)) {
89-
90-
return ExpressionBasedStringQuery.create(countQueryString, method, queryConfiguration);
91-
85+
return ExpressionBasedStringQuery.create(countQueryString, method, queryConfiguration);
9286
}
9387

94-
return query.deriveCountQuery(method.getCountQueryProjection());
88+
return this.query.deriveCountQuery(method.getCountQueryProjection());
9589
});
9690

9791
this.countParameterBinder = Lazy.of(() -> {
@@ -107,7 +101,7 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
107101
this.querySortRewriter = NoOpQuerySortRewriter.INSTANCE;
108102
}
109103

110-
Assert.isTrue(method.isNativeQuery() || !query.usesJdbcStyleParameters(),
104+
Assert.isTrue(method.isNativeQuery() || !this.query.usesJdbcStyleParameters(),
111105
"JDBC style parameters (?) are not supported for JPA queries");
112106
}
113107

@@ -162,7 +156,7 @@ protected Query doCreateCountQuery(JpaParametersParameterAccessor accessor) {
162156
/**
163157
* @return the query
164158
*/
165-
public IntrospectedQuery getQuery() {
159+
public EntityQuery getQuery() {
166160
return query;
167161
}
168162

@@ -210,16 +204,14 @@ protected String potentiallyRewriteQuery(String originalQuery, Sort sort, @Nulla
210204
}
211205

212206
String applySorting(CachableQuery cachableQuery) {
213-
214-
return QueryEnhancerFactory.forQuery(cachableQuery.getDeclaredQuery()).applySorting(cachableQuery.getSort(),
215-
cachableQuery.getAlias());
207+
return cachableQuery.getDeclaredQuery().applySorting(cachableQuery.getSort());
216208
}
217209

218210
/**
219211
* Query Sort Rewriter interface.
220212
*/
221213
interface QuerySortRewriter {
222-
String getSorted(IntrospectedQuery query, Sort sort);
214+
String getSorted(EntityQuery query, Sort sort);
223215
}
224216

225217
/**
@@ -228,7 +220,7 @@ interface QuerySortRewriter {
228220
enum NoOpQuerySortRewriter implements QuerySortRewriter {
229221
INSTANCE;
230222

231-
public String getSorted(IntrospectedQuery query, Sort sort) {
223+
public String getSorted(EntityQuery query, Sort sort) {
232224

233225
if (sort.isSorted()) {
234226
throw new UnsupportedOperationException("NoOpQueryCache does not support sorting");
@@ -247,7 +239,7 @@ class CachingQuerySortRewriter implements QuerySortRewriter {
247239
AbstractStringBasedJpaQuery.this::applySorting);
248240

249241
@Override
250-
public String getSorted(IntrospectedQuery query, Sort sort) {
242+
public String getSorted(EntityQuery query, Sort sort) {
251243

252244
if (sort.isUnsorted()) {
253245
return query.getQueryString();
@@ -266,30 +258,25 @@ public String getSorted(IntrospectedQuery query, Sort sort) {
266258
*/
267259
static class CachableQuery {
268260

269-
private final IntrospectedQuery introspectedQuery;
261+
private final EntityQuery introspectedQuery;
270262
private final String queryString;
271263
private final Sort sort;
272264

273-
CachableQuery(IntrospectedQuery query, Sort sort) {
265+
CachableQuery(EntityQuery query, Sort sort) {
274266

275267
this.introspectedQuery = query;
276268
this.queryString = query.getQueryString();
277269
this.sort = sort;
278270
}
279271

280-
IntrospectedQuery getDeclaredQuery() {
272+
EntityQuery getDeclaredQuery() {
281273
return introspectedQuery;
282274
}
283275

284276
Sort getSort() {
285277
return sort;
286278
}
287279

288-
@Nullable
289-
String getAlias() {
290-
return introspectedQuery.getAlias();
291-
}
292-
293280
@Override
294281
public boolean equals(Object o) {
295282

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/DeclaredQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public interface DeclaredQuery {
2828
* @param query the JPQL query string.
2929
* @return
3030
*/
31-
static DeclaredQuery jpql(String query) {
31+
static DeclaredQuery ofJpql(String query) {
3232
return new DefaultDeclaredQuery(query, false);
3333
}
3434

@@ -38,7 +38,7 @@ static DeclaredQuery jpql(String query) {
3838
* @param query the native query string.
3939
* @return
4040
*/
41-
static DeclaredQuery nativeQuery(String query) {
41+
static DeclaredQuery ofNative(String query) {
4242
return new DefaultDeclaredQuery(query, true);
4343
}
4444

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/DefaultDeclaredQuery.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ class DefaultDeclaredQuery implements DeclaredQuery {
3030
this.nativeQuery = nativeQuery;
3131
}
3232

33+
@Override
3334
public String getQueryString() {
3435
return query;
3536
}
3637

38+
@Override
3739
public boolean isNativeQuery() {
3840
return nativeQuery;
3941
}

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/EmptyIntrospectedQuery.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.List;
2020

21+
import org.springframework.data.domain.Sort;
2122
import org.springframework.lang.Nullable;
2223

2324
/**
@@ -26,12 +27,12 @@
2627
* @author Jens Schauder
2728
* @since 2.0.3
2829
*/
29-
class EmptyIntrospectedQuery implements IntrospectedQuery {
30+
class EmptyIntrospectedQuery implements EntityQuery {
3031

3132
/**
3233
* An implementation implementing the NULL-Object pattern for situations where there is no query.
3334
*/
34-
static final IntrospectedQuery EMPTY_QUERY = new EmptyIntrospectedQuery();
35+
static final EntityQuery EMPTY_QUERY = new EmptyIntrospectedQuery();
3536

3637
@Override
3738
public boolean hasNamedParameter() {
@@ -48,11 +49,6 @@ public boolean isNativeQuery() {
4849
return false;
4950
}
5051

51-
@Override
52-
public String getAlias() {
53-
return null;
54-
}
55-
5652
@Override
5753
public boolean hasConstructorExpression() {
5854
return false;
@@ -73,6 +69,11 @@ public IntrospectedQuery deriveCountQuery(@Nullable String countQueryProjection)
7369
return EMPTY_QUERY;
7470
}
7571

72+
@Override
73+
public String applySorting(Sort sort) {
74+
return "";
75+
}
76+
7677
@Override
7778
public boolean usesJdbcStyleParameters() {
7879
return false;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2018-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.repository.query;
17+
18+
import org.springframework.data.domain.Sort;
19+
import org.springframework.lang.Nullable;
20+
import org.springframework.util.ObjectUtils;
21+
22+
/**
23+
* A wrapper for a String representation of a query offering information about the query.
24+
*
25+
* @author Jens Schauder
26+
* @author Diego Krupitza
27+
* @since 2.0.3
28+
*/
29+
interface EntityQuery extends IntrospectedQuery {
30+
31+
/**
32+
* Creates a DeclaredQuery for a JPQL query.
33+
*
34+
* @param query the JPQL query string.
35+
* @return
36+
*/
37+
static EntityQuery introspectJpql(String query, QueryEnhancerFactory queryEnhancer) {
38+
return ObjectUtils.isEmpty(query) ? EmptyIntrospectedQuery.EMPTY_QUERY
39+
: new StringQuery(query, false, queryEnhancer);
40+
}
41+
42+
/**
43+
* Creates a DeclaredQuery for a JPQL query.
44+
*
45+
* @param query the JPQL query string.
46+
* @return
47+
*/
48+
static EntityQuery introspectJpql(String query, QueryEnhancerSelector selector) {
49+
return ObjectUtils.isEmpty(query) ? EmptyIntrospectedQuery.EMPTY_QUERY : new StringQuery(query, false, selector);
50+
}
51+
52+
/**
53+
* Creates a DeclaredQuery for a native query.
54+
*
55+
* @param query the native query string.
56+
* @return
57+
*/
58+
static EntityQuery introspectNativeQuery(String query, QueryEnhancerFactory queryEnhancer) {
59+
return ObjectUtils.isEmpty(query) ? EmptyIntrospectedQuery.EMPTY_QUERY
60+
: new StringQuery(query, true, queryEnhancer);
61+
}
62+
63+
/**
64+
* Creates a DeclaredQuery for a native query.
65+
*
66+
* @param query the native query string.
67+
* @return
68+
*/
69+
static EntityQuery introspectNativeQuery(String query, QueryEnhancerSelector selector) {
70+
return ObjectUtils.isEmpty(query) ? EmptyIntrospectedQuery.EMPTY_QUERY : new StringQuery(query, true, selector);
71+
}
72+
73+
/**
74+
* Returns whether the query is using a constructor expression.
75+
*
76+
* @since 1.10
77+
*/
78+
boolean hasConstructorExpression();
79+
80+
/**
81+
* Returns whether the query uses the default projection, i.e. returns the main alias defined for the query.
82+
*/
83+
boolean isDefaultProjection();
84+
85+
/**
86+
* Creates a new {@literal DeclaredQuery} representing a count query, i.e. a query returning the number of rows to be
87+
* expected from the original query, either derived from the query wrapped by this instance or from the information
88+
* passed as arguments.
89+
*
90+
* @param countQueryProjection an optional return type for the query.
91+
* @return a new {@literal DeclaredQuery} instance.
92+
*/
93+
IntrospectedQuery deriveCountQuery(@Nullable String countQueryProjection);
94+
95+
String applySorting(Sort sort);
96+
97+
/**
98+
* @return whether paging is implemented in the query itself, e.g. using SpEL expressions.
99+
* @since 2.0.6
100+
*/
101+
default boolean usesPaging() {
102+
return false;
103+
}
104+
105+
}

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/ExpressionBasedStringQuery.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,6 @@ class ExpressionBasedStringQuery extends StringQuery {
6767
selector);
6868
}
6969

70-
/**
71-
* Creates an {@link ExpressionBasedStringQuery} from a given {@link DeclaredQuery}.
72-
*
73-
* @param query the original query. Must not be {@literal null}.
74-
* @param metadata the {@link JpaEntityMetadata} for the given entity. Must not be {@literal null}.
75-
* @param parser Parser for resolving SpEL expressions. Must not be {@literal null}.
76-
* @param nativeQuery is a given query native or not
77-
* @return A query supporting SpEL expressions.
78-
*/
79-
static ExpressionBasedStringQuery from(DeclaredQuery query, JpaEntityMetadata<?> metadata,
80-
ValueExpressionParser parser, boolean nativeQuery) {
81-
return new ExpressionBasedStringQuery(query.getQueryString(), metadata, parser, nativeQuery);
82-
}
83-
8470
/**
8571
* @param query, the query expression potentially containing a SpEL expression. Must not be {@literal null}.
8672
* @param metadata the {@link JpaEntityMetadata} for the given entity. Must not be {@literal null}.
@@ -125,8 +111,9 @@ private static boolean containsExpression(String query) {
125111
return query.contains(ENTITY_NAME_VARIABLE_EXPRESSION);
126112
}
127113

128-
public static IntrospectedQuery create(String query, JpaQueryMethod method, JpaQueryConfiguration queryContext) {
129-
return new ExpressionBasedStringQuery(query, method.getEntityInformation(), queryContext.getParser(),
114+
public static EntityQuery create(String query, JpaQueryMethod method, JpaQueryConfiguration queryContext) {
115+
return new ExpressionBasedStringQuery(query, method.getEntityInformation(),
116+
queryContext.getValueExpressionDelegate().getValueExpressionParser(),
130117
method.isNativeQuery(), queryContext.getSelector());
131118
}
132119

0 commit comments

Comments
 (0)