diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt index 966b118353..902c3e8a87 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/InsertStatement.kt @@ -120,7 +120,7 @@ open class InsertStatement(val table: Table, val isIgnore: Boolean = } protected open fun PreparedStatementApi.execInsertFunction(): Pair { - val inserted = if (arguments().count() > 1 || isAlwaysBatch) executeBatch().count() else executeUpdate() + val inserted = if (arguments().count() > 1 || isAlwaysBatch) executeBatch().sum() else executeUpdate() val rs = if (autoIncColumns.isNotEmpty()) { resultSet } else null diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/InsertTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/InsertTests.kt index 025c472ecc..e5fca912a4 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/InsertTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/InsertTests.kt @@ -8,6 +8,7 @@ import org.jetbrains.exposed.dao.id.IdTable import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.statements.BatchInsertStatement import org.jetbrains.exposed.sql.tests.DatabaseTestsBase import org.jetbrains.exposed.sql.tests.TestDB import org.jetbrains.exposed.sql.tests.currentDialectTest @@ -574,4 +575,35 @@ class InsertTests : DatabaseTestsBase() { } } + + class BatchInsertOnConflictDoNothing( + table: Table, + ) : BatchInsertStatement(table) { + override fun prepareSQL(transaction: Transaction) = buildString { + append(super.prepareSQL(transaction)) + append(" ON CONFLICT (id) DO NOTHING") + } + } + + @Test fun `batch insert number of inserted rows is accurate`() { + val tab = object : Table("tab") { + val id = varchar("id", 10).uniqueIndex() + } + + withTables(TestDB.allH2TestDB + listOf(TestDB.MYSQL), tab) { + tab.insert { it[id] = "foo" } + + val numInserted = BatchInsertOnConflictDoNothing(tab).run { + addBatch() + this[tab.id] = "foo" + + addBatch() + this[tab.id] = "bar" + + execute(this@withTables) + } + + assertEquals(1, numInserted) + } + } }