删除触发器后的Postgres不触发

时间:2019-03-07 15:27:42

标签: postgresql database-trigger

我创建了此触发函数。它适用于INSERT和UPDATE。使用DELETE操作时,不会触发此功能。我只是尝试了之后和之前,结果是相同的。

有人能找到证明这一点的理由吗?

DECLARE
v_old_data json;
v_new_data json;
BEGIN

IF (TG_OP = 'UPDATE') THEN
    v_old_data := row_to_json(OLD);
    v_new_data := row_to_json(NEW);
    INSERT INTO auditoria.auditoria_geral (audi_table,audi_user,audi_op,audi_old,audi_new,audi_query)
    VALUES (TG_TABLE_NAME::TEXT,new.clnt_autorstat,substring(TG_OP,1,1),v_old_data,v_new_data, current_query());
    RETURN NEW;
ELSIF (TG_OP = 'DELETE') THEN
    v_old_data := row_to_json(OLD);
    INSERT INTO auditoria.auditoria_geral (audi_table,audi_user,audi_op,audi_old,audi_query)
    VALUES (TG_TABLE_NAME::TEXT,old.clnt_autorstat,substring(TG_OP,1,1),v_old_data, current_query());
    RETURN null;
ELSIF (TG_OP = 'INSERT') THEN
    v_new_data := row_to_json(NEW);
    INSERT INTO auditoria.auditoria_geral (audi_table,audi_user,audi_op,audi_new,audi_query)
    VALUES (TG_TABLE_NAME::TEXT,new.clnt_autorstat,substring(TG_OP,1,1),v_new_data, current_query());
    RETURN NEW;
ELSE
    RAISE WARNING '[AUDIT.IF_MODIFIED_FUNC] - Other action occurred: %, at %',TG_OP,now();
    RETURN NULL;
END IF;

EXCEPTION
WHEN data_exception THEN
    RAISE WARNING '[AUDIT.IF_MODIFIED_FUNC] - UDF ERROR [DATA EXCEPTION] - SQLSTATE: %, SQLERRM: %',SQLSTATE,SQLERRM;
    RETURN NULL;
WHEN unique_violation THEN
    RAISE WARNING '[AUDIT.IF_MODIFIED_FUNC] - UDF ERROR [UNIQUE] - SQLSTATE: %, SQLERRM: %',SQLSTATE,SQLERRM;
    RETURN NULL;
WHEN OTHERS THEN
    RAISE WARNING '[AUDIT.IF_MODIFIED_FUNC] - UDF ERROR [OTHER] - SQLSTATE: %, SQLERRM: %',SQLSTATE,SQLERRM;
    RETURN NULL;
END;


CREATE TRIGGER auditoria_clientes
AFTER INSERT OR DELETE OR UPDATE 
ON public.clientes
FOR EACH ROW
EXECUTE PROCEDURE public.auditoria_clientes();

添加了创建触发代码。

                        Table "public.clientes"
    Column        |            Type             |                           Modifiers
----------------------+-----------------------------+----------------------------------------------------------------
clnt_codigo          | integer                     | not null default nextval('clientes_clnt_codigo_seq'::regclass)
clnt_primeinome      | character varying(50)       |
clnt_ultimonome      | character varying(50)       |
clnt_genero          | character varying(15)       |
clnt_tp_pele         | character varying(50)       |
clnt_dtnasc          | date                        |
clnt_telefone        | character varying(50)       |
clnt_email           | character varying(100)      |
clnt_conhecto        | character varying(50)       |
clnt_obs             | character varying(1000)     |
clnt_status          | character varying(50)       |
clnt_timestamp       | timestamp without time zone |
clnt_autorstat       | character varying(50)       |
clnt_morada          | character varying(200)      |
clnt_codpostal       | character varying(8)        |
clnt_sms             | boolean                     | default false
clnt_generico        | boolean                     | default false
clnt_mes             | integer                     | default 0
clnt_tipo            | character varying(50)       |
clnt_lojahabitual    | integer                     | default 0
clnt_spa             | boolean                     | default false
clnt_telefone2       | character varying(50)       |
clnt_dtcriacao       | date                        |
clnt_timestampaceita | timestamp without time zone |
Indexes:
"CLIENTES_pkey" PRIMARY KEY, btree (clnt_codigo)
"WDIDX_CLIENTES_CLNT_DTNASC" btree (clnt_dtnasc)
"WDIDX_CLIENTES_CLNT_EMAIL" btree (clnt_email)
"WDIDX_CLIENTES_CLNT_GENERICO" btree (clnt_generico)
"WDIDX_CLIENTES_CLNT_PRIMEINOME" btree (clnt_primeinome)
"WDIDX_CLIENTES_CLNT_TELEFONE" btree (clnt_telefone)
"WDIDX_CLIENTES_CLNT_ULTIMONOME" btree (clnt_ultimonome)
"fki_codpostal" btree (clnt_codpostal)
Triggers:
auditoria_clientes AFTER INSERT OR DELETE OR UPDATE ON clientes FOR EACH ROW EXECUTE PROCEDURE auditoria_clientes()

按照注释中的要求添加了表格说明。

0 个答案:

没有答案