wiki:Servicos/Z-Push/Triggers

Version 4 (modified by viani, 3 years ago) (diff)

--

Crie as seguintes triggers do Z-Push no banco de dados do Expresso

CREATE OR REPLACE FUNCTION phpgw_cal_zpush() RETURNS TRIGGER AS $phpgw_cal_zpush$
    BEGIN
        IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
            IF (NEW.reference != 0) THEN
                UPDATE phpgw_cal SET last_update = (date_part('epoch'::text, ('now'::text)::timestamp(3) with time zone) * (1000)::double precision)::bigint WHERE cal_id = NEW.reference;
            END IF;
        END IF;
        IF (TG_OP = 'DELETE') THEN
            IF (OLD.reference != 0) THEN
                UPDATE phpgw_cal SET last_update = (date_part('epoch'::text, ('now'::text)::timestamp(3) with time zone) * (1000)::double precision)::bigint WHERE cal_id = OLD.reference;
            END IF;
        END IF;
        RETURN NULL;
    END;
$phpgw_cal_zpush$ language plpgsql;

DROP TRIGGER phpgw_cal_zpush ON phpgw_cal CASCADE;

CREATE TRIGGER phpgw_cal_zpush AFTER INSERT OR UPDATE OR DELETE ON phpgw_cal
    FOR EACH ROW EXECUTE PROCEDURE phpgw_cal_zpush();




CREATE OR REPLACE FUNCTION phpgw_cal_repeats_zpush() RETURNS TRIGGER AS $phpgw_cal_repeats_zpush$
    BEGIN
        IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
            IF EXISTS(SELECT 1 FROM phpgw_cal WHERE cal_id = NEW.cal_id) THEN
                UPDATE phpgw_cal SET last_update = (date_part('epoch'::text, ('now'::text)::timestamp(3) with time zone) * (1000)::double precision)::bigint WHERE cal_id = NEW.cal_id;
            END IF;
        END IF;
        IF (TG_OP = 'DELETE') THEN
            IF EXISTS(SELECT 1 FROM phpgw_cal WHERE cal_id = OLD.cal_id) THEN
                UPDATE phpgw_cal SET last_update = (date_part('epoch'::text, ('now'::text)::timestamp(3) with time zone) * (1000)::double precision)::bigint WHERE cal_id = OLD.cal_id;
            END IF;
        END IF;
        RETURN NULL;
    END;
$phpgw_cal_repeats_zpush$ language plpgsql;

DROP TRIGGER phpgw_cal_repeats_zpush ON phpgw_cal_repeats CASCADE;

CREATE TRIGGER phpgw_cal_repeats_zpush AFTER INSERT OR UPDATE OR DELETE ON phpgw_cal_repeats
    FOR EACH ROW EXECUTE PROCEDURE phpgw_cal_repeats_zpush();

GRANT EXECUTE ON FUNCTION phpgw_cal_zpush() TO zpush_role;
GRANT EXECUTE ON FUNCTION phpgw_cal_repeats_zpush() TO zpush_role;