Skip to content

Commit 7068a8a

Browse files
christophstroblmp911de
authored andcommitted
Refactoring.
See #3622 Original pull request: #3527
1 parent 38f5c11 commit 7068a8a

27 files changed

+304
-206
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ OR TREAT(p AS SmallProject).name LIKE 'Persist%'
5555
OR p.description LIKE "cost overrun"
5656
""";
5757

58-
query = DeclaredQuery.ofJpql(s);
58+
query = DeclaredQuery.jpqlQuery(s);
5959
enhancer = QueryEnhancerFactory.forQuery(query).create(query);
6060
}
6161
}

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

+1-1
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.ofNative(s));
59+
enhancer = new JSqlParserQueryEnhancer(DeclaredQuery.nativeQuery(s));
6060
}
6161
}
6262

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2025 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 java.util.Collections;
19+
import java.util.List;
20+
21+
22+
/**
23+
* @author Christoph Strobl
24+
*/
25+
final class BindableQuery implements DeclaredQuery {
26+
27+
private final DeclaredQuery source;
28+
private final String bindableQueryString;
29+
private final List<ParameterBinding> bindings;
30+
private final boolean usesJdbcStyleParameters;
31+
32+
public BindableQuery(DeclaredQuery source, String bindableQueryString, List<ParameterBinding> bindings, boolean usesJdbcStyleParameters) {
33+
this.source = source;
34+
this.bindableQueryString = bindableQueryString;
35+
this.bindings = bindings;
36+
this.usesJdbcStyleParameters = usesJdbcStyleParameters;
37+
}
38+
39+
@Override
40+
public boolean isNativeQuery() {
41+
return source.isNativeQuery();
42+
}
43+
44+
boolean hasBindings() {
45+
return !bindings.isEmpty();
46+
}
47+
48+
boolean usesJdbcStyleParameters() {
49+
return usesJdbcStyleParameters;
50+
}
51+
52+
@Override
53+
public String getQueryString() {
54+
return bindableQueryString;
55+
}
56+
57+
public BindableQuery unifyBindings(BindableQuery comparisonQuery) {
58+
if (comparisonQuery.hasBindings() && !comparisonQuery.bindings.equals(this.bindings)) {
59+
return new BindableQuery(source, bindableQueryString, comparisonQuery.bindings, usesJdbcStyleParameters);
60+
}
61+
return this;
62+
}
63+
64+
public List<ParameterBinding> getBindings() {
65+
return Collections.unmodifiableList(bindings);
66+
}
67+
}

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

+9-14
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,28 @@
2323
* @author Mark Paluch
2424
* @since 2.0.3
2525
*/
26-
public interface DeclaredQuery {
26+
public interface DeclaredQuery extends StructuredQuery {
2727

2828
/**
2929
* Creates a DeclaredQuery for a JPQL query.
3030
*
31-
* @param query the JPQL query string.
32-
* @return
31+
* @param jpql the JPQL query string.
32+
* @return new instance of {@link DeclaredQuery}.
3333
*/
34-
static DeclaredQuery ofJpql(String query) {
35-
return new DefaultDeclaredQuery(query, false);
34+
static DeclaredQuery jpqlQuery(String jpql) {
35+
return new JpqlQuery(jpql);
3636
}
3737

3838
/**
3939
* Creates a DeclaredQuery for a native query.
4040
*
41-
* @param query the native query string.
42-
* @return
41+
* @param sql the native query string.
42+
* @return new instance of {@link DeclaredQuery}.
4343
*/
44-
static DeclaredQuery ofNative(String query) {
45-
return new DefaultDeclaredQuery(query, true);
44+
static DeclaredQuery nativeQuery(String sql) {
45+
return new NativeQuery(sql);
4646
}
4747

48-
/**
49-
* Returns the query string.
50-
*/
51-
String getQueryString();
52-
5348
/**
5449
* Return whether the query is a native query of not.
5550
*

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

-68
This file was deleted.

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
*/
3030
public class DefaultQueryEnhancer implements QueryEnhancer {
3131

32-
private final DeclaredQuery query;
32+
private final StructuredQuery query;
3333
private final boolean hasConstructorExpression;
3434
private final @Nullable String alias;
3535
private final String projection;
3636
private final Set<String> joinAliases;
3737

38-
public DefaultQueryEnhancer(DeclaredQuery query) {
38+
public DefaultQueryEnhancer(StructuredQuery query) {
3939
this.query = query;
4040
this.hasConstructorExpression = QueryUtils.hasConstructorExpression(query.getQueryString());
4141
this.alias = QueryUtils.detectAlias(query.getQueryString());
@@ -60,7 +60,9 @@ public String rewrite(QueryRewriteInformation rewriteInformation) {
6060

6161
@Override
6262
public String createCountQueryFor(@Nullable String countProjection) {
63-
return QueryUtils.createCountQueryFor(this.query.getQueryString(), countProjection, this.query.isNativeQuery());
63+
64+
boolean nativeQuery = this.query instanceof DeclaredQuery dc ? dc.isNativeQuery() : true;
65+
return QueryUtils.createCountQueryFor(this.query.getQueryString(), countProjection, nativeQuery);
6466
}
6567

6668
@Override
@@ -84,7 +86,7 @@ public Set<String> getJoinAliases() {
8486
}
8587

8688
@Override
87-
public DeclaredQuery getQuery() {
89+
public StructuredQuery getQuery() {
8890
return this.query;
8991
}
9092
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public boolean isDefaultProjection() {
6363
return false;
6464
}
6565

66+
@Override
67+
public String getQueryString() {
68+
return "";
69+
}
70+
6671
@Override
6772
public List<ParameterBinding> getParameterBindings() {
6873
return Collections.emptyList();
@@ -82,4 +87,9 @@ public String applySorting(Sort sort) {
8287
public boolean usesJdbcStyleParameters() {
8388
return false;
8489
}
90+
91+
@Override
92+
public DeclaredQuery getDeclaredQuery() {
93+
return DeclaredQuery.nativeQuery("");
94+
}
8595
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
* @author Diego Krupitza
2525
* @since 2.0.3
2626
*/
27-
interface IntrospectedQuery extends DeclaredQuery {
27+
interface IntrospectedQuery extends StructuredQuery {
28+
29+
DeclaredQuery getDeclaredQuery();
30+
31+
default String getQueryString() {
32+
return getDeclaredQuery().getQueryString();
33+
}
2834

2935
/**
3036
* @return whether the underlying query has at least one named parameter.

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
*/
7171
public class JSqlParserQueryEnhancer implements QueryEnhancer {
7272

73-
private final DeclaredQuery query;
73+
private final StructuredQuery query;
7474
private final Statement statement;
7575
private final ParsedType parsedType;
7676
private final boolean hasConstructorExpression;
@@ -83,7 +83,7 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
8383
/**
8484
* @param query the query we want to enhance. Must not be {@literal null}.
8585
*/
86-
public JSqlParserQueryEnhancer(DeclaredQuery query) {
86+
public JSqlParserQueryEnhancer(StructuredQuery query) {
8787

8888
this.query = query;
8989
this.statement = parseStatement(query.getQueryString(), Statement.class);
@@ -295,7 +295,7 @@ public Set<String> getSelectionAliases() {
295295
}
296296

297297
@Override
298-
public DeclaredQuery getQuery() {
298+
public StructuredQuery getQuery() {
299299
return this.query;
300300
}
301301

@@ -373,8 +373,8 @@ public String createCountQueryFor(@Nullable String countProjection) {
373373
return createCountQueryFor(selectBody, countProjection, primaryAlias);
374374
}
375375

376-
private static String createCountQueryFor(PlainSelect selectBody, @Nullable String countProjection,
377-
@Nullable String primaryAlias) {
376+
private static String createCountQueryFor(StructuredQuery query, PlainSelect selectBody,
377+
@Nullable String countProjection, @Nullable String primaryAlias) {
378378

379379
// remove order by
380380
selectBody.setOrderByElements(null);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2025 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+
/**
19+
* @author Christoph Strobl
20+
*/
21+
final class JpqlQuery implements DeclaredQuery {
22+
23+
private final String jpql;
24+
25+
JpqlQuery(String jpql) {
26+
this.jpql = jpql;
27+
}
28+
29+
@Override
30+
public boolean isNativeQuery() {
31+
return false;
32+
}
33+
34+
@Override
35+
public String getQueryString() {
36+
return jpql;
37+
}
38+
}

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

-5
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,11 @@ protected TypedQuery<Long> doCreateCountQuery(JpaParametersParameterAccessor acc
185185
EntityManager em = getEntityManager();
186186
TypedQuery<Long> countQuery;
187187

188-
String cacheKey;
189188
if (namedCountQueryIsPresent) {
190-
cacheKey = countQueryName;
191189
countQuery = em.createNamedQuery(countQueryName, Long.class);
192-
193190
} else {
194191

195192
String countQueryString = entityQuery.get().deriveCountQuery(countProjection).getQueryString();
196-
countQueryString = potentiallyRewriteQuery(countQueryString, accessor.getSort(), accessor.getPageable());
197-
cacheKey = countQueryString;
198193
countQuery = em.createQuery(countQueryString, Long.class);
199194
}
200195

0 commit comments

Comments
 (0)