From 7e1bec2c626d113704da17275782ff8b1f9ce7c7 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Thu, 2 Feb 2023 11:08:34 +0100 Subject: [PATCH] Add support for all kinds of join to SelectBuilder. Original pull request #1421 See #592 --- .../core/sql/DefaultSelectBuilder.java | 11 +++++++ .../relational/core/sql/SelectBuilder.java | 10 +++++++ .../sql/render/SelectRendererUnitTests.java | 30 ++++++++++--------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/DefaultSelectBuilder.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/DefaultSelectBuilder.java index 96936fd9..cc6dc2f4 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/DefaultSelectBuilder.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/DefaultSelectBuilder.java @@ -181,6 +181,11 @@ class DefaultSelectBuilder implements SelectBuilder, SelectAndFrom, SelectFromAn return new JoinBuilder(table, this, JoinType.LEFT_OUTER_JOIN); } + @Override + public SelectOn join(TableLike table, JoinType joinType) { + return new JoinBuilder(table, this, joinType); + } + public DefaultSelectBuilder join(Join join) { this.joins.add(join); @@ -323,6 +328,12 @@ class DefaultSelectBuilder implements SelectBuilder, SelectAndFrom, SelectFromAn return selectBuilder.leftOuterJoin(table); } + @Override + public SelectOn join(TableLike table, JoinType joinType) { + selectBuilder.join(finishJoin()); + return selectBuilder.join(table, joinType); + } + @Override public SelectFromAndJoin limitOffset(long limit, long offset) { selectBuilder.join(finishJoin()); diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/SelectBuilder.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/SelectBuilder.java index aa6e2bd3..1c6d2560 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/SelectBuilder.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/sql/SelectBuilder.java @@ -486,6 +486,16 @@ public interface SelectBuilder { * @see SQL#table(String) */ SelectOn leftOuterJoin(TableLike table); + + /** + * Declar a join, where the join type ({@code INNER}, {@code LEFT OUTER}, {@code RIGHT OUTER}, {@code FULL OUTER}) + * is specified by an extra argument. + * + * @param table the table to join. Must not be {@literal null}. + * @param joinType the type of joi. Must not be {@literal null}. + * @return {@code this} builder. + */ + SelectOn join(TableLike table, Join.JoinType joinType); } /** diff --git a/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java b/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java index 5ebf971e..a613fdcb 100644 --- a/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java +++ b/spring-data-relational/src/test/java/org/springframework/data/relational/core/sql/render/SelectRendererUnitTests.java @@ -21,20 +21,7 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.data.relational.core.dialect.PostgresDialect; import org.springframework.data.relational.core.dialect.RenderContextFactory; -import org.springframework.data.relational.core.sql.AnalyticFunction; -import org.springframework.data.relational.core.sql.Column; -import org.springframework.data.relational.core.sql.Comparison; -import org.springframework.data.relational.core.sql.Conditions; -import org.springframework.data.relational.core.sql.Expressions; -import org.springframework.data.relational.core.sql.Functions; -import org.springframework.data.relational.core.sql.InlineQuery; -import org.springframework.data.relational.core.sql.LockMode; -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.SqlIdentifier; -import org.springframework.data.relational.core.sql.StatementBuilder; -import org.springframework.data.relational.core.sql.Table; +import org.springframework.data.relational.core.sql.*; import org.springframework.util.StringUtils; /** @@ -154,6 +141,21 @@ class SelectRendererUnitTests { + "LEFT OUTER JOIN department ON employee.department_id = department.id"); } + @Test // GH-1421 + void shouldRenderFullOuterJoin() { + + Table employee = SQL.table("employee"); + Table department = SQL.table("department"); + + Select select = Select.builder().select(employee.column("id"), department.column("name")) // + .from(employee) // + .join(department, Join.JoinType.FULL_OUTER_JOIN).on(employee.column("department_id")).equals(department.column("id")) // + .build(); + + assertThat(SqlRenderer.toString(select)).isEqualTo("SELECT employee.id, department.name FROM employee " + + "FULL OUTER JOIN department ON employee.department_id = department.id"); + } + @Test // DATAJDBC-309 void shouldRenderSimpleJoinWithAnd() {