1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-13 14:22:43 +03:00

Add PL/Python functions for quoting strings

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
This commit is contained in:
Peter Eisentraut
2011-02-22 23:33:44 +02:00
parent 3e6b305d9e
commit 1c51c7d5ff
8 changed files with 179 additions and 7 deletions

View File

@@ -79,6 +79,7 @@ REGRESS = \
plpython_types \
plpython_error \
plpython_unicode \
plpython_quote \
plpython_drop
# where to find psql for running the tests
PSQLDIR = $(bindir)

View File

@@ -0,0 +1,56 @@
-- test quoting functions
CREATE FUNCTION quote(t text, how text) RETURNS text AS $$
if how == "literal":
return plpy.quote_literal(t)
elif how == "nullable":
return plpy.quote_nullable(t)
elif how == "ident":
return plpy.quote_ident(t)
else:
raise plpy.Error("unrecognized quote type %s" % how)
$$ LANGUAGE plpythonu;
SELECT quote(t, 'literal') FROM (VALUES
('abc'),
('a''bc'),
('''abc'''),
(''),
(''''),
('xyzv')) AS v(t);
quote
-----------
'abc'
'a''bc'
'''abc'''
''
''''
'xyzv'
(6 rows)
SELECT quote(t, 'nullable') FROM (VALUES
('abc'),
('a''bc'),
('''abc'''),
(''),
(''''),
(NULL)) AS v(t);
quote
-----------
'abc'
'a''bc'
'''abc'''
''
''''
NULL
(6 rows)
SELECT quote(t, 'ident') FROM (VALUES
('abc'),
('a b c'),
('a " ''abc''')) AS v(t);
quote
--------------
abc
"a b c"
"a "" 'abc'"
(3 rows)

View File

@@ -43,9 +43,9 @@ contents.sort()
return ", ".join(contents)
$$ LANGUAGE plpythonu;
select module_contents();
module_contents
-------------------------------------------------------------------------------------------
Error, Fatal, SPIError, debug, error, execute, fatal, info, log, notice, prepare, warning
module_contents
---------------------------------------------------------------------------------------------------------------------------------------
Error, Fatal, SPIError, debug, error, execute, fatal, info, log, notice, prepare, quote_ident, quote_literal, quote_nullable, warning
(1 row)
CREATE FUNCTION elog_test() RETURNS void

View File

@@ -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

View File

@@ -0,0 +1,33 @@
-- test quoting functions
CREATE FUNCTION quote(t text, how text) RETURNS text AS $$
if how == "literal":
return plpy.quote_literal(t)
elif how == "nullable":
return plpy.quote_nullable(t)
elif how == "ident":
return plpy.quote_ident(t)
else:
raise plpy.Error("unrecognized quote type %s" % how)
$$ LANGUAGE plpythonu;
SELECT quote(t, 'literal') FROM (VALUES
('abc'),
('a''bc'),
('''abc'''),
(''),
(''''),
('xyzv')) AS v(t);
SELECT quote(t, 'nullable') FROM (VALUES
('abc'),
('a''bc'),
('''abc'''),
(''),
(''''),
(NULL)) AS v(t);
SELECT quote(t, 'ident') FROM (VALUES
('abc'),
('a b c'),
('a " ''abc''')) AS v(t);