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
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 }
```
  • Loading branch information
yeogai committed Feb 27, 2023
1 parent 6b367af commit d5241e0
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 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 @@ -107,45 +107,60 @@ abstract class ColumnSet : FieldSet {
fun slice(columns: List<Expression<*>>): FieldSet = Slice(this, columns)
}

/** 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.
*/
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.
*/
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.
*/
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.
*/
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.
*/
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 d5241e0

Please sign in to comment.