簡體   English   中英

Postgres 觸發器檢查與現有記錄的日期重疊

[英]Postgres trigger to check date overlap with already existing records

我有一個表,有 2 列,開始日期和結束日期。

  • 開始日期為必填項
  • 結束日期是可選的(所以這是一個基本上永遠不會結束的時期)

我正在創建一個觸發器,以確保沒有記錄與其他記錄重疊,到目前為止我做了這個

DROP TABLE IF EXISTS public.working_hours;

CREATE TABLE public.working_hours (
    id serial NOT NULL,
    date_start date NOT NULL,
    date_end date NULL
);

-- Insert some valid data
INSERT INTO public.working_hours (date_start,date_end) VALUES ('2020-12-10'::date,'2020-12-20'::date);

-- Setup trigger to check
create or REPLACE FUNCTION check_date_overlap()
RETURNS trigger as
$body$
declare
    temprow record;
    a date;
    b date;
    c date;
    d date;
begin
    c := new.date_start;
    d := new.date_end;

    if d is null  -- End date is optional
    then 
        d := '9999-12-31'::date;
    end if;

    for temprow in
        SELECT *
        FROM public.working_hours
        WHERE id != new.id -- Avoid the record itself which is under update
        ORDER BY date_start
    loop    
        a := temprow.date_start;
        b := temprow.date_end;

        if b is null  -- End date is optional
        then 
            b := '9999-12-31'::date;
        end if;
        
        /*
         * temprow:     A-------------B
         * new:             C----D
         */ 
        if a < c and b > d
        then
            RAISE EXCEPTION 'case A: record is overlapping with record %', temprow.id;
        end if;
    
        /*
         * temprow:         A----B
         * new:         C-------------D
         */ 
        if a > c and b < d
        then
            RAISE EXCEPTION 'case B: record is overlapping with record %', temprow.id;
        end if;
        
        /*
         * tn:  A-------------B
         * new:       C-------------D
         */ 
        if a < c and c < b and b < d
        then
            RAISE EXCEPTION 'case C: record is overlapping with record %', temprow.id;  
        end if;
            
        /*
         * temprow:           A-------------B
         * new:       C-------------D
         */ 
        if c < a and a < d and d < b
        then
            RAISE EXCEPTION 'case D: record is overlapping with record %', temprow.id;  
        end if;
    
        /*
         * temprow:                   A-------------B
         * new:         C-------------D
         */
        if c < a and a = d and d < b
        then
            RAISE EXCEPTION 'case E: record is overlapping with record %', temprow.id;  
        end if;
    
        /*
         * temprow:     A-------------B
         * new:                       C-------------D
         */ 
        if a < c and b = c and b < d
        then
            RAISE EXCEPTION 'case F: record is overlapping with record %', temprow.id;  
        end if;
        
    end loop;
   
    RETURN NEW;
end
$body$
LANGUAGE plpgsql;



drop trigger if exists on_check_date_overlap on public.working_hours;


create trigger on_check_date_overlap
    before update or insert
    on public.working_hours
    for each row
    execute procedure check_date_overlap();
    


-- Test case A fail
-- INSERT INTO public.working_hours (date_start,date_end) VALUES ('2020-12-15','2020-12-18');

-- Test case B fail
-- INSERT INTO public.working_hours (date_start,date_end) VALUES ('2020-12-5','2020-12-25');

-- Test case C fail
-- INSERT INTO public.working_hours (date_start,date_end) VALUES ('2020-12-15','2020-12-25');

-- Test case D fail
-- INSERT INTO public.working_hours (date_start,date_end) VALUES ('2020-12-5','2020-12-15');

-- Test case E fail
-- INSERT INTO public.working_hours (date_start,date_end) VALUES ('2020-12-5','2020-12-10');

-- Test case F fail
-- INSERT INTO public.working_hours (date_start,date_end) VALUES ('2020-12-20','2020-12-25');

-- Test success
-- INSERT INTO public.working_hours (date_start,date_end) VALUES ('2020-12-21','2020-12-25');

你可以看到我正在一個一個地測試所有的重疊條件,這個關於我使用 9999 年之前的日期解決的非結束時間段的問題,以使所有工作正常。

我分享的這段代碼正在工作,在它的最后你可以找到一個以失敗結尾的插入語句(與給定的情況有關),

這些檢查很多是“手動”的,我想知道這是否可以通過使用 intersect 或類似的查詢來實現,但我還沒有找到可行的方法


編輯基於@GMB 方法,這是最終結果

DROP TABLE IF EXISTS public.working_hours;

CREATE TABLE public.working_hours (
    id serial NOT NULL,
    status varchar(255) not null,
    user_id int NOT null,
    date_start date NOT NULL,
    date_end date NULL
);

alter table public.working_hours
ADD CONSTRAINT prevent_overlap
EXCLUDE USING gist (user_id WITH =, daterange(date_start, coalesce(date_end, 'infinity'),  '[]') WITH &&)
where (status = 'active')
;

-- Insert some valid data
INSERT INTO public.working_hours (status, user_id, date_start,date_end) VALUES ('active', 1, '2020-12-10'::date,'2020-12-20'::date);
INSERT INTO public.working_hours (status, user_id, date_start,date_end) VALUES ('deleted', 1, '2020-12-5'::date,'2020-12-15'::date);
INSERT INTO public.working_hours (status, user_id, date_start,date_end) VALUES ('active', 2, '2020-12-10'::date,'2020-12-20'::date);

-- Updating from deleted to active will fail
update public.working_hours set status = 'active' where id = 2;

在我的實際場景中,我還有一列定義記錄是否處於活動狀態,因此我在定義中添加了 where 子句。 我還移到了單獨的ADD CONSTRAINT語句,因為我的表已經存在,所以我只添加了這個。

無需復雜的觸發代碼。 您可以使用排除約束來簡化和有效地做您想做的事情:

CREATE TABLE public.working_hours (
    id serial NOT NULL,
    date_start date NOT NULL,
    date_end date NULL,
    EXCLUDE USING gist (daterange(date_start, coalesce(date_end, 'infinity'),  '[]') WITH &&)
);

daterange()的參數[]使范圍在兩端都包含在內,這就是我理解您的問題的方式。


編輯:如果您希望排除基於另一列,請說user_id

CREATE TABLE public.working_hours (
    id serial NOT NULL,
    user_id int NOT NULL
    date_start date NOT NULL,
    date_end date NULL,
    EXCLUDE USING gist (
        user_id WITH =,
        daterange(date_start, coalesce(date_end, 'infinity'),  '[]') WITH &&
    )
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM