From 093ae242901764a9534948eee73988779e33fb61 Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Sat, 20 Jun 2009 10:46:20 +1200 Subject: [PATCH] Push the rrule_event_overlaps function into the 8.1 compatibility functions. --- dba/rrule_functions-8.1.sql | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/dba/rrule_functions-8.1.sql b/dba/rrule_functions-8.1.sql index ce537b0a..eb2f309e 100644 --- a/dba/rrule_functions-8.1.sql +++ b/dba/rrule_functions-8.1.sql @@ -652,3 +652,53 @@ BEGIN END LOOP; END; $$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; + + +------------------------------------------------------------------------------------------------------ +-- In most cases we just want to know if there *is* an event overlapping the range, so we have a +-- specific function for that. Note that this is *not* strict, and can be called with NULLs. +------------------------------------------------------------------------------------------------------ +CREATE or REPLACE FUNCTION rrule_event_overlaps( TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE, TEXT, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH TIME ZONE ) + RETURNS BOOLEAN AS $$ +DECLARE + dtstart ALIAS FOR $1; + dtend ALIAS FOR $2; + repeatrule ALIAS FOR $3; + in_mindate ALIAS FOR $4; + in_maxdate ALIAS FOR $5; + base_date TIMESTAMP WITH TIME ZONE; + mindate TIMESTAMP WITH TIME ZONE; + maxdate TIMESTAMP WITH TIME ZONE; +BEGIN + + IF dtstart IS NULL THEN + RETURN NULL; + END IF; + IF dtend IS NULL THEN + base_date := dtstart; + ELSE + base_date := dtend; + END IF; + + IF in_mindate IS NULL THEN + mindate := current_date - '10 years'::interval; + ELSE + mindate := in_mindate; + END IF; + + IF in_maxdate IS NULL THEN + maxdate := current_date + '10 years'::interval; + ELSE + -- If we add the duration onto the event, then an overlap occurs if dtend <= increased end of range. + maxdate := in_maxdate + (base_date - dtstart); + END IF; + + IF repeatrule IS NULL THEN + RETURN (dtstart <= maxdate AND base_date >= mindate); + END IF; + + SELECT d INTO mindate FROM rrule_event_instances_range( base_date, repeatrule, mindate, maxdate, 60 ) d LIMIT 1; + RETURN FOUND; + +END; +$$ LANGUAGE 'plpgsql' IMMUTABLE;