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

run.smt.ktest.db.query.impl.QueryBuilderImpl.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.QueryBuilder
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource
import org.springframework.jdbc.core.namedparam.SqlParameterSource
import java.sql.Date

/**
 * Represents SQL query
 */
abstract class QueryBuilderImpl(private var _query: String?) : QueryBuilder {
    /**
     * Which implementation from spring-jdbc to use for parameters
     */
    protected var parameterImplementation: () -> SqlParameterSource = { MapSqlParameterSource(parameters) }

    /**
     * Map of parameters
     */
    override var parameters: MutableMap = mutableMapOf()

    /**
     * Query string
     */
    override var query: String
        get() = _query ?: throw IllegalStateException("No query string present")
        set(value) {
            _query = value
        }

    /**
     * Add/overwrite parameter, exclusive with `parametersFrom`
     */
    override fun parameter(name: String, value: Any?) {
        parameters[name] = value
    }

    /**
     * Extract parameters from given pojo, exclusive with `parameter`
     */
    override fun parametersFrom(pojo: Any) {
        parameterImplementation = { BeanPropertySqlParameterSource(pojo) }
    }

    /**
     * Converts some complex types into JDBC representation
     */
    protected fun prepareData(v: Any?): Any? {
        return when (v) {
            is java.util.Date -> Date(v.time)
            else -> v
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy