@@ -161,6 +161,66 @@ void rewritesNamedLikeToUniqueParametersIfNecessary() {
161
161
assertThat (((MethodInvocationArgument ) parameterBinding .getOrigin ()).identifier ().getName ()).isEqualTo ("firstname" );
162
162
}
163
163
164
+ @ Test // GH-3784
165
+ void rewritesNamedLikeToUniqueParametersRetainingCountQuery () {
166
+
167
+ DeclaredQuery query = new StringQuery (
168
+ "select u from User u where u.firstname like %:firstname or u.firstname like :firstname% or u.firstname = :firstname" ,
169
+ false ).deriveCountQuery (null );
170
+
171
+ assertThat (query .getQueryString ()) //
172
+ .isEqualTo (
173
+ "select count(u) from User u where u.firstname like :firstname or u.firstname like :firstname_1 or u.firstname = :firstname_2" );
174
+
175
+ List <ParameterBinding > bindings = query .getParameterBindings ();
176
+ assertThat (bindings ).hasSize (3 );
177
+
178
+ LikeParameterBinding binding = (LikeParameterBinding ) bindings .get (0 );
179
+ assertThat (binding ).isNotNull ();
180
+ assertThat (binding .getOrigin ()).isEqualTo (ParameterOrigin .ofParameter ("firstname" ));
181
+ assertThat (binding .getName ()).isEqualTo ("firstname" );
182
+ assertThat (binding .getType ()).isEqualTo (Type .ENDING_WITH );
183
+
184
+ binding = (LikeParameterBinding ) bindings .get (1 );
185
+ assertThat (binding ).isNotNull ();
186
+ assertThat (binding .getOrigin ()).isEqualTo (ParameterOrigin .ofParameter ("firstname" ));
187
+ assertThat (binding .getName ()).isEqualTo ("firstname_1" );
188
+ assertThat (binding .getType ()).isEqualTo (Type .STARTING_WITH );
189
+
190
+ ParameterBinding parameterBinding = bindings .get (2 );
191
+ assertThat (parameterBinding ).isNotNull ();
192
+ assertThat (parameterBinding .getOrigin ()).isEqualTo (ParameterOrigin .ofParameter ("firstname" ));
193
+ assertThat (parameterBinding .getName ()).isEqualTo ("firstname_2" );
194
+ assertThat (((MethodInvocationArgument ) parameterBinding .getOrigin ()).identifier ().getName ()).isEqualTo ("firstname" );
195
+ }
196
+
197
+ @ Test // GH-3784
198
+ void rewritesExpressionsLikeToUniqueParametersRetainingCountQuery () {
199
+
200
+ DeclaredQuery query = new StringQuery (
201
+ "select u from User u where u.firstname like %:#{firstname} or u.firstname like :#{firstname}%" , false )
202
+ .deriveCountQuery (null );
203
+
204
+ assertThat (query .getQueryString ()) //
205
+ .isEqualTo (
206
+ "select count(u) from User u where u.firstname like :__$synthetic$__1 or u.firstname like :__$synthetic$__2" );
207
+
208
+ List <ParameterBinding > bindings = query .getParameterBindings ();
209
+ assertThat (bindings ).hasSize (2 );
210
+
211
+ LikeParameterBinding binding = (LikeParameterBinding ) bindings .get (0 );
212
+ assertThat (binding ).isNotNull ();
213
+ assertThat (binding .getOrigin ().isExpression ()).isTrue ();
214
+ assertThat (binding .getName ()).isEqualTo ("__$synthetic$__1" );
215
+ assertThat (binding .getType ()).isEqualTo (Type .ENDING_WITH );
216
+
217
+ binding = (LikeParameterBinding ) bindings .get (1 );
218
+ assertThat (binding ).isNotNull ();
219
+ assertThat (binding .getOrigin ().isExpression ()).isTrue ();
220
+ assertThat (binding .getName ()).isEqualTo ("__$synthetic$__2" );
221
+ assertThat (binding .getType ()).isEqualTo (Type .STARTING_WITH );
222
+ }
223
+
164
224
@ Test // GH-3041
165
225
void rewritesPositionalLikeToUniqueParametersIfNecessary () {
166
226
@@ -264,6 +324,48 @@ void detectsMultipleNamedInParameterBindings() {
264
324
assertNamedBinding (ParameterBinding .class , "bar" , bindings .get (2 ));
265
325
}
266
326
327
+ @ Test // GH-3784
328
+ void deriveCountQueryWithNamedInRetainsOrigin () {
329
+
330
+ String queryString = "select u from User u where (:logins) IS NULL OR LOWER(u.login) IN (:logins)" ;
331
+ DeclaredQuery query = new StringQuery (queryString , false ).deriveCountQuery (null );
332
+
333
+ assertThat (query .getQueryString ())
334
+ .isEqualTo ("select count(u) from User u where (:logins) IS NULL OR LOWER(u.login) IN (:logins_1)" );
335
+
336
+ List <ParameterBinding > bindings = query .getParameterBindings ();
337
+ assertThat (bindings ).hasSize (2 );
338
+
339
+ assertNamedBinding (ParameterBinding .class , "logins" , bindings .get (0 ));
340
+ assertThat ((MethodInvocationArgument ) bindings .get (0 ).getOrigin ()).extracting (MethodInvocationArgument ::identifier )
341
+ .extracting (BindingIdentifier ::getName ).isEqualTo ("logins" );
342
+
343
+ assertNamedBinding (InParameterBinding .class , "logins_1" , bindings .get (1 ));
344
+ assertThat ((MethodInvocationArgument ) bindings .get (1 ).getOrigin ()).extracting (MethodInvocationArgument ::identifier )
345
+ .extracting (BindingIdentifier ::getName ).isEqualTo ("logins" );
346
+ }
347
+
348
+ @ Test // GH-3784
349
+ void deriveCountQueryWithPositionalInRetainsOrigin () {
350
+
351
+ String queryString = "select u from User u where (?1) IS NULL OR LOWER(u.login) IN (?1)" ;
352
+ DeclaredQuery query = new StringQuery (queryString , false ).deriveCountQuery (null );
353
+
354
+ assertThat (query .getQueryString ())
355
+ .isEqualTo ("select count(u) from User u where (?1) IS NULL OR LOWER(u.login) IN (?2)" );
356
+
357
+ List <ParameterBinding > bindings = query .getParameterBindings ();
358
+ assertThat (bindings ).hasSize (2 );
359
+
360
+ assertPositionalBinding (ParameterBinding .class , 1 , bindings .get (0 ));
361
+ assertThat ((MethodInvocationArgument ) bindings .get (0 ).getOrigin ()).extracting (MethodInvocationArgument ::identifier )
362
+ .extracting (BindingIdentifier ::getPosition ).isEqualTo (1 );
363
+
364
+ assertPositionalBinding (InParameterBinding .class , 2 , bindings .get (1 ));
365
+ assertThat ((MethodInvocationArgument ) bindings .get (1 ).getOrigin ()).extracting (MethodInvocationArgument ::identifier )
366
+ .extracting (BindingIdentifier ::getPosition ).isEqualTo (1 );
367
+ }
368
+
267
369
@ Test // DATAJPA-461
268
370
void detectsPositionalInParameterBindings () {
269
371
0 commit comments