Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add upsert #4

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes

## Unreleased

### Feature

- added `DSLContext.upsert`

## 2024-03-26 / 0.3.0

### Feature
Expand Down
30 changes: 30 additions & 0 deletions src/main/kotlin/ls/jooq/execute/RecordMutators.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,33 @@ suspend fun <R : UpdatableRecord<R>> DSLContext.insertAndRefreshRecord(record: U
record.from(inserted)
return record
}

/**
* Upserts a [record].
* If the record already exists, it updates the existing record; otherwise, it inserts a new record.
*
* @param record The record to be upserted.
* @return The upserted record.
*/
suspend fun <R : UpdatableRecord<R>> DSLContext.upsert(record: R): R =
insertInto(record.table)
.set(record)
.onDuplicateKeyUpdate()
.set(record)
.returning()
.awaitFirst()

/**
* Upserts a record created from the [init] block.
* If the record already exists, it updates the existing record; otherwise, it inserts a new record.
*
* @param init a function to modify the record before upsertion
* @return The upserted record.
* @throws IllegalStateException if the record has no default constructor
*/
suspend inline fun <reified R : UpdatableRecord<R>> DSLContext.upsert(init: R.() -> Unit): R {
val constructor = checkNotNull(R::class.java.getConstructor()) { "no default constructor found for ${R::class}" }
val record = constructor.newInstance()
record.init()
return upsert(record)
}
52 changes: 52 additions & 0 deletions src/test/kotlin/ls/jooq/execute/RecordMutatorsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import DBExtension
import DBTest
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.reactive.awaitFirst
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactive.awaitSingle
import ls.jooq.db.generated.Tables
import ls.jooq.db.generated.tables.records.AuthorRecord
Expand All @@ -15,6 +18,10 @@ class RecordMutatorsTest : FreeSpec({

val ctx = DBExtension.dslContext

afterTest {
ctx.deleteFrom(Tables.AUTHOR).awaitFirst()
}

"DSLContext.insertAndRefreshRecord" - {

"should insert the record" {
Expand Down Expand Up @@ -60,6 +67,51 @@ class RecordMutatorsTest : FreeSpec({
}
}

"DSLContext.upsert<R>()" - {

"should insert if the given primary key doesn't exist" {
ctx.selectFrom(Tables.AUTHOR).awaitFirstOrNull().shouldBeNull()
val author = ctx.upsert<AuthorRecord> {
id = 1
firstName = "Max"
lastName = "Muster"
}

val foundAuthor = ctx.selectFrom(Tables.AUTHOR).awaitFirst()
foundAuthor shouldBe author
}

"should insert if no primary key is set and the primary key is generated" {
ctx.selectFrom(Tables.AUTHOR).awaitFirstOrNull().shouldBeNull()
val author = ctx.upsert<AuthorRecord> {
firstName = "Max"
lastName = "Muster"
}

val foundAuthor = ctx.selectFrom(Tables.AUTHOR).awaitFirst()
foundAuthor shouldBe author
}

"should update values of existing record with same primary key" {
ctx.selectFrom(Tables.AUTHOR).awaitFirstOrNull().shouldBeNull()
val author = ctx.upsert<AuthorRecord> {
firstName = "Max"
lastName = "Muster"
}

val updatedAuthor = ctx.upsert<AuthorRecord> {
id = author.id
firstName = "Not max"
lastName = "Muster"
}

val foundAuthor = ctx.selectFrom(Tables.AUTHOR).awaitFirst()
foundAuthor.firstName shouldBe "Not max"
updatedAuthor.id shouldBe foundAuthor.id
author.id shouldBe foundAuthor.id
}
}

"DSLContext.updateAndExecute()" - {

"should create an update statement for the given record and execute it" {
Expand Down
Loading