【发布时间】:2015-05-05 22:46:34
【问题描述】:
我是 SLICK (2.1) 的新手,在使用 union 创建我的第一个查询时迷失了方向。因为这些参数最终是从外部(通过 Web 界面)提供的,所以我将它们设置为可选的。请参阅下面代码中的注释。如何创建合适的查询?
我的实际课程更复杂,为了这个问题,我对其进行了简化。
case class MyStuff(id: Int, value: Int, text: String)
class MyTable (tag: Tag) extends Table[MyStuff](tag, "MYSTUFF"){
def id = column[Int]("ID", O NotNull)
def value = column[Int]("VALUE", O NotNull)
def text = column[String]("TEXT", O NotNull)
def * =
(id,
value,
text).shaped <> ((MyStuff.apply _).tupled, MyStuff.unapply)
}
object myTable extends TableQuery(new MyTable(_)){
def getStuff(ids: Option[List[Int]], values: Option[List[Int]])(implicit session: Session): Option[List[MyStuff]] = {
/*
1) If 'ids' are given, retrieve all matching entries, if any.
2) If 'values' are given, retrieve all matching entries (if any), union with the results of the previous step, and remove duplicate entries.
4) If neither 'ids' nor 'values' are given, retrieve all entries.
*/
}
}
getStuff 是这样调用的:
db: Database withSession { implicit session => val myStuff = myTable.getStuff(...) }
【问题讨论】:
-
O NotNull 不应显式写入。这是 Slick 中的默认设置。