Guest User

Untitled

a guest
Jul 4th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. -- Function: ecm.card_custum_declaration_save(text, text)
  2.  
  3. -- DROP FUNCTION ecm.card_custum_declaration_save(text, text);
  4.  
  5. CREATE OR REPLACE FUNCTION ecm.card_custum_declaration_save(uid text, in_json text)
  6.   RETURNS integer AS
  7.  
  8.  
  9. $BODY$
  10. import json
  11. x=json.loads(in_json)
  12.  
  13. actor = plpy.execute("select distinct(id) from ou.users usr where lower(usr.uid) = lower('" + uid + "')")
  14. x['actor'] = actor[0]['id']
  15.  
  16. doc_id = plpy.execute("select ecm.card_common_save('"+uid+"','"+in_json+"') as did")
  17. x['doc_id_new'] = doc_id[0]['did']
  18.  
  19.  
  20. if not x.get('doc_cardid'): raise Exception('Error 1')
  21. if not x.get('contractorid'): raise Exception('Error 2')
  22. if not x.get('start_date'): raise Exception('Error 3')
  23. if not x.get('end_date'): raise Exception('Error 4')
  24.  
  25.  
  26. if not x.get('doc_id'):
  27.     sql="""insert into card_custom_declarations(
  28.         contractorid,
  29.         start_date,
  30.         end_date
  31.     ) values (
  32.         $1,
  33.         $2,
  34.         $3
  35.     ) returning id"""
  36.     pr=plpy.prepare(sql, [
  37.         "integer", #contractorid
  38.         "date", #start_date
  39.         "date", #end_date
  40.     ])
  41.     new_id=plpy.execute(pr,[
  42.         x['contractorid'],
  43.         x['start_date'],
  44.         x['end_date']
  45.     ])
  46.  
  47.    
  48. else:
  49.     sql="""update card_custom_declarations set
  50.         contractorid =$1
  51.         where start_date=$2 and end_date=$3"""
  52.     pr = plpy.prepare(sql,[
  53.         "integer",
  54.         "date",
  55.         "date"])       
  56.     plpy.execute(pr,[
  57.         x['contractorid'],
  58.         x['start_date'],
  59.         x['end_date']
  60.     ])
  61.  
  62.  
  63.  
  64.  
  65. return x['doc_id_new'];
  66. $BODY$
  67.  
  68.  
  69.   LANGUAGE plpythonu VOLATILE
  70.   COST 100;
  71. ALTER FUNCTION ecm.card_custum_declaration_save(text, text)
  72.   OWNER TO "user";
Advertisement
Add Comment
Please, Sign In to add comment