2020-11-12 12:25:48 +03:00
|
|
|
CREATE OR REPLACE function hdb_catalog.#{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
|
2021-01-06 23:21:39 +03:00
|
|
|
/* NOTE: formerly we used TG_TABLE_NAME in place of tableName here. However in the case of
|
|
|
|
partitioned tables this will give the name of the partitioned table and since we use the table name to
|
|
|
|
get the event trigger configuration from the schema, this fails because the event trigger is only created
|
|
|
|
on the original table. */
|
2019-09-13 02:22:01 +03:00
|
|
|
IF (TG_OP <> 'UPDATE') OR (_old <> _new) THEN
|
2021-01-06 23:21:39 +03:00
|
|
|
PERFORM hdb_catalog.insert_event_log(CAST(#{schemaName} AS text), CAST(#{tableName} 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
|
2021-01-06 23:21:39 +03:00
|
|
|
PERFORM hdb_catalog.insert_event_log(CAST(#{schemaName} AS text), CAST(#{tableName} 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-11-12 12:25:48 +03:00
|
|
|
CREATE TRIGGER #{qualifiedTriggerName} AFTER #{operation} ON #{qualifiedTable} FOR EACH ROW EXECUTE PROCEDURE hdb_catalog.#{qualifiedTriggerName}();
|