DROP FUNCTION IF EXISTS givePath;

DELIMITER |

CREATE FUNCTION givePath (FId INT) RETURNS CHAR(255)
BEGIN
DECLARE mypath VARCHAR(255) DEFAULT "";
DECLARE cur_name VARCHAR(20);
DECLARE cur_id, prev_id INT;

	SET cur_id = FId;

	REPEAT
	      SET prev_id=cur_id;

	      SELECT f.Nome INTO cur_name
	      FROM   File f
	      WHERE  f.Id=cur_id;

	      SET mypath=CONCAT('/', cur_name, mypath);

	      SELECT f.Padre INTO cur_id
	      FROM   File f
	      WHERE  f.Id=cur_id;
    
	UNTIL cur_id = prev_id
	END REPEAT;

	/* Nota: se l'Id passato non corrisponde ad un file 'cur_id'
	/* diventa NULL e si esce subito */
	
	RETURN mypath;
END|

DELIMITER ;