/******************************************************************************/
/****              GENERATED BY IBEXPERT 26/01/2023 15:06:00               ****/
/******************************************************************************/

/******************************************************************************/
/****     FOLLOWING SET SQL DIALECT IS JUST FOR THE DATABASE COMPARER      ****/
/******************************************************************************/
SET SQL DIALECT 3;



/******************************************************************************/
/****                                TABLES                                ****/
/******************************************************************************/


CREATE GENERATOR GTBPERFIL_PEDIDO_COR;

CREATE TABLE TBPERFIL_PEDIDO_COR (
    ID      INTEGER NOT NULL,
    COR_ID  INTEGER,
    PERFIL_PEDIDO  VARCHAR(10)
);

/******************************************************************************/
/****                             PRIMARY KEYS                             ****/
/******************************************************************************/

ALTER TABLE TBPERFIL_PEDIDO_COR ADD CONSTRAINT PK_TBPERFIL_PEDIDO_COR PRIMARY KEY (ID);


/******************************************************************************/
/****                               TRIGGERS                               ****/
/******************************************************************************/



SET TERM ^ ;



/******************************************************************************/
/****                         TRIGGERS FOR TABLES                          ****/
/******************************************************************************/



/* TRIGGER: TTBPERFIL_PEDIDO_COR_B01D */
CREATE OR ALTER TRIGGER TTBPERFIL_PEDIDO_COR_B01D FOR TBPERFIL_PEDIDO_COR
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' COR EXCLUIDA: '||OLD.COR_ID||' - '||(SELECT FIRST 1 M.DESCRICAO FROM TBMODELO M WHERE M.CODIGO = OLD.COR_ID);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL',(SELECT FIRST 1 P.CONTROLE FROM TBPERFIL P WHERE P.ID = OLD.PERFIL_PEDIDO AND P.TABELA = 'PED'), HISTORICO);
    END
END
^


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


/* TRIGGER: TTBPERFIL_PEDIDO_COR_B02I */
CREATE OR ALTER TRIGGER TTBPERFIL_PEDIDO_COR_B01D FOR TBPERFIL_PEDIDO_COR
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' COR EXCLUIDA: '||OLD.COR_ID||' - '||(SELECT FIRST 1 M.DESCRICAO FROM TBCOR M WHERE M.CODIGO = OLD.COR_ID);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL',(SELECT FIRST 1 P.CONTROLE FROM TBPERFIL P WHERE P.ID = OLD.PERFIL_PEDIDO AND P.TABELA = 'PED'), HISTORICO);
    END
END
^

SET TERM ; ^