Skip to content

Commit 7e1bec2

Browse files
committed
Add support for all kinds of join to SelectBuilder.
Original pull request #1421 See #592
1 parent 5334001 commit 7e1bec2

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

Diff for: spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/DefaultSelectBuilder.java

+11
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ public SelectOn leftOuterJoin(TableLike table) {
181181
return new JoinBuilder(table, this, JoinType.LEFT_OUTER_JOIN);
182182
}
183183

184+
@Override
185+
public SelectOn join(TableLike table, JoinType joinType) {
186+
return new JoinBuilder(table, this, joinType);
187+
}
188+
184189
public DefaultSelectBuilder join(Join join) {
185190
this.joins.add(join);
186191

@@ -323,6 +328,12 @@ public SelectOn leftOuterJoin(TableLike table) {
323328
return selectBuilder.leftOuterJoin(table);
324329
}
325330

331+
@Override
332+
public SelectOn join(TableLike table, JoinType joinType) {
333+
selectBuilder.join(finishJoin());
334+
return selectBuilder.join(table, joinType);
335+
}
336+
326337
@Override
327338
public SelectFromAndJoin limitOffset(long limit, long offset) {
328339
selectBuilder.join(finishJoin());

Diff for: spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/SelectBuilder.java

+10
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,16 @@ interface SelectJoin extends SelectLock, BuildSelect {
486486
* @see SQL#table(String)
487487
*/
488488
SelectOn leftOuterJoin(TableLike table);
489+
490+
/**
491+
* Declar a join, where the join type ({@code INNER}, {@code LEFT OUTER}, {@code RIGHT OUTER}, {@code FULL OUTER})
492+
* is specified by an extra argument.
493+
*
494+
* @param table the table to join. Must not be {@literal null}.
495+
* @param joinType the type of joi. Must not be {@literal null}.
496+
* @return {@code this} builder.
497+
*/
498+
SelectOn join(TableLike table, Join.JoinType joinType);
489499
}
490500

491501
/**

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

+16-14
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222
import org.springframework.data.relational.core.dialect.PostgresDialect;
2323
import org.springframework.data.relational.core.dialect.RenderContextFactory;
24-
import org.springframework.data.relational.core.sql.AnalyticFunction;
25-
import org.springframework.data.relational.core.sql.Column;
26-
import org.springframework.data.relational.core.sql.Comparison;
27-
import org.springframework.data.relational.core.sql.Conditions;
28-
import org.springframework.data.relational.core.sql.Expressions;
29-
import org.springframework.data.relational.core.sql.Functions;
30-
import org.springframework.data.relational.core.sql.InlineQuery;
31-
import org.springframework.data.relational.core.sql.LockMode;
32-
import org.springframework.data.relational.core.sql.OrderByField;
33-
import org.springframework.data.relational.core.sql.SQL;
34-
import org.springframework.data.relational.core.sql.Select;
35-
import org.springframework.data.relational.core.sql.SqlIdentifier;
36-
import org.springframework.data.relational.core.sql.StatementBuilder;
37-
import org.springframework.data.relational.core.sql.Table;
24+
import org.springframework.data.relational.core.sql.*;
3825
import org.springframework.util.StringUtils;
3926

4027
/**
@@ -154,6 +141,21 @@ void shouldRenderOuterJoin() {
154141
+ "LEFT OUTER JOIN department ON employee.department_id = department.id");
155142
}
156143

144+
@Test // GH-1421
145+
void shouldRenderFullOuterJoin() {
146+
147+
Table employee = SQL.table("employee");
148+
Table department = SQL.table("department");
149+
150+
Select select = Select.builder().select(employee.column("id"), department.column("name")) //
151+
.from(employee) //
152+
.join(department, Join.JoinType.FULL_OUTER_JOIN).on(employee.column("department_id")).equals(department.column("id")) //
153+
.build();
154+
155+
assertThat(SqlRenderer.toString(select)).isEqualTo("SELECT employee.id, department.name FROM employee "
156+
+ "FULL OUTER JOIN department ON employee.department_id = department.id");
157+
}
158+
157159
@Test // DATAJDBC-309
158160
void shouldRenderSimpleJoinWithAnd() {
159161

0 commit comments

Comments
 (0)