Question

In my Scala Playframework app I'm trying to, with executeInsert, create a sms_token(class). Then with the primary key create a unique token, add it to sms_token and save it with executeUpdate.

case class SmsToken(id: Option[Long], token: String, phoneNumber: String, startDate: Option[Date], endDate: Option[Date], used: Boolean, tempReviewGrade: Option[Int], tempReviewText: Option[String])

object SmsToken {
  val simple = {
    get[Option[Long]]("id") ~
      get[String]("token") ~
      get[String]("phone_number") ~
      get[Option[Date]]("start_date") ~
      get[Option[Date]]("end_date") ~
      get[Boolean]("used") ~
      get[Option[Int]]("temp_review_grade") ~
      get[Option[String]]("temp_review_text") map {
      case id ~ token ~ phone_number ~ start_date ~ end_date ~ used ~ temp_review_grade ~ temp_review_text => SmsToken(id, token, phone_number, start_date, end_date, used, temp_review_grade, temp_review_text)
    }
  }
}

method:

 def createToken(n: String): Option[Long] = {

    var addedTokenPk = 0L

    val result = DB.withConnection {
      implicit connection =>
        SQL("insert into sms_token(token, phone_number, start_date, end_date, used, temp_review_grade, temp_review_text) values({token},{phone_number},{start_date},{end_date},{used}, {temp_review_grade}, {temp_review_text})").on(
          'token -> "",
          'phone_number -> n,
          'start_date -> new Date(),
          'end_date -> new Date(),
          'used -> 0,
          'temp_review_grade -> 0,
          'temp_review_text -> ""
        ).executeInsert()
    }
    result match {
      case Some(pk) => {
        addedTokenPk = pk.asInstanceOf[Long]
      }
      case None => println("YAAARRRRR")
    }

    if (addedTokenPk != 0L) {

      val token = Util.createUniqueToken(addedTokenPk)

      DB.withConnection {
        implicit connection =>
          SQL("update sms_token s set s.token={token} where s.id={id}").on(
            'id -> ("" + addedTokenPk).toLong,
            'token -> token
          ).executeUpdate()
      }
      return Some(addedTokenPk)
    }
    None
  }

my question is regarding this part:

result match {
    case Some(pk) => {
        addedTokenPk = pk.asInstanceOf[Long]
    }
    case None => println("YAAARRRRR")
}

executeInsert returns an Any object and since the key is Long I'm doing asInstanceOf[Long] since I need it later in the update part. I'm a Scala noob so I'm not sure that this is correct.. Maybe there is some better Scala way?

Was it helpful?

Solution

You get the Long id if you invoke map on the executeInsert result

DB.withConnection { implicit connection =>
   SQL("...").executeInsert().map(id => println(id))
}

OTHER TIPS

An alternative is to pass the simple parser to executeInsert:

val result: SmsToken = DB.withConnection {
  implicit connection =>
    SQL("insert into sms_token(token, phone_number, start_date, end_date, used, temp_review_grade, temp_review_text) values({token},{phone_number},{start_date},{end_date},{used}, {temp_review_grade}, {temp_review_text})").on(
      'token -> "",
      'phone_number -> n,
      'start_date -> new Date(),
      'end_date -> new Date(),
      'used -> 0,
      'temp_review_grade -> 0,
      'temp_review_text -> ""
    ).executeInsert(simple.single)
}

This often allows you to do the above in one pass, and in this case you'll have access to pk and the token allowing you to reference result attributes in the database query.

Note: I was searching to try and do exactly this and stumbled on this question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top