*
方法:
这将返回默认投影- 这就是您描述的方式:
'我通常感兴趣的所有列(或计算值)'。
你的表可以有几个字段;您只需要一个默认投影的子集。默认投影必须与表的类型参数匹配。
让我们一次一个。没有这些<>
东西,只有*
:
// First take: Only the Table Defintion, no case class:
object Bars extends Table[(Int, String)]("bar") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def * = id ~ name // Note: Just a simple projection, not using .? etc
}
// Note that the case class 'Bar' is not to be found. This is
// an example without it (with only the table definition)
像这样的表定义将让您进行如下查询:
implicit val session: Session = // ... a db session obtained from somewhere
// A simple select-all:
val result = Query(Bars).list // result is a List[(Int, String)]
的默认投影(Int, String)
导致List[(Int, String)]
诸如此类的简单查询。
// SELECT b.name, 1 FROM bars b WHERE b.id = 42;
val q =
for (b <- Bars if b.id === 42)
yield (b.name ~ 1)
// yield (b.name, 1) // this is also allowed:
// tuples are lifted to the equivalent projection.
是什么类型的q
?它是一个Query
与投影(String, Int)
。调用时,它根据投影返回一个List
元(String, Int)
组。
val result: List[(String, Int)] = q.list
在这种情况下,您已经在理解yield
子句中定义了您想要的投影。for
现在关于<>
和Bar.unapply
。
这提供了所谓的映射投影。
到目前为止,我们已经看到 slick 如何让您在 Scala 中表达返回列投影(或计算值)的查询;因此,在执行这些查询时,您必须将查询的结果行视为Scala 元组。元组的类型将与定义的 Projection 匹配(根据您
for
在前面示例中的理解,或默认*
投影)。这就是为什么返回where
的类型和类型field1 ~ field2
的投影的原因。Projection2[A, B]
A
field1
B
field2
q.list.map {
case (name, n) => // do something with name:String and n:Int
}
Queury(Bars).list.map {
case (id, name) => // do something with id:Int and name:String
}
我们正在处理元组,如果我们有太多列,这可能会很麻烦。我们不想将结果视为TupleN
具有命名字段的对象。
(id ~ name) // A projection
// Assuming you have a Bar case class:
case class Bar(id: Int, name: String) // For now, using a plain Int instead
// of Option[Int] - for simplicity
(id ~ name <> (Bar, Bar.unapply _)) // A MAPPED projection
// Which lets you do:
Query(Bars).list.map ( b.name )
// instead of
// Query(Bars).list.map { case (_, name) => name }
// Note that I use list.map instead of mapResult just for explanation's sake.
这是如何运作的?<>
接受投影Projection2[Int, String]
并返回类型上的映射投影Bar
。这两个参数巧妙地Bar, Bar.unapply _
告诉了这个(Int, String)
投影必须如何映射到一个案例类。
这是一个双向映射;Bar
是案例类构造函数,所以这是从(id: Int, name: String)
到 a所需的信息Bar
。如果unapply
你猜到了,那就是相反的。
从哪里来unapply
?这是可用于任何普通案例类的标准 Scala 方法 - 只需定义即可Bar
为您提供Bar.unapply
一个提取器,可用于取回id
和name
构建
Bar
的:
val bar1 = Bar(1, "one")
// later
val Bar(id, name) = bar1 // id will be an Int bound to 1,
// name a String bound to "one"
// Or in pattern matching
val bars: List[Bar] = // gotten from somewhere
val barNames = bars.map {
case Bar(_, name) => name
}
val x = Bar.unapply(bar1) // x is an Option[(String, Int)]
因此,您的默认投影可以映射到您最希望使用的案例类:
object Bars extends Table[Bar]("bar") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def * = id ~ name <>(Bar, Bar.unapply _)
}
或者您甚至可以按查询拥有它:
case class Baz(name: String, num: Int)
// SELECT b.name, 1 FROM bars b WHERE b.id = 42;
val q1 =
for (b <- Bars if b.id === 42)
yield (b.name ~ 1 <> (Baz, Baz.unapply _))
这里的类型q1
是Query
a映射到 的投影Baz
。调用时,它返回一个List
对象Baz
:
val result: List[Baz] = q1.list
最后,顺便说一句,.?
提供选项提升- Scala 处理可能不是的值的方式。
(id ~ name) // Projection2[Int, String] // this is just for illustration
(id.? ~ name) // Projection2[Option[Int], String]
总而言之,它将与您的原始定义很好地配合使用Bar
:
case class Bar(id: Option[Int] = None, name: String)
// SELECT b.id, b.name FROM bars b WHERE b.id = 42;
val q0 =
for (b <- Bars if b.id === 42)
yield (b.id.? ~ b.name <> (Bar, Bar.unapply _))
q0.list // returns a List[Bar]
回应关于 Slick 如何使用for
理解的评论:
不知何故,单子总是设法出现并要求成为解释的一部分......
因为理解不仅仅特定于集合。它们可以用于任何类型的Monad,并且集合只是 Scala 中可用的多种 monad 类型之一。
但由于集合很熟悉,它们为解释提供了一个很好的起点:
val ns = 1 to 100 toList; // Lists for familiarity
val result =
for { i <- ns if i*i % 2 == 0 }
yield (i*i)
// result is a List[Int], List(4, 16, 36, ...)
在 Scala 中,for 理解是方法(可能是嵌套的)方法调用的语法糖:上面的代码(或多或少)等价于:
ns.filter(i => i*i % 2 == 0).map(i => i*i)
基本上,任何带有filter
, map
,flatMap
方法(换句话说,一个Monad)的东西都可以用在
推导for
式中来代替ns
. 一个很好的例子是Option monad。这是前面的示例,其中相同的for
语句同时适用于
monadList
和Option
monad:
// (1)
val result =
for {
i <- ns // ns is a List monad
i2 <- Some(i*i) // Some(i*i) is Option
if i2 % 2 == 0 // filter
} yield i2
// Slightly more contrived example:
def evenSqr(n: Int) = { // return the square of a number
val sqr = n*n // only when the square is even
if (sqr % 2 == 0) Some (sqr)
else None
}
// (2)
result =
for {
i <- ns
i2 <- evenSqr(i) // i2 may/maynot be defined for i!
} yield i2
在最后一个示例中,转换可能如下所示:
// 1st example
val result =
ns.flatMap(i => Some(i*i)).filter(i2 => i2 %2 ==0)
// Or for the 2nd example
result =
ns.flatMap(i => evenSqr(i))
在 Slick 中,查询是一元的——它们只是带有map
,flatMap
和filter
方法的对象。所以for
理解(显示在*
方法的解释中)只是转化为:
val q =
Query(Bars).filter(b => b.id === 42).map(b => b.name ~ 1)
// Type of q is Query[(String, Int)]
val r: List[(String, Int)] = q.list // Actually run the query
如您所见,flatMap
和map
用于filter
通过
每次调用andQuery
的重复转换来生成 a 。在集合的情况下,这些方法实际上迭代和过滤集合,但在 Slick 中它们用于生成 SQL。更多详细信息:
Scala Slick 如何将 Scala 代码转换为 JDBC?Query(Bars)
filter
map