English | 简体中文

api-docs / org.ktorm.support.mysql / bulkInsertOrUpdate

bulkInsertOrUpdate

fun <T : BaseTable<*>> Database.bulkInsertOrUpdate(
    table: T,
    block: BulkInsertOrUpdateStatementBuilder<T>.() -> Unit
): Int
(source code)

Bulk insert records to the table, determining if there is a key conflict while inserting each of them,
and automatically performs updates if any conflict exists.

Usage:

database.bulkInsertOrUpdate(Employees) {
item {
set(it.id, 1)
set(it.name, "vince")
set(it.job, "engineer")
set(it.salary, 1000)
set(it.hireDate, LocalDate.now())
set(it.departmentId, 1)
}
item {
set(it.id, 5)
set(it.name, "vince")
set(it.job, "engineer")
set(it.salary, 1000)
set(it.hireDate, LocalDate.now())
set(it.departmentId, 1)
}
onDuplicateKey {
set(it.salary, it.salary + 900)
}
}

Generated SQL:

insert into t_employee (id, name, job, salary, hire_date, department_id)
values (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)
on duplicate key update salary = salary + ?

Parameters

table - the table to be inserted.

block - the DSL block used to construct the expression.

Since
3.3.0

Return
the effected row count.

See Also

bulkInsert

fun <T : BaseTable<*>> T.bulkInsertOrUpdate(
    block: BulkInsertOrUpdateStatementBuilder<T>.() -> Unit
): Int
(source code)

Bulk insert records to the table, determining if there is a key conflict while inserting each of them,
and automatically performs updates if any conflict exists.

Usage:

Employees.bulkInsertOrUpdate {
item {
set(it.id, 1)
set(it.name, "vince")
set(it.job, "engineer")
set(it.salary, 1000)
set(it.hireDate, LocalDate.now())
set(it.departmentId, 1)
}
item {
set(it.id, 5)
set(it.name, "vince")
set(it.job, "engineer")
set(it.salary, 1000)
set(it.hireDate, LocalDate.now())
set(it.departmentId, 1)
}
onDuplicateKey {
set(it.salary, it.salary + 900)
}
}

Generated SQL:

insert into t_employee (id, name, job, salary, hire_date, department_id)
values (?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?)
on duplicate key update salary = salary + ?

Parameters

block - the DSL block used to construct the expression.

Since
3.3.0

Return
the effected row count.

See Also

bulkInsert