2

我正在使用 Slick 3.0:https ://github.com/slick/slick/tree/3.0.0

示例代码如下所示:

class Coffees(tag: Tag) extends Table[(String, Double)](tag, "COFFEES") {
  def name = column[String]("COF_NAME", O.PrimaryKey)
  def price = column[Double]("PRICE")
  def * = (name, price)
}
val coffees = TableQuery[Coffees]
val coffeeNames: Future[Seq[Double]] = db.run(
  coffees.map(_.price).result
)

我认为这里描述的result方法coffees.map(_.price).result是:

http://www.scala-lang.org/api/2.11.5/index.html#scala.collection.mutable.Builder@result():到

这是一个Builder类的方法。

但是,coffees.map(_.price)Query类而不是Builder类,而且Query类似乎不是类的子Builder类。此外,似乎没有从Query类到Builder类的隐式转换。并且Query类没有一个名为result.

那么如何将result其应用于Query对象。有人对此有想法吗?

4

2 回答 2

1

result方法不是来自 Builder 类。它在JdbcActionComponent.scala文件的QueryActionExtensionMethodsImpl类中定义。

于 2015-05-12T04:30:33.470 回答
0

在 /slick/profile/BasicProfile.scala 中,

  trait API extends CommonAPI with CommonImplicits {
    implicit def repQueryActionExtensionMethods[U](rep: Rep[U]): QueryActionExtensionMethods[U, NoStream] =
      createQueryActionExtensionMethods[U, NoStream](queryCompiler.run(rep.toNode).tree, ())
    implicit def streamableQueryActionExtensionMethods[U, C[_]](q: Query[_,U, C]): StreamingQueryActionExtensionMethods[C[U], U] =
      createStreamingQueryActionExtensionMethods[C[U], U](queryCompiler.run(q.toNode).tree, ())
    implicit def runnableCompiledQueryActionExtensionMethods[RU](c: RunnableCompiled[_, RU]): QueryActionExtensionMethods[RU, NoStream] =
      createQueryActionExtensionMethods[RU, NoStream](c.compiledQuery, c.param)
    implicit def streamableCompiledQueryActionExtensionMethods[RU, EU](c: StreamableCompiled[_, RU, EU]): StreamingQueryActionExtensionMethods[RU, EU] =
      createStreamingQueryActionExtensionMethods[RU, EU](c.compiledQuery, c.param)
    // Applying a CompiledFunction always results in only a RunnableCompiled, not a StreamableCompiled, so we need this:
    implicit def runnableStreamableCompiledQueryActionExtensionMethods[R, RU, EU, C[_]](c: RunnableCompiled[Query[R, EU, C], RU]): StreamingQueryActionExtensionMethods[RU, EU] =
      createStreamingQueryActionExtensionMethods[RU, EU](c.compiledQuery, c.param)
    // This only works on Scala 2.11 due to SI-3346:
    implicit def recordQueryActionExtensionMethods[M, R](q: M)(implicit shape: Shape[_ <: FlatShapeLevel, M, R, _]): QueryActionExtensionMethods[R, NoStream] =
      createQueryActionExtensionMethods[R, NoStream](queryCompiler.run(shape.toNode(q)).tree, ())
  }

有一个方法叫做repQueryActionExtensionMethodsRep(的超类Query)转换成QueryActionExtensionMethods

于 2015-05-12T05:55:27.733 回答