-
Notifications
You must be signed in to change notification settings - Fork 695
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
Support range types for Postgres #1298
Comments
I will take a look of this issue soon. |
@OlivierCavadenti Did you take a look? |
@OlivierCavadenti Was there ever a resolution for these supported range types in exposed? I tried implementing my own custom column types in exposed to support tstzrange however, the exposed JDBC driver complains when trying to insert a tstzrange formatted string into the tstzrange formatted column. Here is an example of the error: |
Hi @philipchang-RL The casting exception you're seeing is most likely because you need to also override Here's a more detailed explanation of the error if you're interested. Using current version 0.50.0, here's an example of how to implement a custom range type, specifically an class IntRangeColumnType : ColumnType<IntRange>() {
override fun sqlType(): String = "int4range"
override fun nonNullValueToString(value: IntRange): String = "[${value.first},${value.last}]"
override fun setParameter(stmt: PreparedStatementApi, index: Int, value: Any?) {
val parameterValue: PGobject? = value?.let {
PGobject().apply {
type = sqlType()
this.value = nonNullValueToString(it as IntRange)
}
}
super.setParameter(stmt, index, parameterValue)
}
override fun valueFromDB(value: Any): IntRange? = when (value) {
is PGobject -> value.value?.let {
val components = it.trim('[', ')').split(',')
IntRange(components.first().toInt(), components.last().toInt() - 1)
}
else -> error("Retrieved unexpected value of type ${value::class.simpleName}")
}
}
fun Table.intRange(name: String): Column<IntRange> = registerColumn(name, IntRangeColumnType())
// and how to use it
object Tester : Table("tester") {
val amounts = intRange("amounts")
}
Tester.insert {
it[amounts] = 10..<100
} |
Postgres has many built-in range types for fields: https://www.postgresql.org/docs/current/rangetypes.html
PostgreSQL comes with the following built-in range types:
The text was updated successfully, but these errors were encountered: