如何使用带有 where 子句的 ScalikeJDBC 的返回()

jen*_*jen 2 scala scalikejdbc

我正在尝试将 PostgresRETURNING与 ScalikeJDBC一起使用(请参阅https://github.com/scalikejdbc/scalikejdbc/issues/559

这如何与where子句一起使用。的returning(...)是其成员UpdateSQLBuilder,而where返回一个ConditionSQLBuilder

update(Post)
   .set(sqls"${p.views}=${p.views}+${newViews}")
   .where.eq(p.id,id)
   .returning(p.id,p.lastUpdated, p.views) // does not work as it is not a member of ConditionSQLBuilder
Run Code Online (Sandbox Code Playgroud)

小智 5

正如你所说,returning不是ConditionSQLBuilder. 但是,您可以appendwhere子句之后使用method ,因为它是在 上定义的ConditionSQLBuilder

update(Post)
   .set(sqls"${p.views} = ${p.views} + ${newViews}")
   .where.eq(p.id, id)
   .append(sqls"returning ${p.id}, ${p.lastUpdated}, ${p.views}"
Run Code Online (Sandbox Code Playgroud)

并且,如果您将使用构建器将返回的列映射到一个对象中,如下所示:

val postOpt = withSQL(builder).map(Post(p)).single().apply()
Run Code Online (Sandbox Code Playgroud)

然后您必须在您的 中使用${p.result.columnName}而不是,您将其作为参数传递给方法。要映射所有列,只需使用.${p.columnName}SQLSyntaxappend${p.result.*}