/******************************************************************************/
/****              GENERATED BY IBEXPERT 15/06/2023 13:55:35               ****/
/******************************************************************************/

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



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


CREATE GENERATOR GTBPEDIDO_VINCULO;

CREATE TABLE TBPEDIDO_VINCULO (
    ID                   INTEGER,
    USUARIO_ID			 INTEGER,
    DATAHORA          	 TIMESTAMP DEFAULT 'NOW'
);




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



SET TERM ^ ;

ALTER TABLE TBPERFIL_PEDIDO_USUARIOS
ADD PERMITE_VINCULAR_PEDIDO VARCHAR(1) CHARACTER SET UTF8 
DEFAULT 0 
COLLATE UTF8^

ALTER TABLE TBPEDIDO
ADD VINCULO_ID INTEGER DEFAULT 0^

CREATE INDEX ITBPEDIDO_25
ON TBPEDIDO (VINCULO_ID)^


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



/* TRIGGER: TTBPEDIDO_VINCULO_B01I */
CREATE OR ALTER TRIGGER TTBPEDIDO_VINCULO_B01I FOR TBPEDIDO_VINCULO
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF(NEW.ID IS NULL) THEN
  BEGIN
     NEW.ID = GEN_ID(GTBPEDIDO_VINCULO,1);
  END
  
  IF(NEW.USUARIO_ID IS NULL) THEN
  BEGIN
     NEW.USUARIO_ID = FN_CURRENT_USER_ID();
  END 
END
^


/* TRIGGER: TTBPEDIDO_VINCULO_B01D */
CREATE OR ALTER TRIGGER TTBPEDIDO_VINCULO_B01D FOR TBPEDIDO_VINCULO
ACTIVE BEFORE DELETE POSITION 0
AS
BEGIN  
  /*REMOVE OS VINCULOS EXISTENTES DOS PEDIDOS*/
  IF(OLD.ID > 0) THEN
  BEGIN
     UPDATE TBPEDIDO PED SET PED.VINCULO_ID = 0 WHERE PED.VINCULO_ID = OLD.ID;
  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;

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