Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use StaticQuery in Slick 3.0.0?

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)
    }
}
like image 379
sedovav Avatar asked Jun 15 '15 13:06

sedovav


Video Answer


1 Answers

Only solution I managed to find was a bit hackish:

import slick.driver.HsqldbDriver.api._

def fetchResult[T](sql: String) = {
    database.run(sqlu"""#$sql""")
}
like image 129
mr MR Avatar answered Sep 25 '22 04:09

mr MR