DROP PROCEDURE IF EXISTS `update_profile_picture`;
DELIMITER ;;
CREATE PROCEDURE `update_profile_picture` (
    IN p_filename VARCHAR(100),
    IN p_session_id CHAR(64)
)
proc:BEGIN
    IF (SELECT COUNT(session_id) FROM sessions WHERE session_id = LOWER(p_session_id)) = 0 OR p_session_id IS NULL THEN
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'update_profile_picture: No user logged in';
        LEAVE proc;
    END IF;
    SELECT user_id INTO @user_id FROM sessions WHERE session_id = LOWER(p_session_id);
    IF p_filename = '' OR p_filename IS NULL THEN
        SIGNAL SQLSTATE '45001' SET MESSAGE_TEXT = 'update_profile_picture: No profile picture specified';
        LEAVE proc;
    END IF;
    UPDATE users SET picture = p_filename WHERE id = @user_id;
    SELECT 0 AS 'status';
END ;;
DELIMITER ;
