Skip to content

Commit

Permalink
Getting number of rows inserted from Table.insert or Table.insertIgno…
Browse files Browse the repository at this point in the history
…re? #851
  • Loading branch information
Tapac committed Aug 4, 2021
1 parent 530c893 commit 6e170ca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ open class Transaction(private val transactionImpl: TransactionInterface) : User
}
}

if (delta > warnLongQueriesDuration ?: Long.MAX_VALUE) {
if (delta > (warnLongQueriesDuration ?: Long.MAX_VALUE)) {
exposedLogger.warn("Long query: ${describeStatement(delta, lazySQL.value)}", RuntimeException())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import org.jetbrains.exposed.sql.vendors.currentDialect
import org.jetbrains.exposed.sql.vendors.inProperCase
import java.sql.ResultSet
import java.sql.SQLException
import kotlin.properties.Delegates

open class InsertStatement<Key : Any>(val table: Table, val isIgnore: Boolean = false) : UpdateBuilder<Int>(StatementType.INSERT, listOf(table)) {

var insertedCount: Int by Delegates.notNull()

var resultedValues: List<ResultRow>? = null
private set

Expand Down Expand Up @@ -116,6 +120,7 @@ open class InsertStatement<Key : Any>(val table: Table, val isIgnore: Boolean =
override fun PreparedStatementApi.executeInternal(transaction: Transaction): Int {
val (inserted, rs) = execInsertFunction()
return inserted.apply {
insertedCount = this
resultedValues = processResults(rs, this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,22 @@ class InsertTests : DatabaseTestsBase() {

val insertIgnoreSupportedDB = TestDB.values().toList() -
listOf(TestDB.SQLITE, TestDB.MYSQL, TestDB.H2_MYSQL, TestDB.POSTGRESQL, TestDB.POSTGRESQLNG)

withTables(insertIgnoreSupportedDB, idTable) {
val id = idTable.insertIgnore {
val insertedStatement = idTable.insertIgnore {
it[idTable.id] = EntityID(1, idTable)
it[idTable.name] = "1"
} get idTable.id
assertEquals(1, id.value)
}
assertEquals(1, insertedStatement[idTable.id].value)
assertEquals(1, insertedStatement.insertedCount)

val notInsertedStatement = idTable.insertIgnore {
it[idTable.id] = EntityID(1, idTable)
it[idTable.name] = "2"
}

assertEquals(1, notInsertedStatement[idTable.id].value)
assertEquals(0, notInsertedStatement.insertedCount)
}
}

Expand Down

0 comments on commit 6e170ca

Please sign in to comment.