Return a list of column names, list of column type OIDs, and list of
type-specific type modifiers for the columns, respectively.
</para>
+
+ <para>
+ These methods raise an exception when called on a result object from
+ a command that did not produce a result set, e.g.,
+ <command>UPDATE</command> without <literal>RETURNING</literal>, or
+ <command>DROP TABLE</command>. But it is OK to use these methods on
+ a result set containing zero rows.
+ </para>
</listitem>
</varlistentry>
</variablelist>
--
-- plan and result objects
--
-CREATE FUNCTION result_nrows_test() RETURNS int
+CREATE FUNCTION result_metadata_test(cmd text) RETURNS int
AS $$
-plan = plpy.prepare("SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'")
+plan = plpy.prepare(cmd)
plpy.info(plan.status()) # not really documented or useful
result = plpy.execute(plan)
if result.status() > 0:
else:
return None
$$ LANGUAGE plpythonu;
-SELECT result_nrows_test();
+SELECT result_metadata_test($$SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'$$);
INFO: True
-CONTEXT: PL/Python function "result_nrows_test"
+CONTEXT: PL/Python function "result_metadata_test"
INFO: ['foo', 'bar']
-CONTEXT: PL/Python function "result_nrows_test"
+CONTEXT: PL/Python function "result_metadata_test"
INFO: [23, 25]
-CONTEXT: PL/Python function "result_nrows_test"
+CONTEXT: PL/Python function "result_metadata_test"
INFO: [-1, -1]
-CONTEXT: PL/Python function "result_nrows_test"
- result_nrows_test
--------------------
- 2
+CONTEXT: PL/Python function "result_metadata_test"
+ result_metadata_test
+----------------------
+ 2
(1 row)
+SELECT result_metadata_test($$CREATE TEMPORARY TABLE foo1 (a int, b text)$$);
+INFO: True
+CONTEXT: PL/Python function "result_metadata_test"
+ERROR: plpy.Error: command did not produce a result set
+CONTEXT: Traceback (most recent call last):
+ PL/Python function "result_metadata_test", line 6, in <module>
+ plpy.info(result.colnames())
+PL/Python function "result_metadata_test"
-- cursor objects
CREATE FUNCTION simple_cursor_test() RETURNS int AS $$
res = plpy.cursor("select fname, lname from users")
#include "plpython.h"
#include "plpy_resultobject.h"
+#include "plpy_elog.h"
static void PLy_result_dealloc(PyObject *arg);
PyObject *list;
int i;
+ if (!ob->tupdesc)
+ {
+ PLy_exception_set(PLy_exc_error, "command did not produce a result set");
+ return NULL;
+ }
+
list = PyList_New(ob->tupdesc->natts);
for (i = 0; i < ob->tupdesc->natts; i++)
PyList_SET_ITEM(list, i, PyString_FromString(NameStr(ob->tupdesc->attrs[i]->attname)));
PyObject *list;
int i;
+ if (!ob->tupdesc)
+ {
+ PLy_exception_set(PLy_exc_error, "command did not produce a result set");
+ return NULL;
+ }
+
list = PyList_New(ob->tupdesc->natts);
for (i = 0; i < ob->tupdesc->natts; i++)
PyList_SET_ITEM(list, i, PyInt_FromLong(ob->tupdesc->attrs[i]->atttypid));
PyObject *list;
int i;
+ if (!ob->tupdesc)
+ {
+ PLy_exception_set(PLy_exc_error, "command did not produce a result set");
+ return NULL;
+ }
+
list = PyList_New(ob->tupdesc->natts);
for (i = 0; i < ob->tupdesc->natts; i++)
PyList_SET_ITEM(list, i, PyInt_FromLong(ob->tupdesc->attrs[i]->atttypmod));
-- plan and result objects
--
-CREATE FUNCTION result_nrows_test() RETURNS int
+CREATE FUNCTION result_metadata_test(cmd text) RETURNS int
AS $$
-plan = plpy.prepare("SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'")
+plan = plpy.prepare(cmd)
plpy.info(plan.status()) # not really documented or useful
result = plpy.execute(plan)
if result.status() > 0:
return None
$$ LANGUAGE plpythonu;
-SELECT result_nrows_test();
+SELECT result_metadata_test($$SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'$$);
+SELECT result_metadata_test($$CREATE TEMPORARY TABLE foo1 (a int, b text)$$);
-- cursor objects