Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: SQL  |  size: 1.39 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. CREATE OR REPLACE FUNCTION check_presentation_conflict()
  2.     RETURNS TRIGGER AS $check_presentation_conflict$
  3.     DECLARE
  4.         overlap_count RECORD;
  5.         old_id BIGINT;
  6.     BEGIN
  7.         IF NEW.presentation_start<NEW.presentation_end THEN
  8.             -- on update, don't throw overlap on the old record being changed
  9.             IF (TG_OP='UPDATE') THEN
  10.                 old_id := OLD.presentation_timeslots_id;
  11.             ELSE
  12.                 old_id := NULL;
  13.             END IF;
  14.  
  15.             -- OVERLAP CHECK
  16.             SELECT COUNT(*) INTO overlap_count FROM presentation_timeslots
  17.                 WHERE
  18.                      (NEW.fairs_id = presentation_timeslots.fairs_id)
  19.                      AND presentation_timeslots.presentation_timeslots_id != old_id
  20.                      AND ((NEW.presentation_start, NEW.presentation_end)
  21.                            OVERLAPS
  22.                            (presentation_timeslots.presentation_start,
  23.                             presentation_timeslots.presentation_end));
  24.                 IF overlap_count.COUNT = 0 THEN
  25.                     RETURN NEW;
  26.                 ELSE
  27.                     RAISE EXCEPTION 'The presentation overlaps with another one.';
  28.                 END IF;
  29.         ELSE
  30.             RAISE EXCEPTION 'The start of presentation must precede the end.';
  31.         END IF;
  32.     END;
  33. $check_presentation_conflict$ LANGUAGE plpgsql;