relate
relate copied to clipboard
RowParser for Optional Long
I was doing a query like this:
sql"""SELECT SUM(x) as c FROM table WHERE ...""".as[Option[Long]]
and trying to parse with this
implicit val sumParser: RowParser[Long] = RowParser(RowParser.long("c"))
It works fine until SUM(x) is NULL which happens when there are no rows (due to where clause). I get this exception:
Caused by: java.util.NoSuchElementException: None.get
at scala.None$.get(Option.scala:347)
at scala.None$.get(Option.scala:345)
at com.lucidchart.relate.SqlRow.long(SqlRow.scala:116)
at com.lucidchart.relate.RowParser$$anonfun$long$1.apply(RowParser.scala:19)
at com.lucidchart.relate.RowParser$$anonfun$long$1.apply(RowParser.scala:19)
at com.lucidchart.relate.RowParser$$anon$1.parse(RowParser.scala:13)
at com.lucidchart.relate.RowParser$$anonfun$limitedCollection$1$$anonfun$apply$1.apply(RowParser.scala:31)
at com.lucidchart.relate.RowParser$$anonfun$limitedCollection$1$$anonfun$apply$1.apply(RowParser.scala:29)
at com.lucidchart.relate.ResultSetWrapper$class.withResultSet(ResultSetWrapper.scala:18)
at com.lucidchart.relate.SqlRow.withResultSet(SqlRow.scala:16)
I was able to work around it by changing my query but it should be fixed:
sql"""SELECT COALESCE(SUM(x), 0) as c FROM table WHERE ...""".as[Option[Long]]