CREATE TABLE TBREPLICADOR_ACAO (
    ID INTEGER NOT NULL,
    TABELA VARCHAR(100),
    TABELA_ID INTEGER,
    FLAG INTEGER,
    DATA_HORA TIMESTAMP);

ALTER TABLE TBREPLICADOR_ACAO
ADD CONSTRAINT PK_TBREPLICADOR_ACAO
PRIMARY KEY (ID);

ALTER TABLE TBREPLICADOR_ACAO
    ADD FLAG_INSERT INTEGER,
    ADD FLAG_DELETE INTEGER,
    ADD FLAG_UPDATE INTEGER;

CREATE SEQUENCE GTBREPLICADOR_ACAO;

create trigger tbreplicador_acao_bi for tbreplicador_acao
active before insert position 0
as
begin
  if (new.id is null) then
    new.id = gen_id(gtbreplicador_acao,1);
end;

CREATE OR ALTER trigger tr_tnusuario for tnusuario
active before insert or update or delete position 0
AS
declare variable flag_insert integer;
declare variable flag_delete integer;
declare variable flag_update integer;
begin

    flag_insert =0;
    flag_delete =0;
    flag_update =0;

  if(inserting) then
  begin
    flag_insert = 1;
  end

  if(deleting) then
  begin
    flag_delete = 1;
  end

  if(updating) then
  begin
    flag_update = 1;
  end

  if(new.replicar = 0)then
  begin
    insert into tbreplicador_acao (tabela,tabela_id,data_hora,flag_insert,flag_delete,flag_update)
    values (
        'TNUSUARIO',
        new.id,
        current_timestamp,
        :flag_insert,
        :flag_delete,
        :flag_update
        );
  end

end;

