Skip to content

Commit aad6bdd

Browse files
committed
Support SimpleExpression as order by expression
To allow sorting on arbitrary SQL, for example the column index instead of a name. But also more advanced things like functions that can't be expressed using a `SimpleFunction`. orderBy(OrderByField.from(Expressions.just("1")).asc()) => ORDER BY 1 ASC
1 parent 4fabab0 commit aad6bdd

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.relational.core.sql.render;
1717

1818
import org.springframework.data.relational.core.sql.Column;
19+
import org.springframework.data.relational.core.sql.Expressions;
1920
import org.springframework.data.relational.core.sql.OrderByField;
2021
import org.springframework.data.relational.core.sql.SimpleFunction;
2122
import org.springframework.data.relational.core.sql.Visitable;
@@ -81,6 +82,11 @@ Delegation enterNested(Visitable segment) {
8182
return Delegation.delegateTo((SimpleFunctionVisitor)delegate);
8283
}
8384

85+
if (segment instanceof Expressions.SimpleExpression) {
86+
delegate = new ExpressionVisitor(context);
87+
return Delegation.delegateTo((ExpressionVisitor)delegate);
88+
}
89+
8490
return super.enterNested(segment);
8591
}
8692

@@ -90,6 +96,10 @@ Delegation leaveNested(Visitable segment) {
9096
builder.append(delegate.getRenderedPart());
9197
}
9298

99+
if (delegate instanceof ExpressionVisitor) {
100+
builder.append(delegate.getRenderedPart());
101+
}
102+
93103
if (segment instanceof Column) {
94104
builder.append(NameRenderer.fullyQualifiedReference(context, (Column) segment));
95105
}

spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/OrderByClauseVisitorUnitTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import org.junit.jupiter.api.Test;
2121
import org.springframework.data.relational.core.sql.Column;
22+
import org.springframework.data.relational.core.sql.Expression;
23+
import org.springframework.data.relational.core.sql.Expressions;
2224
import org.springframework.data.relational.core.sql.OrderByField;
2325
import org.springframework.data.relational.core.sql.SQL;
2426
import org.springframework.data.relational.core.sql.Select;
@@ -111,4 +113,20 @@ void shouldRenderOrderBySimpleFunction() {
111113
assertThat(visitor.getRenderedPart().toString()).isEqualTo("GREATEST(emp.id, emp.name) ASC");
112114
}
113115

116+
@Test // GH-1348
117+
void shouldRenderOrderBySimpleExpression() {
118+
119+
Table employee = SQL.table("employee").as("emp");
120+
Column column = employee.column("name");
121+
122+
Expression simpleExpression = Expressions.just("1");
123+
124+
Select select = Select.builder().select(column).from(employee).orderBy(OrderByField.from(simpleExpression).asc())
125+
.build();
126+
127+
OrderByClauseVisitor visitor = new OrderByClauseVisitor(new SimpleRenderContext(NamingStrategies.asIs()));
128+
select.visit(visitor);
129+
130+
assertThat(visitor.getRenderedPart().toString()).isEqualTo("1 ASC");
131+
}
114132
}

0 commit comments

Comments
 (0)