【发布时间】:2013-12-27 20:52:47
【问题描述】:
我有 A、B、C 之类的类型(彼此不相关)、一个惰性 postgresql 数据库 db(在另一个类中定义)和带有普通查询的 Slick 2.10;由于泛型,我有点困惑。我想解决以下问题:
def selectT[T](query: String, id: String): List[T] = {
db withSession{
Q.query[String, T](query).list(id)
}
}
所以方法必须具有结果类型List[T]。对于查询中的 T,我将编写类型 A、B 和 C。(我已经定义了 GetResults)。所以这是可能的
if(typeOf[T] <:< typeOf[A]) //using TypeTags
db withSession{
Q.query[String, A](query).list(id)
}
else if(typeOf[T] <:< typeOf[A])
db withSession{
Q.query[String, B](query).list(id)
}
...
每个查询本身都有效,但是 selectT 的结果类型不再适合。另一方面,我不想定义像List[AnyRef] 或List[Any] 这样的结果类型。
此外,这些类不会从 T 继承(这很简单)。
到目前为止我的代码:
import scala.slick.session.Database
import Database.threadLocalSession
import scala.slick.jdbc.{GetResult, StaticQuery => Q}
import scala.reflect.runtime.universe._
import PostgreSQLDatabase.db
object SQLExtension{
implicit val getAResult = GetResult(r => new A(...))
implicit val getBResult = GetResult(r => new B(...))
implicit val getCResult = GetResult(r => new C(...))
def selectT[T](query: String, id: String): List[T] = {
db withSession{
Q.query[String, T](query).list(id)
}
}
}
简而言之:我需要 T 的多个边界/继承/other_soulution_concept,以便我可以在方法内处理 A、B 和 C,但仍然有 List[T] 作为外部结果类型。
谢谢你,新年快乐。
编辑:2014.01.09
@cvogt 我尝试了您的解决方案,它适用于定义。但是如果我想在其他地方调用我的函数,我会遇到问题,could not find implicit value for parameter getResult: scala.slick.jdbc.GetResult[T] 和 not enough arguments for method selectT: (implicit getResult: scala.slick.jdbc.GetResult[T])Option[T]. Unspecified value parameter getResult. 但是我已经导入了我的 GetResults?
protected[dao] def getById(id: String): Option[T] = {
val ret = db.selectT[T](query, id)
ret
}
【问题讨论】:
标签: scala generics scala-2.10 slick