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

com.dbobjekts.statement.SQLParameter.kt Maven / Gradle / Ivy

There is a newer version: 0.6.0-RC2
Show newest version
package com.dbobjekts.statement

import com.dbobjekts.api.AnySqlParameter
import com.dbobjekts.metadata.column.Column
import com.dbobjekts.metadata.column.ColumnAndValue
import java.sql.PreparedStatement

/**
 * Represents a '?' parameter with its value and relative position in a SQL query. SELECT name from PERSON where age > ? and married = ?
 */
data class SqlParameter(
    val oneBasedPosition: Int,
    val column: Column,
    val value: T?
) {

    fun setValueOnStatement(preparedStatement: PreparedStatement) {
        column.putValue(oneBasedPosition, preparedStatement, value)
    }

    override fun toString(): String = column::class.java.getSimpleName() + value + "position " + oneBasedPosition


    companion object {

        fun  fromWhereClauseCondition(condition: Condition): List =
            condition.valueOrColumn.values?.map { SqlParameter(0, condition.column, it) } ?: listOf()

        fun  fromColumnValue(zeroBasedIndex: Int, colValue: ColumnAndValue): SqlParameter =
            SqlParameter(zeroBasedIndex + 1, colValue.column, colValue.value)

    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy