2018-12-18 23:40:27 -05:00
|
|
|
/*
|
|
|
|
This function takes and array of definition object where "name" object is the primary key
|
|
|
|
It will force the entire body of sources to match what is received
|
|
|
|
*/
|
|
|
|
DROP FUNCTION IF EXISTS tps.srce_overwrite_all(jsonb);
|
2018-12-19 00:12:07 -05:00
|
|
|
CREATE FUNCTION tps.srce_overwrite_all(_defn jsonb) RETURNS jsonb
|
2018-12-18 23:40:27 -05:00
|
|
|
AS
|
|
|
|
$f$
|
|
|
|
DECLARE
|
|
|
|
_message jsonb;
|
|
|
|
_MESSAGE_TEXT text;
|
|
|
|
_PG_EXCEPTION_DETAIL text;
|
|
|
|
_PG_EXCEPTION_HINT text;
|
|
|
|
_rebuild BOOLEAN;
|
2018-12-27 00:56:40 -05:00
|
|
|
_list text;
|
2018-12-18 23:40:27 -05:00
|
|
|
BEGIN
|
|
|
|
|
|
|
|
WITH
|
|
|
|
--retain the results of the update by srce
|
|
|
|
_set AS (
|
|
|
|
SELECT
|
|
|
|
j.rn rn
|
|
|
|
,j.e->>'name' srce
|
|
|
|
,j.e defn
|
|
|
|
FROM
|
2018-12-18 23:41:12 -05:00
|
|
|
jsonb_array_elements(_defn) WITH ORDINALITY j(e, rn)
|
2018-12-18 23:40:27 -05:00
|
|
|
)
|
|
|
|
--full join
|
|
|
|
,_full AS (
|
|
|
|
SELECT
|
|
|
|
COALESCE(_srce.srce,_set.srce) srce
|
|
|
|
,CASE COALESCE(_set.srce,'DELETE') WHEN 'DELETE' THEN 'DELETE' ELSE 'SET' END actn
|
|
|
|
,COALESCE(_set.defn,_srce.defn) defn
|
|
|
|
FROM
|
2018-12-19 00:12:07 -05:00
|
|
|
tps.srce _srce
|
2018-12-18 23:40:27 -05:00
|
|
|
FULL OUTER JOIN _set ON
|
|
|
|
_set.srce = _srce.srce
|
|
|
|
)
|
|
|
|
--call functions from list
|
|
|
|
,_do AS (
|
|
|
|
SELECT
|
|
|
|
f.srce
|
|
|
|
,f.actn
|
2018-12-27 00:56:40 -05:00
|
|
|
,COALESCE(setd.message, '{"message":"not inserted"}'::jsonb/*deld.message*/) message
|
2018-12-18 23:40:27 -05:00
|
|
|
FROM
|
|
|
|
_full f
|
|
|
|
LEFT JOIN LATERAL tps.srce_set(defn) setd(message) ON f.actn = 'SET'
|
2018-12-27 00:56:40 -05:00
|
|
|
--dual left joins for functions that touch the same table causes the first left join actions to be undone
|
|
|
|
--LEFT JOIN LATERAL tps.srce_delete(defn) deld(message) ON f.actn = 'DELETE'
|
2018-12-18 23:40:27 -05:00
|
|
|
)
|
|
|
|
--aggregate all the messages into one message
|
|
|
|
----
|
|
|
|
---- should look at rolling back the whole thing if one of the function returns a fail. stored proc could do this.
|
|
|
|
----
|
|
|
|
SELECT
|
|
|
|
jsonb_agg(jsonb_build_object('source',srce,'status',message->>'status','message',message->>'message'))
|
|
|
|
INTO
|
|
|
|
_message
|
|
|
|
FROM
|
|
|
|
_do;
|
|
|
|
|
2018-12-27 00:56:40 -05:00
|
|
|
SELECT string_agg(srce,',') INTO _list FROM tps.srce;
|
|
|
|
RAISE NOTICE 'multi source list: %', _list;
|
|
|
|
|
2018-12-18 23:40:27 -05:00
|
|
|
RETURN _message;
|
|
|
|
|
2018-12-27 00:56:40 -05:00
|
|
|
SELECT string_agg(srce,',') INTO _list FROM tps.srce;
|
|
|
|
RAISE NOTICE 'after return: %', _list;
|
|
|
|
|
2018-12-18 23:40:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
EXCEPTION WHEN OTHERS THEN
|
|
|
|
GET STACKED DIAGNOSTICS
|
|
|
|
_MESSAGE_TEXT = MESSAGE_TEXT,
|
|
|
|
_PG_EXCEPTION_DETAIL = PG_EXCEPTION_DETAIL,
|
|
|
|
_PG_EXCEPTION_HINT = PG_EXCEPTION_HINT;
|
|
|
|
_message:=
|
|
|
|
($$
|
|
|
|
{
|
|
|
|
"status":"fail",
|
|
|
|
"message":"error updating sources"
|
|
|
|
}
|
|
|
|
$$::jsonb)
|
|
|
|
||jsonb_build_object('message_text',_MESSAGE_TEXT)
|
|
|
|
||jsonb_build_object('pg_exception_detail',_PG_EXCEPTION_DETAIL);
|
|
|
|
RETURN _message;
|
|
|
|
END;
|
|
|
|
$f$
|
|
|
|
LANGUAGE plpgsql
|