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

org.komapper.r2dbc.dsl.runner.R2dbcEntityInsertMultipleRunner.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.EntityInsertContext
import org.komapper.core.dsl.metamodel.EntityMetamodel
import org.komapper.core.dsl.runner.EntityInsertMultipleRunner
import org.komapper.r2dbc.R2dbcDatabaseConfig

internal class R2dbcEntityInsertMultipleRunner>(
    context: EntityInsertContext,
    private val entities: List,
) :
    R2dbcRunner> {
    private val runner: EntityInsertMultipleRunner =
        EntityInsertMultipleRunner(context, entities)

    private val support: R2dbcEntityInsertRunnerSupport =
        R2dbcEntityInsertRunnerSupport(context)

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

    override suspend fun run(config: R2dbcDatabaseConfig): List {
        if (entities.isEmpty()) return emptyList()
        val newEntities = preInsert(config)
        val generatedKeys = insert(config, newEntities)
        return postInsert(newEntities, generatedKeys)
    }

    private suspend fun preInsert(config: R2dbcDatabaseConfig): List {
        return entities.map { support.preInsert(config, it) }
    }

    private suspend fun insert(config: R2dbcDatabaseConfig, entities: List): List {
        val statement = runner.buildStatement(config, entities)
        val (_, keys) = support.insert(config, true) { it.executeUpdate(statement) }
        return keys
    }

    private fun postInsert(entities: List, generatedKeys: List): List {
        return runner.postInsert(entities, generatedKeys)
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy