slick-pg
slick-pg copied to clipboard
.length method returns a Rep[Int]
Hey there!
I am raising a discussion about the fact that .length
is returning a Rep[Int]
because when an array is empty, postgres returns null
instead of 0.
So I have two questions:
- How can I handle that case right now from scala?
- Should it be considered to change the return type of length to be
Rep[Option[Int]]
so that we can usegetOrElse(0)
to handle that case?
Thank you,
Can we try arrayField.length.?
, that is, add a .?
to turn it to Rep[Option[Int]]
?
I tried :), the problem is that casts the result when it is returned, not in the query.
In order to be able to match on 0, the SQL compiler needs to add coalesce
around the array_length statement like so: coalesce(array_length("array"), 0)
.
That would be added by having the user specify getOrElse
. But for that the column needs to be set as Rep[Option[Int]]
So, a better solution is to provide a coalesce
function, which accepts a Rep[T]
and a default value for null as the parameters, and returns a Rep[T]
wrapped by pg coalesce
?
That would be a better solution indeed. I could try and make a PR for this if you tell me where I should start.
You can add a CommonFunctions
trait in ExPostgresDriver
, implement it here, then merge the trait into API
trait.