From 3948e909e46bab420d0c40bda21a32bfa501bd19 Mon Sep 17 00:00:00 2001 From: mrmilosz Date: Thu, 29 May 2014 04:31:43 -0400 Subject: [PATCH] callproc: now more compliant with local coding standards. --- psycopg/cursor_type.c | 192 ++++++++++++++++++++++-------------------- 1 file changed, 102 insertions(+), 90 deletions(-) diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index 5f70ce61..d6875a79 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -1024,17 +1024,18 @@ psyco_curs_callproc(cursorObject *self, PyObject *args) PyObject *parameters = Py_None; PyObject *operation = NULL; PyObject *res = NULL; - PyObject *parameter_name = NULL; - PyObject *parameter_name_bytes = NULL; - char *parameter_name_cstr = NULL; - char *parameter_name_cstr_sanitized = NULL; - char **parameter_name_cstr_sanitized_CACHE = NULL; - PyObject *parameter_names = NULL; - if (!PyArg_ParseTuple(args, "s#|O", - &procname, &procname_len, ¶meters - )) - { goto exit; } + int using_dict; + PyObject *pname = NULL; + PyObject *bpname = NULL; + PyObject *pnames = NULL; + char *cpname = NULL; + char **scpnames = NULL; + + if (!PyArg_ParseTuple(args, "s#|O", &procname, &procname_len, + ¶meters)) { + goto exit; + } EXC_IF_CURS_CLOSED(self); EXC_IF_ASYNC_IN_PROGRESS(self, callproc); @@ -1042,7 +1043,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args) if (self->name != NULL) { psyco_set_error(ProgrammingError, self, - "can't call .callproc() on named cursors"); + "can't call .callproc() on named cursors"); goto exit; } @@ -1050,106 +1051,117 @@ psyco_curs_callproc(cursorObject *self, PyObject *args) if (-1 == (nparameters = PyObject_Length(parameters))) { goto exit; } } - /* allocate some memory, build the SQL and create a PyString from it */ + using_dict = nparameters > 0 && PyDict_Check(parameters); - /* a dict requires special handling: we put the parameter names into the SQL */ - if (nparameters > 0 && PyDict_Check(parameters)) { - - parameter_names = PyDict_Keys(parameters); - - /* first we need to ensure the dict's keys are text */ - for(i=0; iconn->pgconn, parameter_name_cstr, strlen(parameter_name_cstr)); - Py_DECREF(parameter_name); - - /* must add the length of the sanitized string to the length of the SQL string */ - sl += strlen(parameter_name_cstr_sanitized); - - parameter_name_cstr_sanitized_CACHE[i] = parameter_name_cstr_sanitized; - } - - Py_DECREF(parameter_names); - - sql = (char*)PyMem_Malloc(sl); - if (sql == NULL) { - PyErr_NoMemory(); - for(i=0; iconn->pgconn, cpname, + strlen(cpname)))) { + PyErr_SetString(PyExc_RuntimeError, + "libpq failed to escape identifier!"); + goto exit; + } + + sl += strlen(scpnames[i]); + } + + sql = (char*)PyMem_Malloc(sl); + if (sql == NULL) { + PyErr_NoMemory(); + goto exit; + } + + sprintf(sql, "SELECT * FROM %s(", procname); + for (i = 0; i < nparameters; i++) { + strcat(sql, scpnames[i]); + strcat(sql, ":=%s,"); + } + sql[sl-2] = ')'; + sql[sl-1] = '\0'; + + if (!(parameters = PyDict_Values(parameters))) { + PyErr_SetString(PyExc_RuntimeError, + "built-in 'values' failed on a Dict!"); + goto exit; + } } - /* a list (or None) is a little bit simpler */ + /* a list (or None, or empty data structure) is a little bit simpler */ else { - sl = procname_len + 17 + nparameters*3 - (nparameters ? 1 : 0); + sl = procname_len + 17 + nparameters * 3 - (nparameters ? 1 : 0); - sql = (char*)PyMem_Malloc(sl); - if (sql == NULL) { - PyErr_NoMemory(); - goto exit; - } + sql = (char*)PyMem_Malloc(sl); + if (sql == NULL) { + PyErr_NoMemory(); + goto exit; + } - sprintf(sql, "SELECT * FROM %s(", procname); - for(i=0; iconn->async, 0)) { - Py_INCREF(parameters); + self->conn->async, 0)) { + /* In the dict case, the parameters are already a new reference */ + if (!using_dict) { + Py_INCREF(parameters); + } res = parameters; } exit: + if (scpnames != NULL) { + for (i = 0; i < nparameters; i++) { + if (scpnames[i] != NULL) { + PQfreemem(scpnames[i]); + } + } + } + PyMem_Del(scpnames); + Py_XDECREF(pnames); Py_XDECREF(operation); PyMem_Free((void*)sql); return res;