2015-05-12 46 views
1

我用油滑3.0:https://github.com/slick/slick/tree/3.0.0在Slick 3.0中,爲什麼`result`方法可以應用到`Query`對象?

的示例代碼如下所示:

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 
) 

我想在這裏說明在coffees.map(_.price).resultresult方法:

http://www.scala-lang.org/api/2.11.5/index.html#[email protected]():To

這是一種Builder類的方法。

然而,coffees.map(_.price)Query類,而不是Builder類,並Query類好像不是Builder類的子類。此外,似乎沒有從Query類到Builder類的隱式轉換。並且Query類沒有稱爲result的方法。

那麼result如何應用於Query對象。有沒有人有關於此的想法?

回答

1

result方法不是來自Builder類。它在QueryActionExtensionMethodsImpl類中定義在JdbcActionComponent.scala文件中。

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

有一個名爲repQueryActionExtensionMethods方法,其Rep(的Query超類)轉換成QueryActionExtensionMethods

相關問題