DELIMITER ;;
DROP PROCEDURE IF EXISTS explode_paths;;
CREATE PROCEDURE explode_paths() BEGIN
DECLARE folders INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE cur CURSOR FOR SELECT path FROM file;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DROP TEMPORARY TABLE IF EXISTS parent_folders;
CREATE TEMPORARY TABLE parent_folders (
parent VARCHAR(255) NOT NULL PRIMARY KEY
) ENGINE=Memory;
OPEN cur;
read_loop: LOOP
FETCH cur INTO value;
IF done THEN
LEAVE read_loop;
END IF;
SET folders = (SELECT LENGTH(value) - LENGTH(REPLACE(value,'/','')));
SET i=1;
WHILE i <= folders DO
INSERT IGNORE INTO parent_folders VALUES (SUBSTRING_INDEX(value,'/',i));
SET i = i + 1;
END WHILE;
END LOOP;
SELECT * FROM parent_folders;
CLOSE cur;
END;;
DELIMITER ;