DROP PROCEDURE IF EXISTS `get_session_user`;
DELIMITER ;;
CREATE PROCEDURE `get_session_user` (
    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 = 'get_session_user: Invalid token';
        LEAVE proc;
    END IF;
    SELECT user_id, user_type INTO @user_id, @user_type FROM sessions WHERE session_id = p_session_id;
    SELECT username INTO @user_name FROM users WHERE id = @user_id;
    SELECT description INTO @description FROM user_type_t WHERE user_type = @user_type;
    SELECT 0 AS 'status', @user_id AS 'user_id', @user_name AS 'username', @description AS 'description', 'get_session_user' AS 'proc';
END ;;
DELIMITER ;
