mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-01-24 18:55:04 +08:00
PL/Python: Add result object str handler
This is intended so that say plpy.debug(rv) prints something useful for debugging query execution results. reviewed by Steve Singer
This commit is contained in:
parent
d2d153fdb0
commit
330ed4ac6c
@ -956,6 +956,17 @@ foo = rv[i]["my_column"]
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term><literal><function>__str__</function>()</literal></term>
|
||||
<listitem>
|
||||
<para>
|
||||
The standard <literal>__str__</literal> method is defined so that it
|
||||
is possible for example to debug query execution results
|
||||
using <literal>plpy.debug(rv)</literal>.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
</para>
|
||||
|
||||
|
@ -263,6 +263,24 @@ CONTEXT: PL/Python function "result_empty_test"
|
||||
|
||||
(1 row)
|
||||
|
||||
CREATE FUNCTION result_str_test(cmd text) RETURNS text
|
||||
AS $$
|
||||
plan = plpy.prepare(cmd)
|
||||
result = plpy.execute(plan)
|
||||
return str(result)
|
||||
$$ LANGUAGE plpythonu;
|
||||
SELECT result_str_test($$SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'$$);
|
||||
result_str_test
|
||||
--------------------------------------------------------------------------------------
|
||||
<PLyResult status=5 nrows=2 rows=[{'foo': 1, 'bar': '11'}, {'foo': 2, 'bar': '22'}]>
|
||||
(1 row)
|
||||
|
||||
SELECT result_str_test($$CREATE TEMPORARY TABLE foo1 (a int, b text)$$);
|
||||
result_str_test
|
||||
--------------------------------------
|
||||
<PLyResult status=4 nrows=0 rows=[]>
|
||||
(1 row)
|
||||
|
||||
-- cursor objects
|
||||
CREATE FUNCTION simple_cursor_test() RETURNS int AS $$
|
||||
res = plpy.cursor("select fname, lname from users")
|
||||
|
@ -22,6 +22,7 @@ static Py_ssize_t PLy_result_length(PyObject *arg);
|
||||
static PyObject *PLy_result_item(PyObject *arg, Py_ssize_t idx);
|
||||
static PyObject *PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx);
|
||||
static int PLy_result_ass_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *slice);
|
||||
static PyObject *PLy_result_str(PyObject *arg);
|
||||
static PyObject *PLy_result_subscript(PyObject *arg, PyObject *item);
|
||||
static int PLy_result_ass_subscript(PyObject *self, PyObject *item, PyObject *value);
|
||||
|
||||
@ -74,7 +75,7 @@ static PyTypeObject PLy_ResultType = {
|
||||
&PLy_result_as_mapping, /* tp_as_mapping */
|
||||
0, /* tp_hash */
|
||||
0, /* tp_call */
|
||||
0, /* tp_str */
|
||||
&PLy_result_str, /* tp_str */
|
||||
0, /* tp_getattro */
|
||||
0, /* tp_setattro */
|
||||
0, /* tp_as_buffer */
|
||||
@ -248,6 +249,26 @@ PLy_result_ass_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *
|
||||
return rv;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
PLy_result_str(PyObject *arg)
|
||||
{
|
||||
PLyResultObject *ob = (PLyResultObject *) arg;
|
||||
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>",
|
||||
Py_TYPE(ob)->tp_name,
|
||||
ob->status,
|
||||
ob->nrows,
|
||||
ob->rows);
|
||||
#else
|
||||
return PyString_FromFormat("<%s status=%ld nrows=%ld rows=%s>",
|
||||
ob->ob_type->tp_name,
|
||||
PyInt_AsLong(ob->status),
|
||||
PyInt_AsLong(ob->nrows),
|
||||
PyString_AsString(PyObject_Str(ob->rows)));
|
||||
#endif
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
PLy_result_subscript(PyObject *arg, PyObject *item)
|
||||
{
|
||||
|
@ -169,6 +169,16 @@ $$ LANGUAGE plpythonu;
|
||||
|
||||
SELECT result_empty_test();
|
||||
|
||||
CREATE FUNCTION result_str_test(cmd text) RETURNS text
|
||||
AS $$
|
||||
plan = plpy.prepare(cmd)
|
||||
result = plpy.execute(plan)
|
||||
return str(result)
|
||||
$$ LANGUAGE plpythonu;
|
||||
|
||||
SELECT result_str_test($$SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'$$);
|
||||
SELECT result_str_test($$CREATE TEMPORARY TABLE foo1 (a int, b text)$$);
|
||||
|
||||
-- cursor objects
|
||||
|
||||
CREATE FUNCTION simple_cursor_test() RETURNS int AS $$
|
||||
|
Loading…
Reference in New Issue
Block a user