在使用光滑时,如何在返回整个更新的表时更新表格表中的几列?
假设SomeTables
是某些TableQuery
,如果你想要,例如,在表中添加一个项目(并返回新添加的项目),通常会编写这样的查询
val returnedItem = SomeTables returning SomeTables += someTable
Run Code Online (Sandbox Code Playgroud)
如果你想更新一个项目并将整个项目返回整个项目,你会怎么做?我怀疑你会做这样的事情
val q = SomeTables.filter(_.id === id).map(x => (x.someColumn,x.anotherColumn)) returning SomeTables
val returnedItem = q.update((3,"test"))
Run Code Online (Sandbox Code Playgroud)
但是以下代码不起作用,我看不到任何关于如何执行此操作的文档
请注意,我知道您可以事先查询该项目,更新它,然后在原始对象上使用copy,但这需要大量的样板(以及数据库跳转)
Slick(v2或v3-M1)不支持此功能; 虽然我没有看到任何禁止它实现的具体原因,但它UPDATE ... RETURNING
不是标准的SQL功能(例如,H2不支持它:http://www.h2database.com/html/grammar.html#update).我将作为练习留给读者探讨如何安全有效地模拟RDBMS缺乏的功能UDPATE ... RETURNING
.
当你在a上调用" returns "时scala.slick.lifted.Query
,它会给你一个JdbcInsertInvokerComponent $ ReturningInsertInvokerDef.update
虽然有方法,但你找不到任何insertOrUpdate
方法; 但是,如果发生插入,则insertOrUpdate
仅返回returning
表达式结果,None
返回更新,因此这里没有帮助.
由此我们可以得出结论,如果您想使用UPDATE ... RETURNING
SQL功能,您需要使用StaticQuery或将自己的补丁滚动到Slick.您可以手动编写查询(并将表格投影重新实现为GetResult/SetParameter序列化程序),或者您可以尝试以下代码片段:
package com.spingo.slick
import scala.slick.driver.JdbcDriver.simple.{queryToUpdateInvoker, Query}
import scala.slick.driver.JdbcDriver.{updateCompiler, queryCompiler, quoteIdentifier}
import scala.slick.jdbc.{ResultConverter, CompiledMapping, JdbcBackend, JdbcResultConverterDomain, GetResult, SetParameter, StaticQuery => Q}
import scala.slick.util.SQLBuilder
import slick.ast._
object UpdateReturning {
implicit class UpdateReturningInvoker[E, U, C[_]](updateQuery: Query[E, U, C]) {
def updateReturning[A, F](returningQuery: Query[A, F, C], v: U)(implicit session: JdbcBackend#Session): List[F] = {
val ResultSetMapping(_,
CompiledStatement(_, sres: SQLBuilder.Result, _),
CompiledMapping(_updateConverter, _)) = updateCompiler.run(updateQuery.toNode).tree
val returningNode = returningQuery.toNode
val fieldNames = returningNode match {
case Bind(_, _, Pure(Select(_, col), _)) =>
List(col.name)
case Bind(_, _, Pure(ProductNode(children), _)) =>
children map { case Select(_, col) => col.name } toList
case Bind(_, TableExpansion(_, _, TypeMapping(ProductNode(children), _, _)), Pure(Ref(_), _)) =>
children map { case Select(_, col) => col.name } toList
}
implicit val pconv: SetParameter[U] = {
val ResultSetMapping(_, compiled, CompiledMapping(_converter, _)) = updateCompiler.run(updateQuery.toNode).tree
val converter = _converter.asInstanceOf[ResultConverter[JdbcResultConverterDomain, U]]
SetParameter[U] { (value, params) =>
converter.set(value, params.ps)
}
}
implicit val rconv: GetResult[F] = {
val ResultSetMapping(_, compiled, CompiledMapping(_converter, _)) = queryCompiler.run(returningNode).tree
val converter = _converter.asInstanceOf[ResultConverter[JdbcResultConverterDomain, F]]
GetResult[F] { p => converter.read(p.rs) }
}
val fieldsExp = fieldNames map (quoteIdentifier) mkString ", "
val sql = sres.sql + s" RETURNING ${fieldsExp}"
val unboundQuery = Q.query[U, F](sql)
unboundQuery(v).list
}
}
}
Run Code Online (Sandbox Code Playgroud)
我确信上面的内容可以改进; 我是基于对Slick内部的一些有限的理解而编写的,它对我有用,可以利用你已经定义的投影/类型映射.
用法:
import com.spingo.slick.UpdateReturning._
val tq = TableQuery[MyTable]
val st = tq filter(_.id === 1048003) map { e => (e.id, e.costDescription) }
st.updateReturning(tq map (identity), (1048003, Some("such cost")))
Run Code Online (Sandbox Code Playgroud)