-- 创建获取所有评论的函数 CREATE OR REPLACE FUNCTION public .get_comments_with_posts() RETURNS TABLE ( comment_id UUID, content TEXT, sentiment_score FLOAT, created_at TIMESTAMP WITH TIME ZONE, updated_at TIMESTAMP WITH TIME ZONE, post_id UUID, user_id UUID, user_profile JSONB, post JSONB ) LANGUAGE plpgsql SECURITY DEFINER AS $$ BEGIN RETURN QUERY SELECT c .comment_id, c .content, c .sentiment_score, c .created_at, c .updated_at, c .post_id, c .user_id, jsonb_build_object( 'id', up.id, 'full_name', up.full_name, 'avatar_url', up.avatar_url ) AS user_profile, jsonb_build_object( 'post_id', p.post_id, 'title', p.title, 'description', p.description, 'platform', p.platform, 'post_url', p.post_url, 'published_at', p.published_at, 'influencer_id', p.influencer_id ) AS post FROM public .comments c LEFT JOIN public .user_profiles up ON c .user_id = up.id LEFT JOIN public .posts p ON c .post_id = p.post_id ORDER BY c .created_at DESC; END; $$; -- 创建获取特定帖子评论的函数 CREATE OR REPLACE FUNCTION public .get_comments_for_post(post_id_param UUID) RETURNS TABLE ( comment_id UUID, content TEXT, sentiment_score FLOAT, created_at TIMESTAMP WITH TIME ZONE, updated_at TIMESTAMP WITH TIME ZONE, post_id UUID, user_id UUID, user_profile JSONB ) LANGUAGE plpgsql SECURITY DEFINER AS $$ BEGIN RETURN QUERY SELECT c .comment_id, c .content, c .sentiment_score, c .created_at, c .updated_at, c .post_id, c .user_id, jsonb_build_object( 'id', up.id, 'full_name', up.full_name, 'avatar_url', up.avatar_url ) AS user_profile FROM public .comments c LEFT JOIN public .user_profiles up ON c .user_id = up.id WHERE c .post_id = post_id_param ORDER BY c .created_at DESC; END; $$;