/******************************************************************************/
/****              GENERATED BY IBEXPERT 19/03/2024 16:50:28               ****/
/******************************************************************************/

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



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


CREATE GENERATOR GTBDRE_FIN_ESTABELECIMENTOS;

CREATE TABLE TBDRE_FIN_ESTABELECIMENTOS (
    ID                  INTEGER NOT NULL,
    DRE_FINANCEIRA_ID   INTEGER,
    ESTABELECIMENTO_ID  INTEGER DEFAULT 0
);




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

ALTER TABLE TBDRE_FIN_ESTABELECIMENTOS ADD CONSTRAINT PK_TBDRE_FIN_ESTABELECIMENTOS PRIMARY KEY (ID);


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



SET TERM ^ ;



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


/* TRIGGER: TTBDRE_FIN_ESTABELECIMENTOS_B01D */
CREATE OR ALTER TRIGGER TTBDRE_FIN_ESTAB_B01D FOR TBDRE_FIN_ESTABELECIMENTOS
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' ESTAB: '||COALESCE((SELECT FIRST 1 FN_LPAD(OLD.ESTABELECIMENTO_ID,4,0)||' - '||ESTAB.NOMEFANTASIA FROM TBESTABELECIMENTO ESTAB WHERE ESTAB.CODIGO = OLD.ESTABELECIMENTO_ID),'')||' EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBDRE_FINANCEIRA',OLD.DRE_FINANCEIRA_ID, HISTORICO);
    END

END
^


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


/* TRIGGER: TTBDRE_FIN_ESTABELECIMENTOS_B02I */
CREATE OR ALTER TRIGGER TTBDRE_FIN_ESTAB_B02I FOR TBDRE_FIN_ESTABELECIMENTOS
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' ESTAB: '||COALESCE((SELECT FIRST 1 FN_LPAD(NEW.ESTABELECIMENTO_ID,4,0)||' - '||ESTAB.NOMEFANTASIA FROM TBESTABELECIMENTO ESTAB WHERE ESTAB.CODIGO = NEW.ESTABELECIMENTO_ID),'')||' INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBDRE_FINANCEIRA',NEW.DRE_FINANCEIRA_ID, HISTORICO);
    END

END
^

SET TERM ; ^



/******************************************************************************/
/****                             DESCRIPTIONS                             ****/
/******************************************************************************/

COMMENT ON TABLE TBDRE_FIN_ESTABELECIMENTOS IS 
'TABELA DE ESTABELECIMENTOS DA DRE FINANCEIRA';



/******************************************************************************/
/****                              PRIVILEGES                              ****/
/******************************************************************************/


/* PRIVILEGES OF USERS */
GRANT ALL ON TBDRE_FIN_ESTABELECIMENTOS TO GC;
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;

/* PRIVILEGES OF ROLES */
GRANT ALL ON TBDRE_FIN_ESTABELECIMENTOS TO RDB$ADMIN;