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

run.smt.ktest.db.query.impl.RespondingQueryImpl.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package run.smt.ktest.db.query.impl

import run.smt.ktest.db.query.RespondingQuery
import run.smt.ktest.db.query.Response
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.RowMapper
import java.sql.ResultSet
import kotlin.reflect.KClass

/**
 * Represents query that returns some result
 */
abstract class RespondingQueryImpl(_query: String?) : QueryBuilderImpl(_query), RespondingQuery {
    private var _mapTo: KClass? = null

    /**
     * Function extracting list of requested values from spring-jdbc
     */
    private lateinit var listLoader: (JdbcTemplate) -> List

    /**
     * Function extracting result as map from spring-jdbc
     */
    private lateinit var mapLoader: (JdbcTemplate) -> Map?

    /**
     * Function extracting single POJO from spring-jdbc
     */
    private lateinit var singleLoader: (JdbcTemplate) -> T?

    /**
     * Class to map result to
     */
    override var mapTo: KClass
        get() = _mapTo ?: throw IllegalStateException("No class present to map to")
        set(value) {
            _mapTo = value
        }

    override fun mapTo(clazz: KClass) {
        _mapTo = clazz
    }

    /**
     * Whether result is primitive type
     */
    protected val forPrimitive: Boolean get() {
        return mapTo.javaPrimitiveType != null || mapTo == String::class
    }

    protected fun listLoader(loader: (JdbcTemplate) -> List) {
        listLoader = loader
    }

    protected fun mapLoader(loader: (JdbcTemplate) -> Map?) {
        mapLoader = loader
    }

    protected fun singleLoader(loader: (JdbcTemplate) -> T?) {
        singleLoader = loader
    }

    /**
     * Execute query
     */
    fun execute(jdbc: JdbcTemplate): Response {
        return BaseResponse(
            mapTo,
            jdbc,
            listLoader,
            mapLoader,
            singleLoader
        )
    }

    /**
     * Adapter for spring-jdbc `RowMapper`
     */
    protected class SimpleRowMapper(mapTo: KClass) : RowMapper {
        private val mapper = Mapper(mapTo)

        override fun mapRow(rs: ResultSet?, rowNum: Int): T?
            = mapper.map(rs?.let { SqlOutputAdapter(rs) })
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy