1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-25 01:02:05 +03:00

PL/Python: Add cursor and execute methods to plan object

Instead of

    plan = plpy.prepare(...)
    res = plpy.execute(plan, ...)

you can now write

    plan = plpy.prepare(...)
    res = plan.execute(...)

or even

    res = plpy.prepare(...).execute(...)

and similarly for the cursor() method.

This is more in object oriented style, and makes the hybrid nature of
the existing execute() function less confusing.

Reviewed-by: Andrew Dunstan <andrew.dunstan@2ndquadrant.com>
This commit is contained in:
Peter Eisentraut
2017-02-25 08:42:25 -05:00
parent 090010f2ec
commit 70ec3f1f8f
8 changed files with 79 additions and 11 deletions

View File

@ -1046,6 +1046,14 @@ rv = plpy.execute(plan, ["name"], 5)
The third argument is the optional row limit as before.
</para>
<para>
Alternatively, you can call the <function>execute</function> method on
the plan object:
<programlisting>
rv = plan.execute(["name"], 5)
</programlisting>
</para>
<para>
Query parameters and result row fields are converted between PostgreSQL
and Python data types as described in <xref linkend="plpython-data">.
@ -1081,7 +1089,9 @@ $$ LANGUAGE plpythonu;
as <literal>plpy.execute</literal> (except for the row limit) and returns
a cursor object, which allows you to process large result sets in smaller
chunks. As with <literal>plpy.execute</literal>, either a query string
or a plan object along with a list of arguments can be used.
or a plan object along with a list of arguments can be used, or
the <function>cursor</function> function can be called as a method of
the plan object.
</para>
<para>
@ -1125,7 +1135,7 @@ $$ LANGUAGE plpythonu;
CREATE FUNCTION count_odd_prepared() RETURNS integer AS $$
odd = 0
plan = plpy.prepare("select num from largetable where num % $1 &lt;&gt; 0", ["integer"])
rows = list(plpy.cursor(plan, [2]))
rows = list(plpy.cursor(plan, [2])) # or: = list(plan.cursor([2]))
return len(rows)
$$ LANGUAGE plpythonu;