27
27
import java .lang .reflect .Method ;
28
28
import java .util .Collection ;
29
29
import java .util .Collections ;
30
+ import java .util .List ;
31
+ import java .util .ArrayList ;
30
32
import java .util .Date ;
31
33
32
34
import org .junit .jupiter .api .BeforeEach ;
66
68
* @author Mingyuan Wu
67
69
* @author Myeonghyeon Lee
68
70
* @author Diego Krupitza
71
+ * @author Philmon Roberts
69
72
*/
70
73
@ ExtendWith (MockitoExtension .class )
71
74
@ MockitoSettings (strictness = Strictness .LENIENT )
@@ -110,8 +113,8 @@ void createsQueryToFindAllEntitiesByStringAttribute() throws Exception {
110
113
dataAccessStrategy );
111
114
PreparedOperation <?> preparedOperation = createQuery (queryMethod , r2dbcQuery , "John" );
112
115
113
- assertThat (preparedOperation . get ( ))
114
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1" );
116
+ assertThat (formatOperation ( preparedOperation ))
117
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1" ) );
115
118
}
116
119
117
120
@ Test // GH-282
@@ -122,8 +125,8 @@ void createsQueryWithIsNullCondition() throws Exception {
122
125
dataAccessStrategy );
123
126
PreparedOperation <?> preparedOperation = createQuery (queryMethod , r2dbcQuery , new Object [] { null });
124
127
125
- assertThat (preparedOperation . get ( ))
126
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name IS NULL" );
128
+ assertThat (formatOperation ( preparedOperation ))
129
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name IS NULL" ) );
127
130
}
128
131
129
132
@ Test // GH-282
@@ -147,8 +150,8 @@ void createsQueryToFindAllEntitiesByTwoStringAttributes() throws Exception {
147
150
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery ,
148
151
getAccessor (queryMethod , new Object [] { "Doe" , "John" }));
149
152
150
- assertThat (preparedOperation . get ( )).isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE
151
- + ".last_name = $1 AND (" + TABLE + ".first_name = $2)" );
153
+ assertThat (formatOperation ( preparedOperation )).isEqualTo ( formatQuery ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE
154
+ + ".last_name = $1 AND (" + TABLE + ".first_name = $2)" )) ;
152
155
}
153
156
154
157
@ Test // GH-282
@@ -160,8 +163,8 @@ void createsQueryToFindAllEntitiesByOneOfTwoStringAttributes() throws Exception
160
163
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery ,
161
164
getAccessor (queryMethod , new Object [] { "Doe" , "John" }));
162
165
163
- assertThat (preparedOperation . get ( )).isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE
164
- + ".last_name = $1 OR (" + TABLE + ".first_name = $2)" );
166
+ assertThat (formatOperation ( preparedOperation )).isEqualTo ( formatQuery ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE
167
+ + ".last_name = $1 OR (" + TABLE + ".first_name = $2)" )) ;
165
168
}
166
169
167
170
@ Test // GH-282, gh-349
@@ -175,8 +178,8 @@ void createsQueryToFindAllEntitiesByDateAttributeBetween() throws Exception {
175
178
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { from , to });
176
179
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
177
180
178
- assertThat (preparedOperation . get ( ))
179
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth BETWEEN $1 AND $2" );
181
+ assertThat (formatOperation ( preparedOperation ))
182
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth BETWEEN $1 AND $2" ) );
180
183
181
184
BindTarget bindTarget = mock (BindTarget .class );
182
185
preparedOperation .bindTo (bindTarget );
@@ -194,8 +197,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeLessThan() throws Exception
194
197
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { 30 });
195
198
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
196
199
197
- assertThat (preparedOperation . get ( ))
198
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age < $1" );
200
+ assertThat (formatOperation ( preparedOperation ))
201
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age < $1" ) );
199
202
}
200
203
201
204
@ Test // GH-282
@@ -207,8 +210,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeLessThanEqual() throws Excep
207
210
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { 30 });
208
211
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
209
212
210
- assertThat (preparedOperation . get ( ))
211
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age <= $1" );
213
+ assertThat (formatOperation ( preparedOperation ))
214
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age <= $1" ) );
212
215
}
213
216
214
217
@ Test // GH-282
@@ -220,8 +223,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeGreaterThan() throws Excepti
220
223
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { 30 });
221
224
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
222
225
223
- assertThat (preparedOperation . get ( ))
224
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age > $1" );
226
+ assertThat (formatOperation ( preparedOperation ))
227
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age > $1" ) );
225
228
}
226
229
227
230
@ Test // GH-282
@@ -233,8 +236,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeGreaterThanEqual() throws Ex
233
236
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { 30 });
234
237
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
235
238
236
- assertThat (preparedOperation . get ( ))
237
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age >= $1" );
239
+ assertThat (formatOperation ( preparedOperation ))
240
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age >= $1" ) );
238
241
}
239
242
240
243
@ Test // GH-282
@@ -246,8 +249,8 @@ void createsQueryToFindAllEntitiesByDateAttributeAfter() throws Exception {
246
249
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { new Date () });
247
250
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
248
251
249
- assertThat (preparedOperation . get ( ))
250
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth > $1" );
252
+ assertThat (formatOperation ( preparedOperation ))
253
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth > $1" ) );
251
254
}
252
255
253
256
@ Test // GH-282
@@ -258,8 +261,8 @@ void createsQueryToFindAllEntitiesByDateAttributeBefore() throws Exception {
258
261
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { new Date () });
259
262
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
260
263
261
- assertThat (preparedOperation . get ( ))
262
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth < $1" );
264
+ assertThat (formatOperation ( preparedOperation ))
265
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth < $1" ) );
263
266
}
264
267
265
268
@ Test // GH-282
@@ -271,8 +274,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeIsNull() throws Exception {
271
274
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [0 ]);
272
275
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
273
276
274
- assertThat (preparedOperation . get ( ))
275
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IS NULL" );
277
+ assertThat (formatOperation ( preparedOperation ))
278
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IS NULL" ) );
276
279
}
277
280
278
281
@ Test // GH-282
@@ -284,8 +287,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeIsNotNull() throws Exception
284
287
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [0 ]);
285
288
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
286
289
287
- assertThat (preparedOperation . get ( ))
288
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IS NOT NULL" );
290
+ assertThat (formatOperation ( preparedOperation ))
291
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IS NOT NULL" ) );
289
292
}
290
293
291
294
@ Test // GH-282
@@ -297,8 +300,8 @@ void createsQueryToFindAllEntitiesByStringAttributeLike() throws Exception {
297
300
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "%John%" });
298
301
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
299
302
300
- assertThat (preparedOperation . get ( ))
301
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1" );
303
+ assertThat (formatOperation ( preparedOperation ))
304
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1" ) );
302
305
}
303
306
304
307
@ Test // GH-282
@@ -310,8 +313,8 @@ void createsQueryToFindAllEntitiesByStringAttributeNotLike() throws Exception {
310
313
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "%John%" });
311
314
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
312
315
313
- assertThat (preparedOperation . get ( ))
314
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name NOT LIKE $1" );
316
+ assertThat (formatOperation ( preparedOperation ))
317
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name NOT LIKE $1" ) );
315
318
}
316
319
317
320
@ Test // GH-282
@@ -323,8 +326,8 @@ void createsQueryToFindAllEntitiesByStringAttributeStartingWith() throws Excepti
323
326
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "Jo" });
324
327
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
325
328
326
- assertThat (preparedOperation . get ( ))
327
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1" );
329
+ assertThat (formatOperation ( preparedOperation ))
330
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1" ) );
328
331
}
329
332
330
333
@ Test // GH-282
@@ -350,8 +353,8 @@ void createsQueryToFindAllEntitiesByStringAttributeEndingWith() throws Exception
350
353
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "hn" });
351
354
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
352
355
353
- assertThat (preparedOperation . get ( ))
354
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1" );
356
+ assertThat (formatOperation ( preparedOperation ))
357
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1" ) );
355
358
}
356
359
357
360
@ Test // GH-282
@@ -377,8 +380,8 @@ void createsQueryToFindAllEntitiesByStringAttributeContaining() throws Exception
377
380
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "oh" });
378
381
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
379
382
380
- assertThat (preparedOperation . get ( ))
381
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1" );
383
+ assertThat (formatOperation ( preparedOperation ))
384
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1" ) );
382
385
}
383
386
384
387
@ Test // GH-282
@@ -404,8 +407,8 @@ void createsQueryToFindAllEntitiesByStringAttributeNotContaining() throws Except
404
407
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "oh" });
405
408
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
406
409
407
- assertThat (preparedOperation . get ( ))
408
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name NOT LIKE $1" );
410
+ assertThat (formatOperation ( preparedOperation ))
411
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name NOT LIKE $1" ) );
409
412
}
410
413
411
414
@ Test // GH-282
@@ -431,9 +434,9 @@ void createsQueryToFindAllEntitiesByIntegerAttributeWithDescendingOrderingByStri
431
434
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "oh" });
432
435
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
433
436
434
- assertThat (preparedOperation . get ( ))
437
+ assertThat (formatOperation ( preparedOperation ))
435
438
.isEqualTo (
436
- "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age = $1 ORDER BY users.last_name DESC" );
439
+ formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age = $1 ORDER BY users.last_name DESC" ) );
437
440
}
438
441
439
442
@ Test // GH-282
@@ -444,9 +447,9 @@ void createsQueryToFindAllEntitiesByIntegerAttributeWithAscendingOrderingByStrin
444
447
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "oh" });
445
448
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
446
449
447
- assertThat (preparedOperation . get ( ))
450
+ assertThat (formatOperation ( preparedOperation ))
448
451
.isEqualTo (
449
- "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age = $1 ORDER BY users.last_name ASC" );
452
+ formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age = $1 ORDER BY users.last_name ASC" ) );
450
453
}
451
454
452
455
@ Test // GH-282
@@ -457,8 +460,8 @@ void createsQueryToFindAllEntitiesByStringAttributeNot() throws Exception {
457
460
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "Doe" });
458
461
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
459
462
460
- assertThat (preparedOperation . get ( ))
461
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".last_name != $1" );
463
+ assertThat (formatOperation ( preparedOperation ))
464
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".last_name != $1" ) );
462
465
}
463
466
464
467
@ Test // GH-282
@@ -471,8 +474,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeIn() throws Exception {
471
474
new Object [] { Collections .singleton (25 ) });
472
475
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
473
476
474
- assertThat (preparedOperation . get ( ))
475
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IN ($1)" );
477
+ assertThat (formatOperation ( preparedOperation ))
478
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IN ($1)" ) );
476
479
}
477
480
478
481
@ Test // GH-282
@@ -484,8 +487,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeNotIn() throws Exception {
484
487
new Object [] { Collections .singleton (25 ) });
485
488
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
486
489
487
- assertThat (preparedOperation . get ( ))
488
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age NOT IN ($1)" );
490
+ assertThat (formatOperation ( preparedOperation ))
491
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age NOT IN ($1)" ) );
489
492
}
490
493
491
494
@ Test // GH-282, gh-698
@@ -497,8 +500,8 @@ void createsQueryToFindAllEntitiesByBooleanAttributeTrue() throws Exception {
497
500
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [0 ]);
498
501
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
499
502
500
- assertThat (preparedOperation . get ( ))
501
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1" );
503
+ assertThat (formatOperation ( preparedOperation ))
504
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1" ) );
502
505
}
503
506
504
507
@ Test // GH-282, gh-698
@@ -510,8 +513,8 @@ void createsQueryToFindAllEntitiesByBooleanAttributeFalse() throws Exception {
510
513
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [0 ]);
511
514
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
512
515
513
- assertThat (preparedOperation . get ( ))
514
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1" );
516
+ assertThat (formatOperation ( preparedOperation ))
517
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1" ) );
515
518
}
516
519
517
520
@ Test // GH-282
@@ -523,8 +526,8 @@ void createsQueryToFindAllEntitiesByStringAttributeIgnoringCase() throws Excepti
523
526
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "John" });
524
527
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
525
528
526
- assertThat (preparedOperation . get ( ))
527
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE UPPER(" + TABLE + ".first_name) = UPPER($1)" );
529
+ assertThat (formatOperation ( preparedOperation ))
530
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE UPPER(" + TABLE + ".first_name) = UPPER($1)" ) );
528
531
}
529
532
530
533
@ Test // GH-282
@@ -587,8 +590,8 @@ void createsQueryWithLimitToFindEntitiesByStringAttribute() throws Exception {
587
590
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "John" });
588
591
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
589
592
590
- assertThat (preparedOperation . get ( ))
591
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 3" );
593
+ assertThat (formatOperation ( preparedOperation ))
594
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 3" ) );
592
595
}
593
596
594
597
@ Test // GH-282
@@ -600,8 +603,8 @@ void createsQueryToFindFirstEntityByStringAttribute() throws Exception {
600
603
RelationalParametersParameterAccessor accessor = getAccessor (queryMethod , new Object [] { "John" });
601
604
PreparedOperation <?> preparedOperation = createQuery (r2dbcQuery , accessor );
602
605
603
- assertThat (preparedOperation . get ( ))
604
- .isEqualTo ("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 1" );
606
+ assertThat (formatOperation ( preparedOperation ))
607
+ .isEqualTo (formatQuery ( "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 1" ) );
605
608
}
606
609
607
610
@ Test // GH-341
@@ -624,8 +627,8 @@ void createsQueryToFindAllEntitiesByStringAttributeWithDistinct() throws Excepti
624
627
dataAccessStrategy );
625
628
PreparedOperation <?> preparedOperation = createQuery (queryMethod , r2dbcQuery , "John" );
626
629
627
- assertThat (preparedOperation . get ( )).isEqualTo ("SELECT " + DISTINCT + " " + TABLE + ".first_name, " + TABLE
628
- + ".foo FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1" );
630
+ assertThat (formatOperation ( preparedOperation )).isEqualTo ( formatQuery ("SELECT " + DISTINCT + " " + TABLE + ".first_name, " + TABLE
631
+ + ".foo FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1" )) ;
629
632
}
630
633
631
634
@ Test // GH-475
@@ -636,9 +639,9 @@ void createsQueryToFindByOpenProjection() throws Exception {
636
639
dataAccessStrategy );
637
640
PreparedOperation <?> preparedOperation = createQuery (queryMethod , r2dbcQuery );
638
641
639
- assertThat (preparedOperation . get ( )).isEqualTo (
640
- "SELECT users.id, users.first_name, users.last_name, users.date_of_birth, users.age, users.active FROM "
641
- + TABLE );
642
+ assertThat (formatOperation ( preparedOperation )).isEqualTo (
643
+ formatQuery ( "SELECT users.id, users.first_name, users.last_name, users.date_of_birth, users.age, users.active FROM "
644
+ + TABLE )) ;
642
645
}
643
646
644
647
@ Test // GH-475
@@ -649,9 +652,9 @@ void createsDtoProjectionQuery() throws Exception {
649
652
dataAccessStrategy );
650
653
PreparedOperation <?> preparedOperation = createQuery (queryMethod , r2dbcQuery );
651
654
652
- assertThat (preparedOperation . get ( )).isEqualTo (
653
- "SELECT users.id, users.first_name, users.last_name, users.date_of_birth, users.age, users.active FROM "
654
- + TABLE );
655
+ assertThat (formatOperation ( preparedOperation )).isEqualTo (
656
+ formatQuery ( "SELECT users.id, users.first_name, users.last_name, users.date_of_birth, users.age, users.active FROM "
657
+ + TABLE )) ;
655
658
}
656
659
657
660
@ Test // GH-363
@@ -716,6 +719,59 @@ private RelationalParametersParameterAccessor getAccessor(R2dbcQueryMethod query
716
719
return new RelationalParametersParameterAccessor (queryMethod , values );
717
720
}
718
721
722
+ private static String formatOperation (PreparedOperation <?> preparedOperation ){
723
+ return formatQuery (preparedOperation .get ());
724
+ }
725
+
726
+ private static String formatQuery (String query ){
727
+ String firstKeyword = "SELECT" ;
728
+ String lastKeyword = "FROM" ;
729
+
730
+ int indexOfFirstKeyWord = query .toUpperCase ().indexOf (firstKeyword );
731
+ int indexOfLastKeyWord = query .toUpperCase ().indexOf (lastKeyword );
732
+
733
+ if (indexOfFirstKeyWord !=0 || indexOfFirstKeyWord >=indexOfLastKeyWord ){
734
+ return query ;
735
+ }
736
+
737
+ String fields = query .substring (firstKeyword .length (), indexOfLastKeyWord );
738
+ String sortedFields = sortFields (fields );
739
+
740
+ StringBuilder formattedQuery = new StringBuilder ();
741
+ formattedQuery .append (firstKeyword );
742
+ formattedQuery .append (" " );
743
+ formattedQuery .append (sortedFields );
744
+ formattedQuery .append (" " );
745
+ formattedQuery .append (query .substring (indexOfLastKeyWord , query .length ()));
746
+
747
+ return formattedQuery .toString ();
748
+ }
749
+
750
+ private static String sortFields (String fields ){
751
+ List <String > sortedFieldsList = new ArrayList <>();
752
+ StringBuilder fieldBuilder = new StringBuilder ();
753
+ StringBuilder sortedFields = new StringBuilder ();
754
+
755
+ for (int i =0 ;i <fields .length ();i ++){
756
+ if (fields .charAt (i )==',' ){
757
+ sortedFieldsList .add (fieldBuilder .toString ().trim ());
758
+ fieldBuilder = new StringBuilder ();
759
+ }else {
760
+ fieldBuilder .append (fields .charAt (i ));
761
+ }
762
+ }
763
+ sortedFieldsList .add (fieldBuilder .toString ().trim ());
764
+ Collections .sort (sortedFieldsList );
765
+
766
+ for (String sortedField : sortedFieldsList ){
767
+ if (sortedFieldsList .get (0 )!=sortedField ){
768
+ sortedFields .append (", " );
769
+ }
770
+ sortedFields .append (sortedField );
771
+ }
772
+ return sortedFields .toString ();
773
+ }
774
+
719
775
@ SuppressWarnings ("ALL" )
720
776
interface UserRepository extends Repository <User , Long > {
721
777
0 commit comments