英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论