Re: PQescapeBytea* version for parameters

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "PostgreSQL-development Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: PQescapeBytea* version for parameters
Date: 2007-07-11 15:36:10
Message-ID: 87ps2z9clx.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

"Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:

> Gregory Stark <stark(at)enterprisedb(dot)com> writes:
>> Do we want something like this which provides a PQescapeByteaParam for
>> escaping bytea strings before passing them as text-mode parameters in
>> PQexecParam?
>
> Seems a lot easier and more efficient to just pass out-of-line bytea
> parameters as binary mode.

Hm, the cause of the problem with using PQescapeBytea with
standard_comforming_strings as a cheap substitute for an actual
PQescapeByteaParam is that it currently escapes ' as '' regardless of the
setting of standard_conforming_string.

else if (*vp == '\'')
{
*rp++ = '\'';
*rp++ = '\'';
}

Shouldn't it escape ' as \' and not '' if standard_conforming_strings is
false?

What I would actually suggest is that it just escape ' and \ the same way it
does binary characters by inserting the bytea escapes \047 and \134. That
actually simplifies the code quite a bit and avoids a lot of special cases for
standard_conforming_strings.

Index: fe-exec.c
===================================================================
RCS file: /home/stark/src/REPOSITORY/pgsql/src/interfaces/libpq/fe-exec.c,v
retrieving revision 1.192
diff -u -r1.192 fe-exec.c
--- fe-exec.c 5 Jan 2007 22:20:01 -0000 1.192
+++ fe-exec.c 11 Jul 2007 15:34:25 -0000
@@ -2755,28 +2755,13 @@
vp = from;
for (i = from_length; i > 0; i--, vp++)
{
- if (*vp < 0x20 || *vp > 0x7e)
+ if (*vp < 0x20 || *vp > 0x7e || *vp == '\'' || *vp == '\\')
{
if (!std_strings)
*rp++ = '\\';
(void) sprintf((char *) rp, "\\%03o", *vp);
rp += 4;
}
- else if (*vp == '\'')
- {
- *rp++ = '\'';
- *rp++ = '\'';
- }
- else if (*vp == '\\')
- {
- if (!std_strings)
- {
- *rp++ = '\\';
- *rp++ = '\\';
- }
- *rp++ = '\\';
- *rp++ = '\\';
- }
else
*rp++ = *vp;
}

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Stefan Kaltenbrunner 2007-07-11 15:42:00 minor compiler warning on OpenBSD
Previous Message Andrew Sullivan 2007-07-11 15:28:16 Re: 2PC-induced lockup