Advertisement
mech_coder

pgsql plpython json via websockets

Oct 30th, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %%sql postgres@system
  2. -- GET GMAIL ROW AS JSON
  3. DROP FUNCTION IF EXISTS es_gmail_row_with_gmail_uid(integer);
  4. CREATE OR REPLACE FUNCTION es_gmail_row_with_gmail_uid(gmail_uid integer)
  5.     RETURNS text AS $funct$
  6.  
  7.     from subprocess                         import Popen            as sub_popen
  8.     from subprocess                         import PIPE             as sub_PIPE
  9.  
  10.     T = {'gmail_uid' : gmail_uid}
  11.  
  12.     qry = """
  13.        SELECT row_to_json(t) res
  14.        FROM (select *
  15.            from (
  16.                SELECT * FROM gmail
  17.                WHERE uid = %(gmail_uid)s
  18.            ) f
  19.        ) t
  20.          """ % T
  21.  
  22.     cmd = [ 'echo "GET /json?gmail=%s"' % plpy.execute(qry)[0]["res"],
  23.             ' | socat - unix:/var/sockets/web_socket_relay.sock,nonblock' ]
  24.     #cmd = [ 'echo "GET /json?gmail=%s"' % plpy.execute(qry)[0]["res"],
  25.     #        ' > /tmp/tmp' ]
  26.     (_out,_err) = sub_popen(''.join(cmd),stdout=sub_PIPE,shell=True).communicate()
  27.     assert _err is None
  28.  
  29.     $funct$
  30.     LANGUAGE plpythonu;
  31.  
  32. %%sql postgres@system
  33. -- GET FILE IDX ROW AS JSON
  34. DROP FUNCTION IF EXISTS es_file_idx_row_with_gmail_uid(integer);
  35. CREATE OR REPLACE FUNCTION es_file_idx_row_with_gmail_uid(gmail_uid integer)
  36.     RETURNS text AS $funct$
  37.  
  38.     from subprocess                         import Popen            as sub_popen
  39.     from subprocess                         import PIPE             as sub_PIPE
  40.    
  41.     T = {'gmail_uid' : gmail_uid}
  42.  
  43.     qry = """
  44.        SELECT row_to_json(t) res
  45.        FROM (select *
  46.            from (
  47.                SELECT * FROM file_idx
  48.                WHERE src_uid = %(gmail_uid)s
  49.            ) f
  50.        ) t
  51.          """ % T
  52.  
  53.     cmd = [ 'echo \'GET /json?file_idx=%s\'' % plpy.execute(qry)[0]["res"],
  54.             ' | socat - unix:/var/sockets/web_socket_relay.sock,nonblock' ]
  55.     (_out,_err) = sub_popen(''.join(cmd),stdout=sub_PIPE,shell=True).communicate()
  56.     assert _err is None
  57.  
  58.     $funct$
  59.     LANGUAGE plpythonu;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement