Index: openacs-4/packages/acs-kernel/sql/oracle/acs-permissions-create.sql =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-kernel/sql/oracle/acs-permissions-create.sql,v diff -u -r1.5 -r1.6 --- openacs-4/packages/acs-kernel/sql/oracle/acs-permissions-create.sql 17 Mar 2003 21:58:22 -0000 1.5 +++ openacs-4/packages/acs-kernel/sql/oracle/acs-permissions-create.sql 21 Mar 2003 14:57:44 -0000 1.6 @@ -11,7 +11,7 @@ -- -- @creation-date 2000-08-13 -- --- @cvs-id acs-permissions-create.sql,v 1.10.2.2 2001/01/12 22:59:20 oumi Exp +-- @cvs-id $Id$ -- @@ -37,225 +37,24 @@ --' create table acs_privileges ( - privilege varchar(100) not null constraint acs_privileges_pk + privilege varchar2(100) not null constraint acs_privileges_pk primary key, - pretty_name varchar(100), - pretty_plural varchar(100) + pretty_name varchar2(100), + pretty_plural varchar2(100) ); create table acs_privilege_hierarchy ( - privilege varchar(100) not null - constraint acs_priv_hier_priv_fk + privilege not null constraint acs_priv_hier_priv_fk references acs_privileges (privilege), - child_privilege varchar(100) not null - constraint acs_priv_hier_child_priv_fk + child_privilege not null constraint acs_priv_hier_child_priv_fk references acs_privileges (privilege), constraint acs_privilege_hierarchy_pk primary key (privilege, child_privilege) ); +-- create bitmap index acs_priv_hier_child_priv_idx on acs_privilege_hierarchy (child_privilege); create index acs_priv_hier_child_priv_idx on acs_privilege_hierarchy (child_privilege); -create table acs_privilege_hierarchy_index ( - privilege varchar(100) not null - constraint acs_priv_hier_priv_fk - references acs_privileges (privilege), - child_privilege varchar(100) not null - constraint acs_priv_hier_child_priv_fk - references acs_privileges (privilege), - tree_sortkey varbit -); - -create index priv_hier_sortkey_idx on -acs_privilege_hierarchy_index (tree_sortkey); - --- Added table to materialize view that previously used --- acs_privilege_descendant_map name --- --- DanW (dcwickstrom@earthlink.net) 30 Jan, 2003 - -create table acs_privilege_descendant_map ( - privilege varchar(100) not null - constraint acs_priv_hier_priv_fk - references acs_privileges (privilege), - descendant varchar(100) not null - constraint acs_priv_hier_child_priv_fk - references acs_privileges (privilege) - -); - --- DRB: Empirical testing showed that even with just 61 entries in the new table --- this index sped things up by roughly 15% - -create index acs_priv_desc_map_idx on acs_privilege_descendant_map(descendant); - --- This trigger is used to create a pseudo-tree hierarchy that --- can be used to emulate tree queries on the acs_privilege_hierarchy table. --- The acs_privilege_hierarchy table maintains the permissions structure, but --- it has a complication in that the same privileges can exist in more than one --- path in the tree. As such, tree queries cannot be represented by the --- usual tree query methods used for openacs. - --- DCW, 2001-03-15. - --- usage: queries directly on acs_privilege_hierarchy don't seem to occur --- in many places. Rather it seems that acs_privilege_hierarchy is --- used to build the view: acs_privilege_descendant_map. I did however --- find one tree query in content-perms.sql that looks like the --- following: - --- select privilege, child_privilege from acs_privilege_hierarchy --- connect by prior privilege = child_privilege --- start with child_privilege = 'cm_perm' - --- This query is used to find all of the ancestor permissions of 'cm_perm'. --- The equivalent query for the postgresql tree-query model would be: - --- select h2.privilege --- from acs_privilege_hierarchy_index h1, --- acs_privilege_hierarchy_index h2 --- where h1.child_privilege = 'cm_perm' --- and h1.tree_sortkey between h2.tree_sortkey and tree_right(h2.tree_sortkey) --- and h2.tree_sortkey <> h1.tree_sortkey; - --- Also since acs_privilege_descendant_map is simply a path enumeration of --- acs_privilege_hierarchy, we should be able to replace the above connect-by --- with: - --- select privilege --- from acs_privilege_descendant_map --- where descendant = 'cm_perm' - --- This would be better, since the same query could be used for both oracle --- and postgresql. - -create or replace function acs_priv_hier_ins_del_tr () returns opaque as ' -declare - new_value integer; - new_key varbit default null; - v_rec record; - deleted_p boolean; -begin - - -- if more than one node was deleted the second trigger call - -- will error out. This check avoids that problem. - - if TG_OP = ''DELETE'' then - select count(*) = 0 into deleted_p - from acs_privilege_hierarchy_index - where old.privilege = privilege - and old.child_privilege = child_privilege; - - if deleted_p then - - return new; - - end if; - end if; - - -- recalculate the table from scratch. - - delete from acs_privilege_hierarchy_index; - - -- first find the top nodes of the tree - - for v_rec in select privilege, child_privilege - from acs_privilege_hierarchy - where privilege - NOT in (select distinct child_privilege - from acs_privilege_hierarchy) - - LOOP - - -- top level node, so find the next key at this level. - - select max(tree_leaf_key_to_int(tree_sortkey)) into new_value - from acs_privilege_hierarchy_index - where tree_level(tree_sortkey) = 1; - - -- insert the new node - - insert into acs_privilege_hierarchy_index - (privilege, child_privilege, tree_sortkey) - values - (v_rec.privilege, v_rec.child_privilege, tree_next_key(null, new_value)); - - -- now recurse down from this node - - PERFORM priv_recurse_subtree(tree_next_key(null, new_value), v_rec.child_privilege); - - end LOOP; - - -- materialize the map view to speed up queries - -- DanW (dcwickstrom@earthlink.net) 30 Jan, 2003 - delete from acs_privilege_descendant_map; - - insert into acs_privilege_descendant_map (privilege, descendant) - select privilege, descendant from acs_privilege_descendant_map_view; - - return new; - -end;' language 'plpgsql'; - -create trigger acs_priv_hier_ins_del_tr after insert or delete -on acs_privilege_hierarchy for each row -execute procedure acs_priv_hier_ins_del_tr (); - -create function priv_recurse_subtree(varbit, varchar) -returns integer as ' -declare - nkey alias for $1; - child_priv alias for $2; - new_value integer; - v_rec record; - new_key varbit; -begin - - -- now iterate over all of the children of the - -- previous node. - - for v_rec in select privilege, child_privilege - from acs_privilege_hierarchy - where privilege = child_priv - - LOOP - - -- calculate the next key for this level and parent - - select max(tree_leaf_key_to_int(tree_sortkey)) into new_value - from acs_privilege_hierarchy_index - where tree_sortkey between nkey and tree_right(nkey) - and tree_level(tree_sortkey) = tree_level(nkey) + 1; - - new_key := tree_next_key(nkey, new_value); - - -- insert the new child node. - - insert into acs_privilege_hierarchy_index - (privilege, child_privilege, tree_sortkey) - values - (v_rec.privilege, v_rec.child_privilege, new_key); - - -- keep recursing down until no more children are found - - PERFORM priv_recurse_subtree(new_key, v_rec.child_privilege); - - end LOOP; - - -- no children found, so insert the child node as its own separate - -- node. - - if NOT FOUND then - insert into acs_privilege_hierarchy_index - (privilege, child_privilege, tree_sortkey) - values - (child_priv, child_priv, tree_next_key(nkey, null)); - end if; - - return null; - -end;' language 'plpgsql'; - --create table acs_privilege_method_rules ( -- privilege not null constraint acs_priv_method_rules_priv_fk -- references acs_privileges (privilege), @@ -294,228 +93,245 @@ -- were granted moderate on a user. --' -create function acs_privilege__create_privilege (varchar,varchar,varchar) -returns integer as ' -declare - create_privilege__privilege alias for $1; - create_privilege__pretty_name alias for $2; -- default null - create_privilege__pretty_plural alias for $3; -- default null -begin +--create or replace view acs_privilege_method_map +--as select r1.privilege, pmr.object_type, pmr.method +-- from acs_privileges r1, acs_privileges r2, acs_privilege_method_rules pmr +-- where r2.privilege in (select distinct rh.child_privilege +-- from acs_privilege_hierarchy rh +-- start with privilege = r1.privilege +-- connect by prior child_privilege = privilege +-- union +-- select r1.privilege +-- from dual) +-- and r2.privilege = pmr.privilege; + +create or replace package acs_privilege +as + + procedure create_privilege ( + privilege in acs_privileges.privilege%TYPE, + pretty_name in acs_privileges.pretty_name%TYPE default null, + pretty_plural in acs_privileges.pretty_plural%TYPE default null + ); + + procedure drop_privilege ( + privilege in acs_privileges.privilege%TYPE + ); + + procedure add_child ( + privilege in acs_privileges.privilege%TYPE, + child_privilege in acs_privileges.privilege%TYPE + ); + + procedure remove_child ( + privilege in acs_privileges.privilege%TYPE, + child_privilege in acs_privileges.privilege%TYPE + ); + +end; +/ +show errors + +create or replace package body acs_privilege +as + + procedure create_privilege ( + privilege in acs_privileges.privilege%TYPE, + pretty_name in acs_privileges.pretty_name%TYPE default null, + pretty_plural in acs_privileges.pretty_plural%TYPE default null + ) + is + begin insert into acs_privileges (privilege, pretty_name, pretty_plural) values - (create_privilege__privilege, - create_privilege__pretty_name, - create_privilege__pretty_plural); - - return 0; -end;' language 'plpgsql'; + (create_privilege.privilege, + create_privilege.pretty_name, + create_privilege.pretty_plural); + end; -create function acs_privilege__create_privilege (varchar) -returns integer as ' -declare - create_privilege__privilege alias for $1; -begin - return acs_privilege__create_privilege(create_privilege__privilege, null, null); -end;' language 'plpgsql'; - - -create function acs_privilege__drop_privilege (varchar) -returns integer as ' -declare - drop_privilege__privilege alias for $1; -begin + procedure drop_privilege ( + privilege in acs_privileges.privilege%TYPE + ) + is + begin delete from acs_privileges - where privilege = drop_privilege__privilege; + where privilege = drop_privilege.privilege; + end; - return 0; -end;' language 'plpgsql'; - -create function acs_privilege__add_child (varchar,varchar) -returns integer as ' -declare - add_child__privilege alias for $1; - add_child__child_privilege alias for $2; -begin + procedure add_child ( + privilege in acs_privileges.privilege%TYPE, + child_privilege in acs_privileges.privilege%TYPE + ) + is + begin insert into acs_privilege_hierarchy (privilege, child_privilege) values - (add_child__privilege, add_child__child_privilege); + (add_child.privilege, add_child.child_privilege); + end; - return 0; -end;' language 'plpgsql'; - -create function acs_privilege__remove_child (varchar,varchar) -returns integer as ' -declare - remove_child__privilege alias for $1; - remove_child__child_privilege alias for $2; -begin + procedure remove_child ( + privilege in acs_privileges.privilege%TYPE, + child_privilege in acs_privileges.privilege%TYPE + ) + is + begin delete from acs_privilege_hierarchy - where privilege = remove_child__privilege - and child_privilege = remove_child__child_privilege; + where privilege = remove_child.privilege + and child_privilege = remove_child.child_privilege; + end; - return 0; -end;' language 'plpgsql'; +end; +/ +show errors + ------------------------------------ -- OPERATIONAL LEVEL: PERMISSIONS -- ------------------------------------ create table acs_permissions ( - object_id integer not null + object_id not null constraint acs_permissions_on_what_id_fk references acs_objects (object_id), - grantee_id integer not null + grantee_id not null constraint acs_permissions_grantee_id_fk references parties (party_id), - privilege varchar(100) not null - constraint acs_permissions_priv_fk + privilege not null constraint acs_permissions_priv_fk references acs_privileges (privilege), constraint acs_permissions_pk primary key (object_id, grantee_id, privilege) ); create index acs_permissions_grantee_idx on acs_permissions (grantee_id); +-- create bitmap index acs_permissions_privilege_idx on acs_permissions (privilege); create index acs_permissions_privilege_idx on acs_permissions (privilege); --- Added table to materialize view that previously used --- acs_privilege_descendant_map name --- --- DanW (dcwickstrom@earthlink.net) 30 Jan, 2003 +create or replace view acs_privilege_descendant_map +as select p1.privilege, p2.privilege as descendant + from acs_privileges p1, acs_privileges p2 + where p2.privilege in (select child_privilege + from acs_privilege_hierarchy + start with privilege = p1.privilege + connect by prior child_privilege = privilege) + or p2.privilege = p1.privilege; --- DRB: I switched this to UNION form because the old view was incredibly --- slow and caused installation of packages to take exponentially increasing --- time. No code should be querying against this view other than the --- trigger that recreates the denormalized map anyway ... - -create view acs_privilege_descendant_map_view -as select distinct h1.privilege, h2.child_privilege as descendant - from acs_privilege_hierarchy_index h1, acs_privilege_hierarchy_index h2 - where h2.tree_sortkey between h1.tree_sortkey and tree_right(h1.tree_sortkey) - union - select privilege, privilege - from acs_privileges; - -create view acs_permissions_all +create or replace view acs_permissions_all as select op.object_id, p.grantee_id, p.privilege from acs_object_paths op, acs_permissions p where op.ancestor_id = p.object_id; -create view acs_object_grantee_priv_map +create or replace view acs_object_grantee_priv_map as select a.object_id, a.grantee_id, m.descendant as privilege from acs_permissions_all a, acs_privilege_descendant_map m where a.privilege = m.privilege; --- New fast version of acs_object_party_privilege_map +-- Fast new acs_object_party_privilege_map based on the denormalized +-- party_approved_member_map. You may now use this map without fear. -create view acs_object_party_privilege_map as +create or replace view acs_object_party_privilege_map as select c.object_id, pdm.descendant as privilege, pamm.member_id as party_id from acs_object_context_index c, acs_permissions p, acs_privilege_descendant_map pdm, party_approved_member_map pamm where c.ancestor_id = p.object_id and pdm.privilege = p.privilege and pamm.party_id = p.grantee_id; -create view all_object_party_privilege_map as +-- Kept to avoid breaking existing code, should eventually go away. + +create or replace view all_object_party_privilege_map as select * from acs_object_party_privilege_map; --- This table acts as a mutex for inserts/deletes from acs_permissions. --- This is used since postgresql's exception handing mechanism is non- --- existant. A dup insert on acs_permissions will roll-back the --- transaction and give an error, which is not what we want. Using a --- separate table for locking allows us exclusive access for --- inserts/deletes, but does not block readers. That way we don't --- slow down permissions-checking which is known to have performance --- problems already. +--create or replace view acs_object_party_method_map +--as select opp.object_id, opp.party_id, pm.object_type, pm.method +-- from acs_object_party_privilege_map opp, acs_privilege_method_map pm +-- where opp.privilege = pm.privilege; --- (OpenACS - DanW) +create or replace package acs_permission +as -create table acs_permissions_lock ( - lck integer -); + procedure grant_permission ( + object_id acs_permissions.object_id%TYPE, + grantee_id acs_permissions.grantee_id%TYPE, + privilege acs_permissions.privilege%TYPE + ); -create function acs_permissions_lock_tr () returns opaque as ' -begin - raise EXCEPTION ''FOR LOCKING ONLY, NO DML STATEMENTS ALLOWED''; - return null; -end;' language 'plpgsql'; + procedure revoke_permission ( + object_id acs_permissions.object_id%TYPE, + grantee_id acs_permissions.grantee_id%TYPE, + privilege acs_permissions.privilege%TYPE + ); -create trigger acs_permissions_lock_tr -before insert or update or delete on acs_permissions_lock -for each row execute procedure acs_permissions_lock_tr(); + function permission_p ( + object_id acs_objects.object_id%TYPE, + party_id parties.party_id%TYPE, + privilege acs_privileges.privilege%TYPE + ) return char; -create function acs_permission__grant_permission (integer, integer, varchar) -returns integer as ' -declare - grant_permission__object_id alias for $1; - grant_permission__grantee_id alias for $2; - grant_permission__privilege alias for $3; - exists_p boolean; -begin - lock table acs_permissions_lock; +end acs_permission; +/ +show errors - select count(*) > 0 into exists_p - from acs_permissions - where object_id = grant_permission__object_id - and grantee_id = grant_permission__grantee_id - and privilege = grant_permission__privilege; +create or replace package body acs_permission +as + procedure grant_permission ( + object_id acs_permissions.object_id%TYPE, + grantee_id acs_permissions.grantee_id%TYPE, + privilege acs_permissions.privilege%TYPE + ) + as + begin + insert into acs_permissions + (object_id, grantee_id, privilege) + values + (object_id, grantee_id, privilege); + exception + when dup_val_on_index then + return; + end grant_permission; + -- + procedure revoke_permission ( + object_id acs_permissions.object_id%TYPE, + grantee_id acs_permissions.grantee_id%TYPE, + privilege acs_permissions.privilege%TYPE + ) + as + begin + delete from acs_permissions + where object_id = revoke_permission.object_id + and grantee_id = revoke_permission.grantee_id + and privilege = revoke_permission.privilege; + end revoke_permission; - if not exists_p then + function permission_p ( + object_id acs_objects.object_id%TYPE, + party_id parties.party_id%TYPE, + privilege acs_privileges.privilege%TYPE + ) return char + as + exists_p char(1); + begin - insert into acs_permissions - (object_id, grantee_id, privilege) - values - (grant_permission__object_id, grant_permission__grantee_id, - grant_permission__privilege); + select decode(count(*),0,'f','t') into exists_p + from dual where exists + (select 1 + from acs_permissions p, party_approved_member_map m, + acs_object_context_index c, acs_privilege_descendant_map h + where p.object_id = c.ancestor_id + and h.descendant = permission_p.privilege + and c.object_id = permission_p.object_id + and m.member_id = permission_p.party_id + and p.privilege = h.privilege + and p.grantee_id = m.party_id); - end if; + return exists_p; - -- exception - -- when dup_val_on_index then - -- return; + end permission_p; + -- +end acs_permission; +/ +show errors - return 0; -end;' language 'plpgsql'; - - --- procedure revoke_permission -create function acs_permission__revoke_permission (integer, integer, varchar) -returns integer as ' -declare - revoke_permission__object_id alias for $1; - revoke_permission__grantee_id alias for $2; - revoke_permission__privilege alias for $3; -begin - lock table acs_permissions_lock; - - delete from acs_permissions - where object_id = revoke_permission__object_id - and grantee_id = revoke_permission__grantee_id - and privilege = revoke_permission__privilege; - - return 0; -end;' language 'plpgsql'; - --- Really speedy version of permission_p written by Don Baccus - -create or replace function acs_permission__permission_p (integer,integer,varchar) -returns boolean as ' -declare - permission_p__object_id alias for $1; - permission_p__party_id alias for $2; - permission_p__privilege alias for $3; - exists_p boolean; -begin - return exists (select 1 - from acs_permissions p, party_approved_member_map m, - acs_object_context_index c, acs_privilege_descendant_map h - where p.object_id = c.ancestor_id - and h.descendant = permission_p__privilege - and c.object_id = permission_p__object_id - and m.member_id = permission_p__party_id - and p.privilege = h.privilege - and p.grantee_id = m.party_id); -end;' language 'plpgsql'; Index: openacs-4/packages/acs-kernel/sql/oracle/groups-body-create.sql =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-kernel/sql/oracle/groups-body-create.sql,v diff -u -r1.5 -r1.6 --- openacs-4/packages/acs-kernel/sql/oracle/groups-body-create.sql 17 Mar 2003 21:58:22 -0000 1.5 +++ openacs-4/packages/acs-kernel/sql/oracle/groups-body-create.sql 21 Mar 2003 14:57:44 -0000 1.6 @@ -3,174 +3,145 @@ -- -- @author rhs@mit.edu -- @creation-date 2000-08-22 --- @cvs-id groups-body-create.sql,v 1.1.4.1 2001/01/12 22:58:33 mbryzek Exp +-- @cvs-id $Id$ -- -------------- -- TRIGGERS -- -------------- --- The insert trigger was dummied up in groups-create.sql, so we just need --- to replace the trigger function, not create the trigger. +create or replace trigger membership_rels_up_tr +before update on membership_rels +for each row +begin + + if :new.member_state = :old.member_state then + return; + end if; --- However, PG 7.3 introduces a new type "trigger" for the return type --- needed for functions called by triggers. "create function" transmorgifies --- the return type "opaque" to "trigger" so PG 7.2 dumps can be restored into --- PG 7.3. But "create or replace" doesn't do it. We can't use "trigger" --- because we currently are still supporting PG 7.2. Isn't life a pleasure? + for map in (select group_id, element_id, rel_type + from group_element_index + where rel_id = :new.rel_id) + loop + if :new.member_state = 'approved' then + party_approved_member.add(map.group_id, map.element_id, map.rel_type); + else + party_approved_member.remove(map.group_id, map.element_id, map.rel_type); + end if; + end loop; --- I'm leaving the triggers we aren't overriding as "create or replace" because --- this will be the right thing to do if the PG folks fix this problem or when --- we drop support of PG 7.2 and no longer need to declare these as type "opaque" +end; +/ +show errors -drop trigger membership_rels_in_tr on membership_rels; -drop function membership_rels_in_tr (); - -create or replace function membership_rels_in_tr () returns opaque as ' +create or replace trigger membership_rels_in_tr +after insert on membership_rels +for each row declare v_object_id_one acs_rels.object_id_one%TYPE; v_object_id_two acs_rels.object_id_two%TYPE; v_rel_type acs_rels.rel_type%TYPE; - v_error text; - map record; + v_error varchar2(4000); begin -- First check if added this relation violated any relational constraints - v_error := rel_constraint__violation(new.rel_id); + v_error := rel_constraint.violation(:new.rel_id); if v_error is not null then - raise EXCEPTION ''-20000: %'', v_error; + raise_application_error(-20000,v_error); end if; select object_id_one, object_id_two, rel_type into v_object_id_one, v_object_id_two, v_rel_type from acs_rels - where rel_id = new.rel_id; + where rel_id = :new.rel_id; - -- Insert a row for me in the group_element_index. + -- Insert a row for me in the group_member_index. insert into group_element_index (group_id, element_id, rel_id, container_id, rel_type, ancestor_rel_type) values - (v_object_id_one, v_object_id_two, new.rel_id, v_object_id_one, - v_rel_type, ''membership_rel''); + (v_object_id_one, v_object_id_two, :new.rel_id, v_object_id_one, + v_rel_type, 'membership_rel'); - if new.member_state = ''approved'' then - perform party_approved_member__add(v_object_id_one, v_object_id_two, new.rel_id, v_rel_type); + if :new.member_state = 'approved' then + party_approved_member.add(v_object_id_one, v_object_id_two, v_rel_type); end if; -- For all groups of which I am a component, insert a - -- row in the group_element_index. - for map in select distinct group_id + -- row in the group_member_index. + for map in (select distinct group_id from group_component_map - where component_id = v_object_id_one - loop - + where component_id = v_object_id_one) loop insert into group_element_index (group_id, element_id, rel_id, container_id, rel_type, ancestor_rel_type) values - (map.group_id, v_object_id_two, new.rel_id, v_object_id_one, - v_rel_type, ''membership_rel''); + (map.group_id, v_object_id_two, :new.rel_id, v_object_id_one, + v_rel_type, 'membership_rel'); - if new.member_state = ''approved'' then - perform party_approved_member__add(map.group_id, v_object_id_two, new.rel_id, v_rel_type); + if :new.member_state = 'approved' then + party_approved_member.add(map.group_id, v_object_id_two, v_rel_type); end if; end loop; +end; +/ +show errors - return new; - -end;' language 'plpgsql'; - -create trigger membership_rels_in_tr after insert on membership_rels -for each row execute procedure membership_rels_in_tr (); - -create or replace function membership_rels_up_tr () returns opaque as ' -declare - map record; +create or replace trigger membership_rels_del_tr +before delete on membership_rels +for each row +declare + v_error varchar2(4000); begin - - if new.member_state = old.member_state then - return new; - end if; - - for map in select group_id, element_id, rel_type - from group_element_index - where rel_id = new.rel_id - loop - if new.member_state = ''approved'' then - perform party_approved_member__add(map.group_id, map.element_id, new.rel_id, map.rel_type); - else - perform party_approved_member__remove(map.group_id, map.element_id, new.rel_id, map.rel_type); - end if; - end loop; - - return new; - -end;' language 'plpgsql'; - -create trigger membership_rels_up_tr before update on membership_rels -for each row execute procedure membership_rels_up_tr (); - -create or replace function membership_rels_del_tr () returns opaque as ' -declare - v_error text; - map record; -begin -- First check if removing this relation would violate any relational constraints - v_error := rel_constraint__violation_if_removed(old.rel_id); + v_error := rel_constraint.violation_if_removed(:old.rel_id); if v_error is not null then - raise EXCEPTION ''-20000: %'', v_error; + raise_application_error(-20000,v_error); end if; - for map in select group_id, element_id, rel_type - from group_element_index - where rel_id = old.rel_id + for map in (select group_id, element_id, rel_type + from group_element_index + where rel_id = :old.rel_id) loop - perform party_approved_member__remove(map.group_id, map.element_id, old.rel_id, map.rel_type); + party_approved_member.remove(map.group_id, map.element_id, map.rel_type); end loop; delete from group_element_index - where rel_id = old.rel_id; + where rel_id = :old.rel_id; - return old; +end; +/ +show errors; -end;' language 'plpgsql'; - -create trigger membership_rels_del_tr before delete on membership_rels -for each row execute procedure membership_rels_del_tr (); - -drop trigger composition_rels_in_tr on composition_rels; -drop function composition_rels_in_tr (); - -create or replace function composition_rels_in_tr () returns opaque as ' +create or replace trigger composition_rels_in_tr +after insert on composition_rels +for each row declare v_object_id_one acs_rels.object_id_one%TYPE; v_object_id_two acs_rels.object_id_two%TYPE; v_rel_type acs_rels.rel_type%TYPE; - v_error text; - map record; + v_error varchar2(4000); begin -- First check if added this relation violated any relational constraints - v_error := rel_constraint__violation(new.rel_id); - + v_error := rel_constraint.violation(:new.rel_id); if v_error is not null then - raise EXCEPTION ''-20000: %'', v_error; + raise_application_error(-20000,v_error); end if; select object_id_one, object_id_two, rel_type into v_object_id_one, v_object_id_two, v_rel_type from acs_rels - where rel_id = new.rel_id; + where rel_id = :new.rel_id; -- Insert a row for me in group_element_index insert into group_element_index (group_id, element_id, rel_id, container_id, rel_type, ancestor_rel_type) values - (v_object_id_one, v_object_id_two, new.rel_id, v_object_id_one, - v_rel_type, ''composition_rel''); + (v_object_id_one, v_object_id_two, :new.rel_id, v_object_id_one, + v_rel_type, 'composition_rel'); -- Make my elements be elements of my new composite group insert into group_element_index @@ -189,18 +160,17 @@ -- For all direct or indirect containers of my new composite group, -- add me and add my elements - for map in select distinct group_id + for map in (select distinct group_id from group_component_map - where component_id = v_object_id_one - LOOP + where component_id = v_object_id_one) loop -- Add a row for me insert into group_element_index (group_id, element_id, rel_id, container_id, rel_type, ancestor_rel_type) values - (map.group_id, v_object_id_two, new.rel_id, v_object_id_one, - v_rel_type, ''composition_rel''); + (map.group_id, v_object_id_two, :new.rel_id, v_object_id_one, + v_rel_type, 'composition_rel'); -- Add rows for my elements insert into group_element_index @@ -218,42 +188,39 @@ and rel_id = m.rel_id); end loop; - return new; +end; +/ +show errors -end;' language 'plpgsql'; - -create trigger composition_rels_in_tr after insert on composition_rels -for each row execute procedure composition_rels_in_tr (); - -- -- TO DO: See if this can be optimized now that the member and component -- mapping tables have been combined -- -create or replace function composition_rels_del_tr () returns opaque as ' +create or replace trigger composition_rels_del_tr +before delete on composition_rels +for each row declare v_object_id_one acs_rels.object_id_one%TYPE; v_object_id_two acs_rels.object_id_two%TYPE; - n_rows integer; - v_error text; - map record; + n_rows integer; + v_error varchar2(4000); begin -- First check if removing this relation would violate any relational constraints - v_error := rel_constraint__violation_if_removed(old.rel_id); + v_error := rel_constraint.violation_if_removed(:old.rel_id); if v_error is not null then - raise EXCEPTION ''-20000: %'', v_error; + raise_application_error(-20000,v_error); end if; select object_id_one, object_id_two into v_object_id_one, v_object_id_two from acs_rels - where rel_id = old.rel_id; + where rel_id = :old.rel_id; - for map in select * + for map in (select * from group_component_map - where rel_id = old.rel_id - LOOP + where rel_id = :old.rel_id) loop delete from group_element_index - where rel_id = old.rel_id; + where rel_id = :old.rel_id; select count(*) into n_rows from group_component_map @@ -264,13 +231,13 @@ delete from group_element_index where group_id = map.group_id and container_id = map.component_id - and ancestor_rel_type = ''membership_rel''; + and ancestor_rel_type = 'membership_rel'; end if; end loop; - for map in select * + for map in (select * from group_component_map where group_id in (select group_id from group_component_map @@ -284,8 +251,7 @@ union select v_object_id_two from dual) - and group_contains_p(group_id, component_id, rel_id) = ''f'' - LOOP + and group_contains_p(group_id, component_id, rel_id) = 'f') loop delete from group_element_index where group_id = map.group_id @@ -301,48 +267,41 @@ delete from group_element_index where group_id = map.group_id and container_id = map.component_id - and ancestor_rel_type = ''membership_rel''; + and ancestor_rel_type = 'membership_rel'; end if; end loop; +end; +/ +show errors - return old; -end;' language 'plpgsql'; - -create trigger composition_rels_del_tr before delete on composition_rels -for each row execute procedure composition_rels_del_tr (); - --- show errors - - -------------------- -- PACKAGE BODIES -- -------------------- --- create or replace package body composition_rel --- function new -select define_function_args('composition_rel__new','rel_id,rel_type;composition_rel,object_id_one,object_id_two,creation_user,creation_ip'); +create or replace package body composition_rel +as -create or replace function composition_rel__new (integer,varchar,integer,integer,integer,varchar) -returns integer as ' -declare - new__rel_id alias for $1; -- default null - rel_type alias for $2; -- default ''composition_rel'' - object_id_one alias for $3; - object_id_two alias for $4; - creation_user alias for $5; -- default null - creation_ip alias for $6; -- default null - v_rel_id integer; -begin - v_rel_id := acs_rel__new ( - new__rel_id, - rel_type, - object_id_one, - object_id_two, - object_id_one, - creation_user, - creation_ip + function new ( + rel_id in composition_rels.rel_id%TYPE default null, + rel_type in acs_rels.rel_type%TYPE default 'composition_rel', + object_id_one in acs_rels.object_id_one%TYPE, + object_id_two in acs_rels.object_id_two%TYPE, + creation_user in acs_objects.creation_user%TYPE default null, + creation_ip in acs_objects.creation_ip%TYPE default null + ) return composition_rels.rel_id%TYPE + is + v_rel_id integer; + begin + v_rel_id := acs_rel.new ( + rel_id => rel_id, + rel_type => rel_type, + object_id_one => object_id_one, + object_id_two => object_id_two, + context_id => object_id_one, + creation_user => creation_user, + creation_ip => creation_ip ); insert into composition_rels @@ -351,613 +310,509 @@ (v_rel_id); return v_rel_id; - -end;' language 'plpgsql'; + end; --- function new -create or replace function composition_rel__new (integer,integer) -returns integer as ' -declare - object_id_one alias for $1; - object_id_two alias for $2; -begin - return composition_rel__new(null, - ''composition_rel'', - object_id_one, - object_id_two, - null, - null); -end;' language 'plpgsql'; + procedure delete ( + rel_id in composition_rels.rel_id%TYPE + ) + is + begin + acs_rel.delete(rel_id); + end; --- procedure delete -create or replace function composition_rel__delete (integer) -returns integer as ' -declare - rel_id alias for $1; -begin - PERFORM acs_rel__delete(rel_id); - - return 0; -end;' language 'plpgsql'; - - --- function check_path_exists_p -create or replace function composition_rel__check_path_exists_p (integer,integer) -returns boolean as ' -declare - component_id alias for $1; - container_id alias for $2; - row record; -begin + function check_path_exists_p ( + component_id in groups.group_id%TYPE, + container_id in groups.group_id%TYPE + ) return char + is + begin if component_id = container_id then - return ''t''; + return 't'; end if; - for row in select r.object_id_one as parent_id + for row in (select r.object_id_one as parent_id from acs_rels r, composition_rels c where r.rel_id = c.rel_id - and r.object_id_two = component_id - LOOP - if composition_rel__check_path_exists_p(row.parent_id, container_id) = ''t'' then - return ''t''; + and r.object_id_two = component_id) loop + if check_path_exists_p(row.parent_id, container_id) = 't' then + return 't'; end if; end loop; - return ''f''; - -end;' language 'plpgsql'; + return 'f'; + end; + function check_index ( + component_id in groups.group_id%TYPE, + container_id in groups.group_id%TYPE + ) return char + is + result char(1); + n_rows integer; + begin + result := 't'; --- function check_index -create or replace function composition_rel__check_index (integer,integer) -returns boolean as ' -declare - check_index__component_id alias for $1; - check_index__container_id alias for $2; - result boolean; - n_rows integer; - dc record; - r1 record; - r2 record; -begin - result := ''t''; - -- Loop through all the direct containers (DC) of COMPONENT_ID -- that are also contained by CONTAINER_ID and verify that the -- GROUP_COMPONENT_INDEX contains the (GROUP_ID, DC.REL_ID, -- CONTAINER_ID) triple. - for dc in select r.rel_id, r.object_id_one as container_id + for dc in (select r.rel_id, r.object_id_one as container_id from acs_rels r, composition_rels c where r.rel_id = c.rel_id - and r.object_id_two = check_index__component_id - LOOP + and r.object_id_two = component_id) loop - if composition_rel__check_path_exists_p(dc.container_id, - check_index__container_id) = ''t'' then - select case when count(*) = 0 then 0 else 1 end into n_rows + if check_path_exists_p(dc.container_id, + check_index.container_id) = 't' then + select decode(count(*),0,0,1) into n_rows from group_component_index - where group_id = check_index__container_id - and component_id = check_index__component_id + where group_id = check_index.container_id + and component_id = check_index.component_id and rel_id = dc.rel_id; if n_rows = 0 then - result := ''f''; - PERFORM acs_log__error(''composition_rel.check_representation'', - ''Row missing from group_component_index for ('' || - ''group_id = '' || check_index__container_id || '', '' || - ''component_id = '' || check_index__component_id || '', '' || - ''rel_id = '' || dc.rel_id || '')''); + result := 'f'; + acs_log.error('composition_rel.check_representation', + 'Row missing from group_component_index for (' || + 'group_id = ' || container_id || ', ' || + 'component_id = ' || component_id || ', ' || + 'rel_id = ' || dc.rel_id || ')'); end if; end if; end loop; -- Loop through all the containers of CONTAINER_ID. - for r1 in select r.object_id_one as container_id + for r1 in (select r.object_id_one as container_id from acs_rels r, composition_rels c where r.rel_id = c.rel_id - and r.object_id_two = check_index__container_id + and r.object_id_two = check_index.container_id union - select check_index__container_id as container_id - from dual - LOOP + select check_index.container_id + from dual) loop -- Loop through all the components of COMPONENT_ID and make a -- recursive call. - for r2 in select r.object_id_two as component_id + for r2 in (select r.object_id_two as component_id from acs_rels r, composition_rels c where r.rel_id = c.rel_id - and r.object_id_one = check_index__component_id + and r.object_id_one = check_index.component_id union - select check_index__component_id as component_id - from dual - LOOP - if (r1.container_id != check_index__container_id or - r2.component_id != check_index__component_id) and - composition_rel__check_index(r2.component_id, r1.container_id) = ''f'' then - result := ''f''; + select check_index.component_id + from dual) loop + if (r1.container_id != check_index.container_id or + r2.component_id != check_index.component_id) and + check_index(r2.component_id, r1.container_id) = 'f' then + result := 'f'; end if; end loop; end loop; return result; - -end;' language 'plpgsql'; + end; + function check_representation ( + rel_id in composition_rels.rel_id%TYPE + ) return char + is + container_id groups.group_id%TYPE; + component_id groups.group_id%TYPE; + result char(1); + begin + result := 't'; --- function check_representation -create or replace function composition_rel__check_representation (integer) -returns boolean as ' -declare - check_representation__rel_id alias for $1; - container_id groups.group_id%TYPE; - component_id groups.group_id%TYPE; - result boolean; - row record; -begin - result := ''t''; - - if acs_object__check_representation(check_representation__rel_id) = ''f'' then - result := ''f''; + if acs_object.check_representation(rel_id) = 'f' then + result := 'f'; end if; select object_id_one, object_id_two into container_id, component_id from acs_rels - where rel_id = check_representation__rel_id; + where rel_id = check_representation.rel_id; - -- First let us check that the index has all the rows it should. - if composition_rel__check_index(component_id, container_id) = ''f'' then - result := ''f''; + -- First let's check that the index has all the rows it should. + if check_index(component_id, container_id) = 'f' then + result := 'f'; end if; - -- Now let us check that the index doesn''t have any extraneous rows + -- Now let's check that the index doesn't have any extraneous rows -- relating to this relation. - for row in select * + for row in (select * from group_component_index - where rel_id = check_representation__rel_id - LOOP - if composition_rel__check_path_exists_p(row.component_id, row.group_id) = ''f'' then - result := ''f''; - PERFORM acs_log__error(''composition_rel.check_representation'', - ''Extraneous row in group_component_index: '' || - ''group_id = '' || row.group_id || '', '' || - ''component_id = '' || row.component_id || '', '' || - ''rel_id = '' || row.rel_id || '', '' || - ''container_id = '' || row.container_id || ''.''); + where rel_id = check_representation.rel_id) loop + if check_path_exists_p(row.component_id, row.group_id) = 'f' then + result := 'f'; + acs_log.error('composition_rel.check_representation', + 'Extraneous row in group_component_index: ' || + 'group_id = ' || row.group_id || ', ' || + 'component_id = ' || row.component_id || ', ' || + 'rel_id = ' || row.rel_id || ', ' || + 'container_id = ' || row.container_id || '.'); end if; end loop; return result; - -end;' language 'plpgsql'; + end; +end composition_rel; +/ +show errors --- show errors --- create or replace package body membership_rel --- function new -select define_function_args('membership_rel__new','rel_id,rel_type;membership_rel,object_id_one,object_id_two,member_state;approved,creation_user,creation_ip'); -create or replace function membership_rel__new (integer,varchar,integer,integer,varchar,integer,varchar) -returns integer as ' -declare - new__rel_id alias for $1; -- default null - rel_type alias for $2; -- default ''membership_rel'' - object_id_one alias for $3; - object_id_two alias for $4; - new__member_state alias for $5; -- default ''approved'' - creation_user alias for $6; -- default null - creation_ip alias for $7; -- default null - v_rel_id integer; -begin - v_rel_id := acs_rel__new ( - new__rel_id, - rel_type, - object_id_one, - object_id_two, - object_id_one, - creation_user, - creation_ip +create or replace package body membership_rel +as + + function new ( + rel_id in membership_rels.rel_id%TYPE default null, + rel_type in acs_rels.rel_type%TYPE default 'membership_rel', + object_id_one in acs_rels.object_id_one%TYPE, + object_id_two in acs_rels.object_id_two%TYPE, + member_state in membership_rels.member_state%TYPE default 'approved', + creation_user in acs_objects.creation_user%TYPE default null, + creation_ip in acs_objects.creation_ip%TYPE default null + ) return membership_rels.rel_id%TYPE + is + v_rel_id integer; + begin + v_rel_id := acs_rel.new ( + rel_id => rel_id, + rel_type => rel_type, + object_id_one => object_id_one, + object_id_two => object_id_two, + context_id => object_id_one, + creation_user => creation_user, + creation_ip => creation_ip ); insert into membership_rels (rel_id, member_state) values - (v_rel_id, new__member_state); + (v_rel_id, new.member_state); return v_rel_id; - -end;' language 'plpgsql'; + end; --- function new -create or replace function membership_rel__new (integer,integer) -returns integer as ' -declare - object_id_one alias for $1; - object_id_two alias for $2; -begin - return membership_rel__new(null, - ''membership_rel'', - object_id_one, - object_id_two, - ''approved'', - null, - null); -end;' language 'plpgsql'; - --- procedure ban -create or replace function membership_rel__ban (integer) -returns integer as ' -declare - ban__rel_id alias for $1; -begin + procedure ban ( + rel_id in membership_rels.rel_id%TYPE + ) + is + begin update membership_rels - set member_state = ''banned'' - where rel_id = ban__rel_id; + set member_state = 'banned' + where rel_id = ban.rel_id; + end; - return 0; -end;' language 'plpgsql'; - - --- procedure approve -create or replace function membership_rel__approve (integer) -returns integer as ' -declare - approve__rel_id alias for $1; -begin + procedure approve ( + rel_id in membership_rels.rel_id%TYPE + ) + is + begin update membership_rels - set member_state = ''approved'' - where rel_id = approve__rel_id; + set member_state = 'approved' + where rel_id = approve.rel_id; + end; - return 0; -end;' language 'plpgsql'; - - --- procedure reject -create or replace function membership_rel__reject (integer) -returns integer as ' -declare - reject__rel_id alias for $1; -begin + procedure reject ( + rel_id in membership_rels.rel_id%TYPE + ) + is + begin update membership_rels - set member_state = ''rejected'' - where rel_id = reject__rel_id; + set member_state = 'rejected' + where rel_id = reject.rel_id; + end; - return 0; -end;' language 'plpgsql'; - - --- procedure unapprove -create or replace function membership_rel__unapprove (integer) -returns integer as ' -declare - unapprove__rel_id alias for $1; -begin + procedure unapprove ( + rel_id in membership_rels.rel_id%TYPE + ) + is + begin update membership_rels - set member_state = ''needs approval'' - where rel_id = unapprove__rel_id; + set member_state = 'needs approval' + where rel_id = unapprove.rel_id; + end; - return 0; -end;' language 'plpgsql'; - - --- procedure deleted -create or replace function membership_rel__deleted (integer) -returns integer as ' -declare - deleted__rel_id alias for $1; -begin + procedure deleted ( + rel_id in membership_rels.rel_id%TYPE + ) + is + begin update membership_rels - set member_state = ''deleted'' - where rel_id = deleted__rel_id; + set member_state = 'deleted' + where rel_id = deleted.rel_id; + end; - return 0; -end;' language 'plpgsql'; + procedure delete ( + rel_id in membership_rels.rel_id%TYPE + ) + is + begin + acs_rel.delete(rel_id); + end; + function check_index ( + group_id in groups.group_id%TYPE, + member_id in parties.party_id%TYPE, + container_id in groups.group_id%TYPE + ) return char + is + result char(1); + n_rows integer; + begin --- procedure delete -create or replace function membership_rel__delete (integer) -returns integer as ' -declare - rel_id alias for $1; -begin - PERFORM acs_rel__delete(rel_id); - - return 0; -end;' language 'plpgsql'; - - --- function check_index -create or replace function membership_rel__check_index (integer,integer,integer) -returns boolean as ' -declare - check_index__group_id alias for $1; - check_index__member_id alias for $2; - check_index__container_id alias for $3; - result boolean; - n_rows integer; - row record; -begin - select count(*) into n_rows - from group_element_index - where group_id = check_index__group_id - and member_id = check_index__member_id - and container_id = check_index__container_id; + from group_member_index + where group_id = check_index.group_id + and member_id = check_index.member_id + and container_id = check_index.container_id; if n_rows = 0 then - result := ''f''; - PERFORM acs_log__error(''membership_rel.check_representation'', - ''Row missing from group_element_index: '' || - ''group_id = '' || check_index__group_id || '', '' || - ''member_id = '' || check_index__member_id || '', '' || - ''container_id = '' || check_index__container_id || ''.''); + result := 'f'; + acs_log.error('membership_rel.check_representation', + 'Row missing from group_member_index: ' || + 'group_id = ' || group_id || ', ' || + 'member_id = ' || member_id || ', ' || + 'container_id = ' || container_id || '.'); end if; - for row in select r.object_id_one as container_id + for row in (select r.object_id_one as container_id from acs_rels r, composition_rels c where r.rel_id = c.rel_id - and r.object_id_two = check_index__group_id - LOOP - if membership_rel__check_index(row.container_id, check_index__member_id, check_index__container_id) = ''f'' then - result := ''f''; + and r.object_id_two = group_id) loop + if check_index(row.container_id, member_id, container_id) = 'f' then + result := 'f'; end if; end loop; return result; - -end;' language 'plpgsql'; + end; + function check_representation ( + rel_id in membership_rels.rel_id%TYPE + ) return char + is + group_id groups.group_id%TYPE; + member_id parties.party_id%TYPE; + result char(1); + begin + result := 't'; --- function check_representation -create or replace function membership_rel__check_representation (integer) -returns boolean as ' -declare - check_representation__rel_id alias for $1; - group_id groups.group_id%TYPE; - member_id parties.party_id%TYPE; - result boolean; - row record; -begin - result := ''t''; - - if acs_object__check_representation(check_representation__rel_id) = ''f'' then - result := ''f''; + if acs_object.check_representation(rel_id) = 'f' then + result := 'f'; end if; select r.object_id_one, r.object_id_two into group_id, member_id from acs_rels r, membership_rels m where r.rel_id = m.rel_id - and m.rel_id = check_representation__rel_id; + and m.rel_id = check_representation.rel_id; - if membership_rel__check_index(group_id, member_id, group_id) = ''f'' then - result := ''f''; + if check_index(group_id, member_id, group_id) = 'f' then + result := 'f'; end if; - for row in select * + for row in (select * from group_member_index - where rel_id = check_representation__rel_id - LOOP - if composition_rel__check_path_exists_p(row.container_id, - row.group_id) = ''f'' then - result := ''f''; - PERFORM acs_log__error(''membership_rel.check_representation'', - ''Extra row in group_member_index: '' || - ''group_id = '' || row.group_id || '', '' || - ''member_id = '' || row.member_id || '', '' || - ''container_id = '' || row.container_id || ''.''); + where rel_id = check_representation.rel_id) loop + if composition_rel.check_path_exists_p(row.container_id, + row.group_id) = 'f' then + result := 'f'; + acs_log.error('membership_rel.check_representation', + 'Extra row in group_member_index: ' || + 'group_id = ' || row.group_id || ', ' || + 'member_id = ' || row.member_id || ', ' || + 'container_id = ' || row.container_id || '.'); end if; end loop; return result; - -end;' language 'plpgsql'; + end; - --- create or replace package body acs_group --- function new -select define_function_args('acs_group__new','group_id,object_type;group,creation_date;now(),creation_user,creation_ip,email,url,group_name,join_policy,context_id'); +end membership_rel; +/ +show errors -create or replace function acs_group__new (integer,varchar,timestamptz,integer,varchar,varchar,varchar,varchar,varchar,integer) -returns integer as ' -declare - new__group_id alias for $1; -- default null - new__object_type alias for $2; -- default ''group'' - new__creation_date alias for $3; -- default now() - new__creation_user alias for $4; -- default null - new__creation_ip alias for $5; -- default null - new__email alias for $6; -- default null - new__url alias for $7; -- default null - new__group_name alias for $8; - new__join_policy alias for $9; -- default null - new__context_id alias for $10; -- default null - v_group_id groups.group_id%TYPE; - v_group_type_exists_p integer; - v_join_policy groups.join_policy%TYPE; -begin + + +create or replace package body acs_group +is + function new ( + group_id in groups.group_id%TYPE default null, + object_type in acs_objects.object_type%TYPE + default 'group', + creation_date in acs_objects.creation_date%TYPE + default sysdate, + creation_user in acs_objects.creation_user%TYPE + default null, + creation_ip in acs_objects.creation_ip%TYPE default null, + email in parties.email%TYPE default null, + url in parties.url%TYPE default null, + group_name in groups.group_name%TYPE, + join_policy in groups.join_policy%TYPE default null, + context_id in acs_objects.context_id%TYPE default null + ) + return groups.group_id%TYPE + is + v_group_id groups.group_id%TYPE; + v_group_type_exists_p integer; + v_join_policy groups.join_policy%TYPE; + begin v_group_id := - party__new(new__group_id, new__object_type, new__creation_date, - new__creation_user, new__creation_ip, new__email, - new__url, new__context_id); + party.new(group_id, object_type, creation_date, creation_user, + creation_ip, email, url, context_id); - v_join_policy := new__join_policy; + v_join_policy := join_policy; - -- if join policy was not specified, select the default based on group type - if v_join_policy is null or v_join_policy = '''' then + -- if join policy wasn't specified, select the default based on group type + if v_join_policy is null then select count(*) into v_group_type_exists_p from group_types - where group_type = new__object_type; + where group_type = object_type; if v_group_type_exists_p = 1 then select default_join_policy into v_join_policy from group_types - where group_type = new__object_type; + where group_type = object_type; else - v_join_policy := ''open''; + v_join_policy := 'open'; end if; end if; insert into groups (group_id, group_name, join_policy) values - (v_group_id, new__group_name, v_join_policy); + (v_group_id, group_name, v_join_policy); - -- setup the permissible relationship types for this group - -- DRB: we have to call nextval() directly because the select may - -- return more than one row. The sequence hack will only compute - -- one nextval value causing the insert to fail ("may" in PG, which - -- is actually broken. It should ALWAYS return exactly one value for - -- the view. In PG it may or may not depending on the optimizer''s - -- mood. PG group seems uninterested in acknowledging the fact that - -- this is a bug) - + -- setup the permissible relationship types for this group insert into group_rels (group_rel_id, group_id, rel_type) - select nextval(''t_acs_object_id_seq''), v_group_id, g.rel_type + select acs_object_id_seq.nextval, v_group_id, g.rel_type from group_type_rels g - where g.group_type = new__object_type; + where g.group_type = new.object_type; return v_group_id; - -end;' language 'plpgsql'; + end new; --- function new -create or replace function acs_group__new (varchar) returns integer as ' -declare - gname alias for $1; -begin - return acs_group__new(null, - ''group'', - now(), - null, - null, - null, - null, - gname, - null, - null); -end;' language 'plpgsql'; --- procedure delete -create or replace function acs_group__delete (integer) -returns integer as ' -declare - delete__group_id alias for $1; - row record; -begin + procedure delete ( + group_id in groups.group_id%TYPE + ) + is + begin -- Delete all segments defined for this group - for row in select segment_id + for row in (select segment_id from rel_segments - where group_id = delete__group_id - LOOP - PERFORM rel_segment__delete(row.segment_id); + where group_id = acs_group.delete.group_id) loop + + rel_segment.delete(row.segment_id); + end loop; -- Delete all the relations of any type to this group - for row in select r.rel_id, t.package_name + for row in (select r.rel_id, t.package_name from acs_rels r, acs_object_types t where r.rel_type = t.object_type - and (r.object_id_one = delete__group_id - or r.object_id_two = delete__group_id) - LOOP - execute ''select '' || row.package_name || ''__delete('' || row.rel_id || '')''; + and (r.object_id_one = acs_group.delete.group_id + or r.object_id_two = acs_group.delete.group_id)) loop + execute immediate 'begin ' || row.package_name || '.delete(' || row.rel_id || '); end;'; end loop; - PERFORM party__delete(delete__group_id); + party.delete(group_id); + end delete; - return 0; -end;' language 'plpgsql'; - - --- function name -create or replace function acs_group__name (integer) -returns varchar as ' -declare - name__group_id alias for $1; - name__group_name varchar(200); -begin + function name ( + group_id in groups.group_id%TYPE + ) + return varchar2 + is + group_name varchar2(200); + begin select group_name - into name__group_name + into group_name from groups - where group_id = name__group_id; + where group_id = name.group_id; - return name__group_name; - -end;' language 'plpgsql'; + return group_name; + end name; -create or replace function acs_group__member_p (integer, integer, boolean) -returns boolean as ' -declare - p_party_id alias for $1; - p_group_id alias for $2; - p_cascade_membership alias for $3; -begin - if p_cascade_membership then - return count(*) > 0 + function member_p ( + party_id in parties.party_id%TYPE, + group_id in groups.group_id%TYPE, + cascade_membership char + ) + return char + is + m_result integer; + begin + + if cascade_membership = 't' then + select count(*) + into m_result from group_member_map - where group_id = p_group_id and - member_id = p_party_id; + where group_id = member_p.group_id and + member_id = member_p.party_id; + + if m_result > 0 then + return 't'; + end if; else - return count(*) > 0 + select count(*) + into m_result from acs_rels rels, all_object_party_privilege_map perm where perm.object_id = rels.rel_id - and perm.privilege = ''read'' - and rels.rel_type = ''membership_rel'' - and rels.object_id_one = p_group_id - and rels.object_id_two = p_party_id; + and perm.privilege = 'read' + and rels.rel_type = 'membership_rel' + and rels.object_id_one = member_p.group_id + and rels.object_id_two = member_p.party_id; + + if m_result > 0 then + return 't'; + end if; end if; -end;' language 'plpgsql'; + return 'f'; + end member_p; --- function check_representation -create or replace function acs_group__check_representation (integer) -returns boolean as ' -declare - group_id alias for $1; - res boolean; - comp record; - memb record; -begin - res := ''t''; - PERFORM acs_log__notice(''acs_group.check_representation'', - ''Running check_representation on group '' || group_id); + function check_representation ( + group_id in groups.group_id%TYPE + ) return char + is + result char(1); + begin + result := 't'; + acs_log.notice('acs_group.check_representation', + 'Running check_representation on group ' || group_id); - if acs_object__check_representation(group_id) = ''f'' then - res := ''f''; + if acs_object.check_representation(group_id) = 'f' then + result := 'f'; end if; - for comp in select c.rel_id + for c in (select c.rel_id from acs_rels r, composition_rels c where r.rel_id = c.rel_id - and r.object_id_one = group_id - LOOP - if composition_rel__check_representation(comp.rel_id) = ''f'' then - res := ''f''; + and r.object_id_one = group_id) loop + if composition_rel.check_representation(c.rel_id) = 'f' then + result := 'f'; end if; end loop; - for memb in select m.rel_id + for m in (select m.rel_id from acs_rels r, membership_rels m where r.rel_id = m.rel_id - and r.object_id_one = group_id - LOOP - if membership_rel__check_representation(memb.rel_id) = ''f'' then - res := ''f''; + and r.object_id_one = group_id) loop + if membership_rel.check_representation(m.rel_id) = 'f' then + result := 'f'; end if; end loop; - PERFORM acs_log__notice(''acs_group.check_representation'', - ''Done running check_representation on group '' || group_id); + acs_log.notice('acs_group.check_representation', + 'Done running check_representation on group ' || group_id); + return result; + end; - return res; - -end;' language 'plpgsql'; - - - --- show errors +end acs_group; +/ +show errors Index: openacs-4/packages/acs-kernel/sql/oracle/rel-segments-body-create.sql =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-kernel/sql/oracle/rel-segments-body-create.sql,v diff -u -r1.3 -r1.4 --- openacs-4/packages/acs-kernel/sql/oracle/rel-segments-body-create.sql 17 Mar 2003 21:58:22 -0000 1.3 +++ openacs-4/packages/acs-kernel/sql/oracle/rel-segments-body-create.sql 21 Mar 2003 14:57:44 -0000 1.4 @@ -3,7 +3,7 @@ -- -- @author Oumi Mehrotra oumi@arsdigita.com -- @creation-date 2000-11-22 --- @cvs-id rel-segments-body-create.sql,v 1.1.4.1 2001/01/12 22:58:33 mbryzek Exp +-- @cvs-id $Id$ -- Copyright (C) 1999-2000 ArsDigita Corporation -- This is free software distributed under the terms of the GNU Public @@ -14,168 +14,146 @@ -- PACKAGE BODY -- ------------------ --- rel_segment__new -- full version -create function rel_segment__new (integer,varchar,timestamptz,integer,varchar,varchar,varchar,varchar,integer,varchar,integer) -returns integer as ' -declare - new__segment_id alias for $1; -- default null - object_type alias for $2; -- default ''rel_segment'' - creation_date alias for $3; -- default now() - creation_user alias for $4; -- default null - creation_ip alias for $5; -- default null - email alias for $6; -- default null - url alias for $7; -- default null - new__segment_name alias for $8; - new__group_id alias for $9; - new__rel_type alias for $10; - context_id alias for $11; -- default null - v_segment_id rel_segments.segment_id%TYPE; -begin +create or replace package body rel_segment +is + function new ( + segment_id in rel_segments.segment_id%TYPE default null, + object_type in acs_objects.object_type%TYPE + default 'rel_segment', + creation_date in acs_objects.creation_date%TYPE + default sysdate, + creation_user in acs_objects.creation_user%TYPE + default null, + creation_ip in acs_objects.creation_ip%TYPE default null, + email in parties.email%TYPE default null, + url in parties.url%TYPE default null, + segment_name in rel_segments.segment_name%TYPE, + group_id in rel_segments.group_id%TYPE, + rel_type in rel_segments.rel_type%TYPE, + context_id in acs_objects.context_id%TYPE default null + ) return rel_segments.segment_id%TYPE + is + v_segment_id rel_segments.segment_id%TYPE; + begin v_segment_id := - party__new(new__segment_id, object_type, creation_date, creation_user, + party.new(segment_id, object_type, creation_date, creation_user, creation_ip, email, url, context_id); insert into rel_segments (segment_id, segment_name, group_id, rel_type) values - (v_segment_id, new__segment_name, new__group_id, new__rel_type); + (v_segment_id, new.segment_name, new.group_id, new.rel_type); return v_segment_id; - -end;' language 'plpgsql'; + end new; --- rel_segment__new -- overloaded version for specifying only non-default values -create function rel_segment__new (varchar,integer,varchar) -returns integer as ' -declare - new__segment_name alias for $1; - new__group_id alias for $2; - new__rel_type alias for $3; - v_segment_id rel_segments.segment_id%TYPE; -begin + procedure delete ( + segment_id in rel_segments.segment_id%TYPE + ) + is + begin - v_segment_id := rel_segment__new(null, ''rel_segment'', now(), null, null, null, null, new__segment_name, new__group_id, new__rel_type, null); - - return v_segment_id; - -end;' language 'plpgsql'; - - --- procedure delete -create function rel_segment__delete (integer) -returns integer as ' -declare - delete__segment_id alias for $1; - row record; -begin - -- remove all constraints on this segment - for row in select constraint_id + for row in (select constraint_id from rel_constraints - where rel_segment = delete__segment_id - LOOP + where rel_segment = rel_segment.delete.segment_id) loop - PERFORM rel_constraint__delete(row.constraint_id); + rel_constraint.delete(row.constraint_id); end loop; - PERFORM party__delete(delete__segment_id); + party.delete(segment_id); - return 0; -end;' language 'plpgsql'; + end delete; - --- function get -create function rel_segment__get (integer,varchar) -returns integer as ' -declare - get__group_id alias for $1; - get__rel_type alias for $2; - v_segment_id rel_segments.segment_id%TYPE; -begin + -- EXPERIMENTAL / UNSTABLE -- use at your own risk + -- + function get ( + group_id in rel_segments.group_id%TYPE, + rel_type in rel_segments.rel_type%TYPE + ) return rel_segments.segment_id%TYPE + is + v_segment_id rel_segments.segment_id%TYPE; + begin select min(segment_id) into v_segment_id from rel_segments - where group_id = get__group_id - and rel_type = get__rel_type; + where group_id = get.group_id + and rel_type = get.rel_type; return v_segment_id; - -end;' language 'plpgsql'; + end get; -create function rel_segment__get_or_new(integer,varchar) returns integer as ' -declare - gid alias for $1; - typ alias for $2; -begin - return rel_segment__get_or_new(gid,typ,null); -end;' language 'plpgsql'; --- function get_or_new -create function rel_segment__get_or_new (integer,varchar,varchar) -returns integer as ' -declare - get_or_new__group_id alias for $1; - get_or_new__rel_type alias for $2; - segment_name alias for $3; -- default null - v_segment_id rel_segments.segment_id%TYPE; - v_segment_name rel_segments.segment_name%TYPE; -begin + -- EXPERIMENTAL / UNSTABLE -- use at your own risk + -- + -- This function simplifies the use of segments a little by letting + -- you not have to worry about creating and initializing segments. + -- If the segment you're interested in exists, this function + -- returns its segment_id. + -- If the segment you're interested in doesn't exist, this function + -- does a pretty minimal amount of initialization for the segment + -- and returns a new segment_id. + function get_or_new ( + group_id in rel_segments.group_id%TYPE, + rel_type in rel_segments.rel_type%TYPE, + segment_name in rel_segments.segment_name%TYPE + default null + ) return rel_segments.segment_id%TYPE + is + v_segment_id rel_segments.segment_id%TYPE; + v_segment_name rel_segments.segment_name%TYPE; + begin - v_segment_id := rel_segment__get(get_or_new__group_id,get_or_new__rel_type); + v_segment_id := get(group_id, rel_type); if v_segment_id is null then if segment_name is not null then v_segment_name := segment_name; else - select groups.group_name || '' - '' || acs_object_types.pretty_name || - '' segment'' + select groups.group_name || ' - ' || acs_object_types.pretty_name || + ' segment' into v_segment_name from groups, acs_object_types - where groups.group_id = get_or_new__group_id - and acs_object_types.object_type = get_or_new__rel_type; + where groups.group_id = get_or_new.group_id + and acs_object_types.object_type = get_or_new.rel_type; end if; - v_segment_id := rel_segment__new ( - null, - ''rel_segment'', - now(), - null, - null, - null, - null, - v_segment_name, - get_or_new__group_id, - get_or_new__rel_type, - get_or_new__group_id + v_segment_id := rel_segment.new ( + object_type => 'rel_segment', + creation_user => null, + creation_ip => null, + email => null, + url => null, + segment_name => v_segment_name, + group_id => get_or_new.group_id, + rel_type => get_or_new.rel_type, + context_id => get_or_new.group_id ); end if; return v_segment_id; - -end;' language 'plpgsql'; + end get_or_new; - --- function name -create function rel_segment__name (integer) -returns varchar as ' -declare - name__segment_id alias for $1; - name__segment_name varchar(200); -begin + function name ( + segment_id in rel_segments.segment_id%TYPE + ) + return rel_segments.segment_name%TYPE + is + segment_name varchar(200); + begin select segment_name - into name__segment_name + into segment_name from rel_segments - where segment_id = name__segment_id; + where segment_id = name.segment_id; - return name__segment_name; - -end;' language 'plpgsql'; + return segment_name; + end name; +end rel_segment; +/ +show errors - --- show errors - Index: openacs-4/packages/acs-kernel/sql/oracle/rel-segments-create.sql =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-kernel/sql/oracle/rel-segments-create.sql,v diff -u -r1.4 -r1.5 --- openacs-4/packages/acs-kernel/sql/oracle/rel-segments-create.sql 17 Mar 2003 21:58:22 -0000 1.4 +++ openacs-4/packages/acs-kernel/sql/oracle/rel-segments-create.sql 21 Mar 2003 14:57:44 -0000 1.5 @@ -3,7 +3,7 @@ -- -- @author Oumi Mehrotra oumi@arsdigita.com -- @creation-date 2000-11-22 --- @cvs-id rel-segments-create.sql,v 1.1.4.3 2001/01/16 18:54:05 oumi Exp +-- @cvs-id $Id$ -- Copyright (C) 1999-2000 ArsDigita Corporation -- This is free software distributed under the terms of the GNU Public @@ -15,54 +15,45 @@ -- change in the future, particularly the functions marked "EXPERIMENTAL". -- -create function inline_0 () -returns integer as ' begin -- -- Relational Segment: a dynamically derived set of parties, defined -- in terms of a particular type of membership or -- composition to a particular group. -- - PERFORM acs_object_type__create_type ( - ''rel_segment'', - ''Relational Party Segment'', - ''Relational Party Segments'', - ''party'', - ''rel_segments'', - ''segment_id'', - ''rel_segment'', - ''f'', - ''rel_segment'', - ''rel_segment.name'' - ); + acs_object_type.create_type ( + supertype => 'party', + object_type => 'rel_segment', + pretty_name => 'Relational Party Segment', + pretty_plural => 'Relational Party Segments', + table_name => 'rel_segments', + id_column => 'segment_id', + package_name => 'rel_segment', + type_extension_table => 'rel_segment', + name_method => 'rel_segment.name' + ); - return 0; -end;' language 'plpgsql'; +end; +/ +show errors -select inline_0 (); -drop function inline_0 (); - - --- show errors - - -- Note that we do not use on delete cascade on the group_id or -- rel_type column because rel_segments are acs_objects. On delete -- cascade only deletes the corresponding row in this table, not all -- the rows up the type hierarchy. Thus, rel segments must be deleted -- using rel_segment.delete before dropping a relationship type. create table rel_segments ( - segment_id integer not null + segment_id not null constraint rel_segments_segment_id_fk references parties (party_id) constraint rel_segments_pk primary key, - segment_name varchar(230) not null, - group_id integer not null + segment_name varchar2(230) not null, + group_id not null constraint rel_segments_group_id_fk references groups (group_id), - rel_type varchar(100) not null + rel_type not null constraint rel_segments_rel_type_fk references acs_rel_types (rel_type), constraint rel_segments_grp_rel_type_uq unique(group_id, rel_type) @@ -92,264 +83,352 @@ -- create pl/sql package rel_segment +create or replace package rel_segment +is + function new ( + --/** Creates a new relational segment + -- + -- @author Oumi Mehrotra (oumi@arsdigita.com) + -- @creation-date 12/2000 + -- + --*/ + segment_id in rel_segments.segment_id%TYPE default null, + object_type in acs_objects.object_type%TYPE + default 'rel_segment', + creation_date in acs_objects.creation_date%TYPE + default sysdate, + creation_user in acs_objects.creation_user%TYPE + default null, + creation_ip in acs_objects.creation_ip%TYPE default null, + email in parties.email%TYPE default null, + url in parties.url%TYPE default null, + segment_name in rel_segments.segment_name%TYPE, + group_id in rel_segments.group_id%TYPE, + rel_type in rel_segments.rel_type%TYPE, + context_id in acs_objects.context_id%TYPE default null + ) return rel_segments.segment_id%TYPE; + + procedure delete ( + --/** Deletes a relational segment + -- + -- @author Oumi Mehrotra (oumi@arsdigita.com) + -- @creation-date 12/2000 + -- + --*/ + segment_id in rel_segments.segment_id%TYPE + ); + + function name ( + segment_id in rel_segments.segment_id%TYPE + ) return rel_segments.segment_name%TYPE; + + function get ( + --/** EXPERIMENTAL / UNSTABLE -- use at your own risk + -- Get the id of a segment given a group_id and rel_type. + -- This depends on the uniqueness of group_id,rel_type. We + -- might remove the unique constraint in the future, in which + -- case we would also probably remove this function. + -- + -- @author Oumi Mehrotra (oumi@arsdigita.com) + -- @creation-date 12/2000 + -- + --*/ + + group_id in rel_segments.group_id%TYPE, + rel_type in rel_segments.rel_type%TYPE + ) return rel_segments.segment_id%TYPE; + + function get_or_new ( + --/** EXPERIMENTAL / UNSTABLE -- use at your own risk + -- + -- This function simplifies the use of segments a little by letting + -- you not have to worry about creating and initializing segments. + -- If the segment you're interested in exists, this function + -- returns its segment_id. + -- If the segment you're interested in doesn't exist, this function + -- does a pretty minimal amount of initialization for the segment + -- and returns a new segment_id. + -- + -- @author Oumi Mehrotra (oumi@arsdigita.com) + -- @creation-date 12/2000 + -- + --*/ + group_id in rel_segments.group_id%TYPE, + rel_type in rel_segments.rel_type%TYPE, + segment_name in rel_segments.segment_name%TYPE + default null + ) return rel_segments.segment_id%TYPE; + +end rel_segment; +/ +show errors + + ----------- -- Views -- ----------- -create view rel_segment_party_map +create or replace view rel_segment_party_map as select rs.segment_id, gem.element_id as party_id, gem.rel_id, gem.rel_type, gem.group_id, gem.container_id, gem.ancestor_rel_type - from rel_segments rs, group_element_map gem, acs_object_types o1, acs_object_types o2 + from rel_segments rs, + group_element_map gem where gem.group_id = rs.group_id - and o1.object_type = gem.rel_type - and o2.object_type = rs.rel_type - and o1.tree_sortkey between o2.tree_sortkey and tree_right(o2.tree_sortkey); + and rs.rel_type in (select object_type + from acs_object_types + start with object_type = gem.rel_type + connect by prior supertype = object_type); -create view rel_segment_distinct_party_map + +create or replace view rel_segment_distinct_party_map as select distinct segment_id, party_id, ancestor_rel_type from rel_segment_party_map; -create view rel_segment_member_map +create or replace view rel_segment_member_map as select segment_id, party_id as member_id, rel_id, rel_type, group_id, container_id from rel_segment_party_map where ancestor_rel_type = 'membership_rel'; -create view rel_seg_approved_member_map -as select rs.segment_id, gem.element_id as member_id, gem.rel_id, - gem.rel_type, gem.group_id, gem.container_id - from membership_rels mr, group_element_map gem, rel_segments rs, - acs_object_types o1, acs_object_types o2 +create or replace view rel_seg_approved_member_map +as select /*+ ordered */ + rs.segment_id, gem.element_id as member_id, gem.rel_id, gem.rel_type, + gem.group_id, gem.container_id + from membership_rels mr, group_element_map gem, rel_segments rs where rs.group_id = gem.group_id - and rs.rel_type = o2.object_type - and o1.object_type = gem.rel_type - and o1.tree_sortkey between o2.tree_sortkey and tree_right(o2.tree_sortkey) + and rs.rel_type in (select object_type + from acs_object_types + start with object_type = gem.rel_type + connect by prior supertype = object_type) and mr.rel_id = gem.rel_id and mr.member_state = 'approved'; -create view rel_seg_distinct_member_map +create or replace view rel_seg_distinct_member_map as select distinct segment_id, member_id from rel_seg_approved_member_map; +-- The party_approved_member_map table maps all parties to all their +-- members. It's here rather in a logical place for historical reasons. + +-- The count column is needed because composition_rels lead to a lot of +-- redundant data in the group element map (i.e. you can belong to the +-- registered users group an infinite number of times, strange concept) + +-- (it is "cnt" rather than "count" because Oracle confuses it with the +-- "count()" aggregate in some contexts) + -- Though for permission checking we only really need to map parties to -- member users, the old view included identity entries for all parties -- in the system. It doesn't cost all that much to maintain the extra -- rows so we will, just in case some overly clever programmer out there -- depends on it. --- This represents a large amount of redundant data which is separately --- stored in the group_element_index table. We might want to clean this --- up in the future but time constraints on 4.6.1 require I keep this --- relatively simple. Implementing a real "subgroup_rel" would help a --- lot by in itself reducing the number of redundant rows in the two --- tables. - --- DRB: Unfortunately visibility semantics in PostgreSQL are very different --- than in Oracle. This makes it impossible to remove the duplicate --- rows by maintaining a count column as I've done in the Oracle version --- without requiring application code to issue explicit "lock table in --- exclusive mode" statements. This would kill abstraction and be very --- error prone. The PL/pgSQL procs can issue the locks but unfortunately --- statements within such procs don't generate a new snapshot when executed --- but rather work within the context of the caller. This means locks within --- a PL/pgSQL are too late to be of use. Such code works perfectly in Oracle. - --- Maybe people who buy Oracle aren't as dumb as you thought! - create table party_approved_member_map ( party_id integer - constraint party_member_party_nn - not null constraint party_member_party_fk references parties, member_id integer - constraint party_member_member_nn - not null constraint party_member_member_fk references parties, - tag integer - constraint party_member_tag_nn - not null, + cnt integer, constraint party_approved_member_map_pk - primary key (party_id, member_id, tag) + primary key (party_id, member_id) ); -- Need this to speed referential integrity create index party_member_member_idx on party_approved_member_map(member_id); --- Helper functions to maintain the materialized party_approved_member_map. +-- Triggers to maintain party_approved_member_map when parties are create or replaced or +-- destroyed. -create or replace function party_approved_member__add_one(integer, integer, integer) returns integer as ' -declare - p_party_id alias for $1; - p_member_id alias for $2; - p_rel_id alias for $3; +create or replace trigger parties_in_tr after insert on parties +for each row begin - insert into party_approved_member_map - (party_id, member_id, tag) + (party_id, member_id, cnt) values - (p_party_id, p_member_id, p_rel_id); + (:new.party_id, :new.party_id, 1); +end parties_in_tr; +/ +show errors; - return 1; - -end;' language 'plpgsql'; - -create or replace function party_approved_member__add(integer, integer, integer, varchar) returns integer as ' -declare - p_party_id alias for $1; - p_member_id alias for $2; - p_rel_id alias for $3; - p_rel_type alias for $4; - v_segments record; +create or replace trigger parties_del_tr before delete on parties +for each row begin + delete from party_approved_member_map + where party_id = :old.party_id + and member_id = :old.party_id; +end parties_del_tr; +/ +show errors; - perform party_approved_member__add_one(p_party_id, p_member_id, p_rel_id); +-- Triggers to maintain party_approved_member_map when relational segments are +-- create or replaced or destroyed. We only remove the (segment_id, member_id) rows as +-- removing the relational segment itself does not remove members from the +-- group with that rel_type. This was intentional on the part of the aD folks +-- who added relational segments to ACS 4.2. - -- if the relation type is mapped to relational segments unmap them too - - for v_segments in select segment_id - from rel_segments s, acs_object_types o1, acs_object_types o2 - where - o1.object_type = p_rel_type - and o1.tree_sortkey between o2.tree_sortkey and tree_right(o2.tree_sortkey) - and s.rel_type = o2.object_type - and s.group_id = p_party_id - loop - perform party_approved_member__add_one(v_segments.segment_id, p_member_id, p_rel_id); - end loop; - - return 1; - -end;' language 'plpgsql'; - -create or replace function party_approved_member__remove_one(integer, integer, integer) returns integer as ' -declare - p_party_id alias for $1; - p_member_id alias for $2; - p_rel_id alias for $3; +create or replace trigger rel_segments_in_tr before insert on rel_segments +for each row begin + insert into party_approved_member_map + (party_id, member_id, cnt) + select :new.segment_id, element_id, 1 + from group_element_index + where group_id = :new.group_id + and rel_type = :new.rel_type; +end rel_segments_in_tr; +/ +show errors; +create or replace trigger rel_segments_del_tr before delete on rel_segments +for each row +begin delete from party_approved_member_map - where party_id = p_party_id - and member_id = p_member_id - and tag = p_rel_id; + where party_id = :old.segment_id + and member_id in (select element_id + from group_element_index + where group_id = :old.group_id + and rel_type = :old.rel_type); +end parties_del_tr; +/ +show errors; - return 1; +-- DRB: Helper functions to maintain the materialized party_approved_member_map. The counting crap +-- has to do with the fact that composition rels create duplicate rows in groups. -end;' language 'plpgsql'; +create or replace package party_approved_member is + procedure add_one( + p_party_id in parties.party_id%TYPE, + p_member_id in parties.party_id%TYPE + ); -create or replace function party_approved_member__remove(integer, integer, integer, varchar) returns integer as ' -declare - p_party_id alias for $1; - p_member_id alias for $2; - p_rel_id alias for $3; - p_rel_type alias for $4; - v_segments record; -begin + procedure add( + p_party_id in parties.party_id%TYPE, + p_member_id in parties.party_id%TYPE, + p_rel_type in acs_rels.rel_type%TYPE + ); - perform party_approved_member__remove_one(p_party_id, p_member_id, p_rel_id); + procedure remove_one ( + p_party_id in parties.party_id%TYPE, + p_member_id in parties.party_id%TYPE + ); - -- if the relation type is mapped to relational segments unmap them too + procedure remove ( + p_party_id in parties.party_id%TYPE, + p_member_id in parties.party_id%TYPE, + p_rel_type in acs_rels.rel_type%TYPE + ); - for v_segments in select segment_id - from rel_segments s, acs_object_types o1, acs_object_types o2 - where - o1.object_type = p_rel_type - and o1.tree_sortkey between o2.tree_sortkey and tree_right(o2.tree_sortkey) - and s.rel_type = o2.object_type - and s.group_id = p_party_id - loop - perform party_approved_member__remove_one(v_segments.segment_id, p_member_id, p_rel_id); - end loop; +end party_approved_member; +/ +show errors; - return 1; +create or replace package body party_approved_member is -end;' language 'plpgsql'; + procedure add_one( + p_party_id in parties.party_id%TYPE, + p_member_id in parties.party_id%TYPE + ) + is + begin + insert into party_approved_member_map + (party_id, member_id, cnt) + values + (p_party_id, p_member_id, 1); --- Triggers to maintain party_approved_member_map when parties are created or --- destroyed. These don't call the above helper functions because we're just --- creating the identity row for the party. + exception when dup_val_on_index then + update party_approved_member_map + set cnt = cnt + 1 + where party_id = p_party_id + and member_id = p_member_id; -create or replace function parties_in_tr () returns opaque as ' -begin + end add_one; - insert into party_approved_member_map - (party_id, member_id, tag) - values - (new.party_id, new.party_id, 0); + procedure add( + p_party_id in parties.party_id%TYPE, + p_member_id in parties.party_id%TYPE, + p_rel_type in acs_rels.rel_type%TYPE + ) + is + begin - return new; + add_one(p_party_id, p_member_id); -end;' language 'plpgsql'; + -- if the relation type is mapped to a relational segment map that too -create trigger parties_in_tr after insert on parties -for each row execute procedure parties_in_tr (); + for v_segments in (select segment_id + from rel_segments + where group_id = p_party_id + and rel_type in (select object_type + from acs_object_types + start with object_type = p_rel_type + connect by prior supertype = object_type)) + loop + add_one(v_segments.segment_id, p_member_id); + end loop; -create or replace function parties_del_tr () returns opaque as ' -begin + end add; - delete from party_approved_member_map - where party_id = old.party_id - and member_id = old.party_id; + procedure remove_one ( + p_party_id in parties.party_id%TYPE, + p_member_id in parties.party_id%TYPE + ) + is + begin - return old; + update party_approved_member_map + set cnt = cnt - 1 + where party_id = p_party_id + and member_id = p_member_id; -end;' language 'plpgsql'; + delete from party_approved_member_map + where party_id = p_party_id + and member_id = p_member_id + and cnt = 0; -create trigger parties_del_tr before delete on parties -for each row execute procedure parties_del_tr (); + end remove_one; --- Triggers to maintain party_approved_member_map when relational segments are --- created or destroyed. We only remove the (segment_id, member_id) rows as --- removing the relational segment itself does not remove members from the --- group with that rel_type. This was intentional on the part of the aD folks --- who added relational segments to ACS 4.2. + procedure remove ( + p_party_id in parties.party_id%TYPE, + p_member_id in parties.party_id%TYPE, + p_rel_type in acs_rels.rel_type%TYPE + ) + is + begin -create or replace function rel_segments_in_tr () returns opaque as ' -begin + remove_one(p_party_id, p_member_id); - insert into party_approved_member_map - (party_id, member_id, tag) - select new.segment_id, element_id, rel_id - from group_element_index - where group_id = new.group_id - and rel_type = new.rel_type; + -- if the relation type is mapped to a relational segment unmap that too - return new; + for v_segments in (select segment_id + from rel_segments + where group_id = p_party_id + and rel_type in (select object_type + from acs_object_types + start with object_type = p_rel_type + connect by prior supertype = object_type)) + loop + remove_one(v_segments.segment_id, p_member_id); + end loop; -end;' language 'plpgsql'; + end remove; -create trigger rel_segments_in_tr before insert on rel_segments -for each row execute procedure rel_segments_in_tr (); +end party_approved_member; +/ +show errors; -create or replace function rel_segments_del_tr () returns opaque as ' -begin - - delete from party_approved_member_map - where party_id = old.segment_id - and member_id in (select element_id - from group_element_index - where group_id = old.group_id - and rel_type = old.rel_type); - - return old; - -end;' language 'plpgsql'; - -create trigger rel_segments_del_tr before delete on rel_segments -for each row execute procedure rel_segments_del_tr (); - -- View: rel_segment_group_rel_type_map -- -- Result Set: the set of triples (:segment_id, :group_id, :rel_type) such that -- -- IF a party were to be in :group_id -- through a relation of type :rel_type, -- THEN the party would necessarily be in segment :segemnt_id. - -create view rel_segment_group_rel_type_map as +-- +-- +create or replace view rel_segment_group_rel_type_map as select s.segment_id, gcm.component_id as group_id, acs_rel_types.rel_type as rel_type @@ -359,10 +438,9 @@ UNION ALL select group_id, group_id as component_id from groups) gcm, - acs_rel_types, - acs_object_types o1, acs_object_types o2 + acs_rel_types where s.group_id = gcm.group_id - and s.rel_type = o2.object_type - and o1.object_type = acs_rel_types.rel_type - and o1.tree_sortkey between o2.tree_sortkey and tree_right(o2.tree_sortkey); - + and s.rel_type in (select object_type from acs_object_types + start with object_type = acs_rel_types.rel_type + connect by prior supertype = object_type); +