https://kotlinlang.org logo
d

degreat

09/19/2020, 11:49 AM
INSERT IGNORE...
throws an exception when using H2. I'm up-to-date with exposed and h2 drivers... Am I missing anything else?
t

Tristan Blakers

09/21/2020, 1:53 AM
fwiw, I use these extension functions with Exposed. Cribbed with a modificaiton or two from a github repo. This is on Exposed 24.1, may be a better way by now
Copy code
class InsertOrIgnore<Key : Any>(
    table: Table,
    isIgnore: Boolean = false,
    private vararg val keys: Column<*>
) : InsertStatement<Key>(table, isIgnore) {

    override fun prepareSQL(transaction: Transaction): String {
        val tm = TransactionManager.current()
        val onConflict = "ON CONFLICT DO NOTHING"
        return "${super.prepareSQL(transaction)} $onConflict"
    }
}

fun <T : Table> T.insertOrIgnore(vararg keys: Column<*>, body: T.(InsertStatement<Number>) -> Unit) =
    InsertOrIgnore<Number>(this, keys = keys).apply {
        body(this)
        execute(TransactionManager.current())
    }
t

tapac

09/22/2020, 12:43 PM
@degreat "INSERT IGNORE" supported only in MySQL mode on H2. Please check http://www.h2database.com/html/features.html "MySQL Compatibility Mode" section.
148 Views