From 02555012f5b161a51b34509b17693dfcb41a81db Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 24 May 2018 17:11:26 -0400 Subject: [PATCH] cast items in and specify insert columns, create test script --- deploy/setup.sql | 76 ++++++++++++++++++++++++- interface/map_def/srce_map_def_set.sql | 6 +- interface/map_def/test/test_map_set.sql | 27 +++++++++ 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 interface/map_def/test/test_map_set.sql diff --git a/deploy/setup.sql b/deploy/setup.sql index 87e86da..ae98101 100644 --- a/deploy/setup.sql +++ b/deploy/setup.sql @@ -251,4 +251,78 @@ RAISE NOTICE '%',_sql; END $f$ -LANGUAGE plpgsql; \ No newline at end of file +LANGUAGE plpgsql; + +-----set map defintion from json argument +CREATE OR REPLACE FUNCTION tps.srce_map_def_set(_defn jsonb) RETURNS jsonb +AS +$f$ + +DECLARE + _message jsonb; + _MESSAGE_TEXT text; + _PG_EXCEPTION_DETAIL text; + _PG_EXCEPTION_HINT text; + +BEGIN + + BEGIN + + INSERT INTO + tps.map_rm (srce, target, regex, seq, hist) + SELECT + --data source + _defn->>'srce' + --map name + ,_defn->>'name' + --map definition + ,_defn + --map aggregation sequence + ,(_defn->>'sequence')::INTEGER + --history definition + ,jsonb_build_object( + 'hist_defn',_defn + ,'effective',jsonb_build_array(CURRENT_TIMESTAMP,null::timestamptz) + ) || '[]'::jsonb + ON CONFLICT ON CONSTRAINT map_rm_pk DO UPDATE SET + srce = _defn->>'srce' + ,target = _defn->>'name' + ,regex = _defn + ,seq = (_defn->>'sequence')::INTEGER + ,hist = + --the new definition going to position -0- + jsonb_build_object( + 'hist_defn',_defn + ,'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) + ); + + 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; \ No newline at end of file diff --git a/interface/map_def/srce_map_def_set.sql b/interface/map_def/srce_map_def_set.sql index 5127912..cdbd286 100644 --- a/interface/map_def/srce_map_def_set.sql +++ b/interface/map_def/srce_map_def_set.sql @@ -13,7 +13,7 @@ BEGIN BEGIN INSERT INTO - tps.map_rm + tps.map_rm (srce, target, regex, seq, hist) SELECT --data source _defn->>'srce' @@ -22,7 +22,7 @@ BEGIN --map definition ,_defn --map aggregation sequence - ,_defn->>'sequence' + ,(_defn->>'sequence')::INTEGER --history definition ,jsonb_build_object( 'hist_defn',_defn @@ -32,7 +32,7 @@ BEGIN srce = _defn->>'srce' ,target = _defn->>'name' ,regex = _defn - ,seq = _defn->>'sequence' + ,seq = (_defn->>'sequence')::INTEGER ,hist = --the new definition going to position -0- jsonb_build_object( diff --git a/interface/map_def/test/test_map_set.sql b/interface/map_def/test/test_map_set.sql new file mode 100644 index 0000000..4fa13fb --- /dev/null +++ b/interface/map_def/test/test_map_set.sql @@ -0,0 +1,27 @@ +SELECT + * +FROM + tps.srce_map_def_set( + $$ + { + "srce":"dcard", + "sequence":1, + "defn": [ + { + "key": "{Description}", + "map": "y", + "flag": "g", + "field": "f20", + "regex": ".{1,20}", + "retain": "y" + } + ], + "name": "First 20", + "where": [ + {} + ], + "function": "extract", + "description": "pull first 20 characters from description for mapping" + } + $$ + ) \ No newline at end of file