Browse Source

Do not prefix unsafe orer by expressions with table prefix.

Such expressions now get passed on unchanged.

This also deprecates org.springframework.data.r2dbc.query.QueryMapper.getMappedObject(Sort, RelationalPersistentEntity<?>).
It was only used in tests and translates an Order into another Order, which sounds wrong.

Closes #1512
Original pull request: #1513
pull/1649/head
Jens Schauder 3 years ago committed by Mark Paluch
parent
commit
e9459b860a
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 6
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/QueryMapper.java
  2. 2
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/QueryMapperUnitTests.java
  3. 12
      spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/query/QueryMapper.java
  4. 14
      spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/query/QueryMapperUnitTests.java

6
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/QueryMapper.java

@ -93,6 +93,8 @@ public class QueryMapper {
for (Sort.Order order : sort) { for (Sort.Order order : sort) {
SqlSort.validate(order);
OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order); OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
OrderByField orderBy = simpleOrderByField OrderByField orderBy = simpleOrderByField
.withNullHandling(order.getNullHandling()); .withNullHandling(order.getNullHandling());
@ -105,7 +107,9 @@ public class QueryMapper {
private OrderByField createSimpleOrderByField(Table table, RelationalPersistentEntity<?> entity, Sort.Order order) { private OrderByField createSimpleOrderByField(Table table, RelationalPersistentEntity<?> entity, Sort.Order order) {
SqlSort.validate(order); if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
return OrderByField.from(Expressions.just(sqlOrder.getProperty()));
}
Field field = createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext); Field field = createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext);
return OrderByField.from(table.column(field.getMappedColumnName())); return OrderByField.from(table.column(field.getMappedColumnName()));

2
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/QueryMapperUnitTests.java

@ -430,7 +430,7 @@ public class QueryMapperUnitTests {
assertThat(fields) // assertThat(fields) //
.extracting(Objects::toString) // .extracting(Objects::toString) //
.containsExactly("tbl." + unsafeExpression + " ASC"); .containsExactly( unsafeExpression + " ASC");
} }
private Condition map(Criteria criteria) { private Condition map(Criteria criteria) {

12
spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/query/QueryMapper.java

@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.MappingException; import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentPropertyPath; import org.springframework.data.mapping.PersistentPropertyPath;
@ -50,6 +51,8 @@ import org.springframework.r2dbc.core.binding.MutableBindings;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import static org.springframework.data.relational.core.sql.Expressions.*;
/** /**
* Maps {@link CriteriaDefinition} and {@link Sort} objects considering mapping metadata and dialect-specific * Maps {@link CriteriaDefinition} and {@link Sort} objects considering mapping metadata and dialect-specific
* conversion. * conversion.
@ -102,7 +105,9 @@ public class QueryMapper {
* @param sort must not be {@literal null}. * @param sort must not be {@literal null}.
* @param entity related {@link RelationalPersistentEntity}, can be {@literal null}. * @param entity related {@link RelationalPersistentEntity}, can be {@literal null}.
* @return * @return
* @deprecated without replacement.
*/ */
@Deprecated(since = "3.2", forRemoval = true)
public Sort getMappedObject(Sort sort, @Nullable RelationalPersistentEntity<?> entity) { public Sort getMappedObject(Sort sort, @Nullable RelationalPersistentEntity<?> entity) {
if (entity == null) { if (entity == null) {
@ -137,6 +142,8 @@ public class QueryMapper {
for (Sort.Order order : sort) { for (Sort.Order order : sort) {
SqlSort.validate(order);
OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order); OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
OrderByField orderBy = simpleOrderByField OrderByField orderBy = simpleOrderByField
.withNullHandling(order.getNullHandling()); .withNullHandling(order.getNullHandling());
@ -147,9 +154,12 @@ public class QueryMapper {
} }
private OrderByField createSimpleOrderByField(Table table, RelationalPersistentEntity<?> entity, Sort.Order order) { private OrderByField createSimpleOrderByField(Table table, RelationalPersistentEntity<?> entity, Sort.Order order) {
SqlSort.validate(order); if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
return OrderByField.from(Expressions.just(sqlOrder.getProperty()));
}
Field field = createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext); Field field = createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext);
return OrderByField.from(table.column(field.getMappedColumnName())); return OrderByField.from(table.column(field.getMappedColumnName()));

14
spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/query/QueryMapperUnitTests.java

@ -39,6 +39,7 @@ import org.springframework.data.relational.core.sql.Expression;
import org.springframework.data.relational.core.sql.Functions; import org.springframework.data.relational.core.sql.Functions;
import org.springframework.data.relational.core.sql.OrderByField; import org.springframework.data.relational.core.sql.OrderByField;
import org.springframework.data.relational.core.sql.Table; import org.springframework.data.relational.core.sql.Table;
import org.springframework.data.relational.domain.SqlSort;
import org.springframework.r2dbc.core.Parameter; import org.springframework.r2dbc.core.Parameter;
import org.springframework.r2dbc.core.binding.BindMarkersFactory; import org.springframework.r2dbc.core.binding.BindMarkersFactory;
import org.springframework.r2dbc.core.binding.BindTarget; import org.springframework.r2dbc.core.binding.BindTarget;
@ -440,6 +441,19 @@ class QueryMapperUnitTests {
.containsExactly("tbl.unknownField DESC"); .containsExactly("tbl.unknownField DESC");
} }
@Test // GH-1512
public void shouldTablePrefixUnsafeOrderExpression() {
Sort sort = Sort.by(SqlSort.SqlOrder.desc("unknownField").withUnsafe());
List<OrderByField> fields = mapper.getMappedSort(Table.create("tbl"), sort,
mapper.getMappingContext().getRequiredPersistentEntity(Person.class));
assertThat(fields) //
.extracting(Objects::toString) //
.containsExactly("unknownField DESC");
}
@Test // GH-1507 @Test // GH-1507
public void shouldMapSortWithAllowedSpecialCharacters() { public void shouldMapSortWithAllowedSpecialCharacters() {

Loading…
Cancel
Save