PL/Python: Add result metadata functions

Add result object functions .colnames, .coltypes, .coltypmods to
obtain information about the result column names and types, which was
previously not possible in the PL/Python SPI interface.

reviewed by Abhijit Menon-Sen
This commit is contained in:
Peter Eisentraut
2012-01-30 21:38:52 +02:00
parent c6ea8ccea6
commit ee7fa66b19
6 changed files with 80 additions and 5 deletions

View File

@ -117,16 +117,25 @@ SELECT join_sequences(sequences) FROM sequences
--
CREATE FUNCTION result_nrows_test() RETURNS int
AS $$
plan = plpy.prepare("SELECT 1 UNION SELECT 2")
plan = plpy.prepare("SELECT 1 AS foo, '11'::text AS bar UNION SELECT 2, '22'")
plpy.info(plan.status()) # not really documented or useful
result = plpy.execute(plan)
if result.status() > 0:
plpy.info(result.colnames())
plpy.info(result.coltypes())
plpy.info(result.coltypmods())
return result.nrows()
else:
return None
$$ LANGUAGE plpythonu;
SELECT result_nrows_test();
INFO: True
CONTEXT: PL/Python function "result_nrows_test"
INFO: ['foo', 'bar']
CONTEXT: PL/Python function "result_nrows_test"
INFO: [23, 25]
CONTEXT: PL/Python function "result_nrows_test"
INFO: [-1, -1]
CONTEXT: PL/Python function "result_nrows_test"
result_nrows_test
-------------------