再一次证明, python 和 postgresql是完美的结合

还是一样的风格, 独孤求败

-- Function: cms.getlovpath(integer, integer)

-- DROP FUNCTION cms.getlovpath(integer, integer);

CREATE OR REPLACE FUNCTION cms.getlovpath(i_lovd_id integer, i_lov_id integer)
  RETURNS json AS
$BODY$import ujson as json 
import plpy
_sql = 'select k from cms.lov where id=$1';
plan = plpy.prepare(_sql,["integer"])
rv = plpy.execute(plan,[i_lov_id],1)
_k = rv[0]['k']
_sql= 'select vc.lov_id,vc.id,vc.pid,vct.level from (select * from cms.v_lov where lovd_id=$2'\
+""") vc,(select * from connectby('(select * from cms.v_lov where lovd_id="""\
+str(i_lovd_id)+""") t','pid','id','id',$1"""\
+""",0,';') as t(id int, pid int, level int, branch text, pos int)) vct where vct.id=vc.id order by vct.level DESC offset 1"""
rs = {}
#return json.dumps(_sql);
plan = plpy.prepare(_sql,["varchar","integer"])
for row in plpy.cursor(plan,[str(_k),i_lovd_id]):
#	rs[row['lov_id']]={'id':row['id'],'pid':row['pid']}
	rs[row['pid']]=row['lov_id']
return json.dumps(rs);$BODY$
  LANGUAGE plpython3u VOLATILE
  COST 100;
ALTER FUNCTION cms.getlovpath(integer, integer)
  OWNER TO postgres;

你可能感兴趣的:(再一次证明, python 和 postgresql是完美的结合)