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

[MySQL] Fix bug when inserted ids are not returned if rewriteBatchedStatements property set to true #1873

Merged
merged 1 commit into from
Oct 1, 2023
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 @@ -9,6 +9,7 @@ import org.jetbrains.exposed.sql.vendors.currentDialect
import java.io.InputStream
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Statement
import java.sql.Types

class JdbcPreparedStatementImpl(
Expand Down Expand Up @@ -60,7 +61,15 @@ class JdbcPreparedStatementImpl(
if (!statement.isClosed) statement.close()
}

override fun executeBatch(): List<Int> = statement.executeBatch().toList()
override fun executeBatch(): List<Int> {
return statement.executeBatch().map {
when (it) {
Statement.SUCCESS_NO_INFO -> 1
Statement.EXECUTE_FAILED -> 0
else -> it
}
}
}

override fun cancel() {
if (!statement.isClosed) statement.cancel()
Expand Down
1 change: 1 addition & 0 deletions exposed-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation("org.apache.logging.log4j", "log4j-core", Versions.log4j2)

implementation("com.zaxxer", "HikariCP", "4.0.3")
testCompileOnly("mysql", "mysql-connector-java", Versions.mysql80)
testCompileOnly("org.postgresql", "postgresql", Versions.postgre)
testCompileOnly("com.impossibl.pgjdbc-ng", "pgjdbc-ng", Versions.postgreNG)
compileOnly("com.h2database", "h2", Versions.h2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ abstract class DatabaseTestsBase {
withDb(dialect, statement)
}

fun withTables(excludeSettings: List<TestDB>, vararg tables: Table, statement: Transaction.(TestDB) -> Unit) {
fun withTables(excludeSettings: Collection<TestDB>, vararg tables: Table, statement: Transaction.(TestDB) -> Unit) {
Assume.assumeFalse(dialect in excludeSettings)

withDb(dialect) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.jetbrains.exposed.sql.tests.mysql

import com.mysql.cj.conf.PropertyKey
import com.mysql.cj.jdbc.ConnectionImpl
import org.jetbrains.exposed.sql.batchInsert
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.RepeatableTestRule
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.dml.DMLTestsData
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.junit.Rule
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertNotNull

class MysqlTests : DatabaseTestsBase() {

Expand All @@ -19,4 +25,22 @@ class MysqlTests : DatabaseTestsBase() {
assertFalse(TransactionManager.current().exec("SELECT VERSION();") { it.next(); it.getString(1) }.isNullOrEmpty())
}
}

@Test
fun testBatchInsertWithRewriteBatchedStatementsOn() {
val mysqlOnly = TestDB.enabledDialects() - TestDB.MYSQL
withTables(excludeSettings = mysqlOnly, DMLTestsData.Cities) {
val mysqlConnection = this.connection.connection as ConnectionImpl
mysqlConnection.propertySet.getBooleanProperty(PropertyKey.rewriteBatchedStatements).value = true
val cityNames = listOf("FooCity", "BarCity")
val generatedValues = DMLTestsData.Cities.batchInsert(cityNames) { city ->
this[DMLTestsData.Cities.name] = city
}

assertEquals(cityNames.size, generatedValues.size)
generatedValues.forEach {
assertNotNull(it.getOrNull(DMLTestsData.Cities.id))
}
}
}
}