Skip to content

Commit

Permalink
In the convenience methods for join, make column equality optional (#…
Browse files Browse the repository at this point in the history
…1692)

* In the convenience methods for `join`, make column equality optional

Make `onColumn` and `otherColumn` nullable and default `null` in `innerJoin`,
`leftJoin`, `rightJoin`, `fullJoin`, `crossJoin`.

This allows for a more concise syntax when only wanting to use
`additionalConstraint`, as `join` already allows. Example:

```kotlin
A.innerJoin(B) { A.parentId eq B.parentId }
```

* fixup! In the convenience methods for `join`, make column equality optional

---------

Co-authored-by: leonid.stashevsky <leonid.stashevsky@gmail.com>
  • Loading branch information
yeogai and e5l authored Jan 15, 2024
1 parent 29203d6 commit 6d06fdb
Showing 1 changed file with 45 additions and 20 deletions.
65 changes: 45 additions & 20 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,45 +137,70 @@ abstract class ColumnSet : FieldSet {
fun select(columns: List<Expression<*>>): Query = Query(Select(this, columns), null)
}

/** Creates an inner join relation with [otherTable] using [onColumn] and [otherColumn] as the join condition. */
/**
* Creates an inner join relation with [otherTable] using [onColumn] and [otherColumn] equality
* and/or [additionalConstraint] as the join condition.
*
* @throws IllegalStateException if the join cannot be performed. See the exception message for more details.
*/
fun <C1 : ColumnSet, C2 : ColumnSet> C1.innerJoin(
otherTable: C2,
onColumn: C1.() -> Expression<*>,
otherColumn: C2.() -> Expression<*>,
onColumn: (C1.() -> Expression<*>)? = null,
otherColumn: (C2.() -> Expression<*>)? = null,
additionalConstraint: (SqlExpressionBuilder.() -> Op<Boolean>)? = null,
): Join = join(otherTable, JoinType.INNER, onColumn(this), otherColumn(otherTable), additionalConstraint)
): Join = join(otherTable, JoinType.INNER, onColumn?.invoke(this), otherColumn?.invoke(otherTable), additionalConstraint)

/** Creates a left outer join relation with [otherTable] using [onColumn] and [otherColumn] as the join condition. */
/**
* Creates a left outer join relation with [otherTable] using [onColumn] and [otherColumn] equality
* and/or [additionalConstraint] as the join condition.
*
* @throws IllegalStateException if the join cannot be performed. See the exception message for more details.
*/
fun <C1 : ColumnSet, C2 : ColumnSet> C1.leftJoin(
otherTable: C2,
onColumn: C1.() -> Expression<*>,
otherColumn: C2.() -> Expression<*>,
onColumn: (C1.() -> Expression<*>)? = null,
otherColumn: (C2.() -> Expression<*>)? = null,
additionalConstraint: (SqlExpressionBuilder.() -> Op<Boolean>)? = null,
): Join = join(otherTable, JoinType.LEFT, onColumn(), otherTable.otherColumn(), additionalConstraint)
): Join = join(otherTable, JoinType.LEFT, onColumn?.invoke(this), otherColumn?.invoke(otherTable), additionalConstraint)

/** Creates a right outer join relation with [otherTable] using [onColumn] and [otherColumn] as the join condition. */
/**
* Creates a right outer join relation with [otherTable] using [onColumn] and [otherColumn] equality
* and/or [additionalConstraint] as the join condition.
*
* @throws IllegalStateException if the join cannot be performed. See the exception message for more details.
*/
fun <C1 : ColumnSet, C2 : ColumnSet> C1.rightJoin(
otherTable: C2,
onColumn: C1.() -> Expression<*>,
otherColumn: C2.() -> Expression<*>,
onColumn: (C1.() -> Expression<*>)? = null,
otherColumn: (C2.() -> Expression<*>)? = null,
additionalConstraint: (SqlExpressionBuilder.() -> Op<Boolean>)? = null,
): Join = join(otherTable, JoinType.RIGHT, onColumn(), otherTable.otherColumn(), additionalConstraint)
): Join = join(otherTable, JoinType.RIGHT, onColumn?.invoke(this), otherColumn?.invoke(otherTable), additionalConstraint)

/** Creates a full outer join relation with [otherTable] using [onColumn] and [otherColumn] as the join condition. */
/**
* Creates a full outer join relation with [otherTable] using [onColumn] and [otherColumn] equality
* and/or [additionalConstraint] as the join condition.
*
* @throws IllegalStateException if the join cannot be performed. See the exception message for more details.
*/
fun <C1 : ColumnSet, C2 : ColumnSet> C1.fullJoin(
otherTable: C2,
onColumn: C1.() -> Expression<*>,
otherColumn: C2.() -> Expression<*>,
onColumn: (C1.() -> Expression<*>)? = null,
otherColumn: (C2.() -> Expression<*>)? = null,
additionalConstraint: (SqlExpressionBuilder.() -> Op<Boolean>)? = null,
): Join = join(otherTable, JoinType.FULL, onColumn(), otherTable.otherColumn(), additionalConstraint)
): Join = join(otherTable, JoinType.FULL, onColumn?.invoke(this), otherColumn?.invoke(otherTable), additionalConstraint)

/** Creates a cross join relation with [otherTable] using [onColumn] and [otherColumn] as the join condition. */
/**
* Creates a cross join relation with [otherTable] using [onColumn] and [otherColumn] equality
* and/or [additionalConstraint] as the join condition.
*
* @throws IllegalStateException if the join cannot be performed. See the exception message for more details.
*/
fun <C1 : ColumnSet, C2 : ColumnSet> C1.crossJoin(
otherTable: C2,
onColumn: C1.() -> Expression<*>,
otherColumn: C2.() -> Expression<*>,
onColumn: (C1.() -> Expression<*>)? = null,
otherColumn: (C2.() -> Expression<*>)? = null,
additionalConstraint: (SqlExpressionBuilder.() -> Op<Boolean>)? = null,
): Join = join(otherTable, JoinType.CROSS, onColumn(), otherTable.otherColumn(), additionalConstraint)
): Join = join(otherTable, JoinType.CROSS, onColumn?.invoke(this), otherColumn?.invoke(otherTable), additionalConstraint)

/**
* Represents a subset of [fields] from a given [source].
Expand Down

0 comments on commit 6d06fdb

Please sign in to comment.