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.
This commit is contained in:
Daniele Varrazzo 2007-11-09 05:51:12 +00:00
parent 7d408b47d7
commit 272140f5c1
2 changed files with 33 additions and 5 deletions

View File

@ -1,5 +1,11 @@
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
server with ver >= 8.2.
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.
2007-10-21 Daniele Varrazzo <daniele.varrazzo@gmail.com> 2007-10-21 Daniele Varrazzo <daniele.varrazzo@gmail.com>

View File

@ -137,23 +137,45 @@ binary_quote(binaryObject *self)
const char *buffer; const char *buffer;
Py_ssize_t buffer_len; Py_ssize_t buffer_len;
size_t len = 0; size_t len = 0;
PGconn *pgconn = NULL;
const char *quotes = "'%s'";
/* 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)
pgconn = ((connectionObject*)self->conn)->pgconn;
to = (char *)binary_escape((unsigned char*)buffer, (size_t) buffer_len, to = (char *)binary_escape((unsigned char*)buffer, (size_t) buffer_len,
&len, self->conn ? ((connectionObject*)self->conn)->pgconn : NULL); &len, pgconn);
if (to == NULL) { if (to == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
return NULL; return NULL;
} }
if (len > 0) if (len > 0) {
self->buffer = PyString_FromFormat("'%s'", to); /*
* 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);
}
else else
self->buffer = PyString_FromString("''"); self->buffer = PyString_FromString("''");
PQfreemem(to); PQfreemem(to);
} }