Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EXPOSED-493 Update with join query throws if WHERE clause present #2207

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -82,9 +82,13 @@ internal object H2FunctionProvider : FunctionProvider() {
transaction.throwUnsupportedException("H2 doesn't support WHERE in UPDATE with join clause.")
}
val tableToUpdate = columnsAndValues.map { it.first.table }.distinct().singleOrNull()
?: transaction.throwUnsupportedException("H2 supports a join updates with a single table columns to update.")
?: transaction.throwUnsupportedException(
"H2 doesn't support UPDATE with join clause that uses columns from multiple tables."
)
val joinPart = targets.joinParts.singleOrNull()
?: transaction.throwUnsupportedException("H2 supports a join updates with only one table to join.")
?: transaction.throwUnsupportedException(
"H2 doesn't support UPDATE with join clause that uses multiple tables to join."
)
if (joinPart.joinType != JoinType.INNER) {
exposedLogger.warn("All tables in UPDATE statement will be joined with inner join")
}
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 @@ -131,7 +131,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,6 +140,7 @@ 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"
}
Expand All @@ -155,6 +155,50 @@ class UpdateTests : DatabaseTestsBase() {
}
}

@Test
fun testUpdateWithJoinQuery() {
withCitiesAndUsers(exclude = TestDB.ALL_H2_V1 + TestDB.SQLITE) { _, users, userData ->
// single join query using join()
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) { // does not support either multi-table joins or update(where)
// single join query using join() with update(where)
joinWithSubQuery.update({ userData.comment like "Comment%" }) {
it[userData.value] = 0
}

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

// multiple join queries using joinQuery()
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] = 99
}

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

@Test
fun `test that column length checked in update `() {
val stringTable = object : IntIdTable("StringTable") {
Expand Down
Loading