Slick 3.0如何更新变量列列表,该数字仅在运行时知道

use*_*204 6 scala slick-3.0

是否可以更新变量列列表,这个数字仅在运行时通过光滑3.0知道?

下面是我想做的例子(不会编译)

var q: Query[UserTable, UserTable#TableElementType, Seq] = userTable
var columns = List[Any]()
var values = List[Any]()

if (updateCommands.name.isDefined) {
  columns = q.name :: columns
  values = updateCommands.name.get :: values
}

if (updateCommands.surname.isDefined) {
  columns = q.surname :: columns
  values = updateCommands.surname.get :: values
}
q = q.filter(_.id === updateCommands.id).map(columns).update(values)
Run Code Online (Sandbox Code Playgroud)

Art*_*mis 0

在 Slick 3.0 中,他们采用了稍微不同的方法,而不是 updateAll 方法,据我所知,采用了组合器路径。

所以主要思想是定义一些对数据的操作,然后将它们组合到数据库上以进行单次运行。例子:

// let's assume that you have some table classes defined somewhere

// then let's define some actions, they might be really different
val action: SqlAction = YourTable.filter(_id === idToAssert)
val anotherAction = AnotherTable.filter(_.pets === "fun")

// and then we can combine them on a db.run 
val combinedAction =  for {
  someResult <- action
  anotherResult <- anotherAction
} yeild (someResult,anotherResult)

db.run(combinedAction) // that returns actual Future of the result type
Run Code Online (Sandbox Code Playgroud)

以同样的方式,您可以处理列表和序列,为此请查看这里:http://slick.typesafe.com/doc/3.1.0-M1/dbio.html DBIO 有一些功能允许您组合一个动作的动作列表。

我希望这个想法很清楚,如果您有疑问,欢迎评论。