0
votes

I am trying to migrate some code from Slick 2.1 to Slick 3.1 in a Play application and the documentation on DBIO Actions is going completely over my head. And I am having a hard time finding examples anywhere online that show how to migrate uses of StaticQuery to Slick 3.1. Here is the code I had in Slick 2.1:

import scala.slick.jdbc.{GetResult, StaticQuery => Q}
...
object RegionTable {
  ...
  def selectSomething(param1: Double, param2: Double): List[Region] = db.withSession { implicit session =>
    val selectSomethingQuery = Q.query[(Double, Double), Region](
      """SELECT ...""".stripMargin
    )
    selectSomethingQuery((param1, param2)).list
  }
}

Link to the full code:

https://github.com/ProjectSidewalk/SidewalkWebpage/blob/master/app/models/region/RegionTable.scala

Similar questions:

How to use StaticQuery in Slick 3.0.0?

How to port Slick 2.1 plain SQL queries to Slick 3.0

Documentation and helpful blog posts

http://slick.lightbend.com/doc/3.0.0/upgrade.html

http://slick.lightbend.com/doc/3.1.1/upgrade.html

http://slick.lightbend.com/doc/3.1.1/dbio.html

https://grokbase.com/t/gg/scalaquery/15250knfya/slick-3-removes-withsession-dont-get-caught-by-surprise

1

1 Answers

1
votes

For example you have case class Region

case class Region(id: Int, name: String, code: String)

At first step you need to tell slick how to map sql result to your case class by providing GetResult implicit conversation:

implicit val regionGetResult = GetResult(r => Region(r.nextInt, r.nextString, r.nextString))

Than you should construct DBIOAction:

val selectAction = sql"""select * from regions where name = ${param1} and code = ${param2}""".as[Region]

This will return you DBIO[Seq[Region]]. Now you can execute this action with db object and get Future[Seq[Region]] as a result:

db.run(selectAction).map(regions => ...)