diff --git a/ChangeLog b/ChangeLog index 17796666..d445b936 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,10 +1,7 @@ 2007-11-09 Daniele Varrazzo - * Use escape string syntax for binary escape if connected with a - server with ver >= 8.2. - - The feature is only enabled when compiling psycopg with libpq - ver >= 8.0. + * Use escape string syntax for binary escape if connected to a + server requiring it. * Put a limit on the number of notices stored in the connection. diff --git a/psycopg/adapter_binary.c b/psycopg/adapter_binary.c index de41a839..eaea99ad 100644 --- a/psycopg/adapter_binary.c +++ b/psycopg/adapter_binary.c @@ -139,15 +139,35 @@ binary_quote(binaryObject *self) size_t len = 0; PGconn *pgconn = NULL; const char *quotes = "'%s'"; + const char *scs; /* 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)) { /* escape and build quoted buffer */ PyObject_AsCharBuffer(self->wrapped, &buffer, &buffer_len); - if (self->conn) + if (self->conn) { 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, &len, pgconn); if (to == NULL) { @@ -155,24 +175,8 @@ binary_quote(binaryObject *self) return NULL; } - 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 + if (len > 0) self->buffer = PyString_FromFormat(quotes, to); - } else self->buffer = PyString_FromString("''");