CREATE OR ALTER trigger ttbapi_usuario_b02i for tbapi_usuario
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBAPI',NEW.API_ID, HISTORICO);
    END
end;


CREATE OR ALTER trigger ttbapi_usuario_b01u for tbapi_usuario
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF ( OLD.SENHA <> NEW.SENHA) THEN
    BEGIN
        HISTORICO = HISTORICO||' SENHA ALTERADA ';
    END

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

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

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' USUARIO '||NEW.ID|| ' ALTERADO: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBAPI',NEW.API_ID, HISTORICO);
    END
END;

CREATE trigger tbapi_usuario_bd0 for tbapi_usuario
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBAPI',OLD.API_ID, HISTORICO);
    END
end;





