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

📄 plpgsql.sql

📁 PostgreSQL7.4.6 for Linux
💻 SQL
📖 第 1 页 / 共 4 页
字号:
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;	if rec.slotlink != blname then	    update PHone set slotlink = blname where slotname = myname;	end if;	return 0;    end if;    raise exception ''illegal slotlink beginning with %'', mytype;end;' language 'plpgsql';-- ************************************************************-- * Support function to clear out the slotlink field if-- * it still points to specific slot-- ************************************************************create function tg_slotlink_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.slotlink = blname then	    update PSlot set slotlink = '''' 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.slotlink = blname then	    update WSlot set slotlink = '''' 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	    return 0;	end if;	if rec.slotlink = blname then	    update IFace set slotlink = '''' 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	    return 0;	end if;	if rec.slotlink = blname then	    update HSlot set slotlink = '''' 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	    return 0;	end if;	if rec.slotlink = blname then	    update PHone set slotlink = '''' where slotname = myname;	end if;	return 0;    end if;end;' language 'plpgsql';-- ************************************************************-- * Describe the backside of a patchfield slot-- ************************************************************create function pslot_backlink_view(bpchar)returns text as '<<outer>>declare    rec		record;    bltype	char(2);    retval	text;begin    select into rec * from PSlot where slotname = $1;    if not found then        return '''';    end if;    if rec.backlink = '''' then        return ''-'';    end if;    bltype := substr(rec.backlink, 1, 2);    if bltype = ''PL'' then        declare	    rec		record;	begin	    select into rec * from PLine where slotname = outer.rec.backlink;	    retval := ''Phone line '' || trim(rec.phonenumber);	    if rec.comment != '''' then	        retval := retval || '' ('';		retval := retval || rec.comment;		retval := retval || '')'';	    end if;	    return retval;	end;    end if;    if bltype = ''WS'' then        select into rec * from WSlot where slotname = rec.backlink;	retval := trim(rec.slotname) || '' in room '';	retval := retval || trim(rec.roomno);	retval := retval || '' -> '';	return retval || wslot_slotlink_view(rec.slotname);    end if;    return rec.backlink;end;' language 'plpgsql';-- ************************************************************-- * Describe the front of a patchfield slot-- ************************************************************create function pslot_slotlink_view(bpchar)returns text as 'declare    psrec	record;    sltype	char(2);    retval	text;begin    select into psrec * from PSlot where slotname = $1;    if not found then        return '''';    end if;    if psrec.slotlink = '''' then        return ''-'';    end if;    sltype := substr(psrec.slotlink, 1, 2);    if sltype = ''PS'' then	retval := trim(psrec.slotlink) || '' -> '';	return retval || pslot_backlink_view(psrec.slotlink);    end if;    if sltype = ''HS'' then        retval := comment from Hub H, HSlot HS			where HS.slotname = psrec.slotlink			  and H.name = HS.hubname;        retval := retval || '' slot '';	retval := retval || slotno::text from HSlot			where slotname = psrec.slotlink;	return retval;    end if;    return psrec.slotlink;end;' language 'plpgsql';-- ************************************************************-- * Describe the front of a wall connector slot-- ************************************************************create function wslot_slotlink_view(bpchar)returns text as 'declare    rec		record;    sltype	char(2);    retval	text;begin    select into rec * from WSlot where slotname = $1;    if not found then        return '''';    end if;    if rec.slotlink = '''' then        return ''-'';    end if;    sltype := substr(rec.slotlink, 1, 2);    if sltype = ''PH'' then        select into rec * from PHone where slotname = rec.slotlink;	retval := ''Phone '' || trim(rec.slotname);	if rec.comment != '''' then	    retval := retval || '' ('';	    retval := retval || rec.comment;	    retval := retval || '')'';	end if;	return retval;    end if;    if sltype = ''IF'' then	declare	    syrow	System%RowType;	    ifrow	IFace%ROWTYPE;        begin	    select into ifrow * from IFace where slotname = rec.slotlink;	    select into syrow * from System where name = ifrow.sysname;	    retval := syrow.name || '' IF '';	    retval := retval || ifrow.ifname;	    if syrow.comment != '''' then	        retval := retval || '' ('';		retval := retval || syrow.comment;		retval := retval || '')'';	    end if;	    return retval;	end;    end if;    return rec.slotlink;end;' language 'plpgsql';-- ************************************************************-- * View of a patchfield describing backside and patches-- ************************************************************create view Pfield_v1 as select PF.pfname, PF.slotname,	pslot_backlink_view(PF.slotname) as backside,	pslot_slotlink_view(PF.slotname) as patch    from PSlot PF;---- First we build the house - so we create the rooms--insert into Room values ('001', 'Entrance');insert into Room values ('002', 'Office');insert into Room values ('003', 'Office');insert into Room values ('004', 'Technical');insert into Room values ('101', 'Office');insert into Room values ('102', 'Conference');insert into Room values ('103', 'Restroom');insert into Room values ('104', 'Technical');insert into Room values ('105', 'Office');insert into Room values ('106', 'Office');---- Second we install the wall connectors--insert into WSlot values ('WS.001.1a', '001', '', '');insert into WSlot values ('WS.001.1b', '001', '', '');insert into WSlot values ('WS.001.2a', '001', '', '');insert into WSlot values ('WS.001.2b', '001', '', '');insert into WSlot values ('WS.001.3a', '001', '', '');insert into WSlot values ('WS.001.3b', '001', '', '');insert into WSlot values ('WS.002.1a', '002', '', '');insert into WSlot values ('WS.002.1b', '002', '', '');insert into WSlot values ('WS.002.2a', '002', '', '');insert into WSlot values ('WS.002.2b', '002', '', '');insert into WSlot values ('WS.002.3a', '002', '', '');insert into WSlot values ('WS.002.3b', '002', '', '');insert into WSlot values ('WS.003.1a', '003', '', '');insert into WSlot values ('WS.003.1b', '003', '', '');insert into WSlot values ('WS.003.2a', '003', '', '');insert into WSlot values ('WS.003.2b', '003', '', '');insert into WSlot values ('WS.003.3a', '003', '', '');insert into WSlot values ('WS.003.3b', '003', '', '');insert into WSlot values ('WS.101.1a', '101', '', '');insert into WSlot values ('WS.101.1b', '101', '', '');insert into WSlot values ('WS.101.2a', '101', '', '');insert into WSlot values ('WS.101.2b', '101', '', '');insert into WSlot values ('WS.101.3a', '101', '', '');insert into WSlot values ('WS.101.3b', '101', '', '');insert into WSlot values ('WS.102.1a', '102', '', '');insert into WSlot values ('WS.102.1b', '102', '', '');insert into WSlot values ('WS.102.2a', '102', '', '');insert into WSlot values ('WS.102.2b', '102', '', '');insert into WSlot values ('WS.102.3a', '102', '', '');insert into WSlot values ('WS.102.3b', '102', '', '');insert into WSlot values ('WS.105.1a', '105', '', '');insert into WSlot values ('WS.105.1b', '105', '', '');insert into WSlot values ('WS.105.2a', '105', '', '');insert into WSlot values ('WS.105.2b', '105', '', '');insert into WSlot values ('WS.105.3a', '105', '', '');insert into WSlot values ('WS.105.3b', '105', '', '');

⌨️ 快捷键说明

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