mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-27 03:13:43 +03:00
Set an exception in case of PQescapeIdentifier error
Ifdeffed surface reduced.
This commit is contained in:
parent
4003b7c977
commit
54e5349f53
|
@ -1015,6 +1015,34 @@ exit:
|
||||||
#define psyco_curs_callproc_doc \
|
#define psyco_curs_callproc_doc \
|
||||||
"callproc(procname, parameters=None) -- Execute stored procedure."
|
"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 *
|
static PyObject *
|
||||||
psyco_curs_callproc(cursorObject *self, PyObject *args)
|
psyco_curs_callproc(cursorObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
@ -1022,17 +1050,15 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
|
||||||
char *sql = NULL;
|
char *sql = NULL;
|
||||||
Py_ssize_t procname_len, i, nparameters = 0, sl = 0;
|
Py_ssize_t procname_len, i, nparameters = 0, sl = 0;
|
||||||
PyObject *parameters = Py_None;
|
PyObject *parameters = Py_None;
|
||||||
PyObject *pvals = NULL;
|
|
||||||
PyObject *operation = NULL;
|
PyObject *operation = NULL;
|
||||||
PyObject *res = NULL;
|
PyObject *res = NULL;
|
||||||
|
|
||||||
int using_dict;
|
int using_dict;
|
||||||
#if PG_VERSION_HEX >= 0x090000
|
|
||||||
PyObject *pname = NULL;
|
PyObject *pname = NULL;
|
||||||
PyObject *pnames = NULL;
|
PyObject *pnames = NULL;
|
||||||
|
PyObject *pvals = NULL;
|
||||||
char *cpname = NULL;
|
char *cpname = NULL;
|
||||||
char **scpnames = NULL;
|
char **scpnames = NULL;
|
||||||
#endif
|
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "s#|O", &procname, &procname_len,
|
if (!PyArg_ParseTuple(args, "s#|O", &procname, &procname_len,
|
||||||
¶meters)) {
|
¶meters)) {
|
||||||
|
@ -1057,7 +1083,6 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
|
||||||
|
|
||||||
/* a Dict is complicated; the parameter names go into the query */
|
/* a Dict is complicated; the parameter names go into the query */
|
||||||
if (using_dict) {
|
if (using_dict) {
|
||||||
#if PG_VERSION_NUM >= 90000
|
|
||||||
if (!(pnames = PyDict_Keys(parameters))) { goto exit; }
|
if (!(pnames = PyDict_Keys(parameters))) { goto exit; }
|
||||||
if (!(pvals = PyDict_Values(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 (!(pname = psycopg_ensure_bytes(pname))) { goto exit; }
|
||||||
if (!(cpname = Bytes_AsString(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)))) {
|
self->conn->pgconn, cpname, strlen(cpname)))) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -1103,12 +1128,6 @@ psyco_curs_callproc(cursorObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
sql[sl-2] = ')';
|
sql[sl-2] = ')';
|
||||||
sql[sl-1] = '\0';
|
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 */
|
/* 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:
|
exit:
|
||||||
#if PG_VERSION_HEX >= 0x090000
|
|
||||||
if (scpnames != NULL) {
|
if (scpnames != NULL) {
|
||||||
for (i = 0; i < nparameters; i++) {
|
for (i = 0; i < nparameters; i++) {
|
||||||
if (scpnames[i] != NULL) {
|
if (scpnames[i] != NULL) {
|
||||||
|
@ -1156,7 +1174,6 @@ exit:
|
||||||
PyMem_Del(scpnames);
|
PyMem_Del(scpnames);
|
||||||
Py_XDECREF(pname);
|
Py_XDECREF(pname);
|
||||||
Py_XDECREF(pnames);
|
Py_XDECREF(pnames);
|
||||||
#endif
|
|
||||||
Py_XDECREF(operation);
|
Py_XDECREF(operation);
|
||||||
Py_XDECREF(pvals);
|
Py_XDECREF(pvals);
|
||||||
PyMem_Free((void*)sql);
|
PyMem_Free((void*)sql);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user