Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,29 @@ public List<OrderByField> getMappedSort(Table table, Sort sort, @Nullable Relati

SqlSort.validate(order);

OrderByField simpleOrderByField = createSimpleOrderByField(table, entity, order);
OrderByField orderBy = simpleOrderByField.withNullHandling(order.getNullHandling());
mappedOrder.add(order.isAscending() ? orderBy.asc() : orderBy.desc());
if (order instanceof SqlSort.SqlOrder sqlOrder && sqlOrder.isUnsafe()) {
mappedOrder.add(OrderByField.from(Expressions.just(sqlOrder.getProperty())));
continue;
}

Field field = createField(entity, order);

OrderByField orderByField = OrderByField.from( //
table.column(field.getMappedColumnName()), //
order.isAscending() ? Sort.Direction.ASC : Sort.Direction.DESC, //
order.getNullHandling(), //
order.isIgnoreCase() //
);

mappedOrder.add(orderByField);
}

return mappedOrder;

}

private OrderByField createSimpleOrderByField(Table table, RelationalPersistentEntity<?> entity, Sort.Order 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);
return OrderByField.from(table.column(field.getMappedColumnName()));
private Field createField(RelationalPersistentEntity<?> entity, Sort.Order order) {
return createPropertyField(entity, SqlIdentifier.unquoted(order.getProperty()), this.mappingContext);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,11 @@ class SqlStandardOrderByNullPrecedence implements OrderByNullPrecedence {

@Override
public String evaluate(Sort.NullHandling nullHandling) {

switch (nullHandling) {
case NULLS_FIRST: return NULLS_FIRST;
case NULLS_LAST: return NULLS_LAST;
case NATIVE: return UNSPECIFIED;
default:
throw new UnsupportedOperationException("Sort.NullHandling " + nullHandling + " not supported");
}
return switch (nullHandling) {
case NULLS_FIRST -> NULLS_FIRST;
case NULLS_LAST -> NULLS_LAST;
case NATIVE -> UNSPECIFIED;
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,25 @@
*
* @author Mark Paluch
* @author Milan Milanov
* @author Mikhail Polivakha
* @since 1.1
*/
public class OrderByField extends AbstractSegment {

private final Expression expression;
private final @Nullable Sort.Direction direction;
private final Sort.NullHandling nullHandling;
private final boolean caseInsensitive;

private OrderByField(Expression expression, @Nullable Direction direction, NullHandling nullHandling) {
private OrderByField(Expression expression, @Nullable Direction direction, NullHandling nullHandling, boolean caseInsensitive) {

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

this.expression = expression;
this.direction = direction;
this.caseInsensitive = caseInsensitive;
this.nullHandling = nullHandling;
}

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

/**
* Creates a new {@link OrderByField} from an {@link Expression} applying a given ordering.
*
* @param expression must not be {@literal null}.
* @param direction order direction
* @param direction order direction.
* @return the {@link OrderByField}.
*/
public static OrderByField from(Expression expression, Direction direction) {
return new OrderByField(expression, direction, NullHandling.NATIVE);
return new OrderByField(expression, direction, NullHandling.NATIVE, false);
}

/**
* Creates a new {@link OrderByField} from a the current one using ascending sorting.
* Creates a new {@link OrderByField} from an {@link Expression} applying a given ordering and {@link NullHandling}.
*
* @param expression must not be {@literal null}.
* @param direction order direction.
* @param nullHandling the null handling strategy.
* @param caseInsensitive whether the ordering is case-insensitive.
* @return the {@link OrderByField}.
*/
public static OrderByField from(Expression expression, Direction direction, NullHandling nullHandling, boolean caseInsensitive) {
return new OrderByField(expression, direction, nullHandling, caseInsensitive);
}

/**
* Creates a new {@link OrderByField} from a current one using ascending sorting.
*
* @return the new {@link OrderByField} with ascending sorting.
* @see #desc()
*/
public OrderByField asc() {
return new OrderByField(expression, Direction.ASC, nullHandling);
return new OrderByField(expression, Direction.ASC, nullHandling, caseInsensitive);
}

/**
* Creates a new {@link OrderByField} from a the current one using descending sorting.
* Creates a new {@link OrderByField} from a current one using descending sorting.
*
* @return the new {@link OrderByField} with descending sorting.
* @see #asc()
*/
public OrderByField desc() {
return new OrderByField(expression, Direction.DESC, nullHandling);
return new OrderByField(expression, Direction.DESC, nullHandling, caseInsensitive);
}

/**
Expand All @@ -93,7 +109,7 @@ public OrderByField desc() {
* @return the new {@link OrderByField} with {@link NullHandling} applied.
*/
public OrderByField withNullHandling(NullHandling nullHandling) {
return new OrderByField(expression, direction, nullHandling);
return new OrderByField(expression, direction, nullHandling, caseInsensitive);
}

public Expression getExpression() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ static CharSequence fullyQualifiedReference(RenderContext context, Column column
return render(context, namingStrategy.getReferenceName(column));
}

return render(context, SqlIdentifier.from(namingStrategy.getReferenceName(column.getTable()),
namingStrategy.getReferenceName(column)));
return render( //
context, //
SqlIdentifier.from( //
namingStrategy.getReferenceName(column.getTable()), //
namingStrategy.getReferenceName(column) //
)
);
}

/**
Expand Down