AI Resource Pro / website / scripts / create-category-count-functions.sql
| 1 | -- Function to count tools per category |
| 2 | CREATE OR REPLACE FUNCTION get_tool_counts() |
| 3 | RETURNS TABLE ( |
| 4 | category_id BIGINT, |
| 5 | count TEXT |
| 6 | ) |
| 7 | LANGUAGE SQL SECURITY DEFINER |
| 8 | AS $$ |
| 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 |
| 19 | CREATE OR REPLACE FUNCTION get_news_counts() |
| 20 | RETURNS TABLE ( |
| 21 | category_id BIGINT, |
| 22 | count TEXT |
| 23 | ) |
| 24 | LANGUAGE SQL SECURITY DEFINER |
| 25 | AS $$ |
| 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 |
| 36 | CREATE OR REPLACE FUNCTION get_blog_counts() |
| 37 | RETURNS TABLE ( |
| 38 | category_id BIGINT, |
| 39 | count TEXT |
| 40 | ) |
| 41 | LANGUAGE SQL SECURITY DEFINER |
| 42 | AS $$ |
| 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 |
| 53 | GRANT EXECUTE ON FUNCTION get_tool_counts() TO PUBLIC; |
| 54 | GRANT EXECUTE ON FUNCTION get_news_counts() TO PUBLIC; |
| 55 | GRANT EXECUTE ON FUNCTION get_blog_counts() TO PUBLIC; |