5

我在我的数据库中定义了这样的东西

CREATE FUNCTION fun_totalInvestorsFor(issuer varchar(30)) RETURNS INT
NOT DETERMINISTIC
BEGIN
  RETURN (SELECT COUNT(DISTINCT LOYAL3_SHARED_HOLDER_ID) 
      FROM stocks_x_hldr
      WHERE STOCK_TICKER_SIMBOL = issuer AND
            QUANT_PURCHASES > QUANT_SALES);
END;

现在我收到了 Stefan Zeiger(Slick 领导)的答复,将我重定向到这里:Slick 中的用户定义函数

我已经尝试过(在范围内有以下对象):

lazy val db = Database.forURL("jdbc:mysql://localhost:3306/mydb",
driver = "com.mysql.jdbc.Driver", user = "dev", password = "root")
val totalInvestorsFor = SimpleFunction.unary[String, Int]("fun_totalInvestorsFor")
totalInvestorsFor("APPLE") should be (23)

结果:Rep(slick.lifted.SimpleFunction$$anon$2@13fd2ccd fun_totalInvestorsFor, false) was not equal to 23

我也尝试过在 src/main/resources 中有一个 application.conf,如下所示:

tsql = {
  driver = "slick.driver.MySQLDriver$"
  db {
    connectionPool = disabled
    driver = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://localhost/mydb"
  }
}

然后在我的代码中@StaticDatabaseConfig("file:src/main/resources/application.conf#tsql")

tsql"select fun_totalInvestorsFor('APPLE')" should be (23)

结果:Error:(24, 9) Cannot load @StaticDatabaseConfig("file:src/main/resources/application.conf#tsql"): No configuration setting found for key 'tsql' tsql"select fun_totalInvestorsFor('APPLE')" should be (23) ^

我还计划通过sql"call myProc(v1).as[(Int, Int, Int)]调用返回三个值的一个元组的存储过程

有任何想法吗?

编辑:当 生成sql""""SELECT COUNT(DISTINCT LOYAL3_SHARED_HOLDER_ID) FROM stocks_x_hldr WHERE STOCK_TICKER_SIMBOL = issuer AND QUANT_PURCHASES > QUANT_SALES""".as[(Int)] 结果SqlStreamingAction[Vector[Int], Int, Effect]而不是DBIO[Int]文档建议的(根据我推断的)时

4

1 回答 1

3

过去一周我遇到了完全相同的问题。经过一些广泛的研究(请参阅我的帖子here,我将添加对我作为解决方案所做的工作的完整描述),我认为它不能在 Slick 中完成......严格来说不是。

但是,我反对将纯 JDBC 或 Anorm 添加到我们的解决方案堆栈中,所以我确实找到了一个“可接受的”修复,恕我直言。

解决方法是从 Slick 中获取 session 对象,然后使用普通的 JDBC 来管理存储函数/存储过程调用。那时,您可以使用任何使其更容易的第三方库......尽管在我的情况下,我编写了自己的函数来设置调用并返回结果集。

val db = Database.forDataSource(DB.getDataSource)
var response: Option[GPInviteResponse] = None

db.withSession {
    implicit session => {
        // Set up your call here... (See my other post for a more detailed
        // answer with an example:
        // procedure is eg., "{?=call myfunction(?,?,?,?)}"
        val cs = session.conn.prepareCall(procedure.toString)
        // Set up your in and out parameters here
        // eg. cs.setLong(index, value)
        val result = cs.execute()
        val rc = result.head.asInstanceOf[Int]

        response = rc match {
            // Package up the response to the caller
        }
    }
}
db.close()

我知道这很简洁,但正如我所说,请参阅另一个线程以获得更完整的帖子。我现在把它放在一起,很快就会发布答案。

于 2015-05-18T22:59:23.370 回答