UPSERT in PostgreSQL using jOOQ

后端 未结 4 1809
死守一世寂寞
死守一世寂寞 2021-02-03 19:31

I am trying to perform an UPSERT in PostgreSQL using the jOOQ library.

For doing this I am currently trying to implement the following SQL statement in jOOQ: https://st

4条回答
  •  遇见更好的自我
    2021-02-03 20:06

    Inspired by @ud3sh comment with JOOQ 3.11, Kotlin, and the PostgreSQL DSL

    This is an extension function to call upsert directly on the UpdatableRecord object

    import org.jooq.UpdatableRecord
    
    internal fun UpdatableRecord<*>.upsert(): Int {
        if(this.configuration() == null) {
            throw NullPointerException("Attach configuration to record before calling upsert")
        }
        return this.configuration().dsl().insertInto(this.getTable()).set(this).onConflict().doUpdate().set(this).execute()
    }
    

提交回复
热议问题