DROP PROCEDURE IF EXISTS `is_loggedin`;
DELIMITER ;;
CREATE PROCEDURE `is_loggedin` (
    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 = 'is_loggedin: No user logged in';
        LEAVE proc;
    END IF;
    SELECT user_type INTO @user_type FROM sessions WHERE session_id = LOWER(p_session_id);
    SELECT 0 AS 'status', @user_type AS 'user_type', 'is_loggedin' AS 'proc';
END ;;
DELIMITER ;
