Branchwell

AI Resource Pro / website / scripts / create-category-count-functions.sql

1-- Function to count tools per category
2CREATE OR REPLACE FUNCTION get_tool_counts()
3RETURNS TABLE (
4 category_id BIGINT,
5 count TEXT
6)
7LANGUAGE SQL SECURITY DEFINER
8AS $$
9 SELECT
10 category_id,
11 COUNT(*)::TEXT as count
12 FROM
13 tools_categories
14 GROUP BY
15 category_id;
16$$;
17
18-- Function to count news items per category
19CREATE OR REPLACE FUNCTION get_news_counts()
20RETURNS TABLE (
21 category_id BIGINT,
22 count TEXT
23)
24LANGUAGE SQL SECURITY DEFINER
25AS $$
26 SELECT
27 category_id,
28 COUNT(*)::TEXT as count
29 FROM
30 news_categories
31 GROUP BY
32 category_id;
33$$;
34
35-- Function to count blog posts per category
36CREATE OR REPLACE FUNCTION get_blog_counts()
37RETURNS TABLE (
38 category_id BIGINT,
39 count TEXT
40)
41LANGUAGE SQL SECURITY DEFINER
42AS $$
43 SELECT
44 category_id,
45 COUNT(*)::TEXT as count
46 FROM
47 blog_posts_categories
48 GROUP BY
49 category_id;
50$$;
51
52-- Allow public access to these functions
53GRANT EXECUTE ON FUNCTION get_tool_counts() TO PUBLIC;
54GRANT EXECUTE ON FUNCTION get_news_counts() TO PUBLIC;
55GRANT EXECUTE ON FUNCTION get_blog_counts() TO PUBLIC;