Skip to content

Commit 6a9636c

Browse files
hacking
1 parent ac412b0 commit 6a9636c

27 files changed

+308
-215
lines changed

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

Lines changed: 1 addition & 1 deletion
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

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

Lines changed: 67 additions & 0 deletions
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

Lines changed: 9 additions & 14 deletions
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 sealed interface DeclaredQuery extends QueryString permits JpqlQuery, NativeQuery, BindableQuery {
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

Lines changed: 0 additions & 68 deletions
This file was deleted.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
*/
2929
public class DefaultQueryEnhancer implements QueryEnhancer {
3030

31-
private final DeclaredQuery query;
31+
private final QueryString query;
3232
private final boolean hasConstructorExpression;
3333
private final String alias;
3434
private final String projection;
3535
private final Set<String> joinAliases;
3636

37-
public DefaultQueryEnhancer(DeclaredQuery query) {
37+
public DefaultQueryEnhancer(QueryString query) {
3838
this.query = query;
3939
this.hasConstructorExpression = QueryUtils.hasConstructorExpression(query.getQueryString());
4040
this.alias = QueryUtils.detectAlias(query.getQueryString());
@@ -59,7 +59,9 @@ public String rewrite(QueryRewriteInformation rewriteInformation) {
5959

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

6567
@Override
@@ -83,7 +85,7 @@ public Set<String> getJoinAliases() {
8385
}
8486

8587
@Override
86-
public DeclaredQuery getQuery() {
88+
public QueryString getQuery() {
8789
return this.query;
8890
}
8991
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,18 @@ public boolean hasNamedParameter() {
4040
}
4141

4242
@Override
43-
public String getQueryString() {
44-
return "";
45-
}
46-
47-
@Override
48-
public boolean isNativeQuery() {
43+
public boolean hasConstructorExpression() {
4944
return false;
5045
}
5146

5247
@Override
53-
public boolean hasConstructorExpression() {
48+
public boolean isDefaultProjection() {
5449
return false;
5550
}
5651

5752
@Override
58-
public boolean isDefaultProjection() {
59-
return false;
53+
public String getQueryString() {
54+
return "";
6055
}
6156

6257
@Override
@@ -78,4 +73,9 @@ public String applySorting(Sort sort) {
7873
public boolean usesJdbcStyleParameters() {
7974
return false;
8075
}
76+
77+
@Override
78+
public DeclaredQuery getDeclaredQuery() {
79+
return DeclaredQuery.nativeQuery("");
80+
}
8181
}

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

Lines changed: 7 additions & 1 deletion
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 QueryString {
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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
*/
6969
public class JSqlParserQueryEnhancer implements QueryEnhancer {
7070

71-
private final DeclaredQuery query;
71+
private final QueryString query;
7272
private final Statement statement;
7373
private final ParsedType parsedType;
7474
private final boolean hasConstructorExpression;
@@ -81,7 +81,7 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
8181
/**
8282
* @param query the query we want to enhance. Must not be {@literal null}.
8383
*/
84-
public JSqlParserQueryEnhancer(DeclaredQuery query) {
84+
public JSqlParserQueryEnhancer(QueryString query) {
8585

8686
this.query = query;
8787
this.statement = parseStatement(query.getQueryString(), Statement.class);
@@ -292,7 +292,7 @@ public Set<String> getSelectionAliases() {
292292
}
293293

294294
@Override
295-
public DeclaredQuery getQuery() {
295+
public QueryString getQuery() {
296296
return this.query;
297297
}
298298

@@ -366,7 +366,7 @@ public String createCountQueryFor(@Nullable String countProjection) {
366366
return createCountQueryFor(this.query, selectBody, countProjection, primaryAlias);
367367
}
368368

369-
private static String createCountQueryFor(DeclaredQuery query, PlainSelect selectBody,
369+
private static String createCountQueryFor(QueryString query, PlainSelect selectBody,
370370
@Nullable String countProjection, @Nullable String primaryAlias) {
371371

372372
// remove order by
Lines changed: 38 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)