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-301 Update with join throws if additionalConstraint provided #2007

Merged
merged 1 commit into from
Feb 26, 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 @@ -6,6 +6,8 @@ import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi
import org.jetbrains.exposed.sql.vendors.H2Dialect.H2CompatibilityMode
import org.jetbrains.exposed.sql.vendors.H2FunctionProvider
import org.jetbrains.exposed.sql.vendors.OracleDialect
import org.jetbrains.exposed.sql.vendors.PostgreSQLDialect
import org.jetbrains.exposed.sql.vendors.SQLServerDialect
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.jetbrains.exposed.sql.vendors.h2Mode

Expand Down Expand Up @@ -45,20 +47,38 @@ open class UpdateStatement(val targetsSet: ColumnSet, val limit: Int?, val where
}

override fun arguments(): Iterable<Iterable<Pair<IColumnType, Any?>>> = QueryBuilder(true).run {
val dialect = currentDialect
when {
targetsSet is Join && currentDialect is OracleDialect -> {
where?.toQueryBuilder(this)
values.forEach {
registerArgument(it.key, it.value)
}
targetsSet is Join && dialect is OracleDialect -> {
registerAdditionalArgs(targetsSet)
registerWhereArg()
registerUpdateArgs()
}
targetsSet is Join && (dialect is SQLServerDialect || dialect is PostgreSQLDialect) -> {
registerUpdateArgs()
registerAdditionalArgs(targetsSet)
registerWhereArg()
}
targetsSet is Join -> {
registerAdditionalArgs(targetsSet)
registerUpdateArgs()
registerWhereArg()
}
else -> {
values.forEach {
registerArgument(it.key, it.value)
}
where?.toQueryBuilder(this)
registerUpdateArgs()
registerWhereArg()
}
}
if (args.isNotEmpty()) listOf(args) else emptyList()
}

private fun QueryBuilder.registerWhereArg() { where?.toQueryBuilder(this) }

private fun QueryBuilder.registerUpdateArgs() { values.forEach { registerArgument(it.key, it.value) } }

private fun QueryBuilder.registerAdditionalArgs(join: Join) {
join.joinParts.forEach {
it.additionalConstraint?.invoke(SqlExpressionBuilder)?.toQueryBuilder(this)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class UpdateTests : DatabaseTestsBase() {
}

@Test
fun testUpdateWithJoin01() {
fun testUpdateWithSingleJoin() {
withCitiesAndUsers(exclude = listOf(TestDB.SQLITE)) { _, users, userData ->
val join = users.innerJoin(userData)
join.update {
Expand All @@ -80,11 +80,22 @@ class UpdateTests : DatabaseTestsBase() {
assertEquals(it[users.name], it[userData.comment])
assertEquals(123, it[userData.value])
}

val joinWithConstraint = users.innerJoin(userData, { users.id }, { userData.user_id }) { users.id eq "smth" }
joinWithConstraint.update {
it[userData.comment] = users.name
it[userData.value] = 0
}

joinWithConstraint.selectAll().forEach {
assertEquals(it[users.name], it[userData.comment])
assertEquals(0, it[userData.value])
}
}
}

@Test
fun testUpdateWithJoin02() {
fun testUpdateWithMultipleJoins() {
withCitiesAndUsers(exclude = TestDB.allH2TestDB + TestDB.SQLITE) { cities, users, userData ->
val join = cities.innerJoin(users).innerJoin(userData)
join.update {
Expand All @@ -109,7 +120,7 @@ class UpdateTests : DatabaseTestsBase() {
val tableAId = reference("table_a_id", tableA)
}

val supportWhere = TestDB.entries - TestDB.allH2TestDB - TestDB.SQLITE + TestDB.H2_ORACLE
val supportWhere = TestDB.entries - TestDB.allH2TestDB.toSet() - TestDB.SQLITE + TestDB.H2_ORACLE

withTables(tableA, tableB) { testingDb ->
val aId = tableA.insertAndGetId { it[foo] = "foo" }
Expand All @@ -119,6 +130,7 @@ 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 @@ -127,6 +139,11 @@ class UpdateTests : DatabaseTestsBase() {
join.selectAll().single().also {
assertEquals("baz", it[tableB.bar])
}

joinWithConstraint.update({ tableA.foo eq "foo" }) {
it[tableB.bar] = "baz"
}
assertEquals(0, joinWithConstraint.selectAll().count())
} else {
expectException<UnsupportedByDialectException> {
join.update({ tableA.foo eq "foo" }) {
Expand Down
Loading