Skip to content

Commit

Permalink
Fix detekt issues causing max threshold build fail
Browse files Browse the repository at this point in the history
  • Loading branch information
bog-walk committed Jun 6, 2023
1 parent 1f271e5 commit 7c2c527
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ 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)) {
open class InsertStatement<Key : Any>(
val table: Table,
val isIgnore: Boolean = false
) : UpdateBuilder<Int>(StatementType.INSERT, listOf(table)) {

/**
* Returns the number of rows affected by the insert operation.
Expand Down Expand Up @@ -41,7 +44,8 @@ open class InsertStatement<Key : Any>(val table: Table, val isIgnore: Boolean =
val autoGeneratedKeys = arrayListOf<MutableMap<Column<*>, Any?>>()

if (inserted > 0) {
val returnedColumns = (if (currentDialect.supportsOnlyIdentifiersInGeneratedKeys) autoIncColumns else table.columns).mapNotNull { col ->
val columns = if (currentDialect.supportsOnlyIdentifiersInGeneratedKeys) autoIncColumns else table.columns
val returnedColumns = columns.mapNotNull { col ->
@Suppress("SwallowedException")
try {
rs?.findColumn(col.name)?.let { col to it }
Expand All @@ -53,12 +57,17 @@ open class InsertStatement<Key : Any>(val table: Table, val isIgnore: Boolean =
val firstAutoIncColumn = autoIncColumns.firstOrNull { it.autoIncColumnType != null } ?: autoIncColumns.firstOrNull()
if (firstAutoIncColumn != null || returnedColumns.isNotEmpty()) {
while (rs?.next() == true) {
val returnedValues = returnedColumns.associateTo(mutableMapOf()) { it.first to rs.getObject(it.second) }
if (returnedValues.isEmpty() && firstAutoIncColumn != null) returnedValues[firstAutoIncColumn] = rs.getObject(1)
val returnedValues = returnedColumns.associateTo(mutableMapOf()) {
it.first to rs.getObject(it.second)
}
if (returnedValues.isEmpty() && firstAutoIncColumn != null) {
returnedValues[firstAutoIncColumn] = rs.getObject(1)
}
autoGeneratedKeys.add(returnedValues)
}

if (inserted > 1 && firstAutoIncColumn != null && autoGeneratedKeys.isNotEmpty() && !currentDialect.supportsMultipleGeneratedKeys) {
if (inserted > 1 && firstAutoIncColumn != null &&
autoGeneratedKeys.isNotEmpty() && !currentDialect.supportsMultipleGeneratedKeys) {
// H2/SQLite only returns one last generated key...
(autoGeneratedKeys[0][firstAutoIncColumn] as? Number)?.toLong()?.let {
var id = it
Expand Down Expand Up @@ -115,7 +124,8 @@ open class InsertStatement<Key : Any>(val table: Table, val isIgnore: Boolean =
override fun prepareSQL(transaction: Transaction): String {
val values = arguments!!.first()
val sql = values.toSqlString()
return transaction.db.dialect.functionProvider.insert(isIgnore, table, values.map { it.first }, sql, transaction)
val dialect = transaction.db.dialect
return dialect.functionProvider.insert(isIgnore, table, values.map { it.first }, sql, transaction)
}

protected fun List<Pair<Column<*>, Any?>>.toSqlString(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ open class H2Dialect : VendorDialect(dialectName, H2DataTypeProvider, H2Function

val isSecondVersion get() = majorVersion == H2MajorVersion.Two

private fun exactH2Version(transaction: Transaction): String = transaction.db.metadata { databaseProductVersion.substringBefore(" (") }
private fun exactH2Version(transaction: Transaction): String = transaction.db.metadata {
databaseProductVersion.substringBefore(" (")
}

enum class H2CompatibilityMode {
MySQL, MariaDB, SQLServer, Oracle, PostgreSQL
Expand Down

0 comments on commit 7c2c527

Please sign in to comment.