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

com.dbobjekts.metadata.column.NullableColumn.kt Maven / Gradle / Ivy

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

import com.dbobjekts.metadata.DefaultTable
import com.dbobjekts.api.AnyTable
import com.dbobjekts.metadata.ColumnFactory
import java.sql.PreparedStatement
import java.sql.ResultSet

abstract class NullableColumn(
    table: AnyTable = DefaultTable,
    name: String = "dummy",
    internal val sqlType: Int,
    valueClass: Class<*>,
    aggregateType: AggregateType?
) : Column(table,name, valueClass, aggregateType) {
    override fun create(value: I?): ColumnAndValue = NullableColumnAndValue(this, value)
    
    @Suppress("UNCHECKED_CAST")
    open fun distinct(): NullableColumn = ColumnFactory.distinctClone(this) as NullableColumn

    internal fun setNull(): ColumnAndValue = NullableColumnAndValue(this, null)

    override fun retrieveValue(position: Int, rs: ResultSet): I? {
        val value = getValue(position, rs)
        return if (rs.wasNull())
            null else value
    }

    abstract fun getValue(position: Int, resultSet: ResultSet): I?
    abstract fun setValue(position: Int, statement: PreparedStatement, value: I)
    override fun putValue(position: Int, statement: PreparedStatement, value: I?) {
        if (value == null)
            statement.setNull(position, sqlType)
        else
            setValue(position, statement, value)
    }
}