Skip to content

Feature: Support order by SimpleFunction and SimpleExpression #1348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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 @@ -16,22 +16,29 @@
package org.springframework.data.relational.core.sql.render;

import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.Expressions;
import org.springframework.data.relational.core.sql.OrderByField;
import org.springframework.data.relational.core.sql.SimpleFunction;
import org.springframework.data.relational.core.sql.Visitable;
import org.springframework.lang.Nullable;

/**
* {@link PartRenderer} for {@link OrderByField}s.
*
* @author Mark Paluch
* @author Jens Schauder
* @author Chirag Tailor
* @author Koen Punt
* @since 1.1
*/
class OrderByClauseVisitor extends TypedSubtreeVisitor<OrderByField> implements PartRenderer {

private final RenderContext context;

private final StringBuilder builder = new StringBuilder();

@Nullable private PartRenderer delegate;

private boolean first = true;

OrderByClauseVisitor(RenderContext context) {
Expand Down Expand Up @@ -68,8 +75,32 @@ Delegation leaveMatched(OrderByField segment) {
return Delegation.leave();
}

@Override
Delegation enterNested(Visitable segment) {
if (segment instanceof SimpleFunction) {
delegate = new SimpleFunctionVisitor(context);
return Delegation.delegateTo((SimpleFunctionVisitor)delegate);
}

if (segment instanceof Expressions.SimpleExpression) {
delegate = new ExpressionVisitor(context);
return Delegation.delegateTo((ExpressionVisitor)delegate);
}

return super.enterNested(segment);
}

@Override
Delegation leaveNested(Visitable segment) {
if (delegate instanceof SimpleFunctionVisitor) {
builder.append(delegate.getRenderedPart());
delegate = null;
}

if (delegate instanceof ExpressionVisitor) {
builder.append(delegate.getRenderedPart());
delegate = null;
}

if (segment instanceof Column) {
builder.append(NameRenderer.fullyQualifiedReference(context, (Column) segment));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@

import org.junit.jupiter.api.Test;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.Expression;
import org.springframework.data.relational.core.sql.Expressions;
import org.springframework.data.relational.core.sql.OrderByField;
import org.springframework.data.relational.core.sql.SQL;
import org.springframework.data.relational.core.sql.Select;
import org.springframework.data.relational.core.sql.SimpleFunction;
import org.springframework.data.relational.core.sql.Table;

import java.util.Arrays;
import java.util.List;

/**
* Unit tests for {@link OrderByClauseVisitor}.
*
* @author Mark Paluch
* @author Jens Schauder
* @author Koen Punt
*/
class OrderByClauseVisitorUnitTests {

Expand Down Expand Up @@ -88,4 +95,38 @@ void shouldRenderOrderByFullyQualifiedNameWithTableAlias() {
assertThat(visitor.getRenderedPart().toString()).isEqualTo("emp.name ASC");
}

@Test // GH-1348
void shouldRenderOrderBySimpleFunction() {

Table employee = SQL.table("employee").as("emp");
Column column = employee.column("name");
List<Expression> columns = Arrays.asList(employee.column("id"), column);

SimpleFunction simpleFunction = SimpleFunction.create("GREATEST", columns);

Select select = Select.builder().select(column).from(employee)
.orderBy(OrderByField.from(simpleFunction).asc(), OrderByField.from(column).asc()).build();

OrderByClauseVisitor visitor = new OrderByClauseVisitor(new SimpleRenderContext(NamingStrategies.asIs()));
select.visit(visitor);

assertThat(visitor.getRenderedPart().toString()).isEqualTo("GREATEST(emp.id, emp.name) ASC, emp.name ASC");
}

@Test // GH-1348
void shouldRenderOrderBySimpleExpression() {

Table employee = SQL.table("employee").as("emp");
Column column = employee.column("name");

Expression simpleExpression = Expressions.just("1");

Select select = Select.builder().select(column).from(employee).orderBy(OrderByField.from(simpleExpression).asc())
.build();

OrderByClauseVisitor visitor = new OrderByClauseVisitor(new SimpleRenderContext(NamingStrategies.asIs()));
select.visit(visitor);

assertThat(visitor.getRenderedPart().toString()).isEqualTo("1 ASC");
}
}