gpt4 book ai didi

scala - 如何将 ScalikeJDBC 的 returned() 与 where 子句一起使用

转载 作者:行者123 更新时间:2023-12-01 09:43:27 25 4
gpt4 key购买 nike

我正在尝试将 Postgres 的 RETURNING 与 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

最佳答案

正如您所说,返回不是ConditionSQLBuilder的成员。但是,您可以在 where 子句之后使用 append 方法,因为它是在 ConditionSQLBuilder 上定义的:

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

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

val postOpt = withSQL(builder).map(Post(p)).single().apply()

那么您必须在 SQLSyntax 中使用 ${p.result.columnName} 而不是 ${p.columnName},您可以使用作为参数传递给 append 方法。要映射所有列,只需使用 ${p.result.*}

关于scala - 如何将 ScalikeJDBC 的 returned() 与 where 子句一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49069348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com