Using Parser APIs with Zero Columns in Anorm 2.4

I'm really trying to get rid of obsolescence warnings now that I upgraded to Anorm 2.4. I looked at How to handle null in Anorm , but that didn't help me.

Take a simple example: a database table account:

  • id (bigint not null)
  • email_address (varchar is not null)
  • first_name (varchar)
  • last_name (varchar)

I could have 2 functions in my Scala code: getAccountOfIdand getAccountsOfLastName.

  • getAccountOfIdreturns 0 or 1 account, therefore Option[(Long, String, Option[String], Option[String])], to our simple example
  • getAccountsOfLastNamereturns a list of accounts (which could potentially have a size of 0), therefore List[(Long, String, Option[String], String)], to our simple example

Part of the code for these two functions:

def getAccountOfId(id: Long): Option[(Long, String, Option[String], Option[String])] = {
  DB.withConnection { implicit c =>
    val query = """select email_address, first_name, last_name
        from account
        where id = {id};"""

    /* Rest of the code that I struggle with unless I use deprecated functions */
  }
}

def getAccountsOfLastName(lastName: String): List[(Long, String, Option[String], String)] = {
  DB.withConnection { implicit c =>
    val query = """select id, email_address, first_name
        from account
        where last_name = {lastName};"""

    /* Rest of the code that I struggle with unless I use deprecated functions */
  }
}

, " " Anorm Parser API.

+4
2

, :

  def getAccountOfId(id: Long): Option[(Long, String, Option[String], Option[String])] = {
    DB.withConnection { implicit c =>
      val query = """select email_address, first_name, last_name
        from account
        where id = {id};"""

      val rowParser = str("email_address") ~ (str("first_name") ?) ~ (str("last_name") ?) map {
        case emailAddress ~ firstNameOpt ~ lastNameOpt => (id, emailAddress, firstNameOpt, lastNameOpt)
      }

      SQL(query).on("id" -> id).as(rowParser.singleOpt)
    }
  }

  def getAccountsOfLastName(lastName: String): List[(Long, String, Option[String], String)] = {
    DB.withConnection { implicit c =>
      val query = """select id, email_address, first_name
        from account
        where last_name = {lastName};"""

      val rowParser = long("id") ~ str("email_address") ~ (str("first_name") ?) map {
        case id ~ emailAddress ~ firstNameOpt => (id, emailAddress, firstNameOpt, lastName)
      }

      SQL(query).on("lastName" -> lastName).as(rowParser.*)
    }
  }
0

, , Anorm 2.4 case, :

 final case class Role(id: Int,
                      label: String,
                      roletype: Int,
                      lid: Option[Int],
                      aid: Option[Int],
                      created: DateTime,
                      modified: DateTime)

, :

 val roleOptionRowParser = int("id") ~ str("label") ~ int("roletype") ~ (int("lid")?) ~ (int("vid")?) ~ get[DateTime]("created") ~
    get[DateTime]("modified") map {
    case id~label~roletype~lid~vid~created~modified β‡’ Some(Role(id, label, roletype, lid, vid, created, modified))
    case _ β‡’ None
}

? combinator , , SQL. :

SQL(s"""
            | select * from $source
            |    where $clause
            """.stripMargin).on(params : _*).as(rowParser.single).get

'rowParser' roleOptionRowParser, .

, ( ), (,? *), "":

SQL(s"""
            | select * from $source
            |    where $clause
            """.stripMargin).on(params : _*).as(rowParser *).flatten

SQL(s"""
            | select * from $source
            |    where $clause
            """.stripMargin).on(params : _*).as(rowParser ?).flatten

Ah - , "flatten" , Option [Role], , ( ):

case id~label~roletype~lid~vid~created~modified β‡’ Some(Role(id, label, roletype, lid, vid, created, modified))

, , "flatten" Option, "Role".

,

.

+2

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


All Articles