次の機能があります-機能していません-機能:
CREATE FUNCTION permission_cache_update(affected_user_list int[])
RETURNS TABLE(user_id INT4, permission_id INT4)
AS
$BODY$
DECLARE
current_relations SETOF RECORD;
BEGIN
WITH
affected_user AS (
SELECT unnest(affected_user_list) AS u_id
),
affected_relations AS (
SELECT user_role.user_id, role_permission.permission_id
FROM user_role
JOIN role_permission ON user_role.role_id = role_permission.role_id
JOIN affected_user ON affected_user.u_id = user_role.user_id
UNION
SELECT user_permission.user_id, user_permission.permission_id
FROM user_permission
JOIN affected_user ON affected_user.u_id = user_permission.user_id
)
SELECT affected_relations.user_id, affected_relations.permission_id FROM affected_relations
INTO current_relations;
DELETE FROM permission_cache WHERE ARRAY[user_id] <@ affected_user_list;
INSERT INTO permission_cache (user_id, permission_id) SELECT user_id, permission_id FROM current_relations;
END
$BODY$
LANGUAGE plpgsql;
current_relations
現在のユーザーと権限の関係を (INT4, INT4) として保存したい。ループや一時テーブルのない変数でこれを行うことは可能ですか?
後でこのようなものを使用するので、サブクエリとしてではなく、変数として本当に必要です
DELETE FROM permission_cache WHERE ARRAY[user_id] <@ affected_user_list AND NOT IN (SELECT user_id, permission_id FROM current_relations);
INSERT INTO permission_cache (user_id, permission_id) SELECT user_id, permission_id FROM current_relations WHERE NOT EXIST (SELECT user_id, permission_id FROM permission_cache);
テーブル→二次元配列変換なら可能だと思いますが、複雑なので、レコードで可能なら良いのですが…
解決:
レコードまたは 2 次元配列を使用する代わりに、1 次元配列を使用してループを作成する方が簡単でした。
CREATE FUNCTION permission_cache_update(
IN affected_user_list INT4 []
)
RETURNS VOID
AS
$BODY$
DECLARE
user_index INT4;
current_user_id INT4;
current_permission_relations INT4 [];
deleted_permission_relations INT4 [];
inserted_permission_relations INT4 [];
BEGIN
FOR user_index IN 1 .. array_upper(affected_user_list, 1) LOOP
current_user_id := affected_user_list[user_index];
WITH
user_permission_summary AS
(
SELECT
role_permission.permission_id
FROM user_role, role_permission
WHERE role_permission.role_id = user_role.role_id AND user_role.user_id = current_user_id
UNION
SELECT
user_permission.permission_id
FROM user_permission
WHERE user_permission.user_id = current_user_id
)
SELECT
array_agg(permission_id)
FROM user_permission_summary
INTO current_permission_relations;
SELECT
array_agg(permission_cache.permission_id)
FROM permission_cache
WHERE permission_cache.user_id = current_user_id AND ( current_permission_relations IS NULL OR
NOT (ARRAY [permission_cache.permission_id] <@ current_permission_relations) )
INTO deleted_permission_relations;
SELECT
array_agg(inserted_permission_id)
FROM unnest(current_permission_relations) AS inserted_permission_id
WHERE NOT EXISTS(SELECT
1
FROM permission_cache
WHERE permission_cache.user_id = current_user_id AND
permission_cache.permission_id = inserted_permission_id)
INTO inserted_permission_relations;
DELETE FROM permission_cache
WHERE permission_cache.user_id = current_user_id AND
permission_cache.permission_id = ANY (deleted_permission_relations);
INSERT INTO permission_cache (user_id, permission_id)
SELECT
current_user_id,
inserted_permission_id
FROM unnest(inserted_permission_relations) AS inserted_permission_id;
END LOOP;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
off: Pplpgsql は非常に貧弱な言語で、ドキュメンテーションも貧弱です。私はそれが好きではありません...コードのフォーマットについては申し訳ありません。私の IDE でのオートフォーマットはあまり良くありません... :S