How to handle null in Anorm

I have a table with a null column, and when querying a null column it threw an error

 val row: List[(String,String)] = SQL("select top 10 Spare_Part part,Pricing_Category cat from Price_Point_Base")
                .as((str("part"))~ str("cat") map(flatten) *)

I checked the link https://www.playframework.com/documentation/2.0/ScalaAnorm .

It only discards a column with a null value, using something like

SQL("Select name,indepYear from Country")().map { row =>
  row[String]("name") -> row[Option[Int]]("indepYear")
}

But since it is str("part")more compact than row[String]("name")that, so I would like to try using it str("part"), but how do I str("part")work with a null column?

+3
source share
2 answers

, Option[String], , . Anorm null, .

val row: List[(Option[String], String)] = 
    SQL("select top 10 Spare_Part part,Pricing_Category cat from Price_Point_Base")
       .as( get[Option[String]("part") ~ str("cat") map(flatten) *)

, , String. , null :

val parser: RowParser[(String, String)] = {
    get[Option[String]]("part") ~ get[Option[String]]("cat") map {
       case part~cat => (part.getOrElse(""), cat.getOrElse(""))
    }
}

:

val row: List[(String, String)] = 
    SQL("select top 10 Spare_Part part,Pricing_Category cat from Price_Point_Base")
       .as(parser *)
+8

, , . , , , :

def strOpt(columnName: String)(implicit c: Column[Option[String]]): RowParser[Option[String]] =
  get[Option[String]](columnName)(c)

, , , .

0

Source: https://habr.com/ru/post/1611726/


All Articles