summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2014-06-05 13:17:51 +0200
committermrmilosz <milosz@milosz.ca>2015-12-12 17:52:56 -0500
commit54e5349f531df58b08243f74c03ce13ab8708263 (patch)
tree364f46d91ab2f07d6bbbbf356ae9071356424065
parent4003b7c977fba9897da21446707671157a67e6ad (diff)
downloadpsycopg2-54e5349f531df58b08243f74c03ce13ab8708263.tar.gz
Set an exception in case of PQescapeIdentifier error
Ifdeffed surface reduced.
-rw-r--r--psycopg/cursor_type.c43
1 files changed, 30 insertions, 13 deletions
diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c
index d10bfb8..fd8fbbe 100644
--- a/psycopg/cursor_type.c
+++ b/psycopg/cursor_type.c
@@ -1015,6 +1015,34 @@ exit:
#define psyco_curs_callproc_doc \
"callproc(procname, parameters=None) -- Execute stored procedure."
+/* Call PQescapeIdentifier.
+ *
+ * In case of error set a Python exception.
+ *
+ * TODO: this function can become more generic and go into utils
+ */
+static char *
+_escape_identifier(PGconn *pgconn, const char *str, size_t length)
+{
+ char *rv = NULL;
+
+#if PG_VERSION_NUM >= 90000
+ rv = PQescapeIdentifier(pgconn, str, length);
+ if (!rv) {
+ char *msg;
+ if (!(msg = PQerrorMessage(pgconn))) {
+ msg = "no message provided";
+ }
+ PyErr_Format(InterfaceError, "failed to escape identifier: %s", msg);
+ }
+#else
+ PyErr_Format(PyExc_NotImplementedError,
+ "named parameters require psycopg2 compiled against libpq 9.0+");
+#endif
+
+ return rv;
+}
+
static PyObject *
psyco_curs_callproc(cursorObject *self, PyObject *args)
{
@@ -1022,17 +1050,15 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
char *sql = NULL;
Py_ssize_t procname_len, i, nparameters = 0, sl = 0;
PyObject *parameters = Py_None;
- PyObject *pvals = NULL;
PyObject *operation = NULL;
PyObject *res = NULL;
int using_dict;
-#if PG_VERSION_HEX >= 0x090000
PyObject *pname = NULL;
PyObject *pnames = NULL;
+ PyObject *pvals = NULL;
char *cpname = NULL;
char **scpnames = NULL;
-#endif
if (!PyArg_ParseTuple(args, "s#|O", &procname, &procname_len,
&parameters)) {
@@ -1057,7 +1083,6 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
/* a Dict is complicated; the parameter names go into the query */
if (using_dict) {
-#if PG_VERSION_NUM >= 90000
if (!(pnames = PyDict_Keys(parameters))) { goto exit; }
if (!(pvals = PyDict_Values(parameters))) { goto exit; }
@@ -1081,7 +1106,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
if (!(pname = psycopg_ensure_bytes(pname))) { goto exit; }
if (!(cpname = Bytes_AsString(pname))) { goto exit; }
- if (!(scpnames[i] = PQescapeIdentifier(
+ if (!(scpnames[i] = _escape_identifier(
self->conn->pgconn, cpname, strlen(cpname)))) {
goto exit;
}
@@ -1103,12 +1128,6 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
}
sql[sl-2] = ')';
sql[sl-1] = '\0';
-
-#else
- PyErr_SetString(PyExc_NotImplementedError,
- "named parameters require psycopg2 compiled against libpq 9.0+");
- goto exit;
-#endif
}
/* a list (or None, or empty data structure) is a little bit simpler */
@@ -1145,7 +1164,6 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
}
exit:
-#if PG_VERSION_HEX >= 0x090000
if (scpnames != NULL) {
for (i = 0; i < nparameters; i++) {
if (scpnames[i] != NULL) {
@@ -1156,7 +1174,6 @@ exit:
PyMem_Del(scpnames);
Py_XDECREF(pname);
Py_XDECREF(pnames);
-#endif
Py_XDECREF(operation);
Py_XDECREF(pvals);
PyMem_Free((void*)sql);