Skip to content

Commit c3b0ab4

Browse files
committed
Introduced QueryEnhancer API.
Queries often need to be enhanced for example with an "order by" or a "where" clause and `QueryEnhancer` provides a simple inferface that handles all the enhancements. Since native queries can be parsed and processed more efficiently with JSqlParser there is a `QueryEnhancer` implementation using JSqlParser. On the other side there is a default implementation using the custom written parser `DefaultQueryEnhancer`. If a given query cannot be identified as a native one the `DefaultQueryEnhancer` is used. Related tickets spring-projects#2409
1 parent e7a0603 commit c3b0ab4

20 files changed

+1997
-1771
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
8686
@Override
8787
public Query doCreateQuery(JpaParametersParameterAccessor accessor) {
8888

89-
String sortedQueryString = QueryUtils.applySorting(query, accessor.getSort(), query.getAlias());
89+
String sortedQueryString = QueryEnhancerFactory.forQuery(query) //
90+
.applySorting(query, accessor.getSort(), query.getAlias());
9091
ResultProcessor processor = getQueryMethod().getResultProcessor().withDynamicProjection(accessor);
9192

9293
Query query = createJpaQuery(sortedQueryString, processor.getReturnedType());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2022 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+
20+
/**
21+
* The implementation of {@link QueryEnhancer} using {@link QueryUtils}.
22+
*
23+
* @author Diego Krupitza
24+
*/
25+
public class DefaultQueryEnhancer implements QueryEnhancer {
26+
27+
@Override
28+
public String getExistsQueryString(String entityName, String countQueryPlaceHolder, Iterable<String> idAttributes) {
29+
return QueryUtils.getExistsQueryString(entityName, countQueryPlaceHolder, idAttributes);
30+
}
31+
32+
@Override
33+
public String getQueryString(String template, String entityName) {
34+
return QueryUtils.getQueryString(template, entityName);
35+
}
36+
37+
@Override
38+
public String applySorting(DeclaredQuery query, Sort sort, String alias) {
39+
return QueryUtils.applySorting(query.getQueryString(), sort, alias);
40+
}
41+
42+
@Override
43+
public String detectAlias(DeclaredQuery query) {
44+
return QueryUtils.detectAlias(query.getQueryString());
45+
}
46+
47+
@Override
48+
public String createCountQueryFor(DeclaredQuery originalQuery, String countProjection) {
49+
return QueryUtils.createCountQueryFor(originalQuery.getQueryString(), countProjection);
50+
}
51+
52+
@Override
53+
public String getProjection(DeclaredQuery query) {
54+
return QueryUtils.getProjection(query.getQueryString());
55+
}
56+
}

0 commit comments

Comments
 (0)