From 70ec3f1f8f0b753c38a1a582280a02930d7cac5f Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Sat, 25 Feb 2017 08:42:25 -0500 Subject: 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 --- src/pl/plpython/sql/plpython_spi.sql | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/pl/plpython/sql/plpython_spi.sql') diff --git a/src/pl/plpython/sql/plpython_spi.sql b/src/pl/plpython/sql/plpython_spi.sql index 7427de824b..fcf049cb66 100644 --- a/src/pl/plpython/sql/plpython_spi.sql +++ b/src/pl/plpython/sql/plpython_spi.sql @@ -37,6 +37,20 @@ return None ' LANGUAGE plpythonu; +CREATE FUNCTION spi_prepared_plan_test_two(a text) RETURNS text + AS +'if "myplan" not in SD: + q = "SELECT count(*) FROM users WHERE lname = $1" + SD["myplan"] = plpy.prepare(q, [ "text" ]) +try: + rv = SD["myplan"].execute([a]) + return "there are " + str(rv[0]["count"]) + " " + str(a) + "s" +except Exception, ex: + plpy.error(str(ex)) +return None +' + LANGUAGE plpythonu; + CREATE FUNCTION spi_prepared_plan_test_nested(a text) RETURNS text AS 'if "myplan" not in SD: @@ -79,7 +93,7 @@ return a + r -- select nested_call_one('pass this along'); select spi_prepared_plan_test_one('doe'); -select spi_prepared_plan_test_one('smith'); +select spi_prepared_plan_test_two('smith'); select spi_prepared_plan_test_nested('smith'); SELECT join_sequences(sequences) FROM sequences; @@ -275,7 +289,7 @@ plan = plpy.prepare( ["text"]) for row in plpy.cursor(plan, ["w"]): yield row['fname'] -for row in plpy.cursor(plan, ["j"]): +for row in plan.cursor(["j"]): yield row['fname'] $$ LANGUAGE plpythonu; -- cgit v1.2.1