Add support for all kinds of join to SelectBuilder.

Original pull request #1421
See #592
This commit is contained in:
Jens Schauder
2023-02-02 11:08:34 +01:00
parent 5334001ee2
commit 7e1bec2c62
3 changed files with 37 additions and 14 deletions

View File

@@ -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());

View File

@@ -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);
}
/**

View File

@@ -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() {