Skip to content

Commit 82c70ab

Browse files
schaudermp911de
authored andcommitted
Allow for arbitrary conditions as join condition.
Closes #995. Original pull request: #1014.
1 parent d47020d commit 82c70ab

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

+18
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ public SelectOnConditionComparison on(Expression column) {
325325
return this;
326326
}
327327

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

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

353371
if (condition == null) {

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

+11
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,17 @@ interface SelectOn {
501501
* @see Table#column(String)
502502
*/
503503
SelectOnConditionComparison on(Expression column);
504+
505+
/**
506+
* Declare a join condition in one step.
507+
*
508+
* Using conditions allows more flexibility in comparison to {@link #on(Expression)} which only allows for equality comparisons chained together with `AND`.
509+
*
510+
* @param condition must not be {@literal null}.
511+
* @return {@code this} builder.
512+
* @see Conditions
513+
*/
514+
SelectFromAndJoinCondition on(Condition condition);
504515
}
505516

506517
/**

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

+22
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ 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
void shouldRenderMultipleJoinWithAnd() {
161183

0 commit comments

Comments
 (0)