From 57ed3295674f36ab0f42e2d7f8788eabfbfc27c5 Mon Sep 17 00:00:00 2001 From: yeogai <1884809+yeogai@users.noreply.github.com> Date: Mon, 27 Feb 2023 12:06:29 -0500 Subject: [PATCH 1/2] 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 } ``` --- .../kotlin/org/jetbrains/exposed/sql/Table.kt | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt index 53ee9fd78d..c672ea8e80 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt @@ -137,45 +137,60 @@ abstract class ColumnSet : FieldSet { fun select(columns: List>): 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. + */ fun C1.innerJoin( otherTable: C2, - onColumn: C1.() -> Expression<*>, - otherColumn: C2.() -> Expression<*>, + onColumn: (C1.() -> Expression<*>)? = null, + otherColumn: (C2.() -> Expression<*>)? = null, additionalConstraint: (SqlExpressionBuilder.() -> Op)? = 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.leftJoin( otherTable: C2, - onColumn: C1.() -> Expression<*>, - otherColumn: C2.() -> Expression<*>, + onColumn: (C1.() -> Expression<*>)? = null, + otherColumn: (C2.() -> Expression<*>)? = null, additionalConstraint: (SqlExpressionBuilder.() -> Op)? = 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.rightJoin( otherTable: C2, - onColumn: C1.() -> Expression<*>, - otherColumn: C2.() -> Expression<*>, + onColumn: (C1.() -> Expression<*>)? = null, + otherColumn: (C2.() -> Expression<*>)? = null, additionalConstraint: (SqlExpressionBuilder.() -> Op)? = 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.fullJoin( otherTable: C2, - onColumn: C1.() -> Expression<*>, - otherColumn: C2.() -> Expression<*>, + onColumn: (C1.() -> Expression<*>)? = null, + otherColumn: (C2.() -> Expression<*>)? = null, additionalConstraint: (SqlExpressionBuilder.() -> Op)? = 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.crossJoin( otherTable: C2, - onColumn: C1.() -> Expression<*>, - otherColumn: C2.() -> Expression<*>, + onColumn: (C1.() -> Expression<*>)? = null, + otherColumn: (C2.() -> Expression<*>)? = null, additionalConstraint: (SqlExpressionBuilder.() -> Op)? = 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]. From 08d0a35da7c2c8bac06dda86a99c172a2ca2f1ef Mon Sep 17 00:00:00 2001 From: "leonid.stashevsky" Date: Mon, 15 Jan 2024 13:46:38 +0100 Subject: [PATCH 2/2] fixup! In the convenience methods for `join`, make column equality optional --- .../src/main/kotlin/org/jetbrains/exposed/sql/Table.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt index c672ea8e80..52d6c17e48 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Table.kt @@ -140,6 +140,8 @@ abstract class ColumnSet : FieldSet { /** * 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.innerJoin( otherTable: C2, @@ -151,6 +153,8 @@ fun C1.innerJoin( /** * 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.leftJoin( otherTable: C2, @@ -162,6 +166,8 @@ fun C1.leftJoin( /** * 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.rightJoin( otherTable: C2, @@ -173,6 +179,8 @@ fun C1.rightJoin( /** * 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.fullJoin( otherTable: C2, @@ -184,6 +192,8 @@ fun C1.fullJoin( /** * 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.crossJoin( otherTable: C2,