【发布时间】:2015-07-30 05:33:53
【问题描述】:
我在我的数据库中定义了这样的东西
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 领导)的回复,将我重定向到这里:User defined functions in 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](据我推断)
【问题讨论】:
-
试试
totalInvestorsFor("APPLE") should be LiteralColumn(23) -
Rep(slick.lifted.SimpleFunction$$anon$2@2d710f1a fun_totalInvestorsFor, false) was not equal to Rep(LiteralNode 23 (volatileHint=false)) -
它是一个数据库函数。它需要通过要评估的查询内部的数据库。否则,它只是数据库端应该发生的事情的表达式树,而不是结果。
-
your sql"..." 东西应该是一个 DBIO[Vector[Int]]... 我认为是,因为 SqlStreamingAction 是 DBIOAction 的子类型,别名为 DBIO跨度>
-
尝试调用 db.run 看看你得到了什么