2018-05-30 23:58:35 -04:00
DROP FUNCTION IF EXISTS tps . build_srce_view_sql ( text , text ) ;
2018-05-25 10:23:43 -04:00
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 ;
- - _srce text ;
2018-05-23 17:18:17 -04:00
_sql text ;
BEGIN
2018-05-24 13:22:46 -04:00
- - _schema : = ' default ' ;
- - _srce : = ' dcard ' ;
2018-05-23 17:18:17 -04:00
SELECT
2018-12-02 01:41:29 -05:00
' DROP VIEW IF EXISTS tpsv. ' | | s . srce | | ' _ ' | | ( list . e - > > ' name ' ) | | ' ; CREATE VIEW tpsv. ' | | s . srce | | ' _ ' | | ( list . e - > > ' name ' ) | | ' AS SELECT id, logid, allj, ' | | string_agg ( ' (allj#>> '' ' | | rec . PATH : : text | | ' '' ):: ' | | rec . type | | ' AS " ' | | rec . column_name | | ' " ' , ' , ' ) | | ' FROM tps.trans WHERE srce = '' ' | | s . srce | | ' '' ; '
INTO
2018-05-23 17:18:17 -04:00
_sql
FROM
2018-12-02 01:41:29 -05:00
tps . srce s
JOIN LATERAL jsonb_array_elements ( s . defn - > ' schemas ' ) list ( e ) ON TRUE
JOIN LATERAL jsonb_array_elements ( list . e - > ' columns ' ) as cols ( e ) ON TRUE
JOIN LATERAL jsonb_to_record ( cols . e ) AS rec ( PATH text [ ] , " type " text , column_name text ) ON TRUE
2018-05-23 17:18:17 -04:00
WHERE
2018-12-02 01:41:29 -05:00
srce = _srce
AND list . e - > > ' name ' = _schema
2018-05-23 17:18:17 -04:00
GROUP BY
2018-12-02 01:41:29 -05:00
s . srce
, list . e ;
2018-05-23 17:18:17 -04:00
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 $
2018-05-30 23:58:35 -04:00
LANGUAGE plpgsql ;