Spring的R2DBC `DatabaseClient`:如何插入带有`tstzrange`字段的数据

huangapple go评论66阅读模式
英文:

Spring's R2DBC `DatabaseClient`: How to insert with `tstzrange` fields

问题

基本上,在我的WebFlux应用程序中,我有一个Spring R2DBC的DatabaseClient,我正在尝试插入到一个包含类型为tstzrange的行的表中。
当尝试将一个String插入到这一行时,我会得到以下错误:

[42804]列“lifetime”的类型是tstzrange,但表达式的类型是character varying

这似乎是“正常”的,但我找不到关于如何编写自己的Parameter以将我的String编码为tstzrange的文档。

注意:使用相同的String进行插入而不使用绑定参数是有效的。

我在哪里可以找到有关如何实现此功能的文档?

或者是否有人已经为Spring的R2DBC DatabaseClient编写了tstzrange的适配器?

英文:

Basically, in my WebFlux application, I have a Spring R2DBC's DatabaseClient, with which I am trying to insert into a table containing a row typed as tstzrange.
When trying to insert a String into this row, I get the following error:

[42804] column "lifetime" is of type tstzrange but expression is of type character varying
client.sql("""
               update article
               set lifetime = :lifetime
               """)
      .bind("lifetime", range.toString()))

This seems "normal", but I can not find documentation on how to write my own Parameter which can encode my String into a tstzrange.

> Note: inserting using the same String without bind parameters works.

Where can I find documentation on how to implement such feature?

Or did someone already write an adapter for tstzrange in Spring's R2DBC DatabaseClient?

答案1

得分: 0

使用以下“技巧”解决:

client.sql("""
               update article
               set lifetime = tstzrange(:lower::timestamptz, :upper::timestamptz, :bounds)
               """)
      .bind("lower", Parameter.fromOrEmpty(range.getLowerString(), String.class))
      .bind("upper", Parameter.fromOrEmpty(range.getUpperString(), String.class))
      .bind("bounds", Parameter.fromOrEmpty(range.getBoundsString(), String.class))

对此不是特别喜欢,我希望在这样一个看似简单的任务中不必增加复杂性,但至少它有效。

英文:

Solved using the following "trick":

client.sql("""
               update article
               set lifetime = tstzrange(:lower::timestamptz, :upper::timestamptz, :bounds)
               """)
      .bind("lower", Parameter.fromOrEmpty(range.getLowerString(), String.class))
      .bind("upper", Parameter.fromOrEmpty(range.getUpperString(), String.class))
      .bind("bounds", Parameter.fromOrEmpty(range.getBoundsString(), String.class))

Not really a huge fan of this, I wish I would not have to add complexity in such a supposedly simple task, but at least it works.

huangapple
  • 本文由 发表于 2023年7月17日 20:10:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定