JetBrains / JetBrains/Exposed

JavaDateColumnType.kt does NOT contain a real time stamp method, but an Instant

Open
#844 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
9.3k
Forks
798
Avg merge
4d 2h
Merged PRs (30d)
26

Description

This is bad because it will lead to people having to constantly convert back and forth between objects. Thus cluttering their projects with conversion nonsense.

Here is the code I am suggesting to add as part of JavaDateColumnType.kt:

```
package database

import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.ColumnType
import org.jetbrains.exposed.sql.IDateColumnType
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.vendors.currentDialect
import java.sql.Timestamp
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.*

class JavaTimestampColumnType : ColumnType(), IDateColumnType {
override fun sqlType(): String {
return currentDialect.dataTypeProvider.dateTimeType()
}

override fun nonNullValueToString(value: Any): String {
val timestamp = when (value) {
is String -> return value
is Instant -> Timestamp.from(value).toString()
is Timestamp -> value.toString()
else -> error("Unexpected value: $value of ${value::class.qualifiedName}")
}
return timestamp
}

override fun valueFromDB(value: Any): Timestamp = when (value) {
is Timestamp -> value
is String -> Timestamp.valueOf(value)
else -> valueFromDB(value.toString())
}

override fun notNullValueToDB(value: Any): Any {
if (value is Instant) {
return Timestamp.from(value)
} else if (value is String) {
return Timestamp.valueOf(value)
}
return value
}
}
fun Table.realtimestamp(name: String): Column = registerColumn(name, JavaTimestampColumnType())
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.