/***************************************************/
/***************************************************/
/**** TBAVALIACAO_DES_RESP_FATOR ****/

ALTER TABLE TBAVALIACAO_DES_RESP_FATOR
    ADD AVALIACAO_DES_FATOR_ID INTEGER;

SET TERM ^ ;

CREATE OR ALTER trigger ttbavaliacao_des_resp_fat_b1u for tbavaliacao_des_resp_fator
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.AVALIACAO_DES_RESPOSTA_ID <> NEW.AVALIACAO_DES_RESPOSTA_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' AV:'||OLD.AVALIACAO_DES_RESPOSTA_ID||'->'||NEW.AVALIACAO_DES_RESPOSTA_ID;
    END

    IF (OLD.TITULO <> NEW.TITULO) THEN
    BEGIN
        HISTORICO = HISTORICO||' TIT:'||OLD.TITULO||'->'||NEW.TITULO;
    END

    IF (OLD.PONTO <> NEW.PONTO) THEN
    BEGIN
        HISTORICO = HISTORICO||' FATOR '||OLD.titulo||' - PONTOS:'||OLD.PONTO||'->'||NEW.PONTO;
    END

    IF (OLD.AVALIACAO_DES_RESP_FAT_TIPO_ID <> NEW.AVALIACAO_DES_RESP_FAT_TIPO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' TIPO:'||OLD.AVALIACAO_DES_RESP_FAT_TIPO_ID||'->'||NEW.AVALIACAO_DES_RESP_FAT_TIPO_ID;
    END

    IF (OLD.AVALIACAO_DES_FATOR_ID <> NEW.AVALIACAO_DES_FATOR_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' FATOR:'||OLD.AVALIACAO_DES_FATOR_ID||'->'||NEW.AVALIACAO_DES_FATOR_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBAVALIACAO_DES_RESPOSTA', NEW.avaliacao_des_resposta_id,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
    END
END
^

SET TERM ; ^

/***************************************************/
/***************************************************/
/**** TBAVALIACAO_DES_RESP_FORMACAO ****/

ALTER TABLE TBAVALIACAO_DES_RESP_FORMACAO
    ADD AVALIACAO_DES_FORMACAO_ID INTEGER;

SET TERM ^ ;

CREATE OR ALTER trigger ttbavaliacao_des_resp_form_b1u for tbavaliacao_des_resp_formacao
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.AVALIACAO_DES_RESPOSTA_ID <> NEW.AVALIACAO_DES_RESPOSTA_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' AV:'||OLD.AVALIACAO_DES_RESPOSTA_ID||'->'||NEW.AVALIACAO_DES_RESPOSTA_ID;
    END

    IF (OLD.PONTO <> NEW.PONTO) THEN
    BEGIN
        HISTORICO = HISTORICO||' PONTO:'||OLD.PONTO||'->'||NEW.PONTO;
    END

    IF (OLD.AVALIACAO_DES_FORMACAO_ID <> NEW.AVALIACAO_DES_FORMACAO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' FORMACAO:'||OLD.AVALIACAO_DES_FORMACAO_ID||'->'||NEW.AVALIACAO_DES_FORMACAO_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBAVALIACAO_DES_RESP_FORMACAO', OLD.ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
    END
END
^

SET TERM ; ^

/***************************************************/
/***************************************************/
/**** TBAVALIACAO_DES_RESP_FAT_TIPO ****/

ALTER TABLE TBAVALIACAO_DES_RESP_FAT_TIPO
    ADD AVALIACAO_DES_TIPO_ID INTEGER;

SET TERM ^ ;

CREATE OR ALTER trigger ttbavaliac_des_resp_fat_tip_b1u for tbavaliacao_des_resp_fat_tipo
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.AVALIACAO_DES_RESPOSTA_ID <> NEW.AVALIACAO_DES_RESPOSTA_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' AV:'||OLD.AVALIACAO_DES_RESPOSTA_ID||'->'||NEW.AVALIACAO_DES_RESPOSTA_ID;
    END

    IF (OLD.TITULO <> NEW.TITULO) THEN
    BEGIN
        HISTORICO = HISTORICO||' TIT:'||OLD.TITULO||'->'||NEW.TITULO;
    END

    IF (OLD.AVALIACAO_DES_TIPO_ID <> NEW.AVALIACAO_DES_TIPO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' TIPO:'||OLD.AVALIACAO_DES_TIPO_ID||'->'||NEW.AVALIACAO_DES_TIPO_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBAVALIACAO_DES_RESP_FAT_TIPO', OLD.ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
    END
END
^

SET TERM ; ^

/***************************************************/
/***************************************************/
/**** TBAVALIACAO_DES_RESP_RESUMO ****/

ALTER TABLE TBAVALIACAO_DES_RESP_RESUMO
    ADD AVALIACAO_DES_RESUMO_ID INTEGER;

SET TERM ^ ;

CREATE OR ALTER trigger ttbavaliacao_des_resp_res_b1u for tbavaliacao_des_resp_resumo
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.AVALIACAO_DES_RESPOSTA_ID <> NEW.AVALIACAO_DES_RESPOSTA_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' AV:'||OLD.AVALIACAO_DES_RESPOSTA_ID||'->'||NEW.AVALIACAO_DES_RESPOSTA_ID;
    END

    IF (OLD.PESO <> NEW.PESO) THEN
    BEGIN
        HISTORICO = HISTORICO||' PESO:'||OLD.PESO||'->'||NEW.PESO;
    END

    IF (OLD.PONTUACAO_GERAL <> NEW.PONTUACAO_GERAL) THEN
    BEGIN
        HISTORICO = HISTORICO||' PT GRL:'||OLD.PONTUACAO_GERAL||'->'||NEW.PONTUACAO_GERAL;
    END

    IF (OLD.RESULTADO <> NEW.RESULTADO) THEN
    BEGIN
        HISTORICO = HISTORICO||' RESULT:'||OLD.RESULTADO||'->'||NEW.RESULTADO;
    END

    IF (OLD.AVALIACAO_DES_RESUMO_ID <> NEW.AVALIACAO_DES_RESUMO_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||' RESUMO:'||OLD.AVALIACAO_DES_RESUMO_ID||'->'||NEW.AVALIACAO_DES_RESUMO_ID;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBAVALIACAO_DES_RESP_RESUMO', OLD.ID,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
    END
END
^

SET TERM ; ^