Skip to content

Commit

Permalink
fix: EXPOSED-493 Update with join query throws if WHERE clause present
Browse files Browse the repository at this point in the history
Previous refactoring of UpdateStatement.arguments() to properly order registered
arguments in an update-from-join statement did not factor in the possibility that
the join target would be a subquery or QueryAlias. If this query itself holds a WHERE
clause with arguments, the latter will not be registered when preparing the statement,
leading to the same 'parameter not filled' errors as previously.

This PR ensures any query arguments are registered just before the rest of the join
part additional arguments, at whatever point that may be for each database.

Tests for single and multiple joins, and joins with update-where, have been edited
to include joins with a subquery.
  • Loading branch information
bog-walk committed Aug 19, 2024
1 parent 8726aa8 commit c0b7051
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ open class UpdateStatement(val targetsSet: ColumnSet, val limit: Int?, val where

private fun QueryBuilder.registerAdditionalArgs(join: Join) {
join.joinParts.forEach {
(it.joinPart as? QueryAlias)?.query?.prepareSQL(this)
it.additionalConstraint?.invoke(SqlExpressionBuilder)?.toQueryBuilder(this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import org.jetbrains.exposed.exceptions.UnsupportedByDialectException
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentTestDB
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.expectException
import org.jetbrains.exposed.sql.vendors.SQLiteDialect
import org.junit.Test
import java.lang.IllegalArgumentException

class UpdateTests : DatabaseTestsBase() {
private val notSupportLimit by lazy {
Expand Down Expand Up @@ -92,6 +92,20 @@ class UpdateTests : DatabaseTestsBase() {
assertEquals(it[users.name], it[userData.comment])
assertEquals(0, it[userData.value])
}

val userAlias = users.selectAll().where { users.cityId neq 1 }.alias("u2")
val joinWithSubQuery = userData.innerJoin(userAlias, { userData.user_id }, { userAlias[users.id] })
joinWithSubQuery.update {
it[userData.value] = 123
}

joinWithSubQuery.selectAll().forEach {
assertEquals(123, it[userData.value])
}

if (currentTestDB in TestDB.ALL_H2_V1) { // h2_v1 treats 'U2' query as a table/view dependent on USERS
exec("${users.dropStatement().single()} CASCADE")
}
}
}

Expand All @@ -108,6 +122,22 @@ class UpdateTests : DatabaseTestsBase() {
assertEquals(it[users.name], it[userData.comment])
assertEquals(123, it[userData.value])
}

val singleJoinQuery = userData.joinQuery(
on = { userData.user_id eq it[users.id] },
joinPart = { users.selectAll().where { users.cityId neq 1 } }
)
val doubleJoinQuery = singleJoinQuery.joinQuery(
on = { userData.user_id eq it[users.id] },
joinPart = { users.selectAll().where { users.name like "%ey" } }
)
doubleJoinQuery.update {
it[userData.value] = 0
}

doubleJoinQuery.selectAll().forEach {
assertEquals(0, it[userData.value])
}
}
}

Expand All @@ -131,7 +161,6 @@ class UpdateTests : DatabaseTestsBase() {
}

val join = tableA.innerJoin(tableB)
val joinWithConstraint = tableA.innerJoin(tableB, { tableA.id }, { tableB.tableAId }) { tableB.bar eq "foo" }

if (testingDb in supportWhere) {
join.update({ tableA.foo eq "foo" }) {
Expand All @@ -141,10 +170,21 @@ class UpdateTests : DatabaseTestsBase() {
assertEquals("baz", it[tableB.bar])
}

val joinWithConstraint = tableA.innerJoin(tableB, { tableA.id }, { tableB.tableAId }) { tableB.bar eq "foo" }
joinWithConstraint.update({ tableA.foo eq "foo" }) {
it[tableB.bar] = "baz"
}
assertEquals(0, joinWithConstraint.selectAll().count())

val subquery = tableA.selectAll().where { tableA.foo eq "foo" }.alias("sq")
val joinWithSubquery = tableB.innerJoin(subquery, { tableB.tableAId }, { subquery[tableA.id] })
joinWithSubquery.update({ tableB.bar eq "baz" }) {
it[tableB.bar] = "zip"
}

joinWithSubquery.selectAll().single().also {
assertEquals("zip", it[tableB.bar])
}
} else {
expectException<UnsupportedByDialectException> {
join.update({ tableA.foo eq "foo" }) {
Expand Down

0 comments on commit c0b7051

Please sign in to comment.