slick-pg icon indicating copy to clipboard operation
slick-pg copied to clipboard

.length method returns a Rep[Int]

Open callicles opened this issue 8 years ago • 5 comments

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 use getOrElse(0) to handle that case?

Thank you,

callicles avatar Oct 23 '16 20:10 callicles

Can we try arrayField.length.?, that is, add a .? to turn it to Rep[Option[Int]]?

tminglei avatar Oct 23 '16 23:10 tminglei

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]]

callicles avatar Oct 24 '16 02:10 callicles

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?

tminglei avatar Oct 24 '16 02:10 tminglei

That would be a better solution indeed. I could try and make a PR for this if you tell me where I should start.

callicles avatar Oct 24 '16 04:10 callicles

You can add a CommonFunctions trait in ExPostgresDriver, implement it here, then merge the trait into API trait.

tminglei avatar Oct 24 '16 05:10 tminglei