com.dbobjekts.metadata.column.NonNullableColumn.kt Maven / Gradle / Ivy
package com.dbobjekts.metadata.column
import com.dbobjekts.api.AnyTable
import com.dbobjekts.api.exception.StatementExecutionException
import com.dbobjekts.metadata.ColumnFactory
import java.lang.IllegalStateException
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Types
abstract class NonNullableColumn(
table: AnyTable,
name: String,
valueClass: Class<*>,
aggregateType: AggregateType?
) : Column(table,name, valueClass, aggregateType) {
open val nullable: NullableColumn by lazy {
ColumnFactory.nullableColumn(this)
}
override fun create(value: I?): ColumnAndValue = NonNullableColumnAndValue(
this, value ?: throw StatementExecutionException("Value cannot be null in non-null column")
)
@Suppress("UNCHECKED_CAST")
open fun distinct(): NonNullableColumn = ColumnFactory.distinctClone(this) as NonNullableColumn
override fun retrieveValue(
position: Int,
rs: ResultSet
): I? {
val value = getValue(position, rs)
return if (rs.wasNull()) {
throw StatementExecutionException(
"Cannot return null value for non-nullable column $tableDotName. " +
"This happens when the column is selected in an outer join. Use the nullable counterpart of the non-nullable column like so: transaction.select(Employee.name, Department.name.nullable)."
)
} 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?) {
setValue(position, statement, value ?: throw StatementExecutionException("Cannot be null"))
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy