📄 plpgsql.out
字号:
-- ************************************************************-- * BEFORE DELETE on HSlot-- * - prevent from manual manipulation-- ************************************************************create function tg_hslot_bd() returns trigger as 'declare hubrec record;begin select into hubrec * from Hub where name = old.hubname; if not found then return old; end if; if old.slotno > hubrec.nslots then return old; end if; raise exception ''no manual manipulation of HSlot'';end;' language plpgsql;create trigger tg_hslot_bd before delete on HSlot for each row execute procedure tg_hslot_bd();-- ************************************************************-- * BEFORE INSERT on all slots-- * - Check name prefix-- ************************************************************create function tg_chkslotname() returns trigger as 'begin if substr(new.slotname, 1, 2) != tg_argv[0] then raise exception ''slotname must begin with %'', tg_argv[0]; end if; return new;end;' language plpgsql;create trigger tg_chkslotname before insert on PSlot for each row execute procedure tg_chkslotname('PS');create trigger tg_chkslotname before insert on WSlot for each row execute procedure tg_chkslotname('WS');create trigger tg_chkslotname before insert on PLine for each row execute procedure tg_chkslotname('PL');create trigger tg_chkslotname before insert on IFace for each row execute procedure tg_chkslotname('IF');create trigger tg_chkslotname before insert on PHone for each row execute procedure tg_chkslotname('PH');-- ************************************************************-- * BEFORE INSERT or UPDATE on all slots with slotlink-- * - Set slotlink to empty string if NULL value given-- ************************************************************create function tg_chkslotlink() returns trigger as 'begin if new.slotlink isnull then new.slotlink := ''''; end if; return new;end;' language plpgsql;create trigger tg_chkslotlink before insert or update on PSlot for each row execute procedure tg_chkslotlink();create trigger tg_chkslotlink before insert or update on WSlot for each row execute procedure tg_chkslotlink();create trigger tg_chkslotlink before insert or update on IFace for each row execute procedure tg_chkslotlink();create trigger tg_chkslotlink before insert or update on HSlot for each row execute procedure tg_chkslotlink();create trigger tg_chkslotlink before insert or update on PHone for each row execute procedure tg_chkslotlink();-- ************************************************************-- * BEFORE INSERT or UPDATE on all slots with backlink-- * - Set backlink to empty string if NULL value given-- ************************************************************create function tg_chkbacklink() returns trigger as 'begin if new.backlink isnull then new.backlink := ''''; end if; return new;end;' language plpgsql;create trigger tg_chkbacklink before insert or update on PSlot for each row execute procedure tg_chkbacklink();create trigger tg_chkbacklink before insert or update on WSlot for each row execute procedure tg_chkbacklink();create trigger tg_chkbacklink before insert or update on PLine for each row execute procedure tg_chkbacklink();-- ************************************************************-- * BEFORE UPDATE on PSlot-- * - do delete/insert instead of update if name changes-- ************************************************************create function tg_pslot_bu() returns trigger as 'begin if new.slotname != old.slotname then delete from PSlot where slotname = old.slotname; insert into PSlot ( slotname, pfname, slotlink, backlink ) values ( new.slotname, new.pfname, new.slotlink, new.backlink ); return null; end if; return new;end;' language plpgsql;create trigger tg_pslot_bu before update on PSlot for each row execute procedure tg_pslot_bu();-- ************************************************************-- * BEFORE UPDATE on WSlot-- * - do delete/insert instead of update if name changes-- ************************************************************create function tg_wslot_bu() returns trigger as 'begin if new.slotname != old.slotname then delete from WSlot where slotname = old.slotname; insert into WSlot ( slotname, roomno, slotlink, backlink ) values ( new.slotname, new.roomno, new.slotlink, new.backlink ); return null; end if; return new;end;' language plpgsql;create trigger tg_wslot_bu before update on WSlot for each row execute procedure tg_Wslot_bu();-- ************************************************************-- * BEFORE UPDATE on PLine-- * - do delete/insert instead of update if name changes-- ************************************************************create function tg_pline_bu() returns trigger as 'begin if new.slotname != old.slotname then delete from PLine where slotname = old.slotname; insert into PLine ( slotname, phonenumber, comment, backlink ) values ( new.slotname, new.phonenumber, new.comment, new.backlink ); return null; end if; return new;end;' language plpgsql;create trigger tg_pline_bu before update on PLine for each row execute procedure tg_pline_bu();-- ************************************************************-- * BEFORE UPDATE on IFace-- * - do delete/insert instead of update if name changes-- ************************************************************create function tg_iface_bu() returns trigger as 'begin if new.slotname != old.slotname then delete from IFace where slotname = old.slotname; insert into IFace ( slotname, sysname, ifname, slotlink ) values ( new.slotname, new.sysname, new.ifname, new.slotlink ); return null; end if; return new;end;' language plpgsql;create trigger tg_iface_bu before update on IFace for each row execute procedure tg_iface_bu();-- ************************************************************-- * BEFORE UPDATE on HSlot-- * - do delete/insert instead of update if name changes-- ************************************************************create function tg_hslot_bu() returns trigger as 'begin if new.slotname != old.slotname or new.hubname != old.hubname then delete from HSlot where slotname = old.slotname; insert into HSlot ( slotname, hubname, slotno, slotlink ) values ( new.slotname, new.hubname, new.slotno, new.slotlink ); return null; end if; return new;end;' language plpgsql;create trigger tg_hslot_bu before update on HSlot for each row execute procedure tg_hslot_bu();-- ************************************************************-- * BEFORE UPDATE on PHone-- * - do delete/insert instead of update if name changes-- ************************************************************create function tg_phone_bu() returns trigger as 'begin if new.slotname != old.slotname then delete from PHone where slotname = old.slotname; insert into PHone ( slotname, comment, slotlink ) values ( new.slotname, new.comment, new.slotlink ); return null; end if; return new;end;' language plpgsql;create trigger tg_phone_bu before update on PHone for each row execute procedure tg_phone_bu();-- ************************************************************-- * AFTER INSERT or UPDATE or DELETE on slot with backlink-- * - Ensure that the opponent correctly points back to us-- ************************************************************create function tg_backlink_a() returns trigger as 'declare dummy integer;begin if tg_op = ''INSERT'' then if new.backlink != '''' then dummy := tg_backlink_set(new.backlink, new.slotname); end if; return new; end if; if tg_op = ''UPDATE'' then if new.backlink != old.backlink then if old.backlink != '''' then dummy := tg_backlink_unset(old.backlink, old.slotname); end if; if new.backlink != '''' then dummy := tg_backlink_set(new.backlink, new.slotname); end if; else if new.slotname != old.slotname and new.backlink != '''' then dummy := tg_slotlink_set(new.backlink, new.slotname); end if; end if; return new; end if; if tg_op = ''DELETE'' then if old.backlink != '''' then dummy := tg_backlink_unset(old.backlink, old.slotname); end if; return old; end if;end;' language plpgsql;create trigger tg_backlink_a after insert or update or delete on PSlot for each row execute procedure tg_backlink_a('PS');create trigger tg_backlink_a after insert or update or delete on WSlot for each row execute procedure tg_backlink_a('WS');create trigger tg_backlink_a after insert or update or delete on PLine for each row execute procedure tg_backlink_a('PL');-- ************************************************************-- * Support function to set the opponents backlink field-- * if it does not already point to the requested slot-- ************************************************************create function tg_backlink_set(myname bpchar, blname bpchar)returns integer as 'declare mytype char(2); link char(4); rec record;begin mytype := substr(myname, 1, 2); link := mytype || substr(blname, 1, 2); if link = ''PLPL'' then raise exception ''backlink between two phone lines does not make sense''; end if; if link in (''PLWS'', ''WSPL'') then raise exception ''direct link of phone line to wall slot not permitted''; end if; if mytype = ''PS'' then select into rec * from PSlot where slotname = myname; if not found then raise exception ''% does not exist'', myname; end if; if rec.backlink != blname then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -