简体   繁体   English

HDBC-postgreSQL bytea字段以SqlByteString十六进制字符串形式返回

[英]HDBC-postgreSQL bytea fields get returned as SqlByteString hex strings

When I select bytea fields filled with binary data in my Postgres database using the HDBC-postgreSQL driver (version 2.3.2.3), they come out as: 当我使用HDBC-postgreSQL驱动程序(版本2.3.2.3)在Postgres数据库中选择由二进制数据填充的bytea字段时,它们显示为:

SqlByteString "\\x<hex representation of binary data>"

That is, it returns a ByteString which contains a string containing \\x followed by the hex representation of my binary data. 也就是说,它返回一个ByteString ,其中包含一个包含\\x的字符串,后跟我的二进制数据的十六进制表示。 This is inconvenient, dreadfully inefficient and basically makes no sense to me. 这很不方便,效率低下,对我来说基本上没有任何意义。

Is there any reason why it doesn't return a SqlByteString containing a byte string with the actual binary data inside it? 有什么原因为什么它不返回包含字节串以及内部实际二进制数据的SqlByteString Is there something I am missing, or how do I configure the driver to do that? 我缺少什么,或者如何配置驱动程序来做到这一点?

thanks 谢谢

This is a longtime known issue with this library. 这是该库的长期已知问题。 See this bug for example. 例如,请参见此错误

The broader problem is that getting raw bytes requires a fair amount of cleverness that the postgres api doesn't make obvious. 更广泛的问题是,获取原始字节需要相当高的聪明度,而postgres api并不明显。 You have to call your entire query with binary rather than text output (which is arguably better anyway, but would require a rewrite of that portion of the binding layer). 您必须使用二进制而不是文本输出来调用整个查询(无论如何,这可能会更好,但是需要重写绑定层的该部分)。

You can see where pqexecparams is called and note it is called with a last parameter of 0, which by the postgres docs means everything comes back in text. 您可以看到在哪里调用了pqexecparams,并注意到使用最后一个参数0调用了pqexecparams ,这对于postgres文档来说意味着一切都以文本形式返回。 And for postgres that means this funny hex representation you see. 对于postgres,这意味着您会看到这种有趣的十六进制表示形式。

If that argument were swapped to 1, then things could be done more efficiently (including getting raw binary for bytea fields) but Statement.hsc would have to be pervasively rewritten to deserialize those binary values. 如果将该参数交换为1,则可以更高效地完成工作(包括为bytea字段获取原始二进制文件),但是必须普遍重写Statement.hsc才能反序列化那些二进制值。

This is one of those things where its slightly irritating to lots of people, but nobody has yet been sufficiently motivated to go rewrite and debug the whole thing. 这是对许多人有些恼怒的事情之一,但是没有人有足够的动力去重写和调试整个事情。 But, of course, somebody really should! 但是,当然,应该有人! :-) :-)

I've solved this going to postgresql.conf and adding: 我已经解决了这个问题到postgresql.conf并添加:

bytea_output = 'escape' #by default, it's 'hex'

What this dose is that it can now retrieve data exactly the way you inserted it because the output is no longer encoded. 现在,由于不再对输出进行编码,因此它现在可以完全按照插入方式检索数据。

I know it's late, I'm also new to Stack Overflow, but I taught that this information is important. 我知道已经晚了,我也是Stack Overflow的新手,但是我告诉我这些信息很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM