2020-10-30 21:55:53 +03:00
|
|
|
CREATE OR REPLACE function hdb_views.#{qualifiedTriggerName}() RETURNS trigger
|
2019-05-13 12:41:07 +03:00
|
|
|
LANGUAGE plpgsql
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
|
|
|
_old record;
|
|
|
|
_new record;
|
|
|
|
_data json;
|
|
|
|
BEGIN
|
|
|
|
IF TG_OP = 'UPDATE' THEN
|
2019-10-11 08:13:57 +03:00
|
|
|
_old := #{oldRow};
|
|
|
|
_new := #{newRow};
|
2019-05-13 12:41:07 +03:00
|
|
|
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(
|
2019-10-11 08:13:57 +03:00
|
|
|
'old', #{oldPayloadExpression},
|
|
|
|
'new', #{newPayloadExpression}
|
2019-05-13 12:41:07 +03:00
|
|
|
);
|
2019-09-13 02:22:01 +03:00
|
|
|
BEGIN
|
|
|
|
IF (TG_OP <> 'UPDATE') OR (_old <> _new) THEN
|
2019-10-11 08:13:57 +03:00
|
|
|
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);
|
2019-09-13 02:22:01 +03:00
|
|
|
END IF;
|
|
|
|
EXCEPTION WHEN undefined_function THEN
|
|
|
|
IF (TG_OP <> 'UPDATE') OR (_old *<> _new) THEN
|
2019-10-11 08:13:57 +03:00
|
|
|
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);
|
2019-09-13 02:22:01 +03:00
|
|
|
END IF;
|
|
|
|
END;
|
|
|
|
|
2019-05-13 12:41:07 +03:00
|
|
|
RETURN NULL;
|
|
|
|
END;
|
|
|
|
$$;
|
2019-10-11 08:13:57 +03:00
|
|
|
DROP TRIGGER IF EXISTS #{qualifiedTriggerName} ON #{qualifiedTable};
|
2020-10-30 21:55:53 +03:00
|
|
|
CREATE TRIGGER #{qualifiedTriggerName} AFTER #{operation} ON #{qualifiedTable} FOR EACH ROW EXECUTE PROCEDURE hdb_views.#{qualifiedTriggerName}();
|