SET TERM ^ ;



/******************************************************************************/
/****                         Triggers for tables                          ****/
/******************************************************************************/



/* Trigger: TTBIMAGENS_LOGIN_B01D */
CREATE OR ALTER TRIGGER TTBIMAGENS_LOGIN_B01D FOR TBIMAGENS_LOGIN
ACTIVE BEFORE DELETE POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    
    HISTORICO = '';

    IF (OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBIMAGENS_LOGIN', OLD.ID, HISTORICO);
    END
END
^


/* Trigger: TTBIMAGENS_LOGIN_B01I */
CREATE OR ALTER TRIGGER TTBIMAGENS_LOGIN_B01I FOR TBIMAGENS_LOGIN
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
    IF (NEW.ID IS NULL) THEN
        NEW.ID = GEN_ID(GTBIMAGENS_LOGIN, 1);
END
^


/* Trigger: TTBIMAGENS_LOGIN_B01U */
CREATE OR ALTER TRIGGER TTBIMAGENS_LOGIN_B01U FOR TBIMAGENS_LOGIN
ACTIVE BEFORE UPDATE POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN

    HISTORICO = '';

    IF (OLD.DESCRICAO IS DISTINCT FROM NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DESC:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF (OLD.CODIGO_GRADIENTE_A IS DISTINCT FROM NEW.CODIGO_GRADIENTE_A) THEN
    BEGIN
        HISTORICO = HISTORICO||' GRADIENTE A:'||OLD.CODIGO_GRADIENTE_A||'->'||NEW.CODIGO_GRADIENTE_A;
    END

    IF (OLD.CODIGO_GRADIENTE_B IS DISTINCT FROM NEW.CODIGO_GRADIENTE_B) THEN
    BEGIN
        HISTORICO = HISTORICO||' GRADIENTE B:'||OLD.CODIGO_GRADIENTE_B||'->'||NEW.CODIGO_GRADIENTE_B;
    END

    IF (OLD.STATUS IS DISTINCT FROM NEW.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||' STATUS:'||OLD.STATUS||'->'||NEW.STATUS;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBIMAGENS_LOGIN', OLD.ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^


/* Trigger: TTBIMAGENS_LOGIN_B02I */
CREATE OR ALTER TRIGGER TTBIMAGENS_LOGIN_B02I FOR TBIMAGENS_LOGIN
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    
    HISTORICO = '';

    IF (NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBIMAGENS_LOGIN', NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^