From b40f3edcda916b36b9045e7aeac3867367f4808f Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 23 Feb 2018 15:41:30 -0500 Subject: [PATCH] create function jsonb_extract in tps schema --- deploy/ubm_schema.sql | 26 ++++++++++++++++++++++++-- functions/manual_srce.pgsql | 4 ++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/deploy/ubm_schema.sql b/deploy/ubm_schema.sql index ef7eab3..f6ac09a 100644 --- a/deploy/ubm_schema.sql +++ b/deploy/ubm_schema.sql @@ -2,8 +2,8 @@ -- PostgreSQL database dump -- --- Dumped from database version 10.2 --- Dumped by pg_dump version 10.2 +-- Dumped from database version 10rc1 +-- Dumped by pg_dump version 10rc1 SET statement_timeout = 0; SET lock_timeout = 0; @@ -82,6 +82,28 @@ END; $$; +-- +-- Name: jsonb_extract(jsonb, text[]); Type: FUNCTION; Schema: tps; Owner: - +-- + +CREATE FUNCTION jsonb_extract(rec jsonb, key_list text[]) RETURNS jsonb + LANGUAGE plpgsql + AS $$ +DECLARE + t text[]; + j jsonb := '{}'::jsonb; + +BEGIN + FOREACH t SLICE 1 IN ARRAY key_list LOOP + --RAISE NOTICE '%', t; + --RAISE NOTICE '%', t[1]; + j := j || jsonb_build_object(t[1],rec#>t); + END LOOP; + RETURN j; +END; +$$; + + -- -- Name: srce_set(text, jsonb); Type: FUNCTION; Schema: tps; Owner: - -- diff --git a/functions/manual_srce.pgsql b/functions/manual_srce.pgsql index 47e7cb9..c016296 100644 --- a/functions/manual_srce.pgsql +++ b/functions/manual_srce.pgsql @@ -21,7 +21,7 @@ DECLARE _log_id text; BEGIN - _path := 'C:\users\fleet\downloads\discover-recentactivity-20171031.csv'; + _path := 'C:\users\ptrowbridge\documents\tps_etl\sample_discovercard\data.csv'; _srce := 'DCARD'; ----------------------------------------------------build the column list of the temp table---------------------------------------------------------------- @@ -80,7 +80,7 @@ BEGIN ,pending_list AS ( SELECT - jsonb_extract( + tps.jsonb_extract( row_to_json(i)::jsonb ,ext.text_array ) json_key,