/******************************************************************************/
/****              Generated by IBExpert 21/01/2022 11:49:14               ****/
/******************************************************************************/

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



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


CREATE GENERATOR GTBRELATORIO_NECESSIDADE_CONF;

CREATE TABLE TBRELATORIO_NECESSIDADE_CONF(
    ID              INTEGER NOT NULL,
    DESCRICAO       VARCHAR(50),
    TIPO            VARCHAR(1) DEFAULT 0,
    DECIMAIS        INTEGER DEFAULT 0,
    PREFIXO         VARCHAR(10),
    SUFIXO          VARCHAR(10),
    ORDEM           INTEGER,
    FORMULA         BLOB SUB_TYPE 1 SEGMENT SIZE 80
);




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

ALTER TABLE TBRELATORIO_NECESSIDADE_CONF ADD CONSTRAINT PK_TBRELATORIO_NECESSIDADE_CONF PRIMARY KEY (ID);


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



SET TERM ^ ;



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



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

SET TERM ; ^

ALTER TABLE TBRELATORIO_NECESSIDADE_CONF
ADD STATUS VARCHAR(1) CHARACTER SET UTF8 
DEFAULT 0 
COLLATE UTF8;

ALTER TABLE TBRELATORIO_NECESSIDADE_CONF
ADD TOTALIZADOR VARCHAR(1) CHARACTER SET UTF8 
DEFAULT 0 
COLLATE UTF8;

CREATE OR ALTER TRIGGER TTBRELAT_NECESSIDADE_CONF_B01D FOR TBRELATORIO_NECESSIDADE_CONF
ACTIVE BEFORE DELETE POSITION 1
AS
declare variable HISTORICO VARCHAR(500);
begin
  HISTORICO = '';
  IF(OLD.ID > 0)THEN
  BEGIN
     historico = 'REGISTRO EXCLUIDO';
     EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_NECESSIDADE_CONF',OLD.ID,:historico);
  END
end;


CREATE OR ALTER TRIGGER TTBRELAT_NECESSIDADE_CONF_B02I FOR TBRELATORIO_NECESSIDADE_CONF
ACTIVE BEFORE INSERT POSITION 2
AS
declare variable HISTORICO VARCHAR(500);
begin
  HISTORICO = '';
  IF(NEW.ID > 0)THEN
  BEGIN
     historico = 'REGISTRO INCLUIDO';
     EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_NECESSIDADE_CONF',NEW.ID,:historico);
  END
end;

ALTER TABLE TBRELATORIO_NECESSIDADE_CONF
ADD RELATORIO VARCHAR(10) CHARACTER SET utf8 
DEFAULT 'GCWEB22680' 
COLLATE UTF8;


ALTER TABLE TBRELATORIO_NECESSIDADE_CONF
ADD COR VARCHAR(10) CHARACTER SET utf8 
DEFAULT '#FFFFFF' 
COLLATE UTF8;

CREATE OR ALTER TRIGGER TTBRELAT_NECESSIDADE_CONF_B01U FOR TBRELATORIO_NECESSIDADE_CONF
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(1000);
BEGIN

   HISTORICO = '';

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

   IF (OLD.TIPO IS DISTINCT FROM NEW.TIPO) THEN
       BEGIN
       HISTORICO = HISTORICO||' TIPO:'||COALESCE(OLD.TIPO, 0)||'->'||COALESCE(NEW.TIPO, 0);
       END

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

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

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

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

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

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

   IF (OLD.COR IS DISTINCT FROM NEW.COR) THEN
       BEGIN
       HISTORICO = HISTORICO||' COR:'||COALESCE(OLD.COR, '#FFFFFF')||'->'||COALESCE(NEW.COR, '#FFFFFF');
       END

   if (HISTORICO <> '') Then
       Begin
        EXECUTE PROCEDURE SPI_HISTORICO('TBRELATORIO_NECESSIDADE_CONF',OLD.ID,FN_ELIPSES(:Historico,500));
       End
end;
