/******************************************************************************/
/****              Generated by IBExpert 27/03/2024 14:54:37               ****/
/******************************************************************************/

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


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


CREATE GENERATOR GEN_PROTESTOS_ONLINE_SOLICIT_ID;

CREATE TABLE PROTESTOS_ONLINE_SOLICITACOES (
    COD_PROTESTO_ONLINE_SOLICITACAO  DOM_CODIGO NOT NULL /* DOM_CODIGO = INTEGER */,
    COD_PROTESTO_ONLINE              DOM_CODIGO /* DOM_CODIGO = INTEGER */,
    TIPO_SOLICITACAO                 DOM_CHAR_001 /* DOM_CHAR_001 = CHAR(1) */,
    STATUS                           DOM_CODIGO /* DOM_CODIGO = INTEGER */,
    NOME_ARQUIVO                     DOM_TEXTO_030 /* DOM_TEXTO_030 = VARCHAR(30) */,
    DATA_SOLICITACAO                 DOM_DATA_HORA /* DOM_DATA_HORA = DATE */,
    RESUMO                           DOM_TEXTO_999 /* DOM_TEXTO_999 = VARCHAR(1000) */,
    COD_USUARIO                      DOM_CODIGO /* DOM_CODIGO = INTEGER */,
    DATA_INC                         DOM_DATA_HORA /* DOM_DATA_HORA = DATE */
);



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

ALTER TABLE PROTESTOS_ONLINE_SOLICITACOES ADD CONSTRAINT PK_PROTESTOS_ONLINE_SOLICITACOE PRIMARY KEY (COD_PROTESTO_ONLINE_SOLICITACAO);


/******************************************************************************/
/****                             Foreign keys                             ****/
/******************************************************************************/

ALTER TABLE PROTESTOS_ONLINE_SOLICITACOES ADD CONSTRAINT FK_PROTESTOS_ONLINE_SOLICITACOE FOREIGN KEY (COD_PROTESTO_ONLINE) REFERENCES PROTESTOS_ONLINE (COD_PROTESTO_ONLINE);


/******************************************************************************/
/****                               Indices                                ****/
/******************************************************************************/

CREATE INDEX PROTESTOS_ONLINE_SOLICITAC_IDX1 ON PROTESTOS_ONLINE_SOLICITACOES (TIPO_SOLICITACAO);
CREATE INDEX PROTESTOS_ONLINE_SOLICITAC_IDX2 ON PROTESTOS_ONLINE_SOLICITACOES (NOME_ARQUIVO);
CREATE INDEX PROTESTOS_ONLINE_SOLICITAC_IDX3 ON PROTESTOS_ONLINE_SOLICITACOES (STATUS);


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



SET TERM ^ ;



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



/* Trigger: PROTESTOS_ONLINE_SOLIC_CODIGO */
CREATE OR ALTER TRIGGER PROTESTOS_ONLINE_SOLIC_CODIGO FOR PROTESTOS_ONLINE_SOLICITACOES
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.COD_PROTESTO_ONLINE_SOLICITACAO IS NULL) THEN
    NEW.COD_PROTESTO_ONLINE_SOLICITACAO = GEN_ID(GEN_PROTESTOS_ONLINE_SOLICIT_ID, 1);
END
^

/* Trigger: PROTESTOS_ONLINE_SOLIC_DATA_INC */
CREATE OR ALTER TRIGGER PROTESTOS_ONLINE_SOLIC_DATA_INC FOR PROTESTOS_ONLINE_SOLICITACOES
ACTIVE BEFORE INSERT POSITION 2
AS
BEGIN
  NEW.DATA_INC = CURRENT_TIMESTAMP;
END
^

/* Trigger: PROTESTOS_ONLINE_SOLIC_USUARIO */
CREATE OR ALTER TRIGGER PROTESTOS_ONLINE_SOLIC_USUARIO FOR PROTESTOS_ONLINE_SOLICITACOES
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE I_TEMP INTEGER;
BEGIN
  SELECT COD_USUARIO
  FROM FUNCAO_COD_USUARIO
  INTO :I_TEMP;
  NEW.COD_USUARIO = :I_TEMP;
END
^
SET TERM ; ^



/******************************************************************************/
/****                         Fields descriptions                          ****/
/******************************************************************************/

COMMENT ON COLUMN PROTESTOS_ONLINE_SOLICITACOES.TIPO_SOLICITACAO IS
'C: Confirmação
R: Retorno';

COMMENT ON COLUMN PROTESTOS_ONLINE_SOLICITACOES.STATUS IS
'0 = Incluído
1 = Solicitando
2 = Finalizado
3 = Erro';