com.dbobjekts.metadata.column.TimeColumns.kt Maven / Gradle / Ivy
package com.dbobjekts.metadata.column
import com.dbobjekts.api.AnyTable
import com.dbobjekts.util.DateUtil
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Types
import java.time.LocalTime
/**
* Represents a database column whose type is converted from and to a java.sql.Time.
*
* @param name The column name in the corresponding database table
*/
class TimeColumn(table: AnyTable, name: String, aggregateType: AggregateType?) :
NonNullableColumn(table,name, LocalTime::class.java, aggregateType) {
constructor(table: AnyTable, name: String) : this(table, name, null)
fun of(hour: Int, minutes: Int, seconds: Int): ColumnAndValue = create(LocalTime.of(hour, minutes, seconds))
override fun getValue(position: Int, resultSet: ResultSet): LocalTime? = resultSet.getTime(position)?.let { DateUtil.toLocalTime(it) }
override fun setValue(position: Int, statement: PreparedStatement, value: LocalTime) =
statement.setTime(position, DateUtil.toTime(value))
}
class NullableTimeColumn(table: AnyTable, name: String, aggregateType: AggregateType?) :
NullableColumn(table,name, Types.TIME, LocalTime::class.java, aggregateType) {
constructor(table: AnyTable, name: String) : this(table, name, null)
fun of(hour: Int, minutes: Int, seconds: Int): ColumnAndValue = create(LocalTime.of(hour, minutes, seconds))
override fun getValue(position: Int, resultSet: ResultSet): LocalTime? = resultSet.getTime(position)?.let { DateUtil.toLocalTime(it) }
override fun setValue(position: Int, statement: PreparedStatement, value: LocalTime?) =
statement.setTime(position, DateUtil.toTime(value!!))
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy