Skip to content

Commit 76538d8

Browse files
committed
Polishing.
Use switch expression, add author tags, reformat code. See #3811
1 parent aeb8d68 commit 76538d8

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

+20-20
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,26 @@ public abstract class QueryUtils {
108108
private static final String SIMPLE_COUNT_VALUE = "$2";
109109
private static final String COMPLEX_COUNT_VALUE = "$3 $6";
110110
private static final String COMPLEX_COUNT_LAST_VALUE = "$6";
111-
private static final Pattern ORDER_BY_PART = Pattern.compile("(?iu)\\s+order\\s+by\\s+.*", CASE_INSENSITIVE | DOTALL);
111+
private static final Pattern ORDER_BY_PART = compile("(?iu)\\s+order\\s+by\\s+.*", CASE_INSENSITIVE | DOTALL);
112112

113113
private static final Pattern ALIAS_MATCH;
114114
private static final Pattern COUNT_MATCH;
115-
private static final Pattern STARTS_WITH_PAREN = Pattern.compile("^\\s*\\(");
116-
private static final Pattern PARENS_TO_REMOVE = Pattern.compile("(\\(.*\\bfrom\\b[^)]+\\))",
115+
private static final Pattern STARTS_WITH_PAREN = compile("^\\s*\\(");
116+
private static final Pattern PARENS_TO_REMOVE = compile("(\\(.*\\bfrom\\b[^)]+\\))",
117117
CASE_INSENSITIVE | DOTALL | MULTILINE);
118-
private static final Pattern PROJECTION_CLAUSE = Pattern.compile("select\\s+(?:distinct\\s+)?(.+)\\s+from",
119-
Pattern.CASE_INSENSITIVE);
118+
private static final Pattern PROJECTION_CLAUSE = compile("select\\s+(?:distinct\\s+)?(.+)\\s+from", CASE_INSENSITIVE);
120119

121-
private static final Pattern NO_DIGITS = Pattern.compile("\\D+");
120+
private static final Pattern NO_DIGITS = compile("\\D+");
122121

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

126125
private static final String EQUALS_CONDITION_STRING = "%s.%s = :%s";
127-
private static final Pattern ORDER_BY = Pattern.compile("(order\\s+by\\s+)", CASE_INSENSITIVE);
128-
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = Pattern
129-
.compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)", CASE_INSENSITIVE);
126+
private static final Pattern ORDER_BY = compile("(order\\s+by\\s+)", CASE_INSENSITIVE);
127+
private static final Pattern ORDER_BY_IN_WINDOW_OR_SUBSELECT = compile("\\([\\s\\S]*order\\s+by\\s[\\s\\S]*\\)",
128+
CASE_INSENSITIVE);
130129

131-
private static final Pattern NAMED_PARAMETER = Pattern.compile(COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER,
130+
private static final Pattern NAMED_PARAMETER = compile(COLON_NO_DOUBLE_COLON + IDENTIFIER + "|#" + IDENTIFIER,
132131
CASE_INSENSITIVE);
133132

134133
private static final Pattern CONSTRUCTOR_EXPRESSION;
@@ -139,7 +138,7 @@ public abstract class QueryUtils {
139138
private static final int VARIABLE_NAME_GROUP_INDEX = 4;
140139
private static final int COMPLEX_COUNT_FIRST_INDEX = 3;
141140

142-
private static final Pattern PUNCTATION_PATTERN = Pattern.compile(".*((?![._])[\\p{Punct}|\\s])");
141+
private static final Pattern PUNCTATION_PATTERN = compile(".*((?![._])[\\p{Punct}|\\s])");
143142
private static final Pattern FUNCTION_PATTERN;
144143
private static final Pattern FIELD_ALIAS_PATTERN;
145144

@@ -431,13 +430,14 @@ static Set<String> getFunctionAliases(String query) {
431430
}
432431

433432
private static String toJpaDirection(Order order) {
433+
434434
String direction = order.getDirection().name().toLowerCase(Locale.US);
435-
if (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) {
436-
direction += " nulls first";
437-
} else if (order.getNullHandling() == Sort.NullHandling.NULLS_LAST) {
438-
direction += " nulls last";
439-
}
440-
return direction;
435+
436+
return switch (order.getNullHandling()) {
437+
case NATIVE -> direction;
438+
case NULLS_FIRST -> direction + " nulls first";
439+
case NULLS_LAST -> direction + " nulls last";
440+
};
441441
}
442442

443443
/**
@@ -677,7 +677,7 @@ public static List<jakarta.persistence.criteria.Order> toOrders(Sort sort, From<
677677

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

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

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

854854
boolean isLeafProperty = !property.hasNext();

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

+3-2
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)