Fixed selection of the proper binary string quoting.

This commit is contained in:
Daniele Varrazzo 2007-11-09 13:00:37 +00:00
parent 272140f5c1
commit 70e555585e
2 changed files with 24 additions and 23 deletions

View File

@ -1,10 +1,7 @@
2007-11-09 Daniele Varrazzo <daniele.varrazzo@gmail.com> 2007-11-09 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* Use escape string syntax for binary escape if connected with a * Use escape string syntax for binary escape if connected to a
server with ver >= 8.2. server requiring it.
The feature is only enabled when compiling psycopg with libpq
ver >= 8.0.
* Put a limit on the number of notices stored in the connection. * Put a limit on the number of notices stored in the connection.

View File

@ -139,15 +139,35 @@ binary_quote(binaryObject *self)
size_t len = 0; size_t len = 0;
PGconn *pgconn = NULL; PGconn *pgconn = NULL;
const char *quotes = "'%s'"; const char *quotes = "'%s'";
const char *scs;
/* if we got a plain string or a buffer we escape it and save the buffer */ /* if we got a plain string or a buffer we escape it and save the buffer */
if (PyString_Check(self->wrapped) || PyBuffer_Check(self->wrapped)) { if (PyString_Check(self->wrapped) || PyBuffer_Check(self->wrapped)) {
/* escape and build quoted buffer */ /* escape and build quoted buffer */
PyObject_AsCharBuffer(self->wrapped, &buffer, &buffer_len); PyObject_AsCharBuffer(self->wrapped, &buffer, &buffer_len);
if (self->conn) if (self->conn) {
pgconn = ((connectionObject*)self->conn)->pgconn; pgconn = ((connectionObject*)self->conn)->pgconn;
/*
* The presence of the 'standard_conforming_strings' parameters
* means that the server _accepts_ the E'' quote.
*
* If the paramer is off, the PQescapeByteaConn returns
* backslash escaped strings (e.g. '\001' -> "\\001"),
* so the E'' quotes are required to avoid warnings
* if 'escape_string_warning' is set.
*
* If the parameter is on, the PQescapeByteaConn returns
* not escaped strings (e.g. '\001' -> "\001"), relying on the
* fact that the '\' will pass untouched the string parser.
* In this case the E'' quotes are NOT to be used.
*/
scs = PQparameterStatus(pgconn, "standard_conforming_strings");
if (scs && (0 == strcmp("off", scs)))
quotes = "E'%s'";
}
to = (char *)binary_escape((unsigned char*)buffer, (size_t) buffer_len, to = (char *)binary_escape((unsigned char*)buffer, (size_t) buffer_len,
&len, pgconn); &len, pgconn);
if (to == NULL) { if (to == NULL) {
@ -155,24 +175,8 @@ binary_quote(binaryObject *self)
return NULL; return NULL;
} }
if (len > 0) { if (len > 0)
/*
* Backslashes in non-escape (non-E'') strings raise warnings
* by default in PostgreSQL >= 8.2
*
* http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html
* #SQL-SYNTAX-STRINGS
*
* Before PG 8.0 there was no PQserverVersion:
* PQparameterStatus(pgconn, "server_version") can be used,
* but it's harder to compare with.
*/
#if PG_MAJOR_VERSION >= 8
if (pgconn && (PQserverVersion(pgconn) >= 80200))
quotes = "E'%s'";
#endif
self->buffer = PyString_FromFormat(quotes, to); self->buffer = PyString_FromFormat(quotes, to);
}
else else
self->buffer = PyString_FromString("''"); self->buffer = PyString_FromString("''");