Skip to content

Commit 6e626ab

Browse files
committed
Allow for arbitrary conditions as join condition.
#Closes 995.
1 parent 620bf07 commit 6e626ab

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ public SelectOnConditionComparison on(Expression column) {
324324
return this;
325325
}
326326

327+
@Override
328+
public SelectFromAndJoinCondition on(Condition condition) {
329+
330+
if (this.condition == null) {
331+
this.condition = condition;
332+
} else {
333+
this.condition = this.condition.and(condition);
334+
}
335+
336+
return this;
337+
}
338+
327339
/*
328340
* (non-Javadoc)
329341
* @see org.springframework.data.relational.core.sql.SelectBuilder.SelectOnConditionComparison#equals(org.springframework.data.relational.core.sql.Expression)
@@ -347,6 +359,12 @@ public SelectOnConditionComparison and(Expression column) {
347359
}
348360

349361
private void finishCondition() {
362+
363+
// Nothing to do if a complete join condition was used.
364+
if (from == null && to == null) {
365+
return;
366+
}
367+
350368
Comparison comparison = Comparison.create(from, "=", to);
351369

352370
if (condition == null) {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ interface SelectOn {
500500
* @see Table#column(String)
501501
*/
502502
SelectOnConditionComparison on(Expression column);
503+
504+
/**
505+
* Declare a join condition in one step.
506+
*
507+
* This is harder to use but more flexible then the fluent style of {@link #on(Expression)} which only allows for equality comparisons chained together with `AND`.
508+
*
509+
* @param condition Must not be {@literal null}.
510+
* @return @return {@code this} builder.
511+
* @see Conditions
512+
*/
513+
SelectFromAndJoinCondition on(Condition condition);
503514
}
504515

505516
/**

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ public void shouldRenderSimpleJoinWithAnd() {
156156
+ "AND employee.tenant = department.tenant");
157157
}
158158

159+
@Test // GH-995
160+
public void shouldRenderArbitraryJoinCondition() {
161+
162+
Table employee = SQL.table("employee");
163+
Table department = SQL.table("department");
164+
165+
Select select = Select.builder() //
166+
.select(employee.column("id"), department.column("name")) //
167+
.from(employee) //
168+
.join(department) //
169+
.on(
170+
Conditions.isEqual( employee.column("department_id"),department.column("id")) //
171+
.or( //
172+
Conditions.isNotEqual( employee.column("tenant"),department.column("tenant")) //
173+
)) //
174+
.build();
175+
176+
assertThat(SqlRenderer.toString(select)).isEqualTo("SELECT employee.id, department.name FROM employee " //
177+
+ "JOIN department ON employee.department_id = department.id " //
178+
+ "OR employee.tenant != department.tenant");
179+
}
180+
159181
@Test // DATAJDBC-309
160182
public void shouldRenderMultipleJoinWithAnd() {
161183

0 commit comments

Comments
 (0)