All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.provenance.p8e.shared.sql.Upsert.kt Maven / Gradle / Ivy

package io.provenance.p8e.shared.sql

import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.statements.BatchInsertStatement
import org.jetbrains.exposed.sql.transactions.TransactionManager

class BatchInsertOnConflictIgnore(table: Table) : BatchInsertStatement(table, false) {
    override fun prepareSQL(transaction: Transaction): String {
        return super.prepareSQL(transaction) + " ON CONFLICT DO NOTHING"
    }
}

fun  T.batchInsertOnConflictIgnore(data: List, body: T.(BatchInsertOnConflictIgnore, E) -> Unit) {
    data.takeIf { it.isNotEmpty() }
        ?.let {
            val insert = BatchInsertOnConflictIgnore(this)
            data.forEach {
                insert.addBatch()
                body(insert, it)
            }
            TransactionManager.current().exec(insert)
        }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy