如何使用 Doobie 在插入语句中获得可选结果? [英] How to get optional result in insert statements with Doobie?
本文介绍了如何使用 Doobie 在插入语句中获得可选结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可选的插入查询:
val q = sql"insert into some_table (some_field) select 42 where ...(some condition)"
运行此查询:
q.update.withUniqueGeneratedKeys[Option[Long]]("id")
失败
<块引用>结果集已用尽:预计会有更多行
则 condition
为假.
如何使用 Doobie 从插入语句中获取 Optional[Long]
结果?
UPD
.withGeneratedKeys[Long]("id")
仅给出 Long
以供理解
val q = sql"insert into some_table (some_field) select 42 where ...(some condition)"为了 {id <- q.update.withGeneratedKeys[Long]("id")//id 很长_ <- if (<id is present>) <其他一些插入>其他<什么都没有>产量 ID
如何查看id
?
解决方案
正如@Thilo 所评论的,您可以使用 withGeneratedKeys
来返回一个 Stream[F, Long]
(其中 F
是您的效果类型)
val result = q.update.withGeneratedKeys[Long]("id")
这是文档.>
I have an optional insert query:
val q = sql"insert into some_table (some_field) select 42 where ...(some condition)"
Running this query with:
q.update.withUniqueGeneratedKeys[Option[Long]]("id")
fails with
Result set exhausted: more rows expected
then condition
is false.
How to get Optional[Long]
result from insert statements with Doobie?
UPD
.withGeneratedKeys[Long]("id")
gives just Long
in for comprehension
val q = sql"insert into some_table (some_field) select 42 where ...(some condition)"
for {
id <- q.update.withGeneratedKeys[Long]("id") // id is long
_ <- if (<id is present>) <some other inserts> else <nothing>
} yield id
How to check id
?
解决方案
As @Thilo commented, you can use the use withGeneratedKeys
which gives you back a Stream[F, Long]
(where F
is your effect type)
val result = q.update.withGeneratedKeys[Long]("id")
Here is the doc.
这篇关于如何使用 Doobie 在插入语句中获得可选结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文