Is there a way to do REPLACE INTO ... SELECT in My...
# exposed
a
Is there a way to do REPLACE INTO ... SELECT in MySQL using Exposed? I've found insert from select, but can not find an equivalent for replace into. (http://jetbrains.github.io/Exposed/deep-dive-into-dsl.html#insert-from-select) possible that separate select followed by replace is possible for my use case, but would be neat if it could be done.
c
Hi @Alexander There's currently no out-of-the-box way like with
insert()
overloads, but it is possible. You would just need to extend the existing
InsertSelectStatement
class and provide a custom extension function, something like:
Copy code
class ReplaceSelectStatement(
    columns: List<Column<*>>,
    selectQuery: AbstractQuery<*>
) : InsertSelectStatement(columns, selectQuery) {
    override fun prepareSQL(transaction: Transaction, prepared: Boolean): String {
        val querySql = selectQuery.prepareSQL(transaction, prepared)
        return transaction.db.dialect.functionProvider.replace(targets.single(), columns, querySql, transaction)
    }
}

fun <T : Table> T.replace(
    selectQuery: AbstractQuery<*>,
    columns: List<Column<*>> = this.columns.filter { !it.columnType.isAutoInc || it.autoIncColumnType?.nextValExpression != null }
): Int? = ReplaceSelectStatement(columns, selectQuery).execute(TransactionManager.current())
Seems like a small enough addition on our end, so I'll look into trying to get it in.
👍🏾 1
👍 1
a
Thanks!
👍 1