2000-01-06 07:41:55 +01:00
|
|
|
--
|
|
|
|
-- PLPGSQL
|
|
|
|
--
|
2002-08-23 01:31:48 +02:00
|
|
|
-- Scenario:
|
|
|
|
--
|
|
|
|
-- A building with a modern TP cable installation where any
|
|
|
|
-- of the wall connectors can be used to plug in phones,
|
|
|
|
-- ethernet interfaces or local office hubs. The backside
|
|
|
|
-- of the wall connectors is wired to one of several patch-
|
|
|
|
-- fields in the building.
|
|
|
|
--
|
|
|
|
-- In the patchfields, there are hubs and all the slots
|
|
|
|
-- representing the wall connectors. In addition there are
|
|
|
|
-- slots that can represent a phone line from the central
|
|
|
|
-- phone system.
|
|
|
|
--
|
|
|
|
-- Triggers ensure consistency of the patching information.
|
|
|
|
--
|
|
|
|
-- Functions are used to build up powerful views that let
|
|
|
|
-- you look behind the wall when looking at a patchfield
|
|
|
|
-- or into a room.
|
|
|
|
--
|
|
|
|
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create table Room (
|
|
|
|
roomno char(8),
|
|
|
|
comment text
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index Room_rno on Room using btree (roomno bpchar_ops);
|
|
|
|
|
|
|
|
|
|
|
|
create table WSlot (
|
|
|
|
slotname char(20),
|
|
|
|
roomno char(8),
|
|
|
|
slotlink char(20),
|
|
|
|
backlink char(20)
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index WSlot_name on WSlot using btree (slotname bpchar_ops);
|
|
|
|
|
|
|
|
|
|
|
|
create table PField (
|
|
|
|
name text,
|
|
|
|
comment text
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index PField_name on PField using btree (name text_ops);
|
|
|
|
|
|
|
|
|
|
|
|
create table PSlot (
|
|
|
|
slotname char(20),
|
|
|
|
pfname text,
|
|
|
|
slotlink char(20),
|
|
|
|
backlink char(20)
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index PSlot_name on PSlot using btree (slotname bpchar_ops);
|
|
|
|
|
|
|
|
|
|
|
|
create table PLine (
|
|
|
|
slotname char(20),
|
|
|
|
phonenumber char(20),
|
|
|
|
comment text,
|
|
|
|
backlink char(20)
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index PLine_name on PLine using btree (slotname bpchar_ops);
|
|
|
|
|
|
|
|
|
|
|
|
create table Hub (
|
|
|
|
name char(14),
|
|
|
|
comment text,
|
|
|
|
nslots integer
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index Hub_name on Hub using btree (name bpchar_ops);
|
|
|
|
|
|
|
|
|
|
|
|
create table HSlot (
|
|
|
|
slotname char(20),
|
|
|
|
hubname char(14),
|
|
|
|
slotno integer,
|
|
|
|
slotlink char(20)
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index HSlot_name on HSlot using btree (slotname bpchar_ops);
|
|
|
|
create index HSlot_hubname on HSlot using btree (hubname bpchar_ops);
|
|
|
|
|
|
|
|
|
|
|
|
create table System (
|
|
|
|
name text,
|
|
|
|
comment text
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index System_name on System using btree (name text_ops);
|
|
|
|
|
|
|
|
|
|
|
|
create table IFace (
|
|
|
|
slotname char(20),
|
|
|
|
sysname text,
|
|
|
|
ifname text,
|
|
|
|
slotlink char(20)
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index IFace_name on IFace using btree (slotname bpchar_ops);
|
|
|
|
|
|
|
|
|
|
|
|
create table PHone (
|
|
|
|
slotname char(20),
|
|
|
|
comment text,
|
|
|
|
slotlink char(20)
|
|
|
|
);
|
|
|
|
|
|
|
|
create unique index PHone_name on PHone using btree (slotname bpchar_ops);
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- *
|
|
|
|
-- * Trigger procedures and functions for the patchfield
|
|
|
|
-- * test of PL/pgSQL
|
|
|
|
-- *
|
|
|
|
-- ************************************************************
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * AFTER UPDATE on Room
|
|
|
|
-- * - If room no changes let wall slots follow
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_room_au() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
begin
|
|
|
|
if new.roomno != old.roomno then
|
|
|
|
update WSlot set roomno = new.roomno where roomno = old.roomno;
|
|
|
|
end if;
|
|
|
|
return new;
|
|
|
|
end;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_room_au after update
|
|
|
|
on Room for each row execute procedure tg_room_au();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * AFTER DELETE on Room
|
|
|
|
-- * - delete wall slots in this room
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_room_ad() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
begin
|
|
|
|
delete from WSlot where roomno = old.roomno;
|
|
|
|
return old;
|
|
|
|
end;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_room_ad after delete
|
|
|
|
on Room for each row execute procedure tg_room_ad();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * BEFORE INSERT or UPDATE on WSlot
|
|
|
|
-- * - Check that room exists
|
|
|
|
-- ************************************************************
|
2004-09-21 01:00:12 +02:00
|
|
|
create function tg_wslot_biu() returns trigger as $$
|
1998-10-01 05:38:45 +02:00
|
|
|
begin
|
|
|
|
if count(*) = 0 from Room where roomno = new.roomno then
|
2004-09-21 01:00:12 +02:00
|
|
|
raise exception 'Room % does not exist', new.roomno;
|
1998-10-01 05:38:45 +02:00
|
|
|
end if;
|
|
|
|
return new;
|
|
|
|
end;
|
2004-09-21 01:00:12 +02:00
|
|
|
$$ language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_wslot_biu before insert or update
|
|
|
|
on WSlot for each row execute procedure tg_wslot_biu();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * AFTER UPDATE on PField
|
|
|
|
-- * - Let PSlots of this field follow
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_pfield_au() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
begin
|
|
|
|
if new.name != old.name then
|
|
|
|
update PSlot set pfname = new.name where pfname = old.name;
|
|
|
|
end if;
|
|
|
|
return new;
|
|
|
|
end;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_pfield_au after update
|
|
|
|
on PField for each row execute procedure tg_pfield_au();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * AFTER DELETE on PField
|
|
|
|
-- * - Remove all slots of this patchfield
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_pfield_ad() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
begin
|
|
|
|
delete from PSlot where pfname = old.name;
|
|
|
|
return old;
|
|
|
|
end;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_pfield_ad after delete
|
|
|
|
on PField for each row execute procedure tg_pfield_ad();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * BEFORE INSERT or UPDATE on PSlot
|
|
|
|
-- * - Ensure that our patchfield does exist
|
|
|
|
-- ************************************************************
|
2004-09-21 01:00:12 +02:00
|
|
|
create function tg_pslot_biu() returns trigger as $proc$
|
1998-10-01 05:38:45 +02:00
|
|
|
declare
|
|
|
|
pfrec record;
|
|
|
|
rename new to ps;
|
|
|
|
begin
|
|
|
|
select into pfrec * from PField where name = ps.pfname;
|
|
|
|
if not found then
|
2004-09-21 01:00:12 +02:00
|
|
|
raise exception $$Patchfield "%" does not exist$$, ps.pfname;
|
1998-10-01 05:38:45 +02:00
|
|
|
end if;
|
|
|
|
return ps;
|
|
|
|
end;
|
2004-09-21 01:00:12 +02:00
|
|
|
$proc$ language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_pslot_biu before insert or update
|
|
|
|
on PSlot for each row execute procedure tg_pslot_biu();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * AFTER UPDATE on System
|
|
|
|
-- * - If system name changes let interfaces follow
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_system_au() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
begin
|
|
|
|
if new.name != old.name then
|
|
|
|
update IFace set sysname = new.name where sysname = old.name;
|
|
|
|
end if;
|
|
|
|
return new;
|
|
|
|
end;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_system_au after update
|
|
|
|
on System for each row execute procedure tg_system_au();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * BEFORE INSERT or UPDATE on IFace
|
|
|
|
-- * - set the slotname to IF.sysname.ifname
|
|
|
|
-- ************************************************************
|
2004-09-21 01:00:12 +02:00
|
|
|
create function tg_iface_biu() returns trigger as $$
|
1998-10-01 05:38:45 +02:00
|
|
|
declare
|
|
|
|
sname text;
|
|
|
|
sysrec record;
|
|
|
|
begin
|
|
|
|
select into sysrec * from system where name = new.sysname;
|
|
|
|
if not found then
|
2004-09-21 01:00:12 +02:00
|
|
|
raise exception $q$system "%" does not exist$q$, new.sysname;
|
1998-10-01 05:38:45 +02:00
|
|
|
end if;
|
2004-09-21 01:00:12 +02:00
|
|
|
sname := 'IF.' || new.sysname;
|
|
|
|
sname := sname || '.';
|
1998-10-01 05:38:45 +02:00
|
|
|
sname := sname || new.ifname;
|
|
|
|
if length(sname) > 20 then
|
2004-09-21 01:00:12 +02:00
|
|
|
raise exception 'IFace slotname "%" too long (20 char max)', sname;
|
1998-10-01 05:38:45 +02:00
|
|
|
end if;
|
|
|
|
new.slotname := sname;
|
|
|
|
return new;
|
|
|
|
end;
|
2004-09-21 01:00:12 +02:00
|
|
|
$$ language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_iface_biu before insert or update
|
|
|
|
on IFace for each row execute procedure tg_iface_biu();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * AFTER INSERT or UPDATE or DELETE on Hub
|
|
|
|
-- * - insert/delete/rename slots as required
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_hub_a() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
declare
|
|
|
|
hname text;
|
|
|
|
dummy integer;
|
|
|
|
begin
|
|
|
|
if tg_op = ''INSERT'' then
|
|
|
|
dummy := tg_hub_adjustslots(new.name, 0, new.nslots);
|
|
|
|
return new;
|
|
|
|
end if;
|
|
|
|
if tg_op = ''UPDATE'' then
|
|
|
|
if new.name != old.name then
|
|
|
|
update HSlot set hubname = new.name where hubname = old.name;
|
|
|
|
end if;
|
|
|
|
dummy := tg_hub_adjustslots(new.name, old.nslots, new.nslots);
|
|
|
|
return new;
|
|
|
|
end if;
|
|
|
|
if tg_op = ''DELETE'' then
|
|
|
|
dummy := tg_hub_adjustslots(old.name, old.nslots, 0);
|
|
|
|
return old;
|
|
|
|
end if;
|
|
|
|
end;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_hub_a after insert or update or delete
|
|
|
|
on Hub for each row execute procedure tg_hub_a();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * Support function to add/remove slots of Hub
|
|
|
|
-- ************************************************************
|
2004-09-21 01:00:12 +02:00
|
|
|
create function tg_hub_adjustslots(hname bpchar,
|
|
|
|
oldnslots integer,
|
|
|
|
newnslots integer)
|
1998-10-01 05:38:45 +02:00
|
|
|
returns integer as '
|
|
|
|
begin
|
|
|
|
if newnslots = oldnslots then
|
|
|
|
return 0;
|
|
|
|
end if;
|
|
|
|
if newnslots < oldnslots then
|
|
|
|
delete from HSlot where hubname = hname and slotno > newnslots;
|
|
|
|
return 0;
|
|
|
|
end if;
|
|
|
|
for i in oldnslots + 1 .. newnslots loop
|
|
|
|
insert into HSlot (slotname, hubname, slotno, slotlink)
|
|
|
|
values (''HS.dummy'', hname, i, '''');
|
|
|
|
end loop;
|
|
|
|
return 0;
|
2004-09-21 01:00:12 +02:00
|
|
|
end
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
2003-11-21 23:32:49 +01:00
|
|
|
-- Test comments
|
|
|
|
COMMENT ON FUNCTION tg_hub_adjustslots_wrong(bpchar, integer, integer) IS 'function with args';
|
|
|
|
COMMENT ON FUNCTION tg_hub_adjustslots(bpchar, integer, integer) IS 'function with args';
|
|
|
|
COMMENT ON FUNCTION tg_hub_adjustslots(bpchar, integer, integer) IS NULL;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * BEFORE INSERT or UPDATE on HSlot
|
|
|
|
-- * - prevent from manual manipulation
|
|
|
|
-- * - set the slotname to HS.hubname.slotno
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_hslot_biu() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
declare
|
|
|
|
sname text;
|
|
|
|
xname HSlot.slotname%TYPE;
|
|
|
|
hubrec record;
|
|
|
|
begin
|
|
|
|
select into hubrec * from Hub where name = new.hubname;
|
|
|
|
if not found then
|
|
|
|
raise exception ''no manual manipulation of HSlot'';
|
|
|
|
end if;
|
|
|
|
if new.slotno < 1 or new.slotno > hubrec.nslots then
|
|
|
|
raise exception ''no manual manipulation of HSlot'';
|
|
|
|
end if;
|
|
|
|
if tg_op = ''UPDATE'' then
|
|
|
|
if new.hubname != old.hubname then
|
|
|
|
if count(*) > 0 from Hub where name = old.hubname then
|
|
|
|
raise exception ''no manual manipulation of HSlot'';
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
sname := ''HS.'' || trim(new.hubname);
|
|
|
|
sname := sname || ''.'';
|
|
|
|
sname := sname || new.slotno::text;
|
|
|
|
if length(sname) > 20 then
|
|
|
|
raise exception ''HSlot slotname "%" too long (20 char max)'', sname;
|
|
|
|
end if;
|
|
|
|
new.slotname := sname;
|
|
|
|
return new;
|
|
|
|
end;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
create trigger tg_hslot_biu before insert or update
|
|
|
|
on HSlot for each row execute procedure tg_hslot_biu();
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * BEFORE DELETE on HSlot
|
|
|
|
-- * - prevent from manual manipulation
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_hslot_bd() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_chkslotname() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_chkslotlink() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
begin
|
|
|
|
if new.slotlink isnull then
|
|
|
|
new.slotlink := '''';
|
|
|
|
end if;
|
|
|
|
return new;
|
|
|
|
end;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_chkbacklink() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
begin
|
|
|
|
if new.backlink isnull then
|
|
|
|
new.backlink := '''';
|
|
|
|
end if;
|
|
|
|
return new;
|
|
|
|
end;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_pslot_bu() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_wslot_bu() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_pline_bu() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_iface_bu() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_hslot_bu() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_phone_bu() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_backlink_a() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
-- ************************************************************
|
2004-09-21 01:00:12 +02:00
|
|
|
create function tg_backlink_set(myname bpchar, blname bpchar)
|
1998-10-01 05:38:45 +02:00
|
|
|
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
|
2000-10-23 01:32:48 +02:00
|
|
|
raise exception ''% does not exist'', myname;
|
1998-10-01 05:38:45 +02:00
|
|
|
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
|
2000-10-23 01:32:48 +02:00
|
|
|
raise exception ''% does not exist'', myname;
|
1998-10-01 05:38:45 +02:00
|
|
|
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
|
2000-10-23 01:32:48 +02:00
|
|
|
raise exception ''% does not exist'', myname;
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * 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;
|
2004-09-21 01:00:12 +02:00
|
|
|
end
|
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * AFTER INSERT or UPDATE or DELETE on slot with slotlink
|
|
|
|
-- * - Ensure that the opponent correctly points back to us
|
|
|
|
-- ************************************************************
|
2002-08-22 02:01:51 +02:00
|
|
|
create function tg_slotlink_a() returns trigger as '
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2000-10-23 01:32:48 +02:00
|
|
|
raise exception ''% does not exist'', myname;
|
1998-10-01 05:38:45 +02:00
|
|
|
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
|
2000-10-23 01:32:48 +02:00
|
|
|
raise exception ''% does not exist'', myname;
|
1998-10-01 05:38:45 +02:00
|
|
|
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
|
2000-10-23 01:32:48 +02:00
|
|
|
raise exception ''% does not exist'', myname;
|
1998-10-01 05:38:45 +02:00
|
|
|
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
|
2000-10-23 01:32:48 +02:00
|
|
|
raise exception ''% does not exist'', myname;
|
1998-10-01 05:38:45 +02:00
|
|
|
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
|
2000-10-23 01:32:48 +02:00
|
|
|
raise exception ''% does not exist'', myname;
|
1998-10-01 05:38:45 +02:00
|
|
|
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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * 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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * 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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * 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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * 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;
|
2006-02-27 17:09:50 +01:00
|
|
|
' language plpgsql;
|
1998-10-01 05:38:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- ************************************************************
|
|
|
|
-- * 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', '', '');
|
|
|
|
|
|
|
|
insert into WSlot values ('WS.106.1a', '106', '', '');
|
|
|
|
insert into WSlot values ('WS.106.1b', '106', '', '');
|
|
|
|
insert into WSlot values ('WS.106.2a', '106', '', '');
|
|
|
|
insert into WSlot values ('WS.106.2b', '106', '', '');
|
|
|
|
insert into WSlot values ('WS.106.3a', '106', '', '');
|
|
|
|
insert into WSlot values ('WS.106.3b', '106', '', '');
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Now create the patch fields and their slots
|
|
|
|
--
|
|
|
|
insert into PField values ('PF0_1', 'Wallslots basement');
|
|
|
|
|
|
|
|
--
|
|
|
|
-- The cables for these will be made later, so they are unconnected for now
|
|
|
|
--
|
|
|
|
insert into PSlot values ('PS.base.a1', 'PF0_1', '', '');
|
|
|
|
insert into PSlot values ('PS.base.a2', 'PF0_1', '', '');
|
|
|
|
insert into PSlot values ('PS.base.a3', 'PF0_1', '', '');
|
|
|
|
insert into PSlot values ('PS.base.a4', 'PF0_1', '', '');
|
|
|
|
insert into PSlot values ('PS.base.a5', 'PF0_1', '', '');
|
|
|
|
insert into PSlot values ('PS.base.a6', 'PF0_1', '', '');
|
|
|
|
|
|
|
|
--
|
|
|
|
-- These are already wired to the wall connectors
|
|
|
|
--
|
|
|
|
insert into PSlot values ('PS.base.b1', 'PF0_1', '', 'WS.002.1a');
|
|
|
|
insert into PSlot values ('PS.base.b2', 'PF0_1', '', 'WS.002.1b');
|
|
|
|
insert into PSlot values ('PS.base.b3', 'PF0_1', '', 'WS.002.2a');
|
|
|
|
insert into PSlot values ('PS.base.b4', 'PF0_1', '', 'WS.002.2b');
|
|
|
|
insert into PSlot values ('PS.base.b5', 'PF0_1', '', 'WS.002.3a');
|
|
|
|
insert into PSlot values ('PS.base.b6', 'PF0_1', '', 'WS.002.3b');
|
|
|
|
|
|
|
|
insert into PSlot values ('PS.base.c1', 'PF0_1', '', 'WS.003.1a');
|
|
|
|
insert into PSlot values ('PS.base.c2', 'PF0_1', '', 'WS.003.1b');
|
|
|
|
insert into PSlot values ('PS.base.c3', 'PF0_1', '', 'WS.003.2a');
|
|
|
|
insert into PSlot values ('PS.base.c4', 'PF0_1', '', 'WS.003.2b');
|
|
|
|
insert into PSlot values ('PS.base.c5', 'PF0_1', '', 'WS.003.3a');
|
|
|
|
insert into PSlot values ('PS.base.c6', 'PF0_1', '', 'WS.003.3b');
|
|
|
|
|
|
|
|
--
|
|
|
|
-- This patchfield will be renamed later into PF0_2 - so its
|
|
|
|
-- slots references in pfname should follow
|
|
|
|
--
|
|
|
|
insert into PField values ('PF0_X', 'Phonelines basement');
|
|
|
|
|
|
|
|
insert into PSlot values ('PS.base.ta1', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.ta2', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.ta3', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.ta4', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.ta5', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.ta6', 'PF0_X', '', '');
|
|
|
|
|
|
|
|
insert into PSlot values ('PS.base.tb1', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.tb2', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.tb3', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.tb4', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.tb5', 'PF0_X', '', '');
|
|
|
|
insert into PSlot values ('PS.base.tb6', 'PF0_X', '', '');
|
|
|
|
|
|
|
|
insert into PField values ('PF1_1', 'Wallslots 1st floor');
|
|
|
|
|
|
|
|
insert into PSlot values ('PS.1st.a1', 'PF1_1', '', 'WS.101.1a');
|
|
|
|
insert into PSlot values ('PS.1st.a2', 'PF1_1', '', 'WS.101.1b');
|
|
|
|
insert into PSlot values ('PS.1st.a3', 'PF1_1', '', 'WS.101.2a');
|
|
|
|
insert into PSlot values ('PS.1st.a4', 'PF1_1', '', 'WS.101.2b');
|
|
|
|
insert into PSlot values ('PS.1st.a5', 'PF1_1', '', 'WS.101.3a');
|
|
|
|
insert into PSlot values ('PS.1st.a6', 'PF1_1', '', 'WS.101.3b');
|
|
|
|
|
|
|
|
insert into PSlot values ('PS.1st.b1', 'PF1_1', '', 'WS.102.1a');
|
|
|
|
insert into PSlot values ('PS.1st.b2', 'PF1_1', '', 'WS.102.1b');
|
|
|
|
insert into PSlot values ('PS.1st.b3', 'PF1_1', '', 'WS.102.2a');
|
|
|
|
insert into PSlot values ('PS.1st.b4', 'PF1_1', '', 'WS.102.2b');
|
|
|
|
insert into PSlot values ('PS.1st.b5', 'PF1_1', '', 'WS.102.3a');
|
|
|
|
insert into PSlot values ('PS.1st.b6', 'PF1_1', '', 'WS.102.3b');
|
|
|
|
|
|
|
|
insert into PSlot values ('PS.1st.c1', 'PF1_1', '', 'WS.105.1a');
|
|
|
|
insert into PSlot values ('PS.1st.c2', 'PF1_1', '', 'WS.105.1b');
|
|
|
|
insert into PSlot values ('PS.1st.c3', 'PF1_1', '', 'WS.105.2a');
|
|
|
|
insert into PSlot values ('PS.1st.c4', 'PF1_1', '', 'WS.105.2b');
|
|
|
|
insert into PSlot values ('PS.1st.c5', 'PF1_1', '', 'WS.105.3a');
|
|
|
|
insert into PSlot values ('PS.1st.c6', 'PF1_1', '', 'WS.105.3b');
|
|
|
|
|
|
|
|
insert into PSlot values ('PS.1st.d1', 'PF1_1', '', 'WS.106.1a');
|
|
|
|
insert into PSlot values ('PS.1st.d2', 'PF1_1', '', 'WS.106.1b');
|
|
|
|
insert into PSlot values ('PS.1st.d3', 'PF1_1', '', 'WS.106.2a');
|
|
|
|
insert into PSlot values ('PS.1st.d4', 'PF1_1', '', 'WS.106.2b');
|
|
|
|
insert into PSlot values ('PS.1st.d5', 'PF1_1', '', 'WS.106.3a');
|
|
|
|
insert into PSlot values ('PS.1st.d6', 'PF1_1', '', 'WS.106.3b');
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Now we wire the wall connectors 1a-2a in room 001 to the
|
|
|
|
-- patchfield. In the second update we make an error, and
|
|
|
|
-- correct it after
|
|
|
|
--
|
|
|
|
update PSlot set backlink = 'WS.001.1a' where slotname = 'PS.base.a1';
|
|
|
|
update PSlot set backlink = 'WS.001.1b' where slotname = 'PS.base.a3';
|
|
|
|
select * from WSlot where roomno = '001' order by slotname;
|
|
|
|
select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
|
|
|
|
update PSlot set backlink = 'WS.001.2a' where slotname = 'PS.base.a3';
|
|
|
|
select * from WSlot where roomno = '001' order by slotname;
|
|
|
|
select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
|
|
|
|
update PSlot set backlink = 'WS.001.1b' where slotname = 'PS.base.a2';
|
|
|
|
select * from WSlot where roomno = '001' order by slotname;
|
|
|
|
select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Same procedure for 2b-3b but this time updating the WSlot instead
|
|
|
|
-- of the PSlot. Due to the triggers the result is the same:
|
|
|
|
-- WSlot and corresponding PSlot point to each other.
|
|
|
|
--
|
|
|
|
update WSlot set backlink = 'PS.base.a4' where slotname = 'WS.001.2b';
|
|
|
|
update WSlot set backlink = 'PS.base.a6' where slotname = 'WS.001.3a';
|
|
|
|
select * from WSlot where roomno = '001' order by slotname;
|
|
|
|
select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
|
|
|
|
update WSlot set backlink = 'PS.base.a6' where slotname = 'WS.001.3b';
|
|
|
|
select * from WSlot where roomno = '001' order by slotname;
|
|
|
|
select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
|
|
|
|
update WSlot set backlink = 'PS.base.a5' where slotname = 'WS.001.3a';
|
|
|
|
select * from WSlot where roomno = '001' order by slotname;
|
|
|
|
select * from PSlot where slotname ~ 'PS.base.a' order by slotname;
|
|
|
|
|
|
|
|
insert into PField values ('PF1_2', 'Phonelines 1st floor');
|
|
|
|
|
|
|
|
insert into PSlot values ('PS.1st.ta1', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.ta2', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.ta3', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.ta4', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.ta5', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.ta6', 'PF1_2', '', '');
|
|
|
|
|
|
|
|
insert into PSlot values ('PS.1st.tb1', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.tb2', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.tb3', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.tb4', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.tb5', 'PF1_2', '', '');
|
|
|
|
insert into PSlot values ('PS.1st.tb6', 'PF1_2', '', '');
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Fix the wrong name for patchfield PF0_2
|
|
|
|
--
|
|
|
|
update PField set name = 'PF0_2' where name = 'PF0_X';
|
|
|
|
|
|
|
|
select * from PSlot order by slotname;
|
|
|
|
select * from WSlot order by slotname;
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Install the central phone system and create the phone numbers.
|
|
|
|
-- They are weired on insert to the patchfields. Again the
|
|
|
|
-- triggers automatically tell the PSlots to update their
|
|
|
|
-- backlink field.
|
|
|
|
--
|
|
|
|
insert into PLine values ('PL.001', '-0', 'Central call', 'PS.base.ta1');
|
|
|
|
insert into PLine values ('PL.002', '-101', '', 'PS.base.ta2');
|
|
|
|
insert into PLine values ('PL.003', '-102', '', 'PS.base.ta3');
|
|
|
|
insert into PLine values ('PL.004', '-103', '', 'PS.base.ta5');
|
|
|
|
insert into PLine values ('PL.005', '-104', '', 'PS.base.ta6');
|
|
|
|
insert into PLine values ('PL.006', '-106', '', 'PS.base.tb2');
|
|
|
|
insert into PLine values ('PL.007', '-108', '', 'PS.base.tb3');
|
|
|
|
insert into PLine values ('PL.008', '-109', '', 'PS.base.tb4');
|
|
|
|
insert into PLine values ('PL.009', '-121', '', 'PS.base.tb5');
|
|
|
|
insert into PLine values ('PL.010', '-122', '', 'PS.base.tb6');
|
|
|
|
insert into PLine values ('PL.015', '-134', '', 'PS.1st.ta1');
|
|
|
|
insert into PLine values ('PL.016', '-137', '', 'PS.1st.ta3');
|
|
|
|
insert into PLine values ('PL.017', '-139', '', 'PS.1st.ta4');
|
|
|
|
insert into PLine values ('PL.018', '-362', '', 'PS.1st.tb1');
|
|
|
|
insert into PLine values ('PL.019', '-363', '', 'PS.1st.tb2');
|
|
|
|
insert into PLine values ('PL.020', '-364', '', 'PS.1st.tb3');
|
|
|
|
insert into PLine values ('PL.021', '-365', '', 'PS.1st.tb5');
|
|
|
|
insert into PLine values ('PL.022', '-367', '', 'PS.1st.tb6');
|
|
|
|
insert into PLine values ('PL.028', '-501', 'Fax entrance', 'PS.base.ta2');
|
|
|
|
insert into PLine values ('PL.029', '-502', 'Fax 1st floor', 'PS.1st.ta1');
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Buy some phones, plug them into the wall and patch the
|
|
|
|
-- phone lines to the corresponding patchfield slots.
|
|
|
|
--
|
|
|
|
insert into PHone values ('PH.hc001', 'Hicom standard', 'WS.001.1a');
|
|
|
|
update PSlot set slotlink = 'PS.base.ta1' where slotname = 'PS.base.a1';
|
|
|
|
insert into PHone values ('PH.hc002', 'Hicom standard', 'WS.002.1a');
|
|
|
|
update PSlot set slotlink = 'PS.base.ta5' where slotname = 'PS.base.b1';
|
|
|
|
insert into PHone values ('PH.hc003', 'Hicom standard', 'WS.002.2a');
|
|
|
|
update PSlot set slotlink = 'PS.base.tb2' where slotname = 'PS.base.b3';
|
|
|
|
insert into PHone values ('PH.fax001', 'Canon fax', 'WS.001.2a');
|
|
|
|
update PSlot set slotlink = 'PS.base.ta2' where slotname = 'PS.base.a3';
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Install a hub at one of the patchfields, plug a computers
|
|
|
|
-- ethernet interface into the wall and patch it to the hub.
|
|
|
|
--
|
|
|
|
insert into Hub values ('base.hub1', 'Patchfield PF0_1 hub', 16);
|
|
|
|
insert into System values ('orion', 'PC');
|
|
|
|
insert into IFace values ('IF', 'orion', 'eth0', 'WS.002.1b');
|
|
|
|
update PSlot set slotlink = 'HS.base.hub1.1' where slotname = 'PS.base.b2';
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Now we take a look at the patchfield
|
|
|
|
--
|
|
|
|
select * from PField_v1 where pfname = 'PF0_1' order by slotname;
|
|
|
|
select * from PField_v1 where pfname = 'PF0_2' order by slotname;
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Finally we want errors
|
|
|
|
--
|
|
|
|
insert into PField values ('PF1_1', 'should fail due to unique index');
|
|
|
|
update PSlot set backlink = 'WS.not.there' where slotname = 'PS.base.a1';
|
|
|
|
update PSlot set backlink = 'XX.illegal' where slotname = 'PS.base.a1';
|
|
|
|
update PSlot set slotlink = 'PS.not.there' where slotname = 'PS.base.a1';
|
|
|
|
update PSlot set slotlink = 'XX.illegal' where slotname = 'PS.base.a1';
|
|
|
|
insert into HSlot values ('HS', 'base.hub1', 1, '');
|
|
|
|
insert into HSlot values ('HS', 'base.hub1', 20, '');
|
|
|
|
delete from HSlot;
|
|
|
|
insert into IFace values ('IF', 'notthere', 'eth0', '');
|
|
|
|
insert into IFace values ('IF', 'orion', 'ethernet_interface_name_too_long', '');
|
2000-01-06 07:41:55 +01:00
|
|
|
|
2002-08-30 02:28:41 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- The following tests are unrelated to the scenario outlined above;
|
|
|
|
-- they merely exercise specific parts of PL/PgSQL
|
|
|
|
--
|
|
|
|
|
2001-09-21 02:11:31 +02:00
|
|
|
--
|
|
|
|
-- Test recursion, per bug report 7-Sep-01
|
|
|
|
--
|
|
|
|
CREATE FUNCTION recursion_test(int,int) RETURNS text AS '
|
|
|
|
DECLARE rslt text;
|
|
|
|
BEGIN
|
|
|
|
IF $1 <= 0 THEN
|
|
|
|
rslt = CAST($2 AS TEXT);
|
|
|
|
ELSE
|
|
|
|
rslt = CAST($1 AS TEXT) || '','' || recursion_test($1 - 1, $2);
|
|
|
|
END IF;
|
|
|
|
RETURN rslt;
|
2006-02-27 17:09:50 +01:00
|
|
|
END;' LANGUAGE plpgsql;
|
2001-09-21 02:11:31 +02:00
|
|
|
|
|
|
|
SELECT recursion_test(4,3);
|
2002-08-20 07:28:24 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test the FOUND magic variable
|
|
|
|
--
|
|
|
|
CREATE TABLE found_test_tbl (a int);
|
|
|
|
|
2002-08-30 02:28:41 +02:00
|
|
|
create function test_found()
|
2002-08-20 07:28:24 +02:00
|
|
|
returns boolean as '
|
|
|
|
declare
|
|
|
|
begin
|
|
|
|
insert into found_test_tbl values (1);
|
|
|
|
if FOUND then
|
|
|
|
insert into found_test_tbl values (2);
|
|
|
|
end if;
|
|
|
|
|
|
|
|
update found_test_tbl set a = 100 where a = 1;
|
|
|
|
if FOUND then
|
|
|
|
insert into found_test_tbl values (3);
|
|
|
|
end if;
|
|
|
|
|
|
|
|
delete from found_test_tbl where a = 9999; -- matches no rows
|
|
|
|
if not FOUND then
|
|
|
|
insert into found_test_tbl values (4);
|
|
|
|
end if;
|
|
|
|
|
|
|
|
for i in 1 .. 10 loop
|
|
|
|
-- no need to do anything
|
|
|
|
end loop;
|
|
|
|
if FOUND then
|
|
|
|
insert into found_test_tbl values (5);
|
|
|
|
end if;
|
|
|
|
|
|
|
|
-- never executes the loop
|
|
|
|
for i in 2 .. 1 loop
|
|
|
|
-- no need to do anything
|
|
|
|
end loop;
|
|
|
|
if not FOUND then
|
|
|
|
insert into found_test_tbl values (6);
|
|
|
|
end if;
|
|
|
|
return true;
|
2006-02-27 17:09:50 +01:00
|
|
|
end;' language plpgsql;
|
2002-08-20 07:28:24 +02:00
|
|
|
|
|
|
|
select test_found();
|
|
|
|
select * from found_test_tbl;
|
2002-08-30 02:28:41 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test set-returning functions for PL/pgSQL
|
|
|
|
--
|
|
|
|
|
|
|
|
create function test_table_func_rec() returns setof found_test_tbl as '
|
|
|
|
DECLARE
|
|
|
|
rec RECORD;
|
|
|
|
BEGIN
|
|
|
|
FOR rec IN select * from found_test_tbl LOOP
|
|
|
|
RETURN NEXT rec;
|
|
|
|
END LOOP;
|
|
|
|
RETURN;
|
2006-02-27 17:09:50 +01:00
|
|
|
END;' language plpgsql;
|
2002-08-30 02:28:41 +02:00
|
|
|
|
|
|
|
select * from test_table_func_rec();
|
|
|
|
|
|
|
|
create function test_table_func_row() returns setof found_test_tbl as '
|
|
|
|
DECLARE
|
|
|
|
row found_test_tbl%ROWTYPE;
|
|
|
|
BEGIN
|
|
|
|
FOR row IN select * from found_test_tbl LOOP
|
|
|
|
RETURN NEXT row;
|
|
|
|
END LOOP;
|
|
|
|
RETURN;
|
2006-02-27 17:09:50 +01:00
|
|
|
END;' language plpgsql;
|
2002-08-30 02:28:41 +02:00
|
|
|
|
|
|
|
select * from test_table_func_row();
|
|
|
|
|
|
|
|
create function test_ret_set_scalar(int,int) returns setof int as '
|
|
|
|
DECLARE
|
|
|
|
i int;
|
|
|
|
BEGIN
|
|
|
|
FOR i IN $1 .. $2 LOOP
|
|
|
|
RETURN NEXT i + 1;
|
|
|
|
END LOOP;
|
|
|
|
RETURN;
|
2006-02-27 17:09:50 +01:00
|
|
|
END;' language plpgsql;
|
2002-08-30 02:28:41 +02:00
|
|
|
|
|
|
|
select * from test_ret_set_scalar(1,10);
|
2002-09-01 18:28:06 +02:00
|
|
|
|
|
|
|
create function test_ret_set_rec_dyn(int) returns setof record as '
|
|
|
|
DECLARE
|
|
|
|
retval RECORD;
|
|
|
|
BEGIN
|
|
|
|
IF $1 > 10 THEN
|
|
|
|
SELECT INTO retval 5, 10, 15;
|
|
|
|
RETURN NEXT retval;
|
|
|
|
RETURN NEXT retval;
|
|
|
|
ELSE
|
|
|
|
SELECT INTO retval 50, 5::numeric, ''xxx''::text;
|
|
|
|
RETURN NEXT retval;
|
|
|
|
RETURN NEXT retval;
|
|
|
|
END IF;
|
|
|
|
RETURN;
|
2006-02-27 17:09:50 +01:00
|
|
|
END;' language plpgsql;
|
2002-09-01 18:28:06 +02:00
|
|
|
|
|
|
|
SELECT * FROM test_ret_set_rec_dyn(1500) AS (a int, b int, c int);
|
|
|
|
SELECT * FROM test_ret_set_rec_dyn(5) AS (a int, b numeric, c text);
|
|
|
|
|
|
|
|
create function test_ret_rec_dyn(int) returns record as '
|
|
|
|
DECLARE
|
|
|
|
retval RECORD;
|
|
|
|
BEGIN
|
|
|
|
IF $1 > 10 THEN
|
|
|
|
SELECT INTO retval 5, 10, 15;
|
|
|
|
RETURN retval;
|
|
|
|
ELSE
|
|
|
|
SELECT INTO retval 50, 5::numeric, ''xxx''::text;
|
|
|
|
RETURN retval;
|
|
|
|
END IF;
|
2006-02-27 17:09:50 +01:00
|
|
|
END;' language plpgsql;
|
2002-09-01 18:28:06 +02:00
|
|
|
|
|
|
|
SELECT * FROM test_ret_rec_dyn(1500) AS (a int, b int, c int);
|
|
|
|
SELECT * FROM test_ret_rec_dyn(5) AS (a int, b numeric, c text);
|
2002-11-10 01:35:58 +01:00
|
|
|
|
2005-04-05 08:22:17 +02:00
|
|
|
--
|
2005-04-07 16:53:04 +02:00
|
|
|
-- Test handling of OUT parameters, including polymorphic cases.
|
|
|
|
-- Note that RETURN is optional with OUT params; we try both ways.
|
2005-04-05 08:22:17 +02:00
|
|
|
--
|
|
|
|
|
|
|
|
-- wrong way to do it:
|
|
|
|
create function f1(in i int, out j int) returns int as $$
|
|
|
|
begin
|
|
|
|
return i+1;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
create function f1(in i int, out j int) as $$
|
|
|
|
begin
|
|
|
|
j := i+1;
|
|
|
|
return;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select f1(42);
|
|
|
|
select * from f1(42);
|
|
|
|
|
|
|
|
create or replace function f1(inout i int) as $$
|
|
|
|
begin
|
|
|
|
i := i+1;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select f1(42);
|
|
|
|
select * from f1(42);
|
|
|
|
|
|
|
|
drop function f1(int);
|
|
|
|
|
|
|
|
create function f1(in i int, out j int) returns setof int as $$
|
|
|
|
begin
|
|
|
|
j := i+1;
|
|
|
|
return next;
|
|
|
|
j := i+2;
|
|
|
|
return next;
|
|
|
|
return;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select * from f1(42);
|
|
|
|
|
|
|
|
drop function f1(int);
|
|
|
|
|
|
|
|
create function f1(in i int, out j int, out k text) as $$
|
|
|
|
begin
|
|
|
|
j := i;
|
|
|
|
j := j+1;
|
|
|
|
k := 'foo';
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select f1(42);
|
|
|
|
select * from f1(42);
|
|
|
|
|
|
|
|
drop function f1(int);
|
|
|
|
|
|
|
|
create function f1(in i int, out j int, out k text) returns setof record as $$
|
|
|
|
begin
|
|
|
|
j := i+1;
|
|
|
|
k := 'foo';
|
|
|
|
return next;
|
|
|
|
j := j+1;
|
|
|
|
k := 'foot';
|
|
|
|
return next;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select * from f1(42);
|
|
|
|
|
|
|
|
drop function f1(int);
|
|
|
|
|
2005-07-01 22:29:02 +02:00
|
|
|
create function duplic(in i anyelement, out j anyelement, out k anyarray) as $$
|
2005-04-05 08:22:17 +02:00
|
|
|
begin
|
|
|
|
j := i;
|
|
|
|
k := array[j,j];
|
|
|
|
return;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
2005-07-01 22:29:02 +02:00
|
|
|
select * from duplic(42);
|
|
|
|
select * from duplic('foo'::text);
|
2005-04-05 08:22:17 +02:00
|
|
|
|
2005-07-01 22:29:02 +02:00
|
|
|
drop function duplic(anyelement);
|
2005-04-05 08:22:17 +02:00
|
|
|
|
2002-11-10 01:35:58 +01:00
|
|
|
--
|
|
|
|
-- test PERFORM
|
|
|
|
--
|
|
|
|
|
|
|
|
create table perform_test (
|
|
|
|
a INT,
|
|
|
|
b INT
|
|
|
|
);
|
|
|
|
|
|
|
|
create function simple_func(int) returns boolean as '
|
|
|
|
BEGIN
|
|
|
|
IF $1 < 20 THEN
|
|
|
|
INSERT INTO perform_test VALUES ($1, $1 + 10);
|
|
|
|
RETURN TRUE;
|
|
|
|
ELSE
|
|
|
|
RETURN FALSE;
|
|
|
|
END IF;
|
2006-02-27 17:09:50 +01:00
|
|
|
END;' language plpgsql;
|
2002-11-10 01:35:58 +01:00
|
|
|
|
|
|
|
create function perform_test_func() returns void as '
|
|
|
|
BEGIN
|
|
|
|
IF FOUND then
|
|
|
|
INSERT INTO perform_test VALUES (100, 100);
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
PERFORM simple_func(5);
|
|
|
|
|
|
|
|
IF FOUND then
|
|
|
|
INSERT INTO perform_test VALUES (100, 100);
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
PERFORM simple_func(50);
|
|
|
|
|
|
|
|
IF FOUND then
|
|
|
|
INSERT INTO perform_test VALUES (100, 100);
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
RETURN;
|
2006-02-27 17:09:50 +01:00
|
|
|
END;' language plpgsql;
|
2002-11-10 01:35:58 +01:00
|
|
|
|
|
|
|
SELECT perform_test_func();
|
|
|
|
SELECT * FROM perform_test;
|
|
|
|
|
2003-11-21 23:32:49 +01:00
|
|
|
drop table perform_test;
|
2004-07-31 09:39:21 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test error trapping
|
|
|
|
--
|
|
|
|
|
|
|
|
create function trap_zero_divide(int) returns int as $$
|
|
|
|
declare x int;
|
2004-08-01 01:04:58 +02:00
|
|
|
sx smallint;
|
2004-07-31 09:39:21 +02:00
|
|
|
begin
|
|
|
|
begin -- start a subtransaction
|
|
|
|
raise notice 'should see this';
|
|
|
|
x := 100 / $1;
|
|
|
|
raise notice 'should see this only if % <> 0', $1;
|
|
|
|
sx := $1;
|
|
|
|
raise notice 'should see this only if % fits in smallint', $1;
|
|
|
|
if $1 < 0 then
|
|
|
|
raise exception '% is less than zero', $1;
|
|
|
|
end if;
|
|
|
|
exception
|
|
|
|
when division_by_zero then
|
|
|
|
raise notice 'caught division_by_zero';
|
|
|
|
x := -1;
|
|
|
|
when NUMERIC_VALUE_OUT_OF_RANGE then
|
|
|
|
raise notice 'caught numeric_value_out_of_range';
|
|
|
|
x := -2;
|
|
|
|
end;
|
|
|
|
return x;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select trap_zero_divide(50);
|
|
|
|
select trap_zero_divide(0);
|
|
|
|
select trap_zero_divide(100000);
|
|
|
|
select trap_zero_divide(-100);
|
2004-08-01 01:04:58 +02:00
|
|
|
|
|
|
|
create function trap_matching_test(int) returns int as $$
|
|
|
|
declare x int;
|
|
|
|
sx smallint;
|
|
|
|
y int;
|
|
|
|
begin
|
|
|
|
begin -- start a subtransaction
|
|
|
|
x := 100 / $1;
|
|
|
|
sx := $1;
|
|
|
|
select into y unique1 from tenk1 where unique2 =
|
|
|
|
(select unique2 from tenk1 b where ten = $1);
|
|
|
|
exception
|
|
|
|
when data_exception then -- category match
|
|
|
|
raise notice 'caught data_exception';
|
|
|
|
x := -1;
|
|
|
|
when NUMERIC_VALUE_OUT_OF_RANGE OR CARDINALITY_VIOLATION then
|
|
|
|
raise notice 'caught numeric_value_out_of_range or cardinality_violation';
|
|
|
|
x := -2;
|
|
|
|
end;
|
|
|
|
return x;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select trap_matching_test(50);
|
|
|
|
select trap_matching_test(0);
|
|
|
|
select trap_matching_test(100000);
|
|
|
|
select trap_matching_test(1);
|
|
|
|
|
|
|
|
create temp table foo (f1 int);
|
|
|
|
|
|
|
|
create function blockme() returns int as $$
|
|
|
|
declare x int;
|
|
|
|
begin
|
|
|
|
x := 1;
|
|
|
|
insert into foo values(x);
|
|
|
|
begin
|
|
|
|
x := x + 1;
|
|
|
|
insert into foo values(x);
|
2006-06-18 18:21:23 +02:00
|
|
|
-- we assume this will take longer than 2 seconds:
|
2004-08-01 01:04:58 +02:00
|
|
|
select count(*) into x from tenk1 a, tenk1 b, tenk1 c;
|
|
|
|
exception
|
|
|
|
when others then
|
|
|
|
raise notice 'caught others?';
|
|
|
|
return -1;
|
|
|
|
when query_canceled then
|
|
|
|
raise notice 'nyeah nyeah, can''t stop me';
|
|
|
|
x := x * 10;
|
|
|
|
end;
|
|
|
|
insert into foo values(x);
|
|
|
|
return x;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
2006-06-18 18:21:23 +02:00
|
|
|
set statement_timeout to 2000;
|
2004-08-01 01:04:58 +02:00
|
|
|
|
|
|
|
select blockme();
|
|
|
|
|
|
|
|
reset statement_timeout;
|
|
|
|
|
|
|
|
select * from foo;
|
2004-09-10 20:40:09 +02:00
|
|
|
|
2006-08-14 23:14:42 +02:00
|
|
|
drop table foo;
|
|
|
|
|
2004-11-16 19:10:16 +01:00
|
|
|
-- Test for pass-by-ref values being stored in proper context
|
|
|
|
create function test_variable_storage() returns text as $$
|
|
|
|
declare x text;
|
|
|
|
begin
|
|
|
|
x := '1234';
|
|
|
|
begin
|
|
|
|
x := x || '5678';
|
|
|
|
-- force error inside subtransaction SPI context
|
|
|
|
perform trap_zero_divide(-100);
|
|
|
|
exception
|
|
|
|
when others then
|
|
|
|
x := x || '9012';
|
|
|
|
end;
|
|
|
|
return x;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select test_variable_storage();
|
|
|
|
|
2004-09-10 20:40:09 +02:00
|
|
|
--
|
|
|
|
-- test foreign key error trapping
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table master(f1 int primary key);
|
|
|
|
|
|
|
|
create temp table slave(f1 int references master deferrable);
|
|
|
|
|
|
|
|
insert into master values(1);
|
|
|
|
insert into slave values(1);
|
|
|
|
insert into slave values(2); -- fails
|
|
|
|
|
|
|
|
create function trap_foreign_key(int) returns int as $$
|
|
|
|
begin
|
|
|
|
begin -- start a subtransaction
|
|
|
|
insert into slave values($1);
|
|
|
|
exception
|
|
|
|
when foreign_key_violation then
|
|
|
|
raise notice 'caught foreign_key_violation';
|
|
|
|
return 0;
|
|
|
|
end;
|
|
|
|
return 1;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
create function trap_foreign_key_2() returns int as $$
|
|
|
|
begin
|
|
|
|
begin -- start a subtransaction
|
|
|
|
set constraints all immediate;
|
|
|
|
exception
|
|
|
|
when foreign_key_violation then
|
|
|
|
raise notice 'caught foreign_key_violation';
|
|
|
|
return 0;
|
|
|
|
end;
|
|
|
|
return 1;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select trap_foreign_key(1);
|
|
|
|
select trap_foreign_key(2); -- detects FK violation
|
|
|
|
|
|
|
|
begin;
|
|
|
|
set constraints all deferred;
|
|
|
|
select trap_foreign_key(2); -- should not detect FK violation
|
|
|
|
savepoint x;
|
|
|
|
set constraints all immediate; -- fails
|
|
|
|
rollback to x;
|
|
|
|
select trap_foreign_key_2(); -- detects FK violation
|
|
|
|
commit; -- still fails
|
|
|
|
|
|
|
|
drop function trap_foreign_key(int);
|
|
|
|
drop function trap_foreign_key_2();
|
2004-12-19 21:20:27 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test proper snapshot handling in simple expressions
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table users(login text, id serial);
|
|
|
|
|
|
|
|
create function sp_id_user(a_login text) returns int as $$
|
|
|
|
declare x int;
|
|
|
|
begin
|
|
|
|
select into x id from users where login = a_login;
|
|
|
|
if found then return x; end if;
|
|
|
|
return 0;
|
|
|
|
end$$ language plpgsql stable;
|
|
|
|
|
|
|
|
insert into users values('user1');
|
|
|
|
|
|
|
|
select sp_id_user('user1');
|
|
|
|
select sp_id_user('userx');
|
|
|
|
|
|
|
|
create function sp_add_user(a_login text) returns int as $$
|
|
|
|
declare my_id_user int;
|
|
|
|
begin
|
|
|
|
my_id_user = sp_id_user( a_login );
|
|
|
|
IF my_id_user > 0 THEN
|
|
|
|
RETURN -1; -- error code for existing user
|
|
|
|
END IF;
|
|
|
|
INSERT INTO users ( login ) VALUES ( a_login );
|
|
|
|
my_id_user = sp_id_user( a_login );
|
|
|
|
IF my_id_user = 0 THEN
|
|
|
|
RETURN -2; -- error code for insertion failure
|
|
|
|
END IF;
|
|
|
|
RETURN my_id_user;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select sp_add_user('user1');
|
|
|
|
select sp_add_user('user2');
|
|
|
|
select sp_add_user('user2');
|
2004-12-21 19:33:36 +01:00
|
|
|
select sp_add_user('user3');
|
|
|
|
select sp_add_user('user3');
|
2004-12-19 21:20:27 +01:00
|
|
|
|
|
|
|
drop function sp_add_user(text);
|
|
|
|
drop function sp_id_user(text);
|
2005-01-19 05:32:40 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- tests for refcursors
|
|
|
|
--
|
|
|
|
create table rc_test (a int, b int);
|
|
|
|
copy rc_test from stdin;
|
|
|
|
5 10
|
|
|
|
50 100
|
|
|
|
500 1000
|
|
|
|
\.
|
|
|
|
|
|
|
|
create function return_refcursor(rc refcursor) returns refcursor as $$
|
|
|
|
begin
|
|
|
|
open rc for select a from rc_test;
|
|
|
|
return rc;
|
|
|
|
end
|
2006-02-27 17:09:50 +01:00
|
|
|
$$ language plpgsql;
|
2005-01-19 05:32:40 +01:00
|
|
|
|
|
|
|
create function refcursor_test1(refcursor) returns refcursor as $$
|
|
|
|
begin
|
|
|
|
perform return_refcursor($1);
|
|
|
|
return $1;
|
|
|
|
end
|
2006-02-27 17:09:50 +01:00
|
|
|
$$ language plpgsql;
|
2005-01-19 05:32:40 +01:00
|
|
|
|
|
|
|
begin;
|
|
|
|
|
|
|
|
select refcursor_test1('test1');
|
2007-04-29 01:54:59 +02:00
|
|
|
fetch next in test1;
|
2005-01-19 05:32:40 +01:00
|
|
|
|
|
|
|
select refcursor_test1('test2');
|
|
|
|
fetch all from test2;
|
|
|
|
|
|
|
|
commit;
|
|
|
|
|
|
|
|
-- should fail
|
|
|
|
fetch next from test1;
|
|
|
|
|
2005-09-14 20:35:38 +02:00
|
|
|
create function refcursor_test2(int, int) returns boolean as $$
|
2005-01-19 05:32:40 +01:00
|
|
|
declare
|
2005-09-14 20:35:38 +02:00
|
|
|
c1 cursor (param1 int, param2 int) for select * from rc_test where a > param1 and b > param2;
|
2005-01-19 05:32:40 +01:00
|
|
|
nonsense record;
|
|
|
|
begin
|
2005-09-14 20:35:38 +02:00
|
|
|
open c1($1, $2);
|
2005-01-19 05:32:40 +01:00
|
|
|
fetch c1 into nonsense;
|
|
|
|
close c1;
|
|
|
|
if found then
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
end if;
|
|
|
|
end
|
2006-02-27 17:09:50 +01:00
|
|
|
$$ language plpgsql;
|
2005-01-19 05:32:40 +01:00
|
|
|
|
2005-09-14 20:35:38 +02:00
|
|
|
select refcursor_test2(20000, 20000) as "Should be false",
|
|
|
|
refcursor_test2(20, 20) as "Should be true";
|
This patch changes makes some significant changes to how compilation
and parsing work in PL/PgSQL:
- memory management is now done via palloc(). The compiled representation
of each function now has its own memory context. Therefore, the storage
consumed by a function can be reclaimed via MemoryContextDelete().
During compilation, the CurrentMemoryContext is the function's memory
context. This means that a palloc() is sufficient to allocate memory
that will have the same lifetime as the function itself. As a result,
code invoked during compilation should be careful to pfree() temporary
allocations to avoid leaking memory. Since a lot of the code in the
backend is not careful about releasing palloc'ed memory, that means
we should switch into a temporary memory context before invoking
backend functions. A temporary context appropriate for such allocations
is `compile_tmp_cxt'.
- The ability to use palloc() allows us to simply a lot of the code in
the parser. Rather than representing lists of elements via ad hoc
linked lists or arrays, we can use the List type. Rather than doing
malloc followed by memset(0), we can just use palloc0().
- We now check that the user has supplied the right number of parameters
to a RAISE statement. Supplying either too few or too many results in
an error (at runtime).
- PL/PgSQL's parser needs to accept arbitrary SQL statements. Since we
do not want to duplicate the SQL grammar in the PL/PgSQL grammar, this
means we need to be quite lax in what the PL/PgSQL grammar considers
a "SQL statement". This can lead to misleading behavior if there is a
syntax error in the function definition, since we assume a malformed
PL/PgSQL construct is a SQL statement. Furthermore, these errors were
only detected at runtime (when we tried to execute the alleged "SQL
statement" via SPI).
To rectify this, the patch changes the parser to invoke the main SQL
parser when it sees a string it believes to be a SQL expression. This
means that synctically-invalid SQL will be rejected during the
compilation of the PL/PgSQL function. This is only done when compiling
for "validation" purposes (i.e. at CREATE FUNCTION time), so it should
not impose a runtime overhead.
- Fixes for the various buffer overruns I've patched in stable branches
in the past few weeks. I've rewritten code where I thought it was
warranted (unlike the patches applied to older branches, which were
minimally invasive).
- Various other minor changes and cleanups.
- Updates to the regression tests.
2005-02-22 08:18:27 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- tests for "raise" processing
|
|
|
|
--
|
|
|
|
create function raise_test1(int) returns int as $$
|
|
|
|
begin
|
|
|
|
raise notice 'This message has too many parameters!', $1;
|
|
|
|
return $1;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select raise_test1(5);
|
|
|
|
|
|
|
|
create function raise_test2(int) returns int as $$
|
|
|
|
begin
|
|
|
|
raise notice 'This message has too few parameters: %, %, %', $1, $1;
|
|
|
|
return $1;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select raise_test2(10);
|
|
|
|
|
|
|
|
--
|
|
|
|
-- reject function definitions that contain malformed SQL queries at
|
|
|
|
-- compile-time, where possible
|
|
|
|
--
|
|
|
|
create function bad_sql1() returns int as $$
|
|
|
|
declare a int;
|
|
|
|
begin
|
|
|
|
a := 5;
|
|
|
|
Johnny Yuma;
|
|
|
|
a := 10;
|
|
|
|
return a;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
create function bad_sql2() returns int as $$
|
|
|
|
declare r record;
|
|
|
|
begin
|
|
|
|
for r in select I fought the law, the law won LOOP
|
|
|
|
raise notice 'in loop';
|
|
|
|
end loop;
|
|
|
|
return 5;
|
|
|
|
end;$$ language plpgsql;
|
|
|
|
|
|
|
|
-- a RETURN expression is mandatory, except for void-returning
|
|
|
|
-- functions, where it is not allowed
|
|
|
|
create function missing_return_expr() returns int as $$
|
|
|
|
begin
|
|
|
|
return ;
|
|
|
|
end;$$ language plpgsql;
|
|
|
|
|
|
|
|
create function void_return_expr() returns void as $$
|
|
|
|
begin
|
|
|
|
return 5;
|
2005-04-05 08:22:17 +02:00
|
|
|
end;$$ language plpgsql;
|
2005-04-07 16:53:04 +02:00
|
|
|
|
|
|
|
-- VOID functions are allowed to omit RETURN
|
|
|
|
create function void_return_expr() returns void as $$
|
|
|
|
begin
|
|
|
|
perform 2+2;
|
|
|
|
end;$$ language plpgsql;
|
|
|
|
|
|
|
|
select void_return_expr();
|
|
|
|
|
|
|
|
-- but ordinary functions are not
|
|
|
|
create function missing_return_expr() returns int as $$
|
|
|
|
begin
|
|
|
|
perform 2+2;
|
|
|
|
end;$$ language plpgsql;
|
|
|
|
|
|
|
|
select missing_return_expr();
|
|
|
|
|
|
|
|
drop function void_return_expr();
|
|
|
|
drop function missing_return_expr();
|
2005-06-07 04:47:23 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- EXECUTE ... INTO test
|
|
|
|
--
|
|
|
|
|
|
|
|
create table eifoo (i integer, y integer);
|
|
|
|
create type eitype as (i integer, y integer);
|
|
|
|
|
|
|
|
create or replace function execute_into_test(varchar) returns record as $$
|
|
|
|
declare
|
|
|
|
_r record;
|
|
|
|
_rt eifoo%rowtype;
|
|
|
|
_v eitype;
|
|
|
|
i int;
|
|
|
|
j int;
|
|
|
|
k int;
|
|
|
|
begin
|
|
|
|
execute 'insert into '||$1||' values(10,15)';
|
|
|
|
execute 'select (row).* from (select row(10,1)::eifoo) s' into _r;
|
|
|
|
raise notice '% %', _r.i, _r.y;
|
|
|
|
execute 'select * from '||$1||' limit 1' into _rt;
|
|
|
|
raise notice '% %', _rt.i, _rt.y;
|
|
|
|
execute 'select *, 20 from '||$1||' limit 1' into i, j, k;
|
|
|
|
raise notice '% % %', i, j, k;
|
|
|
|
execute 'select 1,2' into _v;
|
|
|
|
return _v;
|
|
|
|
end; $$ language plpgsql;
|
|
|
|
|
|
|
|
select execute_into_test('eifoo');
|
|
|
|
|
|
|
|
drop table eifoo cascade;
|
|
|
|
drop type eitype cascade;
|
2005-06-10 18:23:11 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- SQLSTATE and SQLERRM test
|
|
|
|
--
|
|
|
|
|
2005-06-14 08:43:15 +02:00
|
|
|
create function excpt_test1() returns void as $$
|
2005-06-10 18:23:11 +02:00
|
|
|
begin
|
|
|
|
raise notice '% %', sqlstate, sqlerrm;
|
|
|
|
end; $$ language plpgsql;
|
2005-06-14 08:43:15 +02:00
|
|
|
-- should fail: SQLSTATE and SQLERRM are only in defined EXCEPTION
|
|
|
|
-- blocks
|
|
|
|
select excpt_test1();
|
2005-06-10 18:23:11 +02:00
|
|
|
|
2005-06-14 08:43:15 +02:00
|
|
|
create function excpt_test2() returns void as $$
|
2005-06-10 18:23:11 +02:00
|
|
|
begin
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
raise notice '% %', sqlstate, sqlerrm;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end; $$ language plpgsql;
|
2005-06-14 08:43:15 +02:00
|
|
|
-- should fail
|
|
|
|
select excpt_test2();
|
2005-06-10 18:23:11 +02:00
|
|
|
|
2005-06-14 08:43:15 +02:00
|
|
|
create function excpt_test3() returns void as $$
|
2005-06-10 18:23:11 +02:00
|
|
|
begin
|
|
|
|
begin
|
|
|
|
raise exception 'user exception';
|
|
|
|
exception when others then
|
|
|
|
raise notice 'caught exception % %', sqlstate, sqlerrm;
|
|
|
|
begin
|
|
|
|
raise notice '% %', sqlstate, sqlerrm;
|
|
|
|
perform 10/0;
|
|
|
|
exception
|
|
|
|
when substring_error then
|
|
|
|
-- this exception handler shouldn't be invoked
|
|
|
|
raise notice 'unexpected exception: % %', sqlstate, sqlerrm;
|
|
|
|
when division_by_zero then
|
|
|
|
raise notice 'caught exception % %', sqlstate, sqlerrm;
|
|
|
|
end;
|
|
|
|
raise notice '% %', sqlstate, sqlerrm;
|
|
|
|
end;
|
|
|
|
end; $$ language plpgsql;
|
|
|
|
|
2005-06-14 08:43:15 +02:00
|
|
|
select excpt_test3();
|
|
|
|
drop function excpt_test1();
|
|
|
|
drop function excpt_test2();
|
|
|
|
drop function excpt_test3();
|
|
|
|
|
|
|
|
-- parameters of raise stmt can be expressions
|
|
|
|
create function raise_exprs() returns void as $$
|
|
|
|
declare
|
|
|
|
a integer[] = '{10,20,30}';
|
|
|
|
c varchar = 'xyz';
|
|
|
|
i integer;
|
|
|
|
begin
|
|
|
|
i := 2;
|
|
|
|
raise notice '%; %; %; %; %; %', a, a[i], c, (select c || 'abc'), row(10,'aaa',NULL,30), NULL;
|
|
|
|
end;$$ language plpgsql;
|
|
|
|
|
|
|
|
select raise_exprs();
|
|
|
|
drop function raise_exprs();
|
2005-06-22 03:35:03 +02:00
|
|
|
|
2005-07-02 10:59:48 +02:00
|
|
|
-- continue statement
|
2005-06-22 03:35:03 +02:00
|
|
|
create table conttesttbl(idx serial, v integer);
|
|
|
|
insert into conttesttbl(v) values(10);
|
|
|
|
insert into conttesttbl(v) values(20);
|
|
|
|
insert into conttesttbl(v) values(30);
|
|
|
|
insert into conttesttbl(v) values(40);
|
|
|
|
|
|
|
|
create function continue_test1() returns void as $$
|
|
|
|
declare _i integer = 0; _r record;
|
|
|
|
begin
|
|
|
|
raise notice '---1---';
|
|
|
|
loop
|
|
|
|
_i := _i + 1;
|
|
|
|
raise notice '%', _i;
|
|
|
|
continue when _i < 10;
|
|
|
|
exit;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
raise notice '---2---';
|
|
|
|
<<lbl>>
|
|
|
|
loop
|
|
|
|
_i := _i - 1;
|
|
|
|
loop
|
|
|
|
raise notice '%', _i;
|
|
|
|
continue lbl when _i > 0;
|
|
|
|
exit lbl;
|
|
|
|
end loop;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
raise notice '---3---';
|
|
|
|
<<the_loop>>
|
|
|
|
while _i < 10 loop
|
|
|
|
_i := _i + 1;
|
|
|
|
continue the_loop when _i % 2 = 0;
|
|
|
|
raise notice '%', _i;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
raise notice '---4---';
|
|
|
|
for _i in 1..10 loop
|
|
|
|
begin
|
|
|
|
-- applies to outer loop, not the nested begin block
|
2005-07-02 10:59:48 +02:00
|
|
|
continue when _i < 5;
|
2005-06-22 03:35:03 +02:00
|
|
|
raise notice '%', _i;
|
|
|
|
end;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
raise notice '---5---';
|
|
|
|
for _r in select * from conttesttbl loop
|
|
|
|
continue when _r.v <= 20;
|
|
|
|
raise notice '%', _r.v;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
raise notice '---6---';
|
|
|
|
for _r in execute 'select * from conttesttbl' loop
|
|
|
|
continue when _r.v <= 20;
|
|
|
|
raise notice '%', _r.v;
|
2005-06-22 09:28:47 +02:00
|
|
|
end loop;
|
|
|
|
|
|
|
|
raise notice '---7---';
|
|
|
|
for _i in 1..3 loop
|
|
|
|
raise notice '%', _i;
|
|
|
|
continue when _i = 3;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
raise notice '---8---';
|
|
|
|
_i := 1;
|
|
|
|
while _i <= 3 loop
|
|
|
|
raise notice '%', _i;
|
|
|
|
_i := _i + 1;
|
|
|
|
continue when _i = 3;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
raise notice '---9---';
|
|
|
|
for _r in select * from conttesttbl order by v limit 1 loop
|
|
|
|
raise notice '%', _r.v;
|
|
|
|
continue;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
raise notice '---10---';
|
|
|
|
for _r in execute 'select * from conttesttbl order by v limit 1' loop
|
|
|
|
raise notice '%', _r.v;
|
|
|
|
continue;
|
|
|
|
end loop;
|
2005-06-22 03:35:03 +02:00
|
|
|
end; $$ language plpgsql;
|
|
|
|
|
|
|
|
select continue_test1();
|
|
|
|
|
|
|
|
-- CONTINUE is only legal inside a loop
|
|
|
|
create function continue_test2() returns void as $$
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
continue;
|
|
|
|
end;
|
|
|
|
return;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
-- should fail
|
|
|
|
select continue_test2();
|
|
|
|
|
|
|
|
-- CONTINUE can't reference the label of a named block
|
|
|
|
create function continue_test3() returns void as $$
|
|
|
|
begin
|
|
|
|
<<begin_block1>>
|
|
|
|
begin
|
|
|
|
loop
|
|
|
|
continue begin_block1;
|
|
|
|
end loop;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
-- should fail
|
|
|
|
select continue_test3();
|
|
|
|
|
|
|
|
drop function continue_test1();
|
|
|
|
drop function continue_test2();
|
|
|
|
drop function continue_test3();
|
|
|
|
drop table conttesttbl;
|
2005-07-02 10:59:48 +02:00
|
|
|
|
|
|
|
-- verbose end block and end loop
|
|
|
|
create function end_label1() returns void as $$
|
|
|
|
<<blbl>>
|
|
|
|
begin
|
|
|
|
<<flbl1>>
|
|
|
|
for _i in 1 .. 10 loop
|
|
|
|
exit flbl1;
|
|
|
|
end loop flbl1;
|
|
|
|
<<flbl2>>
|
|
|
|
for _i in 1 .. 10 loop
|
|
|
|
exit flbl2;
|
|
|
|
end loop;
|
|
|
|
end blbl;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select end_label1();
|
|
|
|
drop function end_label1();
|
|
|
|
|
|
|
|
-- should fail: undefined end label
|
|
|
|
create function end_label2() returns void as $$
|
|
|
|
begin
|
|
|
|
for _i in 1 .. 10 loop
|
|
|
|
exit;
|
|
|
|
end loop flbl1;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
-- should fail: end label does not match start label
|
|
|
|
create function end_label3() returns void as $$
|
|
|
|
<<outer_label>>
|
|
|
|
begin
|
|
|
|
<<inner_label>>
|
|
|
|
for _i in 1 .. 10 loop
|
|
|
|
exit;
|
|
|
|
end loop outer_label;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
-- should fail: end label on a block without a start label
|
|
|
|
create function end_label4() returns void as $$
|
|
|
|
<<outer_label>>
|
|
|
|
begin
|
|
|
|
for _i in 1 .. 10 loop
|
|
|
|
exit;
|
|
|
|
end loop outer_label;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
2006-02-12 07:03:38 +01:00
|
|
|
|
|
|
|
-- using list of scalars in fori and fore stmts
|
2006-02-12 07:37:05 +01:00
|
|
|
create function for_vect() returns void as $proc$
|
2006-02-12 07:03:38 +01:00
|
|
|
<<lbl>>declare a integer; b varchar; c varchar; r record;
|
|
|
|
begin
|
2006-02-12 07:37:05 +01:00
|
|
|
-- fori
|
|
|
|
for i in 1 .. 3 loop
|
2006-02-12 07:03:38 +01:00
|
|
|
raise notice '%', i;
|
|
|
|
end loop;
|
2006-02-12 07:37:05 +01:00
|
|
|
-- fore with record var
|
|
|
|
for r in select gs as aa, 'BB' as bb, 'CC' as cc from generate_series(1,4) gs loop
|
|
|
|
raise notice '% % %', r.aa, r.bb, r.cc;
|
|
|
|
end loop;
|
|
|
|
-- fore with single scalar
|
|
|
|
for a in select gs from generate_series(1,4) gs loop
|
2006-02-12 07:03:38 +01:00
|
|
|
raise notice '%', a;
|
|
|
|
end loop;
|
2006-02-12 07:37:05 +01:00
|
|
|
-- fore with multiple scalars
|
|
|
|
for a,b,c in select gs, 'BB','CC' from generate_series(1,4) gs loop
|
2006-02-12 07:03:38 +01:00
|
|
|
raise notice '% % %', a, b, c;
|
|
|
|
end loop;
|
|
|
|
-- using qualified names in fors, fore is enabled, disabled only for fori
|
2006-02-12 07:37:05 +01:00
|
|
|
for lbl.a, lbl.b, lbl.c in execute $$select gs, 'bb','cc' from generate_series(1,4) gs$$ loop
|
2006-02-12 07:03:38 +01:00
|
|
|
raise notice '% % %', a, b, c;
|
|
|
|
end loop;
|
|
|
|
end;
|
2006-02-12 07:37:05 +01:00
|
|
|
$proc$ language plpgsql;
|
|
|
|
|
|
|
|
select for_vect();
|
2006-03-23 05:22:37 +01:00
|
|
|
|
|
|
|
-- regression test: verify that multiple uses of same plpgsql datum within
|
|
|
|
-- a SQL command all get mapped to the same $n parameter. The return value
|
|
|
|
-- of the SELECT is not important, we only care that it doesn't fail with
|
|
|
|
-- a complaint about an ungrouped column reference.
|
|
|
|
create function multi_datum_use(p1 int) returns bool as $$
|
|
|
|
declare
|
|
|
|
x int;
|
|
|
|
y int;
|
|
|
|
begin
|
|
|
|
select into x,y unique1/p1, unique1/$1 from tenk1 group by unique1/p1;
|
|
|
|
return x = y;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select multi_datum_use(42);
|
2006-08-14 23:14:42 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Test STRICT limiter in both planned and EXECUTE invocations.
|
|
|
|
-- Note that a data-modifying query is quasi strict (disallow multi rows)
|
|
|
|
-- by default in the planned case, but not in EXECUTE.
|
|
|
|
--
|
|
|
|
|
|
|
|
create temp table foo (f1 int, f2 int);
|
|
|
|
|
|
|
|
insert into foo values (1,2), (3,4);
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- should work
|
|
|
|
insert into foo values(5,6) returning * into x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- should fail due to implicit strict
|
|
|
|
insert into foo values(7,8),(9,10) returning * into x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- should work
|
|
|
|
execute 'insert into foo values(5,6) returning *' into x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- this should work since EXECUTE isn't as picky
|
|
|
|
execute 'insert into foo values(7,8),(9,10) returning *' into x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
select * from foo;
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- should work
|
|
|
|
select * from foo where f1 = 3 into strict x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- should fail, no rows
|
|
|
|
select * from foo where f1 = 0 into strict x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- should fail, too many rows
|
|
|
|
select * from foo where f1 > 3 into strict x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- should work
|
|
|
|
execute 'select * from foo where f1 = 3' into strict x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- should fail, no rows
|
|
|
|
execute 'select * from foo where f1 = 0' into strict x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
create or replace function footest() returns void as $$
|
|
|
|
declare x record;
|
|
|
|
begin
|
|
|
|
-- should fail, too many rows
|
|
|
|
execute 'select * from foo where f1 > 3' into strict x;
|
|
|
|
raise notice 'x.f1 = %, x.f2 = %', x.f1, x.f2;
|
|
|
|
end$$ language plpgsql;
|
|
|
|
|
|
|
|
select footest();
|
|
|
|
|
|
|
|
drop function footest();
|
2007-04-16 19:21:24 +02:00
|
|
|
|
|
|
|
-- test scrollable cursor support
|
|
|
|
|
|
|
|
create function sc_test() returns setof integer as $$
|
|
|
|
declare
|
|
|
|
c scroll cursor for select f1 from int4_tbl;
|
|
|
|
x integer;
|
|
|
|
begin
|
|
|
|
open c;
|
|
|
|
fetch last from c into x;
|
|
|
|
while found loop
|
|
|
|
return next x;
|
|
|
|
fetch prior from c into x;
|
|
|
|
end loop;
|
|
|
|
close c;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select * from sc_test();
|
|
|
|
|
|
|
|
create or replace function sc_test() returns setof integer as $$
|
|
|
|
declare
|
|
|
|
c no scroll cursor for select f1 from int4_tbl;
|
|
|
|
x integer;
|
|
|
|
begin
|
|
|
|
open c;
|
|
|
|
fetch last from c into x;
|
|
|
|
while found loop
|
|
|
|
return next x;
|
|
|
|
fetch prior from c into x;
|
|
|
|
end loop;
|
|
|
|
close c;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select * from sc_test(); -- fails because of NO SCROLL specification
|
|
|
|
|
|
|
|
create or replace function sc_test() returns setof integer as $$
|
|
|
|
declare
|
|
|
|
c refcursor;
|
|
|
|
x integer;
|
|
|
|
begin
|
|
|
|
open c scroll for select f1 from int4_tbl;
|
|
|
|
fetch last from c into x;
|
|
|
|
while found loop
|
|
|
|
return next x;
|
|
|
|
fetch prior from c into x;
|
|
|
|
end loop;
|
|
|
|
close c;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select * from sc_test();
|
|
|
|
|
|
|
|
create or replace function sc_test() returns setof integer as $$
|
|
|
|
declare
|
|
|
|
c refcursor;
|
|
|
|
x integer;
|
|
|
|
begin
|
|
|
|
open c scroll for execute 'select f1 from int4_tbl';
|
|
|
|
fetch last from c into x;
|
|
|
|
while found loop
|
|
|
|
return next x;
|
|
|
|
fetch relative -2 from c into x;
|
|
|
|
end loop;
|
|
|
|
close c;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select * from sc_test();
|
|
|
|
|
2007-04-29 03:21:09 +02:00
|
|
|
create or replace function sc_test() returns setof integer as $$
|
|
|
|
declare
|
|
|
|
c cursor for select * from generate_series(1, 10);
|
|
|
|
x integer;
|
|
|
|
begin
|
|
|
|
open c;
|
|
|
|
loop
|
|
|
|
move relative 2 in c;
|
|
|
|
if not found then
|
|
|
|
exit;
|
|
|
|
end if;
|
|
|
|
fetch next from c into x;
|
|
|
|
if found then
|
|
|
|
return next x;
|
|
|
|
end if;
|
|
|
|
end loop;
|
|
|
|
close c;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select * from sc_test();
|
|
|
|
|
2007-04-16 19:21:24 +02:00
|
|
|
drop function sc_test();
|
2007-04-29 03:21:09 +02:00
|
|
|
|
2007-07-16 19:01:11 +02:00
|
|
|
-- test qualified variable names
|
|
|
|
|
|
|
|
create function pl_qual_names (param1 int) returns void as $$
|
|
|
|
<<outerblock>>
|
|
|
|
declare
|
|
|
|
param1 int := 1;
|
|
|
|
begin
|
|
|
|
<<innerblock>>
|
|
|
|
declare
|
|
|
|
param1 int := 2;
|
|
|
|
begin
|
|
|
|
raise notice 'param1 = %', param1;
|
|
|
|
raise notice 'pl_qual_names.param1 = %', pl_qual_names.param1;
|
|
|
|
raise notice 'outerblock.param1 = %', outerblock.param1;
|
|
|
|
raise notice 'innerblock.param1 = %', innerblock.param1;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select pl_qual_names(42);
|
|
|
|
|
|
|
|
drop function pl_qual_names(int);
|
2007-07-25 06:19:09 +02:00
|
|
|
|
|
|
|
-- tests for RETURN QUERY
|
|
|
|
create function ret_query1(out int, out int) returns setof record as $$
|
|
|
|
begin
|
|
|
|
$1 := -1;
|
|
|
|
$2 := -2;
|
|
|
|
return next;
|
|
|
|
return query select x + 1, x * 10 from generate_series(0, 10) s (x);
|
|
|
|
return next;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select * from ret_query1();
|
|
|
|
|
|
|
|
create type record_type as (x text, y int, z boolean);
|
|
|
|
|
|
|
|
create or replace function ret_query2(lim int) returns setof record_type as $$
|
|
|
|
begin
|
|
|
|
return query select md5(s.x::text), s.x, s.x > 0
|
|
|
|
from generate_series(-8, lim) s (x) where s.x % 2 = 0;
|
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
2008-04-01 05:51:09 +02:00
|
|
|
select * from ret_query2(8);
|
|
|
|
|
|
|
|
-- test EXECUTE USING
|
|
|
|
create function exc_using(int, text) returns int as $$
|
|
|
|
declare i int;
|
|
|
|
begin
|
|
|
|
for i in execute 'select * from generate_series(1,$1)' using $1+1 loop
|
|
|
|
raise notice '%', i;
|
|
|
|
end loop;
|
|
|
|
execute 'select $2 + $2*3 + length($1)' into i using $2,$1;
|
|
|
|
return i;
|
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
select exc_using(5, 'foobar');
|