DavidNorgren

Untitled

Jan 12th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.11 KB | None | 0 0
  1. CREATE DEFINER=`stuffbyd`@`localhost` PROCEDURE `forum_list`(IN `forum_id` INT)
  2.     NO SQL
  3. BEGIN
  4.         SET max_sp_recursion_depth = 255;
  5.        
  6.         CALL forum_list_children(forum_id, @latest_post, @latest_post_date, @topics, @posts);
  7.        
  8.         SELECT  forum.name AS name,
  9.             forum.description AS description,
  10.         @topics AS topics,
  11.         @posts AS posts,
  12.             post.id AS latest_post,
  13.                 post.DATE AS latest_post_date,
  14.             topic.id AS latest_post_topic,
  15.                 topic.title AS latest_post_topic_title,
  16.                 member.id AS member,
  17.                 member.username AS username
  18.         FROM post
  19.         LEFT JOIN topic ON post.topic = topic.id
  20.         LEFT JOIN member ON post.member = member.id
  21.         LEFT JOIN forum ON forum.id = forum_id
  22.         WHERE post.id = @latest_post;
  23. END
  24.  
  25. CREATE DEFINER=`stuffbyd`@`localhost` PROCEDURE `forum_list_children`(IN `forum_id` INT, OUT `forum_latest_post` INT, OUT `forum_latest_post_date` DATETIME, OUT `forum_topics` INT, OUT `forum_posts` INT)
  26.     NO SQL
  27. BEGIN
  28.         DECLARE cur_id, cur_forum_latest_post, cur_forum_topics, cur_forum_posts INT;
  29.         DECLARE cur_forum_latest_post_date datetime;
  30.         DECLARE done BOOLEAN DEFAULT FALSE;
  31.         DECLARE cur cursor FOR SELECT id FROM forum WHERE parent = forum_id;
  32.         DECLARE continue handler FOR NOT found SET done = TRUE;
  33.        
  34.         -- Find latest post and its date from topics
  35.         SELECT id, DATE INTO forum_latest_post, forum_latest_post_date FROM (
  36.             SELECT post.id AS id, post.DATE AS DATE, topic.forum AS forum
  37.             FROM post
  38.             LEFT JOIN topic
  39.             ON post.topic = topic.id
  40.         ) x WHERE forum = forum_id
  41.         ORDER BY DATE DESC
  42.         LIMIT 1;
  43.        
  44.         -- Find amount of topics and posts
  45.         SELECT COUNT(id) INTO forum_topics FROM topic WHERE topic.forum = forum_id;
  46.         SELECT COUNT(id) INTO forum_posts FROM (
  47.             SELECT post.id AS id, topic.forum AS forum
  48.             FROM post
  49.             LEFT JOIN topic
  50.             ON post.topic = topic.id
  51.         ) x WHERE forum = forum_id;
  52.        
  53.         OPEN cur;
  54.         my_loop: loop
  55.             fetch cur INTO cur_id;
  56.                 IF done THEN
  57.                     leave my_loop;
  58.                 END IF;
  59.                
  60.                 -- Call same function for child
  61.                 CALL forum_list_children(cur_id, @cur_forum_latest_post, @cur_forum_latest_post_date, @cur_forum_topics, @cur_forum_posts);
  62.                
  63.                 -- Is the newest post from this child newer than the previously found?
  64.                 IF (@cur_forum_latest_post_date > forum_latest_post_date) THEN
  65.                     SET forum_latest_post = @cur_forum_latest_post;
  66.                         SET forum_latest_post_date = @cur_forum_latest_post_date;
  67.                 END IF;
  68.                
  69.                 -- Add topics and posts from child to total
  70.                 SET forum_topics = forum_topics + @cur_forum_topics;
  71.                 SET forum_posts = forum_posts + @cur_forum_posts;
  72.         END loop my_loop;
  73.         close cur;
  74. END
Advertisement
Add Comment
Please, Sign In to add comment