开发者

PL/pgSQL: finding all groups person belongs to (also indirectly)

Simple intro:

I have a database with users and groups. Every user might be a member of one or more groups. Every group might have one or more parent groups.

Schema:

CREATE TABLE users(
  u开发者_如何学JAVAsername VARCHAR(64) NOT NULL PRIMARY KEY,
  password VARCHAR(64) NOT NULL,
  enabled BOOLEAN NOT NULL);

CREATE TABLE groups (
  id bigserial NOT NULL PRIMARY KEY,
  group_name VARCHAR(64) NOT NULL);

CREATE TABLE groups_inheritance (
  group_id bigint NOT NULL,
  parent_group_id bigint NOT NULL,
  CONSTRAINT fk_group_inheritance_group FOREIGN KEY(group_id) REFERENCES groups(id),
  CONSTRAINT fk_group_inheritance_group_2 FOREIGN KEY(parent_group_id) REFERENCES groups(id),
  CONSTRAINT unique_uk_groups_inheritance UNIQUE(group_id, parent_group_id));

CREATE TABLE group_members (
  id bigint PRIMARY KEY,
  username VARCHAR(64) NOT NULL,
  group_id bigint NOT NULL,
  CONSTRAINT fk_group_members_username FOREIGN KEY(username) REFERENCES users(username),
  CONSTRAINT fk_group_members_group FOREIGN KEY(group_id) REFERENCES groups(id));

I'm looking for a PL/pgSQL function which finds all groups (their names) particular user belongs to.

Example:

group name: People, group parent: null

group name: Students, group parent: People

group name: Football_players, group parent: People

group name: Basketball_players, group parent: People

user name: Maciej, groups : Students, Football_players

f("Maciej") = {"Students", "People", "Football_players"}

He belongs to "People" just because he belongs to "Students" or "Football_players". He is not a direct member of "People" group.

Thanks in advance!


WITH RECURSIVE group_ancestry AS (
SELECT group_id, username
FROM group_members
UNION
SELECT groups_inheritance.parent_group_id, username
FROM group_ancestry
     JOIN groups_inheritance ON groups_inheritance.group_id = group_ancestry.group_id
)
SELECT username, group_id
FROM group_ancestry


If you have just one level of inheritance (as in example), then you could use such query:

WITH group_ids AS
(
    SELECT group_id
    FROM group_members
    WHERE username LIKE 'Maciej'
)
SELECT group_name
FROM
    (SELECT group_id FROM group_ids
        UNION
    SELECT DISTINCT parent_group_id
    FROM groups_inheritance INNER JOIN group_ids USING(group_id)) g
INNER JOIN groups ON id = group_id;

Result:

    group_name    
------------------
 People
 Students
 Football_players
(3 rows)

PL/pgSQL function:

DROP FUNCTION IF EXISTS f(varchar(64));
CREATE FUNCTION f(username varchar(64))
RETURNS text[] AS $$
DECLARE
    gId bigint;
    pgId bigint;
    gName text;
    result text[] = '{}';
BEGIN
    FOR gId IN SELECT group_id FROM group_members WHERE username LIKE username
    LOOP
        SELECT INTO gName group_name FROM groupS WHERE id = gId;
        result := result || gName;
        FOR pgId IN SELECT parent_group_id FROM groups_inheritance WHERE group_id = gId
        LOOP
            SELECT INTO gName group_name FROM groups WHERE id = pgId;
            IF NOT (result @> ARRAY[gName]) THEN
                result := result || gName;
            END IF;
        END LOOP;
    END LOOP;
    RETURN result;
END $$
LANGUAGE 'plpgsql';

Result:

SELECT f('Maciej');
                 f                  
------------------------------------
 {Students,People,Football_players}
(1 row)

However for nested parent groups I think that recursion should be suitable.

EDIT:

Here is recursion-based variant for nested parent groups:

CREATE OR REPLACE FUNCTION f_recursive(gIdParam bigint, resultArrayParam bigint[])
RETURNS bigint[] AS $$
DECLARE
    pgId bigint;
    resultArray bigint[];
BEGIN
    FOR pgId IN SELECT parent_group_id FROM groups_inheritance WHERE group_id = gIdParam
    LOOP
        IF NOT (resultArrayParam @> ARRAY[pgId]) THEN
            resultArray := resultArray || pgId;
            resultArray := resultArray || f_recursive(pgId, resultArray);
        END IF;
    END LOOP;
    RETURN resultArray;
END $$
LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION f(usernameParam varchar(64))
RETURNS text[] AS $$
DECLARE
    gId bigint;
    resultArray bigint[];
BEGIN
    FOR gId IN SELECT group_id FROM group_members WHERE username LIKE usernameParam
    LOOP
        resultArray := resultArray || gId;
        resultArray := resultArray || f_recursive(gId, resultArray);
    END LOOP;
    RETURN array_agg(group_name) 
            FROM groups INNER JOIN (SELECT unnest(resultArray)) u ON unnest = id;
END $$
LANGUAGE 'plpgsql';

Example insert:

INSERT INTO groups (id, group_name) VALUES
    (1, 'People'), (2, 'Workers'), (3, 'Programmers'),
    (4, 'AI-Programmers'), (5, 'Administators'), (6, 'Managers');

INSERT INTO groups_inheritance (group_id, parent_group_id) VALUES
    (2, 1), (3, 2), (4, 3), (5, 2), (6, 2);

INSERT INTO users (username, password, enabled) VALUES
    ('Maciej', '12345', true);

INSERT INTO group_members (id, username, group_id) VALUES
    (1, 'Maciej', 4), (2, 'Maciej', 5);

Result:

SELECT f('Maciej');
                             f                             
-----------------------------------------------------------
 {AI-Programmers,Programmers,Workers,People,Administators}
(1 row)

Another way is to use WITH query along with RECURSIVE modifier as @araqnid shown.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜