开发者

From a python module, called from a pl/python function, how to get the return value?

开发者 https://www.devze.com 2023-04-06 17:10 出处:网络
Is there any reason this function call would not return \'result\'? CREATE OR REPLACE FUNCTION myfunction (input int, OUT result int) AS $$

Is there any reason this function call would not return 'result'?

CREATE OR REPLACE FUNCTION myfunction (input int, OUT result int) AS $$

result = mymodule.object(input,plpy)
plpy.info(" ========= EXTRA-module result: ===",result)

$$ LANGUAGE plpythonu;

=== Content of mymodule ============

def object(input,plpy):
  import StringIO
  try:
   plan = plpy.prepare("INSERT INTO file VALUES (nextval('primary_sequence'),$1) RETURNING primary_key", ["integer"] )
  except:
 开发者_StackOverflow社区  plpy.error(traceback.format_exc())

  try:
   rv = plpy.execute(plan, [ input ])
   result = rv[0]["primary_key"]
   plpy.info(" ========= INTRA-module result: ===",result)
   return result
  except:
   plpy.error(traceback.format_exc())


I'm not to familiar with plpython, but if it is throwing an error and that isn't getting printed out or passed further up the chain you would never know.

I don't know if you are testing with a command line or not but try putting a print statement in your except blocks to see if it is just erroring out instead of returning.


@ed. Didn't actually need the RETURNS syntax instead of OUT, but your suggestion put me onto the answer. And yes, I feel like a real dummy. This is the beauty of having others review one's work.

with the return result added, things work out nicely. Key assumption I'd made here was that the result = syntax did not actually finish the return within the scope of the calling function. Doh!

CREATE OR REPLACE FUNCTION myfunction (input int, OUT result int) AS $$

result = mymodule.object(input,plpy)
plpy.info(" ========= EXTRA-module result: ===",result)
# This was the key bit:
return result

$$ LANGUAGE plpythonu;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号