他の回答が示唆するように、これは静的クエリで行うのは面倒です。静的クエリ インターフェイスでは、バインド パラメータを .xml として記述する必要がありますProduct
。(Int, Int, String*)
は有効なスカラではなく、使用(Int,Int,List[String])
にはいくつかのクラッジも必要です。さらに、それlocationCodes.size
が常にクエリの数と等しいことを確認する(?, ?...)
必要があるのは脆弱です。
実際には、代わりに query モナドを使用したいので、これはそれほど問題ではありません。これはタイプセーフであり、Slick を使用するための推奨される方法です。
val visitorId: Int = // whatever
val locationCodes = List("loc1","loc2","loc3"...)
// your query, with bind params.
val q = for {
v <- Visits
if v.visitor is visitorId.bind
if v.location_code inSetBind locationCodes
} yield v
// have a look at the generated query.
println(q.selectStatement)
// run the query
q.list
これは、テーブルが次のように設定されていることを前提としています。
case class Visitor(visitor: Int, ... location_code: String)
object Visitors extends Table[Visitor]("visitor") {
def visitor = column[Int]("visitor")
def location_code = column[String]("location_code")
// .. etc
def * = visitor ~ .. ~ location_code <> (Visitor, Visitor.unapply _)
}
メソッドでいつでもクエリをラップできることに注意してください。
def byIdAndLocations(visitorId: Int, locationCodes: List[String]) =
for {
v <- Visits
if v.visitor is visitorId.bind
if v.location_code inSetBind locationCodes
} yield v
}
byIdAndLocations(visitorId, List("loc1", "loc2", ..)) list