All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.komapper.r2dbc.dsl.runner.R2dbcEntityUpdateBatchRunner.kt Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
package org.komapper.r2dbc.dsl.runner

import org.komapper.core.DatabaseConfig
import org.komapper.core.DryRunStatement
import org.komapper.core.dsl.context.EntityUpdateContext
import org.komapper.core.dsl.metamodel.EntityMetamodel
import org.komapper.core.dsl.runner.EntityUpdateBatchRunner
import org.komapper.r2dbc.R2dbcDatabaseConfig

internal class R2dbcEntityUpdateBatchRunner>(
    context: EntityUpdateContext,
    private val entities: List,
) :
    R2dbcRunner> {
    private val runner: EntityUpdateBatchRunner =
        EntityUpdateBatchRunner(context, entities)

    private val support: R2dbcEntityUpdateRunnerSupport =
        R2dbcEntityUpdateRunnerSupport(context)

    override fun check(config: DatabaseConfig) {
        runner.check(config)
    }

    override suspend fun run(config: R2dbcDatabaseConfig): List {
        if (entities.isEmpty()) return emptyList()
        val newEntities = preUpdate(config)
        val batchResults = update(config, newEntities)
        return postUpdate(newEntities, batchResults.map { it.first })
    }

    private fun preUpdate(config: R2dbcDatabaseConfig): List {
        return entities.map { runner.preUpdate(config, it) }
    }

    private suspend fun update(config: R2dbcDatabaseConfig, entities: List): List> {
        val statements = entities.map { runner.buildStatement(config, it) }
        return support.update(config) { it.executeBatch(statements) }
    }

    private fun postUpdate(entities: List, counts: List): List {
        return runner.postUpdate(entities, counts)
    }

    override fun dryRun(config: DatabaseConfig): DryRunStatement {
        return runner.dryRun(config)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy