How to use StaticQuery in Slick 3.0.0?

1.7k views Asked by At

In Slick 2.1 I had the code below to execute an sql-query from a file:

def fetchResult[T](sql: String)(implicit getResult: GetResult[T]): List[T] = {
    val query = Q.queryNA[T](sql)
    try {
        Database.forDataSource(DB.getDataSource())
            .withSession { implicit session => query.list }
    }
    catch {
      case e: Throwable =>
        throw new RunSqlException(s"Query $name execution error", e)
    }
}

In Slick 3.0.0 you use dbConfig.db.run method to execute DBIOAction and get a future of the result. But I can't find a way to transform result of Q.queryNA (which is StaticQuery[Unit, R]) into DBIOAction. Does such a way exist?

I ended up with deprecated calls for now. Help me be better!

def fetchResult[T](sql: String)(implicit getResult: GetResult[T]): Future[List[T]] = Future {
    val query = Q.queryNA[T](sql)
    try {
        this.dbConfig.db.withSession { implicit session => query.list }
    }
    catch {
      case e: Throwable =>
        throw new RunSqlException(s"Query $name execution error", e)
    }
}
1

There are 1 answers

0
mr MR On

Only solution I managed to find was a bit hackish:

import slick.driver.HsqldbDriver.api._

def fetchResult[T](sql: String) = {
    database.run(sqlu"""#$sql""")
}