简体   繁体   English

组合多个语句以创建函数

[英]Combining multiple statements to create a function

I have two tables that store user comments and user likes 我有两个表,用于存储用户评论和用户喜欢

在此处输入图片说明

CREATE TABLE event_comments(
    comment_id SERIAL PRIMARY KEY, e_id integer, thread_id uuid, date_posted bigint, is_root boolean, created_by uuid, body varchar(10000), num_likes integer,
)

CREATE TABLE user_likes(comment_id integer, user_id uuid, liked boolean)


INSERT INTO event_comments(e_id,thread_id,date_posted,is_root,created_by,body,num_likes)
VALUES(3, '9e028aaa-d265-4e27-9528-30858ed8c13d', 1574480595, true, '9e028aaa-d265-4e27-9528-30858ed8c13d','Hi', 1)

INSERT INTO user_likes(comment_id, user_id, liked)
VALUES(3, '9e028aaa-d265-4e27-9528-30858ed8c13d', true)

I'm trying to create a function that takes in the parameters comment_id and user_id. 我正在尝试创建一个接受参数comment_id和user_id的函数。

SELECT likecomment(comment_id, user_id)

__________________________________________________________________________________ __________________________________________________________________________________

The function first checks the user_likes table to see if that user has voted on that specific comment_id. 该函数首先检查user_likes表,以查看该用户是否对该特定的comment_id进行了投票。

1) If the user has not voted (the row doesn't exist) in user_likes, it would INSERT a row 1)如果用户在user_likes中投票 (该行不存在),它将插入一行

INSERT INTO user_likes(comment_id, user_id, liked) VALUES($1, $2, true)

AND it would also increment the num_likes value in user_comments 并且它也会增加user_comments中的num_likes值

UPDATE event_comments SET num_likes = num_likes + 1

2) If the row exists and liked = true, set the liked = null AND subtract 1 from num_likes in user comments 2)如果该行存在并且“喜欢”为true,则设置“喜欢”为null,并从用户注释中的num_likes中减去1

3) if the row exists and liked = null (meaning user retracted a vote) set liked = true add 1 to num_likes in user comments 3)如果该行存在并且“喜欢” = null(表示用户撤回了投票),则设置“喜欢” = true,在用户注释中将num_likes加1

_________________________________________________________________________ _________________________________________________________________________

I've tried to build the function but I can't seem to get it to work. 我尝试构建该函数,但似乎无法使其正常工作。 Also not even sure if this is the correct way to approach this 甚至也不确定这是否是解决此问题的正确方法

CREATE FUNCTION likecomment(comment_id integer, user_id UUID)
RETURNS void AS $$
BEGIN 
    IF EXISTS(SELECT * FROM user_likes WHERE user_likes.comment_id = $1 AND
             user_likes.user_id = $2) THEN
             with t AS (
                SELECT liked FROM user_likes WHERE user_likes.comment_id = $1 AND
                 user_likes.user_id = $2
             ),
             cv AS (
                UPDATE user_likes SET liked = (CASE WHEN t.liked THEN NULL ELSE true END)
                 FROM t
                 WHERE user_likes.comment_id = $1 AND user_likes.user_id = $2
             )
             UPDATE event_comments
             SET num_likes = (CASE WHEN t.liked THEN num_likes - 1 
                              ELSE num_likes = num_likes + 1 END)
             FROM t
             WHERE comment_id = $1 AND created_by = $2;
    ELSE
        WITH nt AS (UPDATE event_comments SET num_likes = num_likes + 1
                   WHERE comment_id = $1 AND created_by = $2)
        INSERT INTO user_likes(comment_id, user_id, liked)
        VALUES($1, $2, true);
    END IF;
END; $$
LANGUAGE plpgsql;
ALTER TABLE user_likes
        ADD CONSTRAINT user_likes_user_id_comment_id_key UNIQUE (user_id, comment_id);


CREATE OR REPLACE FUNCTION likecomment(p_comment_id INTEGER, p_user_id UUID) RETURNS VOID
        LANGUAGE plpgsql
AS
$$
DECLARE
        p_liked BOOLEAN;
BEGIN
        --If the user has not voted (the row doesn't exist) in user_likes, it would INSERT a row
        -- if exists user_likes then liked null
        INSERT INTO user_likes (comment_id, user_id, liked)
        VALUES ($1, $2, TRUE)
        ON CONFLICT (comment_id,user_id)
                DO UPDATE SET liked=CASE WHEN user_likes.liked IS NULL THEN TRUE WHEN user_likes.liked = TRUE THEN NULL END
        RETURNING liked INTO p_liked;

        IF p_liked IS NULL THEN -- p_liked is null  -1 num_likes

                UPDATE event_comments
                SET num_likes= num_likes - 1
                WHERE e_id = $1
                  AND created_by = $2;

        ELSIF p_liked IS TRUE THEN --p_liked is true +1 num_likes

                UPDATE event_comments
                SET num_likes=num_likes + 1
                WHERE e_id = $1
                  AND created_by = $2;

        END IF;
        RETURN;
END;
$$;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM