Skip to content

Commit 2d3edae

Browse files
committed
Draft: accounting for ignore case in sorting
1 parent 38f2af0 commit 2d3edae

File tree

4 files changed

+54
-30
lines changed

4 files changed

+54
-30
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,29 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati
8989

9090
SqlSort.validate(order);
9191

92-
OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
93-
OrderByField orderBy = simpleOrderByField.withNullHandling(order.getNullHandling());
94-
mappedOrder.add(order.isAscending() ? orderBy.asc() : orderBy.desc());
92+
if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
93+
mappedOrder.add(OrderByField.from(Expressions.just(sqlOrder.getProperty())));
94+
continue;
95+
}
96+
97+
Field field = createField(entity, order);
98+
99+
OrderByField orderByField = OrderByField.from( //
100+
table.column(field.getMappedColumnName()), //
101+
order.isAscending() ? Sort.Direction.ASC : Sort.Direction.DESC, //
102+
order.getNullHandling(), //
103+
order.isIgnoreCase() //
104+
);
105+
106+
mappedOrder.add(orderByField);
95107
}
96108

97109
return mappedOrder;
98110

99111
}
100112

101-
private OrderByField createSimpleOrderByField(Table table, RelationalPersistentEntity<?> entity, Sort.Order order) {
102-
103-
if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
104-
return OrderByField.from(Expressions.just(sqlOrder.getProperty()));
105-
}
106-
107-
Field field = createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext);
108-
return OrderByField.from(table.column(field.getMappedColumnName()));
113+
private Field createField(RelationalPersistentEntity<?> entity, Sort.Order order) {
114+
return createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext);
109115
}
110116

111117
/**

spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/OrderByNullPrecedence.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,11 @@ class SqlStandardOrderByNullPrecedence implements OrderByNullPrecedence {
5959

6060
@Override
6161
public String evaluate(Sort.NullHandling nullHandling) {
62-
63-
switch (nullHandling) {
64-
case NULLS_FIRST: return NULLS_FIRST;
65-
case NULLS_LAST: return NULLS_LAST;
66-
case NATIVE: return UNSPECIFIED;
67-
default:
68-
throw new UnsupportedOperationException("Sort.NullHandling " + nullHandling + " not supported");
69-
}
62+
return switch (nullHandling) {
63+
case NULLS_FIRST -> NULLS_FIRST;
64+
case NULLS_LAST -> NULLS_LAST;
65+
case NATIVE -> UNSPECIFIED;
66+
};
7067
}
7168
}
7269
}

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/OrderByField.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,25 @@
2626
*
2727
* @author Mark Paluch
2828
* @author Milan Milanov
29+
* @author Mikhail Polivakha
2930
* @since 1.1
3031
*/
3132
public class OrderByField extends AbstractSegment {
3233

3334
private final Expression expression;
3435
private final @Nullable Sort.Direction direction;
3536
private final Sort.NullHandling nullHandling;
37+
private final boolean caseInsensitive;
3638

37-
private OrderByField(Expression expression, @Nullable Direction direction, NullHandling nullHandling) {
39+
private OrderByField(Expression expression, @Nullable Direction direction, NullHandling nullHandling, boolean caseInsensitive) {
3840

3941
super(expression);
4042
Assert.notNull(expression, "Order by expression must not be null");
4143
Assert.notNull(nullHandling, "NullHandling by expression must not be null");
4244

4345
this.expression = expression;
4446
this.direction = direction;
47+
this.caseInsensitive = caseInsensitive;
4548
this.nullHandling = nullHandling;
4649
}
4750

@@ -52,38 +55,51 @@ private OrderByField(Expression expression, @Nullable Direction direction, NullH
5255
* @return the {@link OrderByField}.
5356
*/
5457
public static OrderByField from(Expression expression) {
55-
return new OrderByField(expression, null, NullHandling.NATIVE);
58+
return new OrderByField(expression, null, NullHandling.NATIVE, false);
5659
}
5760

5861
/**
5962
* Creates a new {@link OrderByField} from an {@link Expression} applying a given ordering.
6063
*
6164
* @param expression must not be {@literal null}.
62-
* @param direction order direction
65+
* @param direction order direction.
6366
* @return the {@link OrderByField}.
6467
*/
6568
public static OrderByField from(Expression expression, Direction direction) {
66-
return new OrderByField(expression, direction, NullHandling.NATIVE);
69+
return new OrderByField(expression, direction, NullHandling.NATIVE, false);
6770
}
6871

6972
/**
70-
* Creates a new {@link OrderByField} from a the current one using ascending sorting.
73+
* Creates a new {@link OrderByField} from an {@link Expression} applying a given ordering and {@link NullHandling}.
74+
*
75+
* @param expression must not be {@literal null}.
76+
* @param direction order direction.
77+
* @param nullHandling the null handling strategy.
78+
* @param caseInsensitive whether the ordering is case-insensitive.
79+
* @return the {@link OrderByField}.
80+
*/
81+
public static OrderByField from(Expression expression, Direction direction, NullHandling nullHandling, boolean caseInsensitive) {
82+
return new OrderByField(expression, direction, nullHandling, caseInsensitive);
83+
}
84+
85+
/**
86+
* Creates a new {@link OrderByField} from a current one using ascending sorting.
7187
*
7288
* @return the new {@link OrderByField} with ascending sorting.
7389
* @see #desc()
7490
*/
7591
public OrderByField asc() {
76-
return new OrderByField(expression, Direction.ASC, nullHandling);
92+
return new OrderByField(expression, Direction.ASC, nullHandling, caseInsensitive);
7793
}
7894

7995
/**
80-
* Creates a new {@link OrderByField} from a the current one using descending sorting.
96+
* Creates a new {@link OrderByField} from a current one using descending sorting.
8197
*
8298
* @return the new {@link OrderByField} with descending sorting.
8399
* @see #asc()
84100
*/
85101
public OrderByField desc() {
86-
return new OrderByField(expression, Direction.DESC, nullHandling);
102+
return new OrderByField(expression, Direction.DESC, nullHandling, caseInsensitive);
87103
}
88104

89105
/**
@@ -93,7 +109,7 @@ public OrderByField desc() {
93109
* @return the new {@link OrderByField} with {@link NullHandling} applied.
94110
*/
95111
public OrderByField withNullHandling(NullHandling nullHandling) {
96-
return new OrderByField(expression, direction, nullHandling);
112+
return new OrderByField(expression, direction, nullHandling, caseInsensitive);
97113
}
98114

99115
public Expression getExpression() {

spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/render/NameRenderer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,13 @@ static CharSequence fullyQualifiedReference(RenderContext context, Column column
9191
return render(context, namingStrategy.getReferenceName(column));
9292
}
9393

94-
return render(context, SqlIdentifier.from(namingStrategy.getReferenceName(column.getTable()),
95-
namingStrategy.getReferenceName(column)));
94+
return render( //
95+
context, //
96+
SqlIdentifier.from( //
97+
namingStrategy.getReferenceName(column.getTable()), //
98+
namingStrategy.getReferenceName(column) //
99+
)
100+
);
96101
}
97102

98103
/**

0 commit comments

Comments
 (0)