Advertisement
mech_coder

pgsql plshu json via websockets

Oct 30th, 2015
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- NEW GMAIL ROW
  2.  
  3. DROP FUNCTION if exists fs_es_gmail_insert() cascade;
  4. DROP TRIGGER if exists fs_es_gmail_insert_trigger ON gmail;
  5. CREATE OR REPLACE FUNCTION fs_es_gmail_insert()
  6.     RETURNS TRIGGER as E'#!/bin/bash
  7.  
  8.    _arr=\'[["orig_msg","all_mail_uid","g_msg_id","msg_id","last_updated","uid"],[\'
  9.    for arg do
  10.        if [ -z "$arg" ]; then
  11.            _arr="$_arr\\\"$arg\\\","
  12.        elif [ ${arg::1} = "{" ]; then
  13.            _arr="$_arr$arg,"
  14.        else
  15.            _arr="$_arr\\\"$arg\\\","
  16.        fi
  17.        _uid="$arg"
  18.    done
  19.    _arr=${_arr::-1}"]]"
  20.    
  21.    _json=$(printf \'%s\n\' "$_arr" | jq -Mc \'transpose | map({ key: .[0], value: .[1] }) | from_entries\')
  22.    echo "GET /json?gmail=$_json&uid=$_uid" | socat - tcp:0.0.0.0:12501,reuseaddr,nonblock
  23.  
  24.    ' LANGUAGE plsh;
  25.  
  26.     CREATE TRIGGER fs_es_gmail_insert_trigger
  27.     BEFORE INSERT ON gmail
  28.     FOR EACH ROW
  29.     EXECUTE PROCEDURE fs_es_gmail_insert();
  30.  
  31.  
  32. -- NEW FILE_IDX ROW
  33.            
  34. DROP FUNCTION if exists fs_es_file_idx_insert() cascade;
  35. DROP TRIGGER if exists fs_es_file_idx_insert_trigger ON file_idx;
  36. CREATE OR REPLACE FUNCTION fs_es_file_idx_insert()
  37.     RETURNS TRIGGER as E'#!/bin/bash
  38.  
  39.        _keys=(src_db src_uid _key _filetype _info _run_ocr _metadata last_updated uid _content)
  40.        key_arr=""
  41.        val_arr=""
  42.        pt=0
  43.        for arg do
  44.            k=${_keys[$pt]}
  45.            if [ -n "$arg" ]; then
  46.                key_arr="$key_arr"\'"\'"$k"\'",\'
  47.                if [ k = "uid" ]; then _uid="$arg"; fi
  48.  
  49.                if [ ${arg::1} = "{" ]; then
  50.                    val_arr="$val_arr$arg,"
  51.                else
  52.                    val_arr="$val_arr\\\"$arg\\\","
  53.                fi
  54.            fi
  55.            let pt=pt+1
  56.        done
  57.  
  58.        key_arr=${key_arr::-1}
  59.        val_arr=${val_arr::-1}
  60.        _arr="[[$key_arr],[$val_arr]]"
  61.            
  62.        echo $_arr > /tmp/tmp
  63.  
  64.        _json=$(printf \'%s\n\' "$_arr" | jq \'transpose | map({ key: .[0], value: .[1] }) | from_entries\')
  65.        echo "GET /json?file_idx=$_json&uid=$_uid" | socat - tcp:0.0.0.0:12501,reuseaddr,nonblock
  66.  
  67.    ' LANGUAGE plsh;
  68.  
  69.     CREATE TRIGGER fs_es_file_idx_insert_trigger
  70.     BEFORE INSERT or UPDATE ON file_idx
  71.     FOR EACH ROW
  72.     EXECUTE PROCEDURE fs_es_file_idx_insert();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement