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

org.komapper.r2dbc.dsl.runner.R2dbcRunner.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.runner.Runner
import org.komapper.r2dbc.R2dbcDatabaseConfig

sealed interface R2dbcRunner : Runner {
    suspend fun run(config: R2dbcDatabaseConfig): T

    data class AndThen(
        private val left: R2dbcRunner,
        private val right: R2dbcRunner,
    ) : R2dbcRunner {

        private val runner: Runner.AndThen = Runner.AndThen(left, right)

        override fun check(config: DatabaseConfig) {
            left.check(config)
            right.check(config)
        }

        override suspend fun run(config: R2dbcDatabaseConfig): RIGHT {
            left.run(config)
            return right.run(config)
        }

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

    data class Map(
        private val runner: R2dbcRunner,
        private val transform: (T) -> S,
    ) : R2dbcRunner {

        private val _runner: Runner.Map = Runner.Map(runner)

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

        override suspend fun run(config: R2dbcDatabaseConfig): S {
            val value = runner.run(config)
            return transform(value)
        }

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

    data class Zip(
        private val left: R2dbcRunner,
        private val right: R2dbcRunner,
    ) : R2dbcRunner> {

        private val runner: Runner.Zip = Runner.Zip(left, right)

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

        override suspend fun run(config: R2dbcDatabaseConfig): Pair {
            val first = left.run(config)
            val second = right.run(config)
            return first to second
        }

        override fun dryRun(config: DatabaseConfig): DryRunStatement {
            return runner.dryRun(config)
        }
    }
    data class FlatMap(
        val runner: R2dbcRunner,
        val transform: (T) -> R2dbcRunner,
    ) : R2dbcRunner {

        private val _runner: Runner.FlatMap = Runner.FlatMap(runner)

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

        override suspend fun run(config: R2dbcDatabaseConfig): S {
            val value = runner.run(config)
            return transform(value).run(config)
        }

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

    data class FlatZip(
        val runner: R2dbcRunner,
        val transform: (T) -> R2dbcRunner,
    ) : R2dbcRunner> {

        private val _runner: Runner.FlatZip = Runner.FlatZip(runner)

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

        override suspend fun run(config: R2dbcDatabaseConfig): Pair {
            val value = runner.run(config)
            return value to transform(value).run(config)
        }

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