/******************************************************************************/
/****              Generated by IBExpert 08/08/2022 08:55:59               ****/
/******************************************************************************/

/******************************************************************************/
/****     Following SET SQL DIALECT is just for the Database Comparer      ****/
/******************************************************************************/
SET SQL DIALECT 3;



/******************************************************************************/
/****                                Tables                                ****/
/******************************************************************************/


CREATE GENERATOR GTBDEFEITO_TRANSACAO_STATUS;

CREATE TABLE TBDEFEITO_TRANSACAO_STATUS (
    ID           INTEGER NOT NULL,
    DESCRICAO    VARCHAR(50) DEFAULT '',
    COR          VARCHAR(20) DEFAULT '#FFFFFF',
    STATUS       INTEGER DEFAULT 1
);

/******************************************************************************/
/****                             Primary keys                             ****/
/******************************************************************************/

ALTER TABLE TBDEFEITO_TRANSACAO_STATUS ADD PRIMARY KEY (ID);

/******************************************************************************/
/****                               Triggers                               ****/
/******************************************************************************/

SET TERM ^ ;

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



/* Trigger: TBDEFEITO_TRANSACAO_STATUS_B01I */
CREATE OR ALTER TRIGGER TBDEFEITO_TRANSACAO_STATUS_B01I FOR TBDEFEITO_TRANSACAO_STATUS
ACTIVE BEFORE INSERT POSITION 0
AS
begin

    IF(NEW.ID IS NULL ) THEN
    BEGIN
        NEW.ID = GEN_ID(GTBDEFEITO_TRANSACAO_STATUS,1);
    END
end
^


/* Trigger: TBDEFEITO_TRANSACAO_STATUS_B02I */
CREATE OR ALTER TRIGGER TBDEFEITO_TRANSACAO_STATUS_B06I FOR TBDEFEITO_TRANSACAO_STATUS
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('TBDEFEITO_TRANSACAO_STATUS',NEW.ID, HISTORICO);
    END
END
^

/* Trigger: TTBDEFEITO_TRANSACAO_STATUS_B01U */
CREATE OR ALTER TRIGGER TTBDEFEITO_TRANSACAO_STATUS_B9U FOR TBDEFEITO_TRANSACAO_STATUS
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN

   HISTORICO = '';

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

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

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

   IF (HISTORICO <> '') THEN
    BEGIN
       HISTORICO = ' REGISTRO ALTERADO: '||HISTORICO;
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBDEFEITO_TRANSACAO_STATUS', 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),
       FN_ELIPSE(:HISTORICO, 500));
    END
END
^


/* Trigger: TBDEFEITO_TRANSACAO_STATUS_B02I */
CREATE OR ALTER TRIGGER TBDEFEITO_TRANSACAO_STATUS_B01D FOR TBDEFEITO_TRANSACAO_STATUS
ACTIVE BEFORE DELETE POSITION 0
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('TBDEFEITO_TRANSACAO_STATUS',OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^