-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.sql
79 lines (66 loc) · 1.92 KB
/
triggers.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
CREATE OR REPLACE FUNCTION get_size_parking()
RETURNS int AS
$$
SELECT 5;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION process_buffer(b buffer)
RETURNS VOID AS
$$
DECLARE
_car_id text;
_last_change timestamptz;
_parked boolean;
BEGIN
SELECT car_id, parked, last_change FROM hub_parking WHERE car_id = ANY(b.proposed_car_ids) INTO _car_id, _parked, _last_change;
IF NOT FOUND THEN
--car probably isn't hub car
RAISE DEBUG 'not found ts: %, filename: %', b.ts, b.filename;
RETURN;
END IF;
-- check if last change is more then 15s
IF _last_change IS NULL OR _last_change +'15s'::interval < clock_timestamp() THEN
RAISE DEBUG 'found car_id: %, parked: %, updating to parked: %', _car_id, _parked, NOT _parked;
UPDATE hub_parking SET parked = NOT parked, last_change = b.ts WHERE car_id = _car_id;
END IF;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION buffer_insert()
RETURNS TRIGGER AS
$$
BEGIN
PERFORM process_buffer(NEW);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS buffer_insert ON buffer;
CREATE TRIGGER buffer_insert
AFTER INSERT
ON buffer
FOR EACH ROW
EXECUTE PROCEDURE buffer_insert();
CREATE OR REPLACE FUNCTION hub_parking_insert_update_delete()
RETURNS TRIGGER AS
$$
DECLARE
_full boolean;
BEGIN
SELECT count(*) >= get_size_parking() FROM hub_parking WHERE parked INTO _full;
--let's notify semafor server
IF _full THEN
PERFORM pg_notify('semafor','obsazeno');
ELSE
PERFORM pg_notify('semafor','volno');
END IF;
IF TG_OP IN ('INSERT','UPDATE') THEN
RETURN NEW;
ELSE
RETURN OLD;
END IF;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS hub_parking_insert_update_delete ON hub_parking;
CREATE TRIGGER hub_parking_insert_update_delete
AFTER INSERT OR UPDATE OF parked OR DELETE
ON hub_parking
FOR EACH ROW
EXECUTE PROCEDURE hub_parking_insert_update_delete();