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

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

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

import io.r2dbc.spi.Row
import org.komapper.core.dsl.expression.ColumnExpression
import org.komapper.core.dsl.metamodel.EntityMetamodel
import org.komapper.core.dsl.query.ProjectionType
import org.komapper.core.dsl.query.Record
import org.komapper.core.dsl.query.RecordImpl
import org.komapper.r2dbc.R2dbcDataOperator

internal object R2dbcRowTransformers {

    fun  singleEntity(metamodel: EntityMetamodel, columns: List> = emptyList(), strategy: ProjectionType = ProjectionType.INDEX): (R2dbcDataOperator, Row) -> ENTITY =
        { dataOperator, row ->
            val mapper = R2dbcEntityMapper(strategy, dataOperator, row)
            mapper.execute(metamodel, columns, true) as ENTITY
        }

    fun  singleColumn(expression: ColumnExpression): (R2dbcDataOperator, Row) -> A? {
        return singleColumn(expression) { it }
    }

    fun  singleNotNullColumn(expression: ColumnExpression): (R2dbcDataOperator, Row) -> A {
        return singleColumn(expression) {
            checkNotNull(it) { "The value of the selected column is null." }
        }
    }

    private fun  singleColumn(
        expression: ColumnExpression,
        transform: (A?) -> R,
    ): (R2dbcDataOperator, Row) -> R =
        { dataOperator, row ->
            val extractor = R2dbcIndexedValueExtractor(dataOperator, row)
            val value = extractor.execute(expression)
            transform(value)
        }

    fun  pairColumns(expressions: Pair, ColumnExpression>): (R2dbcDataOperator, Row) -> Pair {
        return pairColumns(expressions) { it }
    }

    fun  pairNotNullColumns(expressions: Pair, ColumnExpression>): (R2dbcDataOperator, Row) -> Pair {
        return pairColumns(expressions) {
            val first = checkNotNull(it.first) { "The value of the first column is null." }
            val second = checkNotNull(it.second) { "The value of the second column is null." }
            first to second
        }
    }

    private fun  pairColumns(
        expressions: Pair, ColumnExpression>,
        transform: (Pair) -> Pair,
    ): (R2dbcDataOperator, Row) -> Pair =
        { dataOperator, row ->
            val extractor = R2dbcIndexedValueExtractor(dataOperator, row)
            val first = extractor.execute(expressions.first)
            val second = extractor.execute(expressions.second)
            transform(first to second)
        }

    fun  tripleColumns(expressions: Triple, ColumnExpression, ColumnExpression>): (R2dbcDataOperator, Row) -> Triple {
        return tripleColumns(expressions) { it }
    }

    fun  tripleNotNullColumns(expressions: Triple, ColumnExpression, ColumnExpression>): (R2dbcDataOperator, Row) -> Triple {
        return tripleColumns(expressions) {
            val first = checkNotNull(it.first) { "The value of the first column is null." }
            val second = checkNotNull(it.second) { "The value of the second column is null." }
            val third = checkNotNull(it.third) { "The value of the third column is null." }
            Triple(first, second, third)
        }
    }

    private fun  tripleColumns(
        expressions: Triple, ColumnExpression, ColumnExpression>,
        transform: (Triple) -> Triple,
    ): (R2dbcDataOperator, Row) -> Triple =
        { dataOperator, row ->
            val extractor = R2dbcIndexedValueExtractor(dataOperator, row)
            val first = extractor.execute(expressions.first)
            val second = extractor.execute(expressions.second)
            val third = extractor.execute(expressions.third)
            transform(Triple(first, second, third))
        }

    fun multipleColumns(expressions: List>): (R2dbcDataOperator, Row) -> Record = { dataOperator, row ->
        val extractor = R2dbcIndexedValueExtractor(dataOperator, row)
        val map = expressions.associateWith { extractor.execute(it) }
        RecordImpl(map)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy