2018-05-24 17:01:03 -04:00
|
|
|
CREATE OR REPLACE FUNCTION tps.srce_map_def_set(_defn jsonb) RETURNS jsonb
|
2018-05-24 16:54:00 -04:00
|
|
|
AS
|
|
|
|
$f$
|
|
|
|
|
|
|
|
DECLARE
|
|
|
|
_message jsonb;
|
|
|
|
_MESSAGE_TEXT text;
|
|
|
|
_PG_EXCEPTION_DETAIL text;
|
|
|
|
_PG_EXCEPTION_HINT text;
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
INSERT INTO
|
2018-05-24 17:11:26 -04:00
|
|
|
tps.map_rm (srce, target, regex, seq, hist)
|
2018-05-24 16:54:00 -04:00
|
|
|
SELECT
|
2018-05-24 17:03:55 -04:00
|
|
|
--data source
|
2018-05-30 09:48:19 -04:00
|
|
|
ae.r->>'srce'
|
2018-05-24 17:03:55 -04:00
|
|
|
--map name
|
2018-05-30 09:48:19 -04:00
|
|
|
,ae.r->>'name'
|
2018-05-24 17:03:55 -04:00
|
|
|
--map definition
|
2018-06-01 13:37:14 -04:00
|
|
|
,ae.r
|
2018-05-24 17:03:55 -04:00
|
|
|
--map aggregation sequence
|
2018-05-30 09:48:19 -04:00
|
|
|
,(ae.r->>'sequence')::INTEGER
|
2018-05-24 17:03:55 -04:00
|
|
|
--history definition
|
|
|
|
,jsonb_build_object(
|
2018-05-30 09:48:19 -04:00
|
|
|
'hist_defn',ae.r
|
2018-05-24 17:03:55 -04:00
|
|
|
,'effective',jsonb_build_array(CURRENT_TIMESTAMP,null::timestamptz)
|
|
|
|
) || '[]'::jsonb
|
2018-05-30 09:48:19 -04:00
|
|
|
FROM
|
|
|
|
jsonb_array_elements(_defn) ae(r)
|
2018-05-24 16:54:00 -04:00
|
|
|
ON CONFLICT ON CONSTRAINT map_rm_pk DO UPDATE SET
|
2018-05-30 09:48:19 -04:00
|
|
|
srce = excluded.srce
|
|
|
|
,target = excluded.target
|
|
|
|
,regex = excluded.regex
|
|
|
|
,seq = excluded.seq
|
2018-05-24 17:03:55 -04:00
|
|
|
,hist =
|
|
|
|
--the new definition going to position -0-
|
|
|
|
jsonb_build_object(
|
2018-05-30 09:48:19 -04:00
|
|
|
'hist_defn',excluded.regex
|
2018-05-24 17:03:55 -04:00
|
|
|
,'effective',jsonb_build_array(CURRENT_TIMESTAMP,null::timestamptz)
|
|
|
|
)
|
|
|
|
--the previous definition, set upper bound of effective range which was previously null
|
|
|
|
|| jsonb_set(
|
|
|
|
map_rm.hist
|
|
|
|
,'{0,effective,1}'::text[]
|
|
|
|
,to_jsonb(CURRENT_TIMESTAMP)
|
|
|
|
);
|
2018-05-24 16:54:00 -04: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 setting definition"
|
|
|
|
}
|
|
|
|
$$::jsonb)
|
|
|
|
||jsonb_build_object('message_text',_MESSAGE_TEXT)
|
|
|
|
||jsonb_build_object('pg_exception_detail',_PG_EXCEPTION_DETAIL);
|
|
|
|
return _message;
|
|
|
|
END;
|
|
|
|
|
|
|
|
_message:= jsonb_build_object('status','complete','message','definition has been set');
|
|
|
|
return _message;
|
|
|
|
|
|
|
|
END;
|
|
|
|
$f$
|
|
|
|
language plpgsql
|