@@ -69,8 +69,7 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
69
69
* @param valueExpressionDelegate must not be {@literal null}.
70
70
*/
71
71
public AbstractStringBasedJpaQuery (JpaQueryMethod method , EntityManager em , String queryString ,
72
- @ Nullable String countQueryString , QueryRewriter queryRewriter ,
73
- ValueExpressionDelegate valueExpressionDelegate ) {
72
+ @ Nullable String countQueryString , QueryRewriter queryRewriter , ValueExpressionDelegate valueExpressionDelegate ) {
74
73
75
74
super (method , em );
76
75
@@ -99,15 +98,17 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
99
98
});
100
99
101
100
this .queryRewriter = queryRewriter ;
102
- ReturnedType returnedType = method .getResultProcessor ().getReturnedType ();
103
101
104
102
JpaParameters parameters = method .getParameters ();
105
- if ((parameters .hasPageableParameter () || parameters .hasSortParameter ()) && !parameters .hasDynamicProjection ()) {
106
- this .querySortRewriter = new CachingQuerySortRewriter ();
107
- } else if (returnedType .isProjecting () && !returnedType .getReturnedType ().isInterface ()) {
108
- this .querySortRewriter = new ProjectingSortRewriter ();
103
+
104
+ if (parameters .hasDynamicProjection ()) {
105
+ this .querySortRewriter = SimpleQuerySortRewriter .INSTANCE ;
109
106
} else {
110
- this .querySortRewriter = NoOpQuerySortRewriter .INSTANCE ;
107
+ if (parameters .hasPageableParameter () || parameters .hasSortParameter ()) {
108
+ this .querySortRewriter = new CachingQuerySortRewriter ();
109
+ } else {
110
+ this .querySortRewriter = new UnsortedCachingQuerySortRewriter ();
111
+ }
111
112
}
112
113
113
114
Assert .isTrue (method .isNativeQuery () || !query .usesJdbcStyleParameters (),
@@ -119,19 +120,13 @@ public Query doCreateQuery(JpaParametersParameterAccessor accessor) {
119
120
120
121
Sort sort = accessor .getSort ();
121
122
ResultProcessor processor = getQueryMethod ().getResultProcessor ().withDynamicProjection (accessor );
122
-
123
- String sortedQueryString = null ;
124
- if (querySortRewriter .equals (NoOpQuerySortRewriter .INSTANCE ) && accessor .findDynamicProjection () != null && !accessor .findDynamicProjection ().isInterface ()) {
125
- sortedQueryString = getSortedQueryString (new ProjectingSortRewriter (), query , sort , processor .getReturnedType ());
126
- } else {
127
- sortedQueryString = getSortedQueryString (sort , processor .getReturnedType ());
128
- }
129
-
130
- Query query = createJpaQuery (sortedQueryString , sort , accessor .getPageable (), processor .getReturnedType ());
123
+ ReturnedType returnedType = processor .getReturnedType ();
124
+ String sortedQueryString = getSortedQueryString (sort , returnedType );
125
+ Query query = createJpaQuery (sortedQueryString , sort , accessor .getPageable (), returnedType );
131
126
132
127
QueryParameterSetter .QueryMetadata metadata = metadataCache .getMetadata (sortedQueryString , query );
133
128
134
- // it is ok to reuse the binding contained in the ParameterBinder although we create a new query String because the
129
+ // it is ok to reuse the binding contained in the ParameterBinder, although we create a new query String because the
135
130
// parameters in the query do not change.
136
131
return parameterBinder .get ().bindAndPrepare (query , metadata , accessor );
137
132
}
@@ -140,10 +135,6 @@ String getSortedQueryString(Sort sort, ReturnedType returnedType) {
140
135
return querySortRewriter .getSorted (query , sort , returnedType );
141
136
}
142
137
143
- private static String getSortedQueryString (QuerySortRewriter rewriter , DeclaredQuery query , Sort sort , ReturnedType returnedType ) {
144
- return rewriter .getSorted (query , sort , returnedType );
145
- }
146
-
147
138
@ Override
148
139
protected ParameterBinder createBinder () {
149
140
return createBinder (query );
@@ -223,8 +214,8 @@ protected String potentiallyRewriteQuery(String originalQuery, Sort sort, @Nulla
223
214
224
215
String applySorting (CachableQuery cachableQuery ) {
225
216
226
- return QueryEnhancerFactory .forQuery (cachableQuery .getDeclaredQuery ()). rewrite ( cachableQuery . getSort (),
227
- cachableQuery .getReturnedType ());
217
+ return QueryEnhancerFactory .forQuery (cachableQuery .getDeclaredQuery ())
218
+ . rewrite ( new DefaultQueryRewriteInformation ( cachableQuery .getSort (), cachableQuery . getReturnedType () ));
228
219
}
229
220
230
221
/**
@@ -237,21 +228,17 @@ interface QuerySortRewriter {
237
228
/**
238
229
* No-op query rewriter.
239
230
*/
240
- enum NoOpQuerySortRewriter implements QuerySortRewriter {
231
+ enum SimpleQuerySortRewriter implements QuerySortRewriter {
241
232
242
233
INSTANCE ;
243
234
244
235
public String getSorted (DeclaredQuery query , Sort sort , ReturnedType returnedType ) {
245
236
246
- if (sort .isSorted ()) {
247
- throw new UnsupportedOperationException ("NoOpQueryCache does not support sorting" );
248
- }
249
-
250
- return query .getQueryString ();
237
+ return QueryEnhancerFactory .forQuery (query ).rewrite (new DefaultQueryRewriteInformation (sort , returnedType ));
251
238
}
252
239
}
253
240
254
- static class ProjectingSortRewriter implements QuerySortRewriter {
241
+ static class UnsortedCachingQuerySortRewriter implements QuerySortRewriter {
255
242
256
243
private volatile String cachedQueryString ;
257
244
@@ -263,7 +250,8 @@ public String getSorted(DeclaredQuery query, Sort sort, ReturnedType returnedTyp
263
250
264
251
String cachedQueryString = this .cachedQueryString ;
265
252
if (cachedQueryString == null ) {
266
- this .cachedQueryString = cachedQueryString = QueryEnhancerFactory .forQuery (query ).rewrite (sort , returnedType );
253
+ this .cachedQueryString = cachedQueryString = QueryEnhancerFactory .forQuery (query )
254
+ .rewrite (new DefaultQueryRewriteInformation (sort , returnedType ));
267
255
}
268
256
269
257
return cachedQueryString ;
0 commit comments