tps/interface/source_maint/srce_build_view.sql

32 lines
885 B
MySQL
Raw Normal View History

DROP FUNCTION IF EXISTS tps.build_srce_view_sql(text, text);
CREATE OR REPLACE FUNCTION tps.build_srce_view_sql(_srce text, _schema text) RETURNS TEXT
2018-05-24 13:22:46 -04:00
AS
2018-05-23 17:18:17 -04:00
$f$
2018-05-24 13:22:46 -04:00
DECLARE
--_schema text;
2018-05-23 17:18:17 -04:00
_path text[];
2018-05-24 13:22:46 -04:00
--_srce text;
2018-05-23 17:18:17 -04:00
_sql text;
BEGIN
2018-05-24 13:22:46 -04:00
--_schema:= 'default';
_path:= ARRAY['schemas',_schema]::text[];
--_srce:= 'dcard';
2018-05-23 17:18:17 -04:00
SELECT
'DROP VIEW IF EXISTS tpsv.'||_srce||'_'||_path[2]||'; CREATE VIEW tpsv.'||_srce||'_'||_path[2]||' AS SELECT id, logid, allj, '||string_agg('(allj#>>'''||r.PATH::text||''')::'||r.type||' AS "'||r.column_name||'"',', ')||' FROM tps.trans WHERE srce = '''||_srce||''';'
2018-05-23 17:18:17 -04:00
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;
2018-05-24 13:22:46 -04:00
RETURN _sql;
2018-05-23 17:18:17 -04:00
RAISE NOTICE '%',_sql;
END
2018-05-24 13:22:46 -04:00
$f$
LANGUAGE plpgsql;