Advertisement
DrMGC

Untitled

Dec 28th, 2020
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. CREATE OR REPLACE FUNCTION f_route_fix_arrival_departure() RETURNS TRIGGER AS $f_route_fix_arrival_departure$
  3. DECLARE
  4.     old_arrival_id INTEGER;
  5.     old_departure_id INTEGER;
  6.     new_arrival_id INTEGER;
  7.     new_departure_id INTEGER;
  8. BEGIN
  9.     IF TG_OP = 'DELETE' THEN
  10.         IF EXISTS (
  11.             SELECT
  12.                 r.arrival_station_id
  13.             FROM
  14.                 route.route r
  15.             WHERE r.id = OLD.route_id
  16.                 AND r.arrival_station_id = OLD.station_id
  17.         ) THEN
  18.             UPDATE
  19.                 route.route r
  20.             SET
  21.                 r.arrival_station_id = (
  22.                     SELECT s.station_id
  23.                     FROM route.stage s
  24.                     WHERE s.route_id  = r.id
  25.                     ORDER BY s.arrival ASC
  26.                     LIMIT 1
  27.                 )
  28.             WHERE
  29.                 r.id = OLD.ROUTE_ID;
  30.         END IF;
  31.  
  32.  
  33.         IF EXISTS (
  34.             SELECT
  35.                 r.departure_station_id
  36.             FROM
  37.                 route.route r
  38.             WHERE r.id = OLD.route_id
  39.                 AND r.departure_station_id = OLD.station_id
  40.         ) THEN
  41.             UPDATE
  42.                 route.route r
  43.             SET
  44.                 r.departure_station_id = (
  45.                     SELECT s.station_id
  46.                     FROM route.stage s
  47.                     WHERE s.route_id  = r.id
  48.                     ORDER BY s.departure DESC
  49.                     LIMIT 1
  50.                 )
  51.             WHERE
  52.                 r.id = OLD.ROUTE_ID;
  53.         END IF;
  54.     ELSE
  55.         SELECT
  56.             r.arrival_station_id, r.departure_station_id
  57.         INTO
  58.             old_arrival_id, old_departure_id
  59.         FROM
  60.             route.route r
  61.         WHERE
  62.             r.id = NEW.route_id;
  63.    
  64.         IF NEW.arrival < (
  65.             SELECT s.arrival
  66.             FROM route.stage s
  67.             WHERE s.route_id = NEW.route_id
  68.                 AND s.station_id = NEW.station_id
  69.         ) THEN
  70.             new_arrival_id := NEW.station_id;
  71.         ELSE
  72.             new_arrival_id := old_arrival_id;
  73.         END IF;
  74.    
  75.    
  76.         IF NEW.departure > (
  77.             SELECT s.departure
  78.             FROM route.stage s
  79.             WHERE s.route_id = NEW.route_id
  80.                 AND s.station_id = NEW.station_id
  81.         ) THEN
  82.             new_departure_id := NEW.station_id;
  83.         ELSE
  84.             new_departure_id := old_departure_id;
  85.         END IF;
  86.     END IF;
  87.  
  88.     RETURN NULL;
  89. END;
  90. $f_route_fix_arrival_departure$ LANGUAGE plpgsql;
  91.  
  92. DROP TRIGGER IF EXISTS tr_route_fix_arrival_departure ON route.route;
  93. CREATE TRIGGER tr_route_fix_arrival_departure AFTER INSERT OR UPDATE OR DELETE ON route.route
  94.     FOR EACH ROW EXECUTE FUNCTION f_route_fix_arrival_departure();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement