|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.*;
|
19 | 19 |
|
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
20 | 22 | import java.util.stream.Stream;
|
21 | 23 |
|
22 | 24 | import org.assertj.core.api.SoftAssertions;
|
23 | 25 | import org.junit.jupiter.api.Test;
|
24 | 26 | import org.junit.jupiter.params.ParameterizedTest;
|
25 | 27 | import org.junit.jupiter.params.provider.Arguments;
|
26 | 28 | import org.junit.jupiter.params.provider.MethodSource;
|
| 29 | +import org.junit.jupiter.params.provider.ValueSource; |
27 | 30 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
28 | 31 | import org.springframework.data.domain.PageRequest;
|
29 | 32 | import org.springframework.data.domain.Sort;
|
30 | 33 | import org.springframework.data.jpa.domain.JpaSort;
|
31 | 34 | import org.springframework.lang.Nullable;
|
| 35 | +import org.springframework.util.StringUtils; |
32 | 36 |
|
33 | 37 | /**
|
34 | 38 | * Verify that HQL queries are properly transformed through the {@link JpaQueryEnhancer} and the {@link HqlQueryParser}.
|
@@ -1043,6 +1047,40 @@ void createsCountQueryUsingAliasCorrectly() {
|
1043 | 1047 | assertCountQuery("select distinct a, count(b) as c from Employee GROUP BY n","select count(distinct a, count(b)) from Employee AS __ GROUP BY n");
|
1044 | 1048 | }
|
1045 | 1049 |
|
| 1050 | + @Test // GH-3427 |
| 1051 | + void sortShouldBeAppendedWithSpacingInCaseOfSetOperator() { |
| 1052 | + |
| 1053 | + String source = "SELECT tb FROM Test tb WHERE (tb.type='A') UNION SELECT tb FROM Test tb WHERE (tb.type='B')"; |
| 1054 | + String target = createQueryFor(source, Sort.by("Type").ascending()); |
| 1055 | + |
| 1056 | + assertThat(target).contains(" UNION SELECT ").doesNotContainPattern(Pattern.compile(".*\\SUNION")); |
| 1057 | + } |
| 1058 | + |
| 1059 | + @ParameterizedTest // GH-3427 |
| 1060 | + @ValueSource(strings = {"", "res"}) |
| 1061 | + void sortShouldBeAppendedToSubSelectWithSetOperatorInSubselect(String alias) { |
| 1062 | + |
| 1063 | + String prefix = StringUtils.hasText(alias) ? (alias + ".") : ""; |
| 1064 | + String source = "SELECT %sname FROM (SELECT c.name as name FROM Category c UNION SELECT t.name as name FROM Tag t)".formatted(prefix); |
| 1065 | + if(StringUtils.hasText(alias)) { |
| 1066 | + source = source + " %s".formatted(alias); |
| 1067 | + } |
| 1068 | + |
| 1069 | + String target = createQueryFor(source, Sort.by("name").ascending()); |
| 1070 | + |
| 1071 | + assertThat(target).contains(" UNION SELECT ").doesNotContainPattern(Pattern.compile(".*\\SUNION")); |
| 1072 | + assertThat(target).endsWith("order by %sname asc".formatted(prefix)).satisfies(it -> { |
| 1073 | + Pattern pattern = Pattern.compile("order by %sname".formatted(prefix)); |
| 1074 | + Matcher matcher = pattern.matcher(target); |
| 1075 | + int count = 0; |
| 1076 | + while(matcher.find()) { |
| 1077 | + count++; |
| 1078 | + } |
| 1079 | + assertThat(count).describedAs("Found order by clause more than once in: \n%s", it).isOne(); |
| 1080 | + }); |
| 1081 | + |
| 1082 | + } |
| 1083 | + |
1046 | 1084 | private void assertCountQuery(String originalQuery, String countQuery) {
|
1047 | 1085 | assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
|
1048 | 1086 | }
|
|
0 commit comments