⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plpgsql.sql

📁 postgresql8.3.4源码,开源数据库
💻 SQL
📖 第 1 页 / 共 5 页
字号:
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	    update PSlot set backlink = blname where slotname = myname;	end if;	return 0;    end if;    if mytype = ''WS'' then        select into rec * from WSlot where slotname = myname;	if not found then	    raise exception ''% does not exist'', myname;	end if;	if rec.backlink != blname then	    update WSlot set backlink = blname where slotname = myname;	end if;	return 0;    end if;    if mytype = ''PL'' then        select into rec * from PLine where slotname = myname;	if not found then	    raise exception ''% does not exist'', myname;	end if;	if rec.backlink != blname then	    update PLine set backlink = blname where slotname = myname;	end if;	return 0;    end if;    raise exception ''illegal backlink beginning with %'', mytype;end;' language plpgsql;-- ************************************************************-- * Support function to clear out the backlink field if-- * it still points to specific slot-- ************************************************************create function tg_backlink_unset(bpchar, bpchar)returns integer as 'declare    myname	alias for $1;    blname	alias for $2;    mytype	char(2);    rec		record;begin    mytype := substr(myname, 1, 2);    if mytype = ''PS'' then        select into rec * from PSlot where slotname = myname;	if not found then	    return 0;	end if;	if rec.backlink = blname then	    update PSlot set backlink = '''' where slotname = myname;	end if;	return 0;    end if;    if mytype = ''WS'' then        select into rec * from WSlot where slotname = myname;	if not found then	    return 0;	end if;	if rec.backlink = blname then	    update WSlot set backlink = '''' where slotname = myname;	end if;	return 0;    end if;    if mytype = ''PL'' then        select into rec * from PLine where slotname = myname;	if not found then	    return 0;	end if;	if rec.backlink = blname then	    update PLine set backlink = '''' where slotname = myname;	end if;	return 0;    end if;end' language plpgsql;-- ************************************************************-- * AFTER INSERT or UPDATE or DELETE on slot with slotlink-- *	- Ensure that the opponent correctly points back to us-- ************************************************************create function tg_slotlink_a() returns trigger as 'declare    dummy	integer;begin    if tg_op = ''INSERT'' then        if new.slotlink != '''' then	    dummy := tg_slotlink_set(new.slotlink, new.slotname);	end if;	return new;    end if;    if tg_op = ''UPDATE'' then        if new.slotlink != old.slotlink then	    if old.slotlink != '''' then	        dummy := tg_slotlink_unset(old.slotlink, old.slotname);	    end if;	    if new.slotlink != '''' then	        dummy := tg_slotlink_set(new.slotlink, new.slotname);	    end if;	else	    if new.slotname != old.slotname and new.slotlink != '''' then	        dummy := tg_slotlink_set(new.slotlink, new.slotname);	    end if;	end if;	return new;    end if;    if tg_op = ''DELETE'' then        if old.slotlink != '''' then	    dummy := tg_slotlink_unset(old.slotlink, old.slotname);	end if;	return old;    end if;end;' language plpgsql;create trigger tg_slotlink_a after insert or update or delete    on PSlot for each row execute procedure tg_slotlink_a('PS');create trigger tg_slotlink_a after insert or update or delete    on WSlot for each row execute procedure tg_slotlink_a('WS');create trigger tg_slotlink_a after insert or update or delete    on IFace for each row execute procedure tg_slotlink_a('IF');create trigger tg_slotlink_a after insert or update or delete    on HSlot for each row execute procedure tg_slotlink_a('HS');create trigger tg_slotlink_a after insert or update or delete    on PHone for each row execute procedure tg_slotlink_a('PH');-- ************************************************************-- * Support function to set the opponents slotlink field-- * if it does not already point to the requested slot-- ************************************************************create function tg_slotlink_set(bpchar, bpchar)returns integer as 'declare    myname	alias for $1;    blname	alias for $2;    mytype	char(2);    link	char(4);    rec		record;begin    mytype := substr(myname, 1, 2);    link := mytype || substr(blname, 1, 2);    if link = ''PHPH'' then        raise exception 		''slotlink between two phones does not make sense'';    end if;    if link in (''PHHS'', ''HSPH'') then        raise exception 		''link of phone to hub does not make sense'';    end if;    if link in (''PHIF'', ''IFPH'') then        raise exception 		''link of phone to hub does not make sense'';    end if;    if link in (''PSWS'', ''WSPS'') then        raise exception 		''slotlink from patchslot to wallslot 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.slotlink != blname then	    update PSlot set slotlink = blname where slotname = myname;	end if;	return 0;    end if;    if mytype = ''WS'' then        select into rec * from WSlot where slotname = myname;	if not found then	    raise exception ''% does not exist'', myname;	end if;	if rec.slotlink != blname then	    update WSlot set slotlink = blname where slotname = myname;	end if;	return 0;    end if;    if mytype = ''IF'' then        select into rec * from IFace where slotname = myname;	if not found then	    raise exception ''% does not exist'', myname;	end if;	if rec.slotlink != blname then	    update IFace set slotlink = blname where slotname = myname;	end if;	return 0;    end if;    if mytype = ''HS'' then        select into rec * from HSlot where slotname = myname;	if not found then	    raise exception ''% does not exist'', myname;	end if;	if rec.slotlink != blname then	    update HSlot set slotlink = blname where slotname = myname;	end if;	return 0;    end if;    if mytype = ''PH'' then        select into rec * from PHone where slotname = myname;	if not found then	    raise exception ''% does not exist'', myname;	end if;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -