JOOQ dynamic multiple values insert

Viewed 305

I have a list of records and I need to insert it in one query with upsert like

INSERT INTO table1 (name, lastname) 
VALUES ('name1', 'lastname1'), ('name2', 'lastname2') 
    ON CONFLICT DO NOTHING;

Not batch or merge and necessary with 'on conflict' logic

2 Answers

Use the new jOOQ 3.15 InsertValuesStep2.valuesOfRows():

insertInto(table1, table1.name, table1.lastname)
    .valuesOfRows(usersForInsert.map { DSL.row(it.name, it.lastname) })
    .onConflictDoNothing()
    .execute()

Made it so clean with such approach in Kotlin :

insertInto(table1, table1.name, table1.lastname)
            .apply {
                usersForInsert.forEach { user ->
                    values(user.name, user.lastname)
                }
            }
            .onConflictDoNothing()
            .execute()

Thanks

Related