Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

希望能够支持kotlinx-datetime的时间类 #579

Open
sidian123 opened this issue Sep 10, 2024 · 0 comments
Open

希望能够支持kotlinx-datetime的时间类 #579

sidian123 opened this issue Sep 10, 2024 · 0 comments

Comments

@sidian123
Copy link

kotlinx-datetime是kotlin版本的日期时间库,希望能够支持以下类:

kotlinx.datetime.LocalDateTime
kotlinx.datetime.LocalDate
kotlinx.datetime.LocalTime

虽然可以自定义以下SqlType实现,但更适合集成到ktorm的库里(包括ksp):

import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalTime

/**
 * [SqlType] implementation represents `datetime` SQL type.
 */
public object LocalDateTimeSqlType : SqlType<LocalDateTime>(Types.TIMESTAMP, "datetime") {

    override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: LocalDateTime) {
        ps.setTimestamp(index, Timestamp.valueOf(parameter.toJavaLocalDateTime()))
    }

    override fun doGetResult(rs: ResultSet, index: Int): LocalDateTime? {
        return rs.getTimestamp(index)?.toLocalDateTime()?.toKotlinLocalDateTime()
    }
}

/**
 * [SqlType] implementation represents `date` SQL type.
 */
public object LocalDateSqlType : SqlType<LocalDate>(Types.DATE, "date") {

    override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: LocalDate) {
        ps.setDate(index, Date.valueOf(parameter.toJavaLocalDate()))
    }

    override fun doGetResult(rs: ResultSet, index: Int): LocalDate? {
        return rs.getDate(index)?.toLocalDate()?.toKotlinLocalDate()
    }
}

/**
 * [SqlType] implementation represents `time` SQL type.
 */
public object LocalTimeSqlType : SqlType<LocalTime>(Types.TIME, "time") {

    override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: LocalTime) {
        ps.setTime(index, Time.valueOf(parameter.toJavaLocalTime()))
    }

    override fun doGetResult(rs: ResultSet, index: Int): LocalTime? {
        return rs.getTime(index)?.toLocalTime()?.toKotlinLocalTime()
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant