1

I have a problem with slick. It looks like after using it scala is not a statically typed language anymore:)

I used slick to call a SqlServer stored procedure that should return some values (and it does when I call the procedure directly in my SQL client). Unfortunately as you can see below the result is that I have a val with type Option[ProcessUserRoleDto], with ProcessUserRoleDto being just an ordinary case class, that in fact hold value of Some(1) and the '1' is of type java.lang.Integer. How is this possible? Do you have any ideas how to resolve this problem?

case class ProcessUserRoleDto(
  processUserRoleId: Int,
  processUserId: Int,
  processRoleId: Int,
  dataCollectionId: Int,
  recognizingInstanceId: Int,
  processRoleName: String,
  dataCollectionName: String,
  recognizingInstanceName: String,
  isActive: Boolean)

def assign(dto: ProcessUserRoleAssignDto): Future[Option[ProcessUserRoleDto]] = {
  val db = implicitly[SQLServerDriver.backend.DatabaseDef]
  val sql = sql"exec EVO.spProcessUserRoleAssignToRecognizingInstance ${dto.ProcessUser_ID}, ${dto.ProcessRole_ID}, ${dto.RecognizingInstance_ID}"

  implicit val registerProcessUserResult = GetResult { r =>
        ProcessUserRoleDto(r.<<, r.<<, r.<<, r.<<, r.<<, r.<<, r.<<, r.<<, r.<<)
  }
  val resultFut: Future[Option[ProcessUserRoleDto]] = db
                                .run(sql.as[ProcessUserRoleDto])
                                .map(_.headOption)

  val singleResult: Option[ProcessUserRoleDto] = Await.result(resultFut, 10 seconds)

  println(s"singleResult = $singleResult")
  // in runtime result was: Some(1)
  // WTF ?!?

  println(s"class inside Some() = ${singleResult.get.getClass.getCanonicalName}")
  // in runtime result is: java.lang.Integer

  println(s"Option[Class] = ${singleResult.map(_.getClass.getCanonicalName)}")
  // here exception is thrown: java.lang.ClassCastException: java.lang.Integer cannot be cast to ProcessUserRoleDto
}
godfryd
  • 582
  • 3
  • 16

1 Answers1

0

The stored procedure was executing an insert statement and then a select statement. I turns out that it confused the JDBC driver because the insert was returning the number of modified records and that adding "SET NOCOUNT ON" to the stored procedure stoped that (https://stackoverflow.com/a/4809815/1185138).

Still, slick quietly messed up types instead of reaturning a nice error message about that..

Community
  • 1
  • 1
godfryd
  • 582
  • 3
  • 16