如何在Slick 3.0.0中使用StaticQuery? [英] How to use StaticQuery in Slick 3.0.0?

查看:106
本文介绍了如何在Slick 3.0.0中使用StaticQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Slick 2.1中,我有以下代码从文件执行sql查询:

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)
    }
}

在Slick 3.0.0中,您使用dbConfig.db.run方法执行DBIOAction并获得结果的未来.但是我找不到将Q.queryNA(为StaticQuery[Unit, R])的结果转换为DBIOAction的方法.是否存在这种方式?

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)
    }
}

推荐答案

只有我设法找到的解决方案有点黑:

Only solution I managed to find was a bit hackish:

import slick.driver.HsqldbDriver.api._

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

这篇关于如何在Slick 3.0.0中使用StaticQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆