Skip to content

RowParser for Optional Long #53

@krjackso

Description

@krjackso

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions