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

Add OffsetDateTime support for PostgreSQL. #557

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import org.ktorm.schema.SqlType
import java.lang.reflect.InvocationTargetException
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Timestamp
import java.sql.Types
import java.time.OffsetDateTime
import java.time.ZoneId
import java.time.ZoneOffset

/**
* Define a column typed [ShortArraySqlType].
Expand Down Expand Up @@ -366,3 +370,27 @@ public object EarthSqlType : SqlType<Earth>(Types.OTHER, "earth") {
}
}
}

/**
* Define a column typed [OffsetDateTime].
*/
public fun BaseTable<*>.datetimeOffset(name: String): Column<OffsetDateTime> {
return registerColumn(name, OffsetDateTimeSqlType)
}

public object OffsetDateTimeSqlType : SqlType<OffsetDateTime>(Types.TIMESTAMP_WITH_TIMEZONE, "timestamptz") {

override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: OffsetDateTime) {
ps.setTimestamp(index, Timestamp.from(parameter.toInstant()))
}

override fun doGetResult(rs: ResultSet, index: Int): OffsetDateTime? {
val value = rs.getTimestamp(index)
if (rs.wasNull()) {
return null
}
val systemZoneId = ZoneId.systemDefault()
val systemZoneOffset = systemZoneId.rules.getOffset(value.toInstant())
return value.toInstant().atOffset(systemZoneOffset)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.ktorm.support.postgresql

import org.junit.Test
import org.ktorm.dsl.*
import org.ktorm.schema.Table
import org.ktorm.schema.int
import java.time.OffsetDateTime
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class OffsetDateTimeTest : BasePostgreSqlTest() {

object OffsetDatetimes : Table<Nothing>("t_offset_datetime") {
val id = int("id").primaryKey()
val createdAt = datetimeOffset("created_at")
}

@Test
fun test() {
val value = OffsetDateTime.now()
database.insert(OffsetDatetimes) {
set(OffsetDatetimes.createdAt, value)
}
val results = database
.from(OffsetDatetimes)
.select(OffsetDatetimes.createdAt)
.where(OffsetDatetimes.id eq 1)
.map { row ->
row[OffsetDatetimes.createdAt]
}
val offsetDateTime = results[0]
assertNotNull(offsetDateTime)
assertEquals(value.year, offsetDateTime.year)
assertEquals(value.month, offsetDateTime.month)
assertEquals(value.dayOfMonth, offsetDateTime.dayOfMonth)
assertEquals(value.hour, offsetDateTime.hour)
assertEquals(value.minute, offsetDateTime.minute)
assertEquals(value.second, offsetDateTime.second)
assertEquals(value.offset, offsetDateTime.offset)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ drop type if exists mood;
drop table if exists t_json;
drop table t_earthdistance;
drop table if exists t_user;
drop table if exists t_offset_datetime;
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ insert into t_metadata(attrs)
values ('a=>1, b=>2, c=>NULL'::hstore);

insert into t_enum(current_mood)
values ('HAPPY')
values ('HAPPY');

create table t_offset_datetime(
id serial primary key,
created_at timestamptz not null
);