/******************************************************************************/
/****              Generated by IBExpert 14/08/2023 09:40:58               ****/
/******************************************************************************/

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



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


CREATE GENERATOR GTBDRE_FINANCEIRA;

CREATE TABLE TBDRE_FINANCEIRA (
    ID               INTEGER NOT NULL,
    DESCRICAO        VARCHAR(50),
    STATUS           INTEGER
);




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

ALTER TABLE TBDRE_FINANCEIRA ADD CONSTRAINT PK_TBDRE_FINANCEIRA PRIMARY KEY (ID);


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



SET TERM ^ ;



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



/* Trigger: TBDRE_FINANCEIRA_B01U */
CREATE OR ALTER TRIGGER TBDRE_FINANCEIRA_B01U FOR TBDRE_FINANCEIRA
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    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
        EXECUTE PROCEDURE SPI_HISTORICO('TBDRE_FINANCEIRA',new.id, HISTORICO);
    END

END
^


/* Trigger: TTBDRE_FINANCEIRA_B01D */
CREATE OR ALTER TRIGGER TTBDRE_FINANCEIRA_B01D FOR TBDRE_FINANCEIRA
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('TBDRE_FINANCEIRA',OLD.ID, HISTORICO);
    END

END
^


/* Trigger: TTBDRE_FINANCEIRA_B01I */
CREATE OR ALTER TRIGGER TTBDRE_FINANCEIRA_B01I FOR TBDRE_FINANCEIRA
ACTIVE BEFORE INSERT POSITION 0
as
begin
  if (new.id is null) then
    new.id = gen_id(gTBDRE_FINANCEIRA,1);
end
^


/* Trigger: TTBDRE_FINANCEIRA_B02I */
CREATE OR ALTER TRIGGER TTBDRE_FINANCEIRA_B02I FOR TBDRE_FINANCEIRA
ACTIVE BEFORE INSERT POSITION 0
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('TBDRE_FINANCEIRA',NEW.ID, HISTORICO);
    END

END
^

SET TERM ; ^



/******************************************************************************/
/****                             Descriptions                             ****/
/******************************************************************************/

COMMENT ON TABLE TBDRE_FINANCEIRA IS 
'tabela da DRE financeira, regras de calculo e destritivos';



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


/* Privileges of users */
GRANT ALL ON TBDRE_FINANCEIRA 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_FINANCEIRA TO RDB$ADMIN;