From 1c51c7d5ffd407426f314b2cd317ef77f14efb1f Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 22 Feb 2011 23:33:44 +0200 Subject: Add PL/Python functions for quoting strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add functions plpy.quote_ident, plpy.quote_literal, plpy.quote_nullable, which wrap the equivalent SQL functions. To be able to propagate char * constness properly, make the argument of quote_literal_cstr() const char *. This also makes it more consistent with quote_identifier(). Jan UrbaƄski, reviewed by Hitoshi Harada, some refinements by Peter Eisentraut --- src/pl/plpython/plpython.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'src/pl/plpython/plpython.c') diff --git a/src/pl/plpython/plpython.c b/src/pl/plpython/plpython.c index 4e54d3e8b0e..3013fc8a790 100644 --- a/src/pl/plpython/plpython.c +++ b/src/pl/plpython/plpython.c @@ -2637,6 +2637,10 @@ static PyObject *PLy_spi_execute_query(char *query, long limit); static PyObject *PLy_spi_execute_plan(PyObject *, PyObject *, long); static PyObject *PLy_spi_execute_fetch_result(SPITupleTable *, int, int); +static PyObject *PLy_quote_literal(PyObject *self, PyObject *args); +static PyObject *PLy_quote_nullable(PyObject *self, PyObject *args); +static PyObject *PLy_quote_ident(PyObject *self, PyObject *args); + static PyMethodDef PLy_plan_methods[] = { {"status", PLy_plan_status, METH_VARARGS, NULL}, @@ -2751,6 +2755,13 @@ static PyMethodDef PLy_methods[] = { */ {"execute", PLy_spi_execute, METH_VARARGS, NULL}, + /* + * escaping strings + */ + {"quote_literal", PLy_quote_literal, METH_VARARGS, NULL}, + {"quote_nullable", PLy_quote_nullable, METH_VARARGS, NULL}, + {"quote_ident", PLy_quote_ident, METH_VARARGS, NULL}, + {NULL, NULL, 0, NULL} }; @@ -3688,6 +3699,60 @@ PLy_output(volatile int level, PyObject *self, PyObject *args) } +static PyObject * +PLy_quote_literal(PyObject *self, PyObject *args) +{ + const char *str; + char *quoted; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "s", &str)) + return NULL; + + quoted = quote_literal_cstr(str); + ret = PyString_FromString(quoted); + pfree(quoted); + + return ret; +} + +static PyObject * +PLy_quote_nullable(PyObject *self, PyObject *args) +{ + const char *str; + char *quoted; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "z", &str)) + return NULL; + + if (str == NULL) + return PyString_FromString("NULL"); + + quoted = quote_literal_cstr(str); + ret = PyString_FromString(quoted); + pfree(quoted); + + return ret; +} + +static PyObject * +PLy_quote_ident(PyObject *self, PyObject *args) +{ + const char *str; + const char *quoted; + PyObject *ret; + + if (!PyArg_ParseTuple(args, "s", &str)) + return NULL; + + quoted = quote_identifier(str); + ret = PyString_FromString(quoted); + + return ret; +} + + /* * Get the name of the last procedure called by the backend (the * innermost, if a plpython procedure call calls the backend and the -- cgit v1.2.3