Skip to content

Commit 42d8956

Browse files
committed
Polishing.
Use switch expression, add author tags, reformat code. See #3811
1 parent aa29081 commit 42d8956

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,26 @@ public abstract class QueryUtils {
106106
private static final String SIMPLE_COUNT_VALUE = "$2";
107107
private static final String COMPLEX_COUNT_VALUE = "$3 $6";
108108
private static final String COMPLEX_COUNT_LAST_VALUE = "$6";
109-
private static final Pattern ORDER_BY_PART = Pattern.compile("(?iu)\\s+order\\s+by\\s+.*", CASE_INSENSITIVE | DOTALL);
109+
private static final Pattern ORDER_BY_PART = compile("(?iu)\\s+order\\s+by\\s+.*", CASE_INSENSITIVE | DOTALL);
110110

111111
private static final Pattern ALIAS_MATCH;
112112
private static final Pattern COUNT_MATCH;
113-
private static final Pattern STARTS_WITH_PAREN = Pattern.compile("^\\s*\\(");
114-
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))",
113+
private static final Pattern STARTS_WITH_PAREN = compile("^\\s*\\(");
114+
private static final Pattern PARENS_TO_REMOVE = compile("(\\(.*\\bfrom\\b[^)]+\\))",
115115
CASE_INSENSITIVE | DOTALL | MULTILINE);
116-
private static final Pattern PROJECTION_CLAUSE = Pattern.compile("select\\s+(?:distinct\\s+)?(.+)\\s+from",
117-
Pattern.CASE_INSENSITIVE);
116+
private static final Pattern PROJECTION_CLAUSE = compile("select\\s+(?:distinct\\s+)?(.+)\\s+from", CASE_INSENSITIVE);
118117

119-
private static final Pattern NO_DIGITS = Pattern.compile("\\D+");
118+
private static final Pattern NO_DIGITS = compile("\\D+");
120119

121120
private static final String JOIN = "join\\s+(fetch\\s+)?" + IDENTIFIER + "\\s+(as\\s+)?" + IDENTIFIER_GROUP;
122-
private static final Pattern JOIN_PATTERN = Pattern.compile(JOIN, Pattern.CASE_INSENSITIVE);
121+
private static final Pattern JOIN_PATTERN = compile(JOIN, CASE_INSENSITIVE);
123122

124123
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
125-
private static final Pattern ORDER_BY = Pattern.compile("(order\\s+by\\s+)", CASE_INSENSITIVE);
126-
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern
127-
.compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)", CASE_INSENSITIVE);
124+
private static final Pattern ORDER_BY = compile("(order\\s+by\\s+)", CASE_INSENSITIVE);
125+
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)",
126+
CASE_INSENSITIVE);
128127

129-
private static final Pattern NAMED_PARAMETER = Pattern.compile(COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER,
128+
private static final Pattern NAMED_PARAMETER = compile(COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER,
130129
CASE_INSENSITIVE);
131130

132131
private static final Pattern CONSTRUCTOR_EXPRESSION;
@@ -137,7 +136,7 @@ public abstract class QueryUtils {
137136
private static final int VARIABLE_NAME_GROUP_INDEX = 4;
138137
private static final int COMPLEX_COUNT_FIRST_INDEX = 3;
139138

140-
private static final Pattern PUNCTATION_PATTERN = Pattern.compile(".*((?![._])[\\p{Punct}|\\s])");
139+
private static final Pattern PUNCTATION_PATTERN = compile(".*((?![._])[\\p{Punct}|\\s])");
141140
private static final Pattern FUNCTION_PATTERN;
142141
private static final Pattern FIELD_ALIAS_PATTERN;
143142

@@ -429,13 +428,14 @@ static Set<String> getFunctionAliases(String query) {
429428
}
430429

431430
private static String toJpaDirection(Order order) {
431+
432432
String direction = order.getDirection().name().toLowerCase(Locale.US);
433-
if (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) {
434-
direction += " nulls first";
435-
} else if (order.getNullHandling() == Sort.NullHandling.NULLS_LAST) {
436-
direction += " nulls last";
437-
}
438-
return direction;
433+
434+
return switch (order.getNullHandling()) {
435+
case NATIVE -> direction;
436+
case NULLS_FIRST -> direction + " nulls first";
437+
case NULLS_LAST -> direction + " nulls last";
438+
};
439439
}
440440

441441
/**
@@ -676,7 +676,7 @@ public static List<jakarta.persistence.criteria.Order> toOrders(Sort sort, From<
676676

677677
List<jakarta.persistence.criteria.Order> orders = new ArrayList<>();
678678

679-
for (org.springframework.data.domain.Sort.Order order : sort) {
679+
for (Order order : sort) {
680680
orders.add(toJpaOrder(order, from, cb));
681681
}
682682

@@ -833,7 +833,7 @@ private static boolean requiresOuterJoin(From<?, ?> from, PropertyPath property,
833833
// if this path is an optional one to one attribute navigated from the not owning side we also need an
834834
// explicit outer join to avoid https://hibernate.atlassian.net/browse/HHH-12712
835835
// and https://github.com/eclipse-ee4j/jpa-api/issues/170
836-
boolean isInverseOptionalOneToOne = PersistentAttributeType.ONE_TO_ONE == attribute.getPersistentAttributeType()
836+
boolean isInverseOptionalOneToOne = ONE_TO_ONE == attribute.getPersistentAttributeType()
837837
&& StringUtils.hasText(getAnnotationProperty(attribute, "mappedBy", ""));
838838

839839
boolean isLeafProperty = !property.hasNext();

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/DefaultQueryEnhancerUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
* TCK Tests for {@link DefaultQueryEnhancer}.
2727
*
2828
* @author Mark Paluch
29+
* @author Alim Naizabek
2930
*/
30-
public class DefaultQueryEnhancerUnitTests extends QueryEnhancerTckTests {
31+
class DefaultQueryEnhancerUnitTests extends QueryEnhancerTckTests {
3132

3233
@Override
3334
QueryEnhancer createQueryEnhancer(DeclaredQuery declaredQuery) {
@@ -49,7 +50,7 @@ void shouldApplySorting() {
4950
assertThat(sql).isEqualTo("SELECT e FROM Employee e order by e.foo asc, e.bar asc");
5051
}
5152

52-
@Test
53+
@Test // GH-3811
5354
void shouldApplySortingWithNullHandling() {
5455

5556
QueryEnhancer enhancer = createQueryEnhancer(DeclaredQuery.of("SELECT e FROM Employee e", true));

0 commit comments

Comments
 (0)