Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 330ed4a

Browse files
committed
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
1 parent d2d153f commit 330ed4a

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

doc/src/sgml/plpython.sgml

+11
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,17 @@ foo = rv[i]["my_column"]
956956
</para>
957957
</listitem>
958958
</varlistentry>
959+
960+
<varlistentry>
961+
<term><literal><function>__str__</function>()</literal></term>
962+
<listitem>
963+
<para>
964+
The standard <literal>__str__</literal> method is defined so that it
965+
is possible for example to debug query execution results
966+
using <literal>plpy.debug(rv)</literal>.
967+
</para>
968+
</listitem>
969+
</varlistentry>
959970
</variablelist>
960971
</para>
961972

src/pl/plpython/expected/plpython_spi.out

+18
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ CONTEXT: PL/Python function "result_empty_test"
263263

264264
(1 row)
265265

266+
CREATE FUNCTION result_str_test(cmd text) RETURNS text
267+
AS $$
268+
plan = plpy.prepare(cmd)
269+
result = plpy.execute(plan)
270+
return str(result)
271+
$$ LANGUAGE plpythonu;
272+
SELECT result_str_test($$SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'$$);
273+
result_str_test
274+
--------------------------------------------------------------------------------------
275+
<PLyResult status=5 nrows=2 rows=[{'foo': 1, 'bar': '11'}, {'foo': 2, 'bar': '22'}]>
276+
(1 row)
277+
278+
SELECT result_str_test($$CREATE TEMPORARY TABLE foo1 (a int, b text)$$);
279+
result_str_test
280+
--------------------------------------
281+
<PLyResult status=4 nrows=0 rows=[]>
282+
(1 row)
283+
266284
-- cursor objects
267285
CREATE FUNCTION simple_cursor_test() RETURNS int AS $$
268286
res = plpy.cursor("select fname, lname from users")

src/pl/plpython/plpy_resultobject.c

+22-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static Py_ssize_t PLy_result_length(PyObject *arg);
2222
static PyObject *PLy_result_item(PyObject *arg, Py_ssize_t idx);
2323
static PyObject *PLy_result_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx);
2424
static int PLy_result_ass_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *slice);
25+
static PyObject *PLy_result_str(PyObject *arg);
2526
static PyObject *PLy_result_subscript(PyObject *arg, PyObject *item);
2627
static int PLy_result_ass_subscript(PyObject *self, PyObject *item, PyObject *value);
2728

@@ -74,7 +75,7 @@ static PyTypeObject PLy_ResultType = {
7475
&PLy_result_as_mapping, /* tp_as_mapping */
7576
0, /* tp_hash */
7677
0, /* tp_call */
77-
0, /* tp_str */
78+
&PLy_result_str, /* tp_str */
7879
0, /* tp_getattro */
7980
0, /* tp_setattro */
8081
0, /* tp_as_buffer */
@@ -248,6 +249,26 @@ PLy_result_ass_slice(PyObject *arg, Py_ssize_t lidx, Py_ssize_t hidx, PyObject *
248249
return rv;
249250
}
250251

252+
static PyObject *
253+
PLy_result_str(PyObject *arg)
254+
{
255+
PLyResultObject *ob = (PLyResultObject *) arg;
256+
257+
#if PY_MAJOR_VERSION >= 3
258+
return PyUnicode_FromFormat("<%s status=%S nrows=%S rows=%S>",
259+
Py_TYPE(ob)->tp_name,
260+
ob->status,
261+
ob->nrows,
262+
ob->rows);
263+
#else
264+
return PyString_FromFormat("<%s status=%ld nrows=%ld rows=%s>",
265+
ob->ob_type->tp_name,
266+
PyInt_AsLong(ob->status),
267+
PyInt_AsLong(ob->nrows),
268+
PyString_AsString(PyObject_Str(ob->rows)));
269+
#endif
270+
}
271+
251272
static PyObject *
252273
PLy_result_subscript(PyObject *arg, PyObject *item)
253274
{

src/pl/plpython/sql/plpython_spi.sql

+10
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ $$ LANGUAGE plpythonu;
169169

170170
SELECT result_empty_test();
171171

172+
CREATE FUNCTION result_str_test(cmd text) RETURNS text
173+
AS $$
174+
plan = plpy.prepare(cmd)
175+
result = plpy.execute(plan)
176+
return str(result)
177+
$$ LANGUAGE plpythonu;
178+
179+
SELECT result_str_test($$SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'$$);
180+
SELECT result_str_test($$CREATE TEMPORARY TABLE foo1 (a int, b text)$$);
181+
172182
-- cursor objects
173183

174184
CREATE FUNCTION simple_cursor_test() RETURNS int AS $$

0 commit comments

Comments
 (0)