pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedLibpq: PQunescapeBytea
2+ messages / 1 participants
[nested] [flat]
* Libpq: PQunescapeBytea
@ 2007-03-22 13:36 CN <cnliou9@fastmail.fm>
0 siblings, 0 replies; 2+ messages in thread
From: CN @ 2007-03-22 13:36 UTC (permalink / raw)
To: pgsql-interfaces
Hi!
I write binary strings to bytea column by calling PQexecParams and
passing "1" indicating a binary data to parameter paramFormats. This
function is handy of course because there is no need to escape the
to-write binary strings before calling PQexecParams.
Now I notice that in order to restore the string to its original form
(the binary string without escaped), I will call PQgetvalue and pass its
returned value to PQunescapeBytea.
What I don't understand are these two parts of manual (Chapter 29. libpq
- C Library):
[quote]
PQunescapeBytea
Converts a string representation of binary data into binary data ¡X the
reverse of PQescapeBytea. This is needed when retrieving bytea data in
text format, but not when retrieving it in binary format.
[end quote]
Question: Does it mean that there exist some functions being able to
retrieve bytea columns in "binary format"? My understanding of its
meaning is that these functions will automatically restore the data read
from database to their original unescaped binary strings and thus
PQescapeBytea needs not to be called. If it does, what are these
functions? PQgetvalue appears to be not one of them I think.
[quote]
This conversion is not exactly the inverse of PQescapeBytea, because the
string is not expected to be "escaped" when received from PQgetvalue.
[end quote]
Question: Would someone kindly help me understand the above sentence?
TIA
CN
--
http://www.fastmail.fm - Choose from over 50 domains or use your own
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Libpq: PQunescapeBytea
@ 2007-03-23 13:11 CN <cnliou9@fastmail.fm>
0 siblings, 0 replies; 2+ messages in thread
From: CN @ 2007-03-23 13:11 UTC (permalink / raw)
To: pgsql-interfaces
On Thu, 22 Mar 2007 17:23:01 +0100, "Daniel Verite"
<daniel@manitou-mail.org> said:
> CN wrote:
>
> > Question: Does it mean that there exist some functions being able to
> > retrieve bytea columns in "binary format"? My understanding of its
> > meaning is that these functions will automatically restore the data read
> > from database to their original unescaped binary strings and thus
> > PQescapeBytea needs not to be called. If it does, what are these
> > functions? PQgetvalue appears to be not one of them I think.
>
> There's no specific function but when you pass 1 to the
> 'resultFormat' parameter of PQexecParams(), its results are
> returned in binary format.
> Then PQgetvalue() will give you the address of the data and
> PQgetlength() its size. When this data is of type bytea, no
> conversion is required.
Thank you for the explanation!
I did more testings and noted more details not depicted in the document:
- If a text string is written by PQexecParams() with paramFormats set to
1 and PQexec() is called to retrieve the data, unescaping is not needed
to be applied to the value returned by PQgetvalue().
- If a binary string is written by PQexecParams() with paramFormats set
to 1 and PQexec() is called to retrieve the data, it is essential to
call PQescapeBytea to unescape the value returned by PQgetvalue() in
order to restore it to the original binary string.
- If a binary string is written by PQexecParams() with paramFormats set
to 1 and PQexecParams() is called (with resultFormat set to 1) to
retrieve the data, unescaping is not needed to be applied to the value
returned by PQgetvalue().
Regards,
CN
--
http://www.fastmail.fm - The professional email service
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2007-03-23 13:11 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-03-22 13:36 Libpq: PQunescapeBytea CN <cnliou9@fastmail.fm>
2007-03-23 13:11 Re: Libpq: PQunescapeBytea CN <cnliou9@fastmail.fm>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox