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


CREATE GENERATOR GTBPAINEL_CLIENTE;

CREATE TABLE TBPAINEL_CLIENTE (
    ID         INTEGER NOT NULL,
    DESCRICAO  BLOB SUB_TYPE 1 SEGMENT SIZE 80
);




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

ALTER TABLE TBPAINEL_CLIENTE ADD CONSTRAINT PK_NEW_TABLE PRIMARY KEY (ID);


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



SET TERM ^ ;



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



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


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


/* Trigger: TTBPAINEL_CLIENTE_B01U */
CREATE OR ALTER TRIGGER TTBPAINEL_CLIENTE_B01U FOR TBPAINEL_CLIENTE
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(5000);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBPAINEL_CLIENTE', OLD.ID, FN_ELIPSE(HISTORICO, 500));
    END
END
^


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

SET TERM ; ^


