Wondering if the new `KotlinInstantColumnType` ca...
# exposed
d
Wondering if the new
KotlinInstantColumnType
can handle an int field that stores a unix timestamp?
t
Is it fails on getTimestamp call? What database do you use?
d
I didn't actually try it, I just looked through the source code, and saw that you use Instant.parse, and for anything not a String or sql Timestamp, you convert to a string and use that parse function, so it really shouldn't work for integers that represent epochSeconds @tapac
And besides the point, the underlying sqltype isn't integer in your implementation, so when creating the schema, it would create the wrong field type...
I use mysql in that particular case... but this should be an issue for all implementations... I just don't know how often people save epochSeconds as ints...
t
It depends on how you use it. If you use mapping for existing table with long/int column containing instantant value than driver can return in as a Timestamp on ResultSet.getTimestamp call. If you create table from scratch with Exposed than it will create a db-specific type and everything should also work
Also, storing instant as a second representation is not a good idea as instant have millis/nanos parts
d
You have:
Copy code
override fun valueFromDB(value: Any): Instant = when (value) {
        is java.sql.Timestamp -> value.toInstant().toKotlinInstant()
        is String -> Instant.parse(value)
        else -> valueFromDB(value.toString())
    }
in my case it should reach the else... converting the int to a string, and ending up in Instant.parse... if this would be to work, there should have been an extra
is Int -> Instant.fromEpochSeconds(value, 0)
... but I guess this isn't a common use-case... but it would still have been nice to have some kind of
transform(fromDb: (FieldType) -> RequiredType, toDb: (RequiredType) -> FieldType)
... for this and other use-cases...
in the field definition on the Table...
like
val startTimestap: Instant = integer("start_at").transform({ Instant.fromEpochSeconds(it, 0) }, { it.epochSeconds })