graphql-engine/server/src-rsr/trigger.sql.j2
2019-05-13 15:11:07 +05:30

29 lines
1008 B
Django/Jinja

CREATE OR REPLACE function hdb_views.{{QUALIFIED_TRIGGER_NAME}}() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
_old record;
_new record;
_data json;
BEGIN
IF TG_OP = 'UPDATE' THEN
_old := {{OLD_ROW}};
_new := {{NEW_ROW}};
ELSE
/* initialize _old and _new with dummy values for INSERT and UPDATE events*/
_old := row((select 1));
_new := row((select 1));
END IF;
_data := json_build_object(
'old', {{OLD_PAYLOAD_EXPRESSION}},
'new', {{NEW_PAYLOAD_EXPRESSION}}
);
IF (TG_OP <> 'UPDATE') OR (_old <> _new) THEN
PERFORM hdb_catalog.insert_event_log(CAST(TG_TABLE_SCHEMA AS text), CAST(TG_TABLE_NAME AS text), CAST('{{NAME}}' AS text), TG_OP, _data);
END IF;
RETURN NULL;
END;
$$;
DROP TRIGGER IF EXISTS {{QUALIFIED_TRIGGER_NAME}} ON {{QUALIFIED_TABLE}};
CREATE TRIGGER {{QUALIFIED_TRIGGER_NAME}} AFTER {{OPERATION}} ON {{QUALIFIED_TABLE}} FOR EACH ROW EXECUTE PROCEDURE hdb_views.{{QUALIFIED_TRIGGER_NAME}}();