@@ -379,94 +379,85 @@ void doesNotPrefixUnsafeJpaSortFunctionCalls() {
379
379
assertThat (createQueryFor ("select p from Person p" , sort )).endsWith ("order by sum(foo) asc" );
380
380
}
381
381
382
- @ Test // DATAJPA-965, DATAJPA-970
382
+ @ Test // DATAJPA-965, DATAJPA-970, GH-2863
383
383
void doesNotPrefixMultipleAliasedFunctionCalls () {
384
384
385
385
String query = "SELECT AVG(m.price) AS avgPrice, SUM(m.stocks) AS sumStocks FROM Magazine m" ;
386
386
Sort sort = Sort .by ("avgPrice" , "sumStocks" );
387
387
388
- // TODO: Add support for aliased functions
389
- // assertThat(query(query, (Sort) "m")).endsWith("order by avgPrice asc, sumStocks asc");
388
+ assertThat (createQueryFor (query , sort )).endsWith ("order by avgPrice asc, sumStocks asc" );
390
389
}
391
390
392
- @ Test // DATAJPA-965, DATAJPA-970
391
+ @ Test // DATAJPA-965, DATAJPA-970, GH-2863
393
392
void doesNotPrefixSingleAliasedFunctionCalls () {
394
393
395
394
String query = "SELECT AVG(m.price) AS avgPrice FROM Magazine m" ;
396
395
Sort sort = Sort .by ("avgPrice" );
397
396
398
- // TODO: Add support for aliased functions
399
- // assertThat(query(query, (Sort) "m")).endsWith("order by avgPrice asc");
397
+ assertThat (createQueryFor (query , sort )).endsWith ("order by avgPrice asc" );
400
398
}
401
399
402
- @ Test // DATAJPA-965, DATAJPA-970
400
+ @ Test // DATAJPA-965, DATAJPA-970, GH-2863
403
401
void prefixesSingleNonAliasedFunctionCallRelatedSortProperty () {
404
402
405
403
String query = "SELECT AVG(m.price) AS avgPrice FROM Magazine m" ;
406
404
Sort sort = Sort .by ("someOtherProperty" );
407
405
408
- // TODO: Add support for aliased functions
409
- // assertThat(query(query, (Sort) "m")).endsWith("order by m.someOtherProperty asc");
406
+ assertThat (createQueryFor (query , sort )).endsWith ("order by m.someOtherProperty asc" );
410
407
}
411
408
412
- @ Test // DATAJPA-965, DATAJPA-970
409
+ @ Test // DATAJPA-965, DATAJPA-970, GH-2863
413
410
void prefixesNonAliasedFunctionCallRelatedSortPropertyWhenSelectClauseContainsAliasedFunctionForDifferentProperty () {
414
411
415
412
String query = "SELECT m.name, AVG(m.price) AS avgPrice FROM Magazine m" ;
416
413
Sort sort = Sort .by ("name" , "avgPrice" );
417
414
418
- // TODO: Add support for aliased functions
419
- // assertThat(query(query, (Sort) "m")).endsWith("order by m.name asc, avgPrice asc");
415
+ assertThat (createQueryFor (query , sort )).endsWith ("order by m.name asc, avgPrice asc" );
420
416
}
421
417
422
- @ Test // DATAJPA-965, DATAJPA-970
418
+ @ Test // DATAJPA-965, DATAJPA-970, GH-2863
423
419
void doesNotPrefixAliasedFunctionCallNameWithMultipleNumericParameters () {
424
420
425
421
String query = "SELECT SUBSTRING(m.name, 2, 5) AS trimmedName FROM Magazine m" ;
426
422
Sort sort = Sort .by ("trimmedName" );
427
423
428
- // TODO: Add support for aliased functions
429
- // assertThat(query(query, (Sort) "m")).endsWith("order by trimmedName asc");
424
+ assertThat (createQueryFor (query , sort )).endsWith ("order by trimmedName asc" );
430
425
}
431
426
432
- @ Test // DATAJPA-965, DATAJPA-970
427
+ @ Test // DATAJPA-965, DATAJPA-970, GH-2863
433
428
void doesNotPrefixAliasedFunctionCallNameWithMultipleStringParameters () {
434
429
435
430
String query = "SELECT CONCAT(m.name, 'foo') AS extendedName FROM Magazine m" ;
436
431
Sort sort = Sort .by ("extendedName" );
437
432
438
- // TODO: Add support for aliased functions
439
- // assertThat(query(query, (Sort) "m")).endsWith("order by extendedName asc");
433
+ assertThat (createQueryFor (query , sort )).endsWith ("order by extendedName asc" );
440
434
}
441
435
442
- @ Test // DATAJPA-965, DATAJPA-970
436
+ @ Test // DATAJPA-965, DATAJPA-970, GH-2863
443
437
void doesNotPrefixAliasedFunctionCallNameWithUnderscores () {
444
438
445
439
String query = "SELECT AVG(m.price) AS avg_price FROM Magazine m" ;
446
440
Sort sort = Sort .by ("avg_price" );
447
441
448
- // TODO: Add support for aliased functions
449
- // assertThat(query(query, (Sort) "m")).endsWith("order by avg_price asc");
442
+ assertThat (createQueryFor (query , sort )).endsWith ("order by avg_price asc" );
450
443
}
451
444
452
- @ Test // DATAJPA-965, DATAJPA-970
445
+ @ Test // DATAJPA-965, DATAJPA-970, GH-2863
453
446
void doesNotPrefixAliasedFunctionCallNameWithDots () {
454
447
455
448
String query = "SELECT AVG(m.price) AS m.avg FROM Magazine m" ;
456
449
Sort sort = Sort .by ("m.avg" );
457
450
458
- // TODO: Add support for aliased functions
459
- // assertThat(query(query, (Sort) "m")).endsWith("order by m.avg asc");
451
+ assertThatIllegalArgumentException ().isThrownBy (() -> createQueryFor (query , sort ));
460
452
}
461
453
462
- @ Test // DATAJPA-965, DATAJPA-970
454
+ @ Test // DATAJPA-965, DATAJPA-970, GH-2863
463
455
void doesNotPrefixAliasedFunctionCallNameWhenQueryStringContainsMultipleWhiteSpaces () {
464
456
465
457
String query = "SELECT AVG( m.price ) AS avgPrice FROM Magazine m" ;
466
458
Sort sort = Sort .by ("avgPrice" );
467
459
468
- // TODO: Add support for aliased functions
469
- // assertThat(query(query, (Sort) "m")).endsWith("order by avgPrice asc");
460
+ assertThat (createQueryFor (query , sort )).endsWith ("order by avgPrice asc" );
470
461
}
471
462
472
463
@ Test // DATAJPA-1506
@@ -498,18 +489,18 @@ void createCountQuerySupportsLineBreaksInSelectClause() {
498
489
" where user.age = 18\n " );
499
490
}
500
491
501
- @ Test // DATAJPA-1061
492
+ @ Test // DATAJPA-1061, GH-2863
502
493
void appliesSortCorrectlyForFieldAliases () {
503
494
504
495
String query = "SELECT m.price, lower(m.title) AS title, a.name as authorName FROM Magazine m INNER JOIN m.author a" ;
505
496
Sort sort = Sort .by ("authorName" );
506
497
507
498
String fullQuery = createQueryFor (query , sort );
508
499
509
- assertThat (fullQuery ).endsWith ("order by m. authorName asc" );
500
+ assertThat (fullQuery ).endsWith ("order by authorName asc" );
510
501
}
511
502
512
- @ Test // GH-2280
503
+ @ Test // GH-2280, GH-2863
513
504
void appliesOrderingCorrectlyForFieldAliasWithIgnoreCase () {
514
505
515
506
String query = "SELECT customer.id as id, customer.name as name FROM CustomerEntity customer" ;
@@ -518,18 +509,18 @@ void appliesOrderingCorrectlyForFieldAliasWithIgnoreCase() {
518
509
String fullQuery = createQueryFor (query , sort );
519
510
520
511
assertThat (fullQuery ).isEqualTo (
521
- "SELECT customer.id as id, customer.name as name FROM CustomerEntity customer order by lower(customer. name) asc" );
512
+ "SELECT customer.id as id, customer.name as name FROM CustomerEntity customer order by lower(name) asc" );
522
513
}
523
514
524
- @ Test // DATAJPA-1061
515
+ @ Test // DATAJPA-1061, GH-2863
525
516
void appliesSortCorrectlyForFunctionAliases () {
526
517
527
518
String query = "SELECT m.price, lower(m.title) AS title, a.name as authorName FROM Magazine m INNER JOIN m.author a" ;
528
519
Sort sort = Sort .by ("title" );
529
520
530
521
String fullQuery = createQueryFor (query , sort );
531
522
532
- assertThat (fullQuery ).endsWith ("order by m. title asc" );
523
+ assertThat (fullQuery ).endsWith ("order by title asc" );
533
524
}
534
525
535
526
@ Test // DATAJPA-1061
@@ -793,6 +784,52 @@ void sortProperlyAppendsToExistingOrderByWithFunction() {
793
784
"select e from SampleEntity e where function('nativeFunc', ?1) > 'testVal' order by function('nativeFunc', ?1), e.age desc" );
794
785
}
795
786
787
+ @ Test // GH-2626, GH-2863
788
+ void orderByAliasedColumn () {
789
+
790
+ assertThat (createQueryFor ("""
791
+ select
792
+ max(resource.name) as resourceName,
793
+ max(resource.id) as id,
794
+ max(resource.description) as description,
795
+ max(resource.uuid) as uuid,
796
+ max(resource.type) as type,
797
+ max(resource.createdOn) as createdOn,
798
+ max(users.firstName) as authorFirstName,
799
+ max(users.lastName) as authorLastName,
800
+ max(file.version) as version,
801
+ max(file.comment) as comment,
802
+ file.deployed as deployed,
803
+ max(log.date) as modifiedOn
804
+ from Resource resource
805
+ where (
806
+ cast(:startDate as date) is null
807
+ or resource.latestLogRecord.date between cast(:startDate as date) and cast(:endDate as date)
808
+ )
809
+ group by resource.id, file.deployed, log.author.firstName, file.comment
810
+ """ , Sort .by (Sort .Direction .DESC , "uuid" ))).endsWith ("order by uuid desc" );
811
+ }
812
+
813
+ @ Test // GH-2863, GH-2322
814
+ void sortShouldWorkWhenAliasingFunctions () {
815
+
816
+ assertThat (createQueryFor ("""
817
+ SELECT
818
+ DISTINCT(event.id) as id,
819
+ event.name as name,
820
+ MIN(bundle.base_price_amount) as cheapestBundlePrice,
821
+ MIN(DATE(bundle.start)) as earliestBundleStart
822
+ FROM event event
823
+ LEFT JOIN bundle bundle ON event.id = bundle.event_id
824
+ GROUP BY event.id
825
+ """ , Sort .by (Sort .Direction .ASC , "cheapestBundlePrice" ) //
826
+ .and (Sort .by (
827
+ Sort .Direction .ASC , "earliestBundleStart" )) //
828
+ .and (Sort .by (Sort .Direction .ASC ,
829
+ "name" ))))
830
+ .endsWith (" order by cheapestBundlePrice asc, earliestBundleStart asc, name asc" );
831
+ }
832
+
796
833
private void assertCountQuery (String originalQuery , String countQuery ) {
797
834
assertThat (createCountQueryFor (originalQuery )).isEqualTo (countQuery );
798
835
}
0 commit comments