add sql generation function to main setup

This commit is contained in:
Paul Trowbridge 2018-05-24 13:26:41 -04:00
parent 46a1c94643
commit ae77d01d51

View File

@ -151,6 +151,7 @@ ALTER TABLE ONLY tps.trans
-------------create functions------------------------------------------------------------------------------------------------------------------------ -------------create functions------------------------------------------------------------------------------------------------------------------------
-----set source
CREATE FUNCTION tps.srce_set(_defn jsonb) RETURNS jsonb CREATE FUNCTION tps.srce_set(_defn jsonb) RETURNS jsonb
AS AS
$f$ $f$
@ -217,3 +218,36 @@ BEGIN
END; END;
$f$ $f$
LANGUAGE plpgsql; LANGUAGE plpgsql;
-----generate sql to create select based on schema
CREATE FUNCTION tps.build_srce_view_sql(_srce text, _schema text) RETURNS TEXT
AS
$f$
DECLARE
--_schema text;
_path text[];
--_srce text;
_sql text;
BEGIN
--_schema:= 'default';
_path:= ARRAY['schemas',_schema]::text[];
--_srce:= 'dcard';
SELECT
'CREATE VIEW tpsv.'||_srce||'_'||_path[2]||' AS SELECT '||string_agg('(rec#>>'''||r.PATH::text||''')::'||r.type||' AS "'||r.column_name||'"',', ')||' FROM tps.trans WHERE srce = '''||_srce||''''
INTO
_sql
FROM
tps.srce
JOIN LATERAL jsonb_array_elements(defn#>_path) ae(v) ON TRUE
JOIN LATERAL jsonb_to_record (ae.v) AS r(PATH text[], "type" text, column_name text) ON TRUE
WHERE
srce = _srce
GROUP BY
srce.srce;
RETURN _sql;
RAISE NOTICE '%',_sql;
END
$f$
LANGUAGE plpgsql;