Skip to content

Commit 11384ab

Browse files
committed
Add public factory method to SubselectExpression.
This allows using Select instances as Expression where this isn't yet anticipated. Closes #1831
1 parent 112c084 commit 11384ab

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,14 @@ public class SubselectExpression extends Subselect implements Expression {
2727

2828
super(subselect);
2929
}
30+
31+
/**
32+
* Wraps a Select in a {@link SubselectExpression}, for using it as an expression in function calls or similar.
33+
*
34+
* @author Jens Schauder
35+
* @since 3.4
36+
*/
37+
public static Expression of(Select subselect) {
38+
return new SubselectExpression(subselect);
39+
}
3040
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.data.relational.core.sql.*;
2525
import org.springframework.util.StringUtils;
2626

27+
import java.util.List;
28+
2729
/**
2830
* Unit tests for {@link SqlRenderer}.
2931
*
@@ -413,6 +415,29 @@ void shouldRenderInSubselect() {
413415
.isEqualTo("SELECT foo.bar FROM foo WHERE foo.bar IN (SELECT floo.bah FROM floo)");
414416
}
415417

418+
@Test // GH-1831
419+
void shouldRenderSimpleFunctionWithSubselect() {
420+
421+
Table foo = SQL.table("foo");
422+
423+
Table floo = SQL.table("floo");
424+
Column bah = floo.column("bah");
425+
426+
427+
Select subselect = Select.builder().select(bah).from(floo).build();
428+
429+
SimpleFunction func = SimpleFunction.create("func", List.of(SubselectExpression.of(subselect)));
430+
431+
Select select = Select.builder() //
432+
.select(func.as("alias")) //
433+
.from(foo) //
434+
.where(Conditions.isEqual(func, SQL.literalOf(23))) //
435+
.build();
436+
437+
assertThat(SqlRenderer.toString(select))
438+
.isEqualTo("SELECT func(SELECT floo.bah FROM floo) AS alias FROM foo WHERE func(SELECT floo.bah FROM floo) = 23");
439+
}
440+
416441
@Test // DATAJDBC-309
417442
void shouldConsiderNamingStrategy() {
418443

@@ -678,8 +703,8 @@ class AnalyticFunctionsTests {
678703
void renderEmptyOver() {
679704

680705
Select select = StatementBuilder.select( //
681-
AnalyticFunction.create("MAX", salary) //
682-
) //
706+
AnalyticFunction.create("MAX", salary) //
707+
) //
683708
.from(employee) //
684709
.build();
685710

0 commit comments

Comments
 (0)