Skip to content

Commit

Permalink
fix: EXPOSED-551 [SQL Server] Update from join with limit throws synt…
Browse files Browse the repository at this point in the history
…ax exception

- Rebase from main
- Clean up tests to use local properties
  • Loading branch information
bog-walk committed Sep 19, 2024
1 parent f1fb526 commit 327a742
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.like
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.currentDialectTest
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.H2Dialect
Expand All @@ -19,18 +20,9 @@ import kotlin.test.assertTrue
import kotlin.test.expect

class DeleteTests : DatabaseTestsBase() {
private val notSupportLimit by lazy {
val exclude = arrayListOf(
TestDB.POSTGRESQL,
TestDB.POSTGRESQLNG,
TestDB.ORACLE,
TestDB.H2_V2_PSQL,
TestDB.H2_V2_ORACLE
)
if (!SQLiteDialect.ENABLE_UPDATE_DELETE_LIMIT) {
exclude.add(TestDB.SQLITE)
}
exclude
private val limitNotSupported by lazy {
val extra = setOf(TestDB.SQLITE).takeUnless { SQLiteDialect.ENABLE_UPDATE_DELETE_LIMIT }.orEmpty()
TestDB.ALL_POSTGRES_LIKE + TestDB.ALL_ORACLE_LIKE + extra
}

@Test
Expand Down Expand Up @@ -82,24 +74,18 @@ class DeleteTests : DatabaseTestsBase() {
}

@Test
fun testDeleteWithLimitAndOffset01() {
withCitiesAndUsers(exclude = notSupportLimit) { _, _, userData ->
userData.deleteWhere(limit = 1) { userData.value eq 20 }
userData.select(userData.user_id, userData.value).where { userData.value eq 20 }.let {
assertEquals(1L, it.count())
val expected = if (currentDialectTest is H2Dialect) "smth" else "eugene"
assertEquals(expected, it.single()[userData.user_id])
}
}
}

@Test
fun testDeleteWithLimit02() {
val dialects = TestDB.entries - notSupportLimit
withCitiesAndUsers(dialects) { _, _, userData ->
expectException<UnsupportedByDialectException> {
userData.deleteWhere(limit = 1) {
userData.value eq 20
fun testDeleteWithLimit() {
withCitiesAndUsers { _, _, userData ->
if (currentTestDB in limitNotSupported) {
expectException<UnsupportedByDialectException> {
userData.deleteWhere(limit = 1) { userData.value eq 20 }
}
} else {
userData.deleteWhere(limit = 1) { userData.value eq 20 }
userData.select(userData.user_id, userData.value).where { userData.value eq 20 }.let {
assertEquals(1L, it.count())
val expected = if (currentDialectTest is H2Dialect) "smth" else "eugene"
assertEquals(expected, it.single()[userData.user_id])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ import org.junit.Test
import kotlin.test.assertTrue

class UpdateTests : DatabaseTestsBase() {
private val notSupportLimit by lazy {
val exclude = TestDB.ALL_POSTGRES_LIKE
if (!SQLiteDialect.ENABLE_UPDATE_DELETE_LIMIT) {
exclude + TestDB.SQLITE
} else {
exclude
}
private val limitNotSupported by lazy {
val extra = setOf(TestDB.SQLITE).takeUnless { SQLiteDialect.ENABLE_UPDATE_DELETE_LIMIT }.orEmpty()
TestDB.ALL_POSTGRES_LIKE + extra
}

@Test
Expand All @@ -41,30 +37,26 @@ class UpdateTests : DatabaseTestsBase() {
}

@Test
fun testUpdateWithLimit01() {
withCitiesAndUsers(exclude = notSupportLimit) { _, users, _ ->
val aNames = users.select(users.name).where { users.id like "a%" }.map { it[users.name] }
assertEquals(2, aNames.size)

users.update({ users.id like "a%" }, 1) {
it[users.id] = "NewName"
}

val unchanged = users.select(users.name).where { users.id like "a%" }.count()
val changed = users.select(users.name).where { users.id eq "NewName" }.count()
assertEquals(1, unchanged)
assertEquals(1, changed)
}
}
fun testUpdateWithLimit() {
withCitiesAndUsers { _, users, _ ->
if (currentTestDB in limitNotSupported) {
expectException<UnsupportedByDialectException> {
users.update({ users.id like "a%" }, limit = 1) {
it[users.id] = "NewName"
}
}
} else {
val aNames = users.select(users.name).where { users.id like "a%" }.map { it[users.name] }
assertEquals(2, aNames.size)

@Test
fun testUpdateWithLimit02() {
val dialects = TestDB.entries - notSupportLimit
withCitiesAndUsers(dialects) { _, users, _ ->
expectException<UnsupportedByDialectException> {
users.update({ users.id like "a%" }, 1) {
users.update({ users.id like "a%" }, limit = 1) {
it[users.id] = "NewName"
}

val unchanged = users.select(users.name).where { users.id like "a%" }.count()
val changed = users.select(users.name).where { users.id eq "NewName" }.count()
assertEquals(1, unchanged)
assertEquals(1, changed)
}
}
}
Expand Down Expand Up @@ -109,11 +101,11 @@ class UpdateTests : DatabaseTestsBase() {
val valueQuery = join.selectAll().where { userData.value eq updatedValue }
assertEquals(0, valueQuery.count())

join.update(limit = 2) {
it[userData.value] = 123
join.update(limit = maxToUpdate) {
it[userData.value] = updatedValue
}

assertEquals(2, valueQuery.count())
assertEquals(maxToUpdate, valueQuery.count().toInt())
}
}

Expand Down

0 comments on commit 327a742

Please sign in to comment.