How to use StaticQuery in Slick 3.0.0?

丶灬走出姿态 提交于 2019-12-08 16:14:09

问题


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:


Only solution I managed to find was a bit hackish:

import slick.driver.HsqldbDriver.api._

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


来源:https://stackoverflow.com/questions/30846817/how-to-use-staticquery-in-slick-3-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!