Skip to content

Allow for arbitrary conditions as join condition. #1014

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 3 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-995-non-trivial-join-condition-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-995-non-trivial-join-condition-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-995-non-trivial-join-condition-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-995-non-trivial-join-condition-SNAPSHOT</version>
</parent>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-995-non-trivial-join-condition-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-995-non-trivial-join-condition-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@ public SelectOnConditionComparison on(Expression column) {
return this;
}

@Override
public SelectFromAndJoinCondition on(Condition condition) {

if (this.condition == null) {
this.condition = condition;
} else {
this.condition = this.condition.and(condition);
}

return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.relational.core.sql.SelectBuilder.SelectOnConditionComparison#equals(org.springframework.data.relational.core.sql.Expression)
Expand All @@ -348,6 +360,12 @@ public SelectOnConditionComparison and(Expression column) {
}

private void finishCondition() {

// Nothing to do if a complete join condition was used.
if (from == null && to == null) {
return;
}

Comparison comparison = Comparison.create(from, "=", to);

if (condition == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,17 @@ interface SelectOn {
* @see Table#column(String)
*/
SelectOnConditionComparison on(Expression column);

/**
* Declare a join condition in one step.
*
* Using conditions allows more flexibility in comparison to {@link #on(Expression)} which only allows for equality comparisons chained together with `AND`.
*
* @param condition must not be {@literal null}.
* @return {@code this} builder.
* @see Conditions
*/
SelectFromAndJoinCondition on(Condition condition);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ void shouldRenderSimpleJoinWithAnd() {
+ "AND employee.tenant = department.tenant");
}

@Test // GH-995
public void shouldRenderArbitraryJoinCondition() {

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) //
.on(
Conditions.isEqual( employee.column("department_id"),department.column("id")) //
.or( //
Conditions.isNotEqual( employee.column("tenant"),department.column("tenant")) //
)) //
.build();

assertThat(SqlRenderer.toString(select)).isEqualTo("SELECT employee.id, department.name FROM employee " //
+ "JOIN department ON employee.department_id = department.id " //
+ "OR employee.tenant != department.tenant");
}

@Test // DATAJDBC-309
void shouldRenderMultipleJoinWithAnd() {

Expand Down