转义自定义 h2 查询中的冒号(':')

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

Escape colon (':') in custom h2 Query

问题

所以我试图使用h2的JSON_OBJECT函数编写自定义查询。JSON_OBJECT的格式为JSON_OBJECT(key:value),因此在我的Spring存储库中,我写了一个类似这样的查询示例:@Query(value = "SELECT JSON_OBJECT('id':1)", nativeQuery = true)

在h2控制台中执行相同的查询时,结果如预期。但在Spring中,冒号(':')被视为用于变量插入的特殊字符,因此在测试时,它尝试将以下值映射为变量,这当然会引发错误。

我尝试过用\\\\\\进行冒号的转义,还尝试在冒号和值之间加入空格,但似乎都没有帮助。

有关如何转义字符或使Spring认为冒号是可接受字符的任何想法吗?

英文:

So I am trying to write a custom query for h2 using its JSON_OBJECT function. JSON_OBJECT uses a format of JSON_OBJECT(key:value) so as a simple example in my Spring repository I am writing a query like @Query(value = "SELECT JSON_OBJECT('id':1)", nativeQuery = true)

When executing that same query in the h2-console it operates as expected but in Spring the colon(':') is treated as a special character for variable insertion so when testing it, it tries to map the following value as a variable which of course throws an error.

I've tried escaping the colon with \\ and \\\\ and putting a space between the colon and the value but doesnt seem to help.

Any ideas on how to either escape the char or make spring think the colon is an acceptable character?

答案1

得分: 1

实际上,您可以直接使用替代语法JSON_OBJECT(KEY 'id' VALUE 1),无需使用转义的\\:,转义序列会降低查询的可读性。

英文:

Actually you can simply use the alternative syntax JSON_OBJECT(KEY 'id' VALUE 1), there is no need to use escaped \\:, escape sequences make your query less readable.

huangapple
  • 本文由 发表于 2020年9月4日 04:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63731097.html
匿名

发表评论

匿名网友

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

确定