DATAJPA-965 - Polishing.

Removed some unnecessary overrides in JpaOrder.
This commit is contained in:
Oliver Gierke
2016-09-14 11:34:26 +02:00
parent a22c17fc12
commit d7b3b179ba
3 changed files with 17 additions and 32 deletions

View File

@@ -241,6 +241,7 @@ public class JpaSort extends Sort {
Assert.notEmpty(properties, "Properties must not be empty!");
List<Order> orders = new ArrayList<Order>();
for (String property : properties) {
orders.add(new JpaOrder(direction, property));
}
@@ -309,10 +310,17 @@ public class JpaSort extends Sort {
}
/**
* Custom {@link Order} that keeps a flag to indicate unsafe property handling, i.e. the String provided is not
* necessarily a property but can be an arbitrary expression piped into the query execution. We also keep an
* additional {@code ignoreCase} flag around as the constructor of the superclass is private currently.
*
* @author Christoph Strobl
* @author Oliver Gierke
*/
public static class JpaOrder extends Order {
private static final long serialVersionUID = 1L;
private final boolean unsafe;
private final boolean ignoreCase;
@@ -365,32 +373,6 @@ public class JpaSort extends Sort {
return new JpaOrder(getDirection(), getProperty(), nullHandling, isIgnoreCase(), this.unsafe);
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Sort.Order#nullsFirst()
*/
@Override
public JpaOrder nullsFirst() {
return with(NullHandling.NULLS_FIRST);
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Sort.Order#nullsLast()
*/
@Override
public JpaOrder nullsLast() {
return with(NullHandling.NULLS_LAST);
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Sort.Order#nullsNative()
*/
public JpaOrder nullsNative() {
return with(NullHandling.NATIVE);
}
/**
* Creates new {@link Sort} with potentially unsafe {@link Order} instances.
*
@@ -403,9 +385,11 @@ public class JpaSort extends Sort {
Assert.noNullElements(properties, "Properties must not contain null values!");
List<Order> orders = new ArrayList<Order>();
for (String property : properties) {
orders.add(new JpaOrder(getDirection(), property, getNullHandling(), isIgnoreCase(), this.unsafe));
}
return new Sort(orders);
}

View File

@@ -75,7 +75,6 @@ public abstract class QueryUtils {
public static final String COUNT_QUERY_STRING = "select count(%s) from %s x";
public static final String DELETE_ALL_QUERY_STRING = "delete from %s x";
private static final String DEFAULT_ALIAS = "x";
private static final String COUNT_REPLACEMENT_TEMPLATE = "select count(%s) $5$6$7";
private static final String SIMPLE_COUNT_VALUE = "$2";
private static final String COMPLEX_COUNT_VALUE = "$3$6";
@@ -106,6 +105,10 @@ public abstract class QueryUtils {
private static final String FUNCTION_ALIAS_GROUP_NAME = "alias";
private static final Pattern FUNCTION_PATTERN;
private static final String UNSAFE_PROPERTY_REFERENCE = "Sort expression '%s' must only contain property references or "
+ "aliases used in the select clause. If you really want to use something other than that for sorting, please use "
+ "JpaSort.unsafe(…)!";
static {
StringBuilder builder = new StringBuilder();
@@ -298,7 +301,7 @@ public abstract class QueryUtils {
* @param query
* @return
*/
static Set<String> getFunctionAliases(String query) {
private static Set<String> getFunctionAliases(String query) {
Set<String> result = new HashSet<String>();
Matcher matcher = FUNCTION_PATTERN.matcher(query);
@@ -306,6 +309,7 @@ public abstract class QueryUtils {
while (matcher.find()) {
String alias = matcher.group(FUNCTION_ALIAS_GROUP_NAME);
if (StringUtils.hasText(alias)) {
result.add(alias);
}
@@ -629,8 +633,7 @@ public abstract class QueryUtils {
}
if (PUNCTATION_PATTERN.matcher(order.getProperty()).find()) {
throw new InvalidDataAccessApiUsageException(String
.format("Sort expression '%s' must not contain functions or expressions. Please use JpaSort.unsafe.", order));
throw new InvalidDataAccessApiUsageException(String.format(UNSAFE_PROPERTY_REFERENCE, order));
}
}
}

View File

@@ -48,9 +48,7 @@ public class QueryUtilsUnitTests {
@Test
public void createsCountQueryCorrectly() throws Exception {
assertCountQuery(QUERY, COUNT_QUERY);
}
/**