Обсуждение: Libpq: PQunescapeBytea
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 — 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
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. -- Daniel PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org
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. -- DanielPostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org
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