我有一个Postgres函数,我需要检查特定值是否在时间戳数组中。这是功能:
CREATE OR REPLACE FUNCTION public.get_appointments(
for_business_id INTEGER,
range_start DATE,
range_end DATE,
for_staff_id INTEGER
)
RETURNS SETOF appointment
LANGUAGE plpgsql STABLE
AS $function$
DECLARE
appointment appointment;
recurrence TIMESTAMP;
appointment_length INTERVAL;
parent_id UUID;
BEGIN
FOR appointment IN
SELECT *
FROM appointment
WHERE business_id = for_business_id
AND (
recurrence_pattern IS NOT NULL
OR (
recurrence_pattern IS NULL
AND starts_at BETWEEN range_start AND range_end
)
)
LOOP
IF appointment.recurrence_pattern IS NULL THEN
RETURN NEXT appointment;
CONTINUE;
END IF;
appointment_length := appointment.ends_at - appointment.starts_at;
parent_id := appointment.id;
FOR recurrence IN
SELECT *
FROM generate_recurrences(
appointment.recurrence_pattern,
appointment.starts_at,
range_start,
range_end
)
LOOP
EXIT WHEN recurrence::date > range_end;
-- THIS IS THE LINE IN QUESTION
CONTINUE WHEN recurrence::date < range_start OR recurrence = ANY(appointment.recurrence_exceptions);
appointment.id := uuid_generate_v5(uuid_nil(), parent_id::varchar || recurrence::varchar);
appointment.parent_id := parent_id;
appointment.starts_at := recurrence;
appointment.ends_at := recurrence + appointment_length;
appointment.recurrence_pattern := appointment.recurrence_pattern;
appointment.recurrence_exceptions := NULL;
appointment.is_recurrence := true;
RETURN NEXT appointment;
END LOOP;
END LOOP;
RETURN;
END;
$function$;
您会在第二个LOOP
语句之后看到,有一个CONTINUE
语句。我希望跳过循环的迭代,如果recurrence
(这是时间戳,但是text
格式)变量超出范围或者如果它被列为appointment
的一部分'recurrence_exceptions
数组。 recurrence_exceptions
数组包含timestamp
s。
这个想法是,如果约会被列为例外,则不会返回。不幸的是,无论我做什么,似乎ANY
运算符都没有按预期工作。为了测试这一点,我从recurrence_exception
数组中取出了一个值并将CONTINUE
语句更改为:
CONTINUE WHEN recurrence::date < range_start OR recurrence = '2016-09-20 18:07:26';
这并没有像预期的那样恢复那种复发。
我是否正确使用了这个?
谢谢!