summaryrefslogtreecommitdiff
path: root/psycopg/adapter_qstring.c
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2007-11-11 08:53:44 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2007-11-11 08:53:44 +0000
commit75cb5d75d78b985268a7abba89313ef174583bd1 (patch)
tree005743e41ab1e9e5d8ec2b309618a786c07a6c87 /psycopg/adapter_qstring.c
parenta6ea092acc13da27856fc796b00db6d450c6122b (diff)
downloadpsycopg2-75cb5d75d78b985268a7abba89313ef174583bd1.tar.gz
Use escape string syntax for string escape if connected to a server
requiring it. Added a connection flag to store whether E''-style quoting is required: this avoids repeated PQparameterStatus() calls. Added a test case to verify correct behavior on strings, unicode and binary data. Tested with PG versions from 7.4 to 8.3b2, with any server 'standard_conforming_strings' setting and with 'PSYCOPG_OWN_QUOTING' too.
Diffstat (limited to 'psycopg/adapter_qstring.c')
-rw-r--r--psycopg/adapter_qstring.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/psycopg/adapter_qstring.c b/psycopg/adapter_qstring.c
index ac2c9aa..ae95cb6 100644
--- a/psycopg/adapter_qstring.c
+++ b/psycopg/adapter_qstring.c
@@ -94,6 +94,7 @@ qstring_quote(qstringObject *self)
PyObject *str;
char *s, *buffer;
Py_ssize_t len;
+ int equote; /* buffer offset if E'' quotes are needed */
/* if the wrapped object is an unicode object we can encode it to match
self->encoding but if the encoding is not specified we don't know what
@@ -141,20 +142,22 @@ qstring_quote(qstringObject *self)
/* encode the string into buffer */
PyString_AsStringAndSize(str, &s, &len);
- buffer = (char *)PyMem_Malloc((len*2+3) * sizeof(char));
+ buffer = (char *)PyMem_Malloc((len*2+4) * sizeof(char));
if (buffer == NULL) {
Py_DECREF(str);
PyErr_NoMemory();
return NULL;
}
+ equote = (self->conn && ((connectionObject*)self->conn)->equote) ? 1 : 0;
+
{ /* Call qstring_escape with the GIL released, then reacquire the GIL
* before verifying that the results can fit into a Python string; raise
* an exception if not. */
size_t qstring_res;
Py_BEGIN_ALLOW_THREADS
- qstring_res = qstring_escape(buffer+1, s, len,
+ qstring_res = qstring_escape(buffer+equote+1, s, len,
self->conn ? ((connectionObject*)self->conn)->pgconn : NULL);
Py_END_ALLOW_THREADS
@@ -166,10 +169,12 @@ qstring_quote(qstringObject *self)
return NULL;
}
len = (Py_ssize_t) qstring_res;
- buffer[0] = '\'' ; buffer[len+1] = '\'';
+ if (equote)
+ buffer[0] = 'E';
+ buffer[equote] = '\'' ; buffer[len+equote+1] = '\'';
}
- self->buffer = PyString_FromStringAndSize(buffer, len+2);
+ self->buffer = PyString_FromStringAndSize(buffer, len+equote+2);
PyMem_Free(buffer);
Py_DECREF(str);