/******************************************************************************/
/****              Generated by IBExpert 01/03/2024 11:15:56               ****/
/******************************************************************************/

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



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


CREATE GENERATOR GTBPERFIL_DATA_PROGRAMADA;

CREATE TABLE TBPERFIL_DATA_PROGRAMADA (
    ID                     INTEGER NOT NULL,
    DATA_PROGRAMADA        DATE,
    PERFIL                 VARCHAR(10),
    DATAHORA               TIMESTAMP DEFAULT 'NOW'
);




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

ALTER TABLE TBPERFIL_DATA_PROGRAMADA ADD CONSTRAINT PK_TBPERFIL_DATA_PROGRAMADA PRIMARY KEY (ID);


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



SET TERM ^ ;



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



/* Trigger: TBPERFIL_DATA_PROG_B02I */
CREATE OR ALTER TRIGGER TTBPERFIL_DATA_PROG_B02I FOR TBPERFIL_DATA_PROGRAMADA
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0 ) THEN
    BEGIN
        HISTORICO = HISTORICO||' DATA PROGRAMADA '||FORMATDATE(NEW.DATA_PROGRAMADA)||' INCLUIDA.';
    END

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


/* Trigger: TTBPERFIL_CONDICAO_PAG_B01D */
CREATE OR ALTER TRIGGER TTBPERFIL_DATA_PROG_B01D FOR TBPERFIL_DATA_PROGRAMADA
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0 ) THEN
    BEGIN
        HISTORICO = HISTORICO||' DATA PROGRAMADA '||FORMATDATE(OLD.DATA_PROGRAMADA)||' EXCLUIDA.';
    END

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


/* Trigger: TTBPERFIL_CONDICAO_PAG_B01I */
CREATE OR ALTER TRIGGER TTBPERFIL_DATA_PROG_B01I FOR TBPERFIL_DATA_PROGRAMADA
ACTIVE BEFORE INSERT POSITION 0
as
begin
  IF (NEW.ID IS NULL) THEN
  BEGIN
    NEW.ID = GEN_ID(GTBPERFIL_DATA_PROGRAMADA,1);
  END
END
^

SET TERM ; ^

/******************************************************************************/
/****                              Privileges                              ****/
/******************************************************************************/


/* Privileges of users */
GRANT SELECT ON MON$ATTACHMENTS TO PUBLIC;
GRANT SELECT ON MON$CALL_STACK TO PUBLIC;
GRANT SELECT ON MON$CONTEXT_VARIABLES TO PUBLIC;
GRANT SELECT ON MON$DATABASE TO PUBLIC;
GRANT SELECT ON MON$IO_STATS TO PUBLIC;
GRANT SELECT ON MON$MEMORY_USAGE TO PUBLIC;
GRANT SELECT ON MON$RECORD_STATS TO PUBLIC;
GRANT SELECT ON MON$STATEMENTS TO PUBLIC;
GRANT SELECT ON MON$TABLE_STATS TO PUBLIC;
GRANT SELECT ON MON$TRANSACTIONS TO PUBLIC;