I'm using postgres with a value field that is numeric
. JOOQ turns it into BigDecimal
. My JOOQ query is:
val sumField = DSL.sum(TABLE.VALUE)
val query = dsl().select(TABLE.id.`as`("id"), sumField.`as`("sum"))
.from(TABLE)
.groupBy(TABLE.id)
.fetch()
.map{
record -> SumById(
id = record.get("id").toString(),
sum = record.get("sum").toString().toInt()
)
}
where SumById
is a data class with id
and sum
fields:
data class SumById (val id: String, val sum: Int)
I can get the Int
sum by doing .toString().toInt()
. But I was wondering if there's a better way to turn BigDecimal
to Int
.
PS
I guess this is more of a kotlin question than a JOOQ question.
Yes, there is. Just reuse your column referenes from earlier:
record -> SumById(
id = record.get(TABLE.id.`as`("id")),
sum = record.get(sumField.`as`("sum"))
)
Alternatively, you can store those in local variables as well. Another option is to use Record2.value1()
and Record2.value2()
record -> SumById(
id = record.value1(),
sum = record.value2()
)
... as the type information is maintained by the jOOQ API across your query (up to degrees of 22, at least).
In jOOQ 3.10, you may also be able to use the new Kotlin destructuring support, where a Record2<String, BigDecimal>
can be destructured as follows:
val (id, sum) = record;