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 edd497b9c9
commit 01325e62e2
4 changed files with 17 additions and 34 deletions

View File

@@ -241,6 +241,7 @@ public class JpaSort extends Sort {
Assert.notEmpty(properties, "Properties must not be empty!"); Assert.notEmpty(properties, "Properties must not be empty!");
List<Order> orders = new ArrayList<Order>(); List<Order> orders = new ArrayList<Order>();
for (String property : properties) { for (String property : properties) {
orders.add(new JpaOrder(direction, property)); 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 Christoph Strobl
* @author Oliver Gierke
*/ */
public static class JpaOrder extends Order { public static class JpaOrder extends Order {
private static final long serialVersionUID = 1L;
private final boolean unsafe; private final boolean unsafe;
private final boolean ignoreCase; private final boolean ignoreCase;
@@ -365,32 +373,6 @@ public class JpaSort extends Sort {
return new JpaOrder(getDirection(), getProperty(), nullHandling, isIgnoreCase(), this.unsafe); 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. * 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!"); Assert.noNullElements(properties, "Properties must not contain null values!");
List<Order> orders = new ArrayList<Order>(); List<Order> orders = new ArrayList<Order>();
for (String property : properties) { for (String property : properties) {
orders.add(new JpaOrder(getDirection(), property, getNullHandling(), isIgnoreCase(), this.unsafe)); orders.add(new JpaOrder(getDirection(), property, getNullHandling(), isIgnoreCase(), this.unsafe));
} }
return new Sort(orders); 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 COUNT_QUERY_STRING = "select count(%s) from %s x";
public static final String DELETE_ALL_QUERY_STRING = "delete 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 COUNT_REPLACEMENT_TEMPLATE = "select count(%s) $5$6$7";
private static final String SIMPLE_COUNT_VALUE = "$2"; private static final String SIMPLE_COUNT_VALUE = "$2";
private static final String COMPLEX_COUNT_VALUE = "$3$6"; private static final String COMPLEX_COUNT_VALUE = "$3$6";
@@ -109,6 +108,10 @@ public abstract class QueryUtils {
private static final String FUNCTION_ALIAS_GROUP_NAME = "alias"; private static final String FUNCTION_ALIAS_GROUP_NAME = "alias";
private static final Pattern FUNCTION_PATTERN; 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 { static {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
@@ -315,7 +318,7 @@ public abstract class QueryUtils {
* @param query * @param query
* @return * @return
*/ */
static Set<String> getFunctionAliases(String query) { private static Set<String> getFunctionAliases(String query) {
Set<String> result = new HashSet<String>(); Set<String> result = new HashSet<String>();
Matcher matcher = FUNCTION_PATTERN.matcher(query); Matcher matcher = FUNCTION_PATTERN.matcher(query);
@@ -323,6 +326,7 @@ public abstract class QueryUtils {
while (matcher.find()) { while (matcher.find()) {
String alias = matcher.group(FUNCTION_ALIAS_GROUP_NAME); String alias = matcher.group(FUNCTION_ALIAS_GROUP_NAME);
if (StringUtils.hasText(alias)) { if (StringUtils.hasText(alias)) {
result.add(alias); result.add(alias);
} }
@@ -675,9 +679,7 @@ public abstract class QueryUtils {
} }
if (PUNCTATION_PATTERN.matcher(order.getProperty()).find()) { if (PUNCTATION_PATTERN.matcher(order.getProperty()).find()) {
throw new InvalidDataAccessApiUsageException(String throw new InvalidDataAccessApiUsageException(String.format(UNSAFE_PROPERTY_REFERENCE, order));
.format("Sort expression '%s' must not contain functions or expressions. Please use JpaSort.unsafe.", order));
} }
} }
} }

View File

@@ -226,5 +226,4 @@ public class JpaSortTests {
assertThat(sort.getOrderFor("colleagues.roles.name"), is(not(instanceOf(JpaOrder.class)))); assertThat(sort.getOrderFor("colleagues.roles.name"), is(not(instanceOf(JpaOrder.class))));
assertThat(sort.getOrderFor("foo.bar"), is(instanceOf(JpaOrder.class))); assertThat(sort.getOrderFor("foo.bar"), is(instanceOf(JpaOrder.class)));
} }
} }

View File

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