Scala 我是否可以使用变量字段构建更新查询而不使用普通SQL?

Scala 我是否可以使用变量字段构建更新查询而不使用普通SQL?,scala,scala-2.10,slick,Scala,Scala 2.10,Slick,我是否可以在不使用普通SQL的情况下构建具有可变字段数的更新查询 例如,单个列的更新很简单——我只是让它创建窄查询 Query(RolesTable).filter((role: RolesTable.type) => role.id === role_id).map((role: RolesTable.type) => role.name).update(name) 但是,如果角色有5个字段,我想允许API客户端向我发送他想更新的字段,该怎么办?如何构造这样的查询?这里的关键是在

我是否可以在不使用普通SQL的情况下构建具有可变字段数的更新查询

例如,单个列的更新很简单——我只是让它创建窄查询

Query(RolesTable).filter((role: RolesTable.type) => role.id === role_id).map((role: RolesTable.type) => role.name).update(name)

但是,如果
角色
有5个字段,我想允许API客户端向我发送他想更新的字段,该怎么办?如何构造这样的查询?

这里的关键是在生成所选列时使用
~
操作符。使用文档中的示例模型:

case class User(id: Option[Int], first: String, last: String)
object UsersTable extends Table[User]("users") {
  def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
  def first = column[String]("first")
  def last = column[String]("last")
  def * = id.? ~ first ~ last <> (User, User.unapply _)
}
然后,
update
调用将是:

cols.update((newFirst,newLast))
此外,如果您愿意,您可以重新编写您的选择,以便于理解,因为它会将其缩短一点:

val cols = for(user <- UsersTable if (user.id === userId)) yield user.first ~ user.last
cols.update((newFirst,newLast))

val cols=for(用户,但是如果应该连接的字段只有在运行时才知道呢?我猜你不能用我上面的方法做到这一点。将列组合在一起会创建一个投影,该投影将静态类型化为组合在一起的列。然后,
更新
签名基于此项目的类型可以在编译时检查它们的正确性。我只是不确定如何使用动态列列表,然后使用一组动态值来调用
update
val cols = for(user <- UsersTable if (user.id === userId)) yield user.first ~ user.last
cols.update((newFirst,newLast))