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

org.komapper.r2dbc.dsl.runner.R2dbcRelationInsertValuesRunner.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.RelationInsertValuesContext
import org.komapper.core.dsl.expression.Operand
import org.komapper.core.dsl.metamodel.EntityMetamodel
import org.komapper.core.dsl.metamodel.IdGenerator
import org.komapper.core.dsl.metamodel.PropertyMetamodel
import org.komapper.core.dsl.runner.RelationInsertValuesRunner
import org.komapper.r2dbc.R2dbcDatabaseConfig
import org.komapper.r2dbc.R2dbcExecutor

internal class R2dbcRelationInsertValuesRunner>(
    private val context: RelationInsertValuesContext,
) : R2dbcRunner> {
    private val runner: RelationInsertValuesRunner = RelationInsertValuesRunner(context)

    private val support: R2dbcRelationInsertValuesSupport = R2dbcRelationInsertValuesSupport(context)

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

    override suspend fun run(config: R2dbcDatabaseConfig): Pair {
        return support.handleIdGenerator(
            config,
            object : R2dbcRelationInsertValuesSupport.Callback> {
                override suspend fun autoIncrement(idGenerator: IdGenerator.AutoIncrement): Pair {
                    val generatedColumn = runner.preInsertUsingAutoIncrement(idGenerator)
                    val (count, keys) = insert(config, generatedColumn = generatedColumn)
                    return runner.postInsertUsingAutoIncrement(count, keys)
                }

                override suspend fun sequence(idGenerator: IdGenerator.Sequence, id: ID): Pair {
                    val idAssignment = runner.preInsertUsingSequence(idGenerator, id)
                    val (count, _) = insert(config, idAssignment)
                    return count to id
                }

                override suspend fun other(): Pair {
                    val (count, _) = insert(config)
                    return count to null
                }
            },
        )
    }

    private suspend fun insert(
        config: R2dbcDatabaseConfig,
        idAssignment: Pair, Operand>? = null,
        generatedColumn: String? = null,
    ): Pair> {
        val statement = runner.buildStatement(config, idAssignment)
        val executor = R2dbcExecutor(config, context.options, generatedColumn)
        return executor.executeUpdate(statement)
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy