DROP PROCEDURE IF EXISTS `get_cart`;
DELIMITER ;;
CREATE PROCEDURE `get_cart` (
    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_cart: No user logged in';
        LEAVE proc;
    END IF;
    SELECT user_id INTO @user_id FROM sessions WHERE session_id = LOWER(p_session_id);
    IF (SELECT COUNT(product_id) FROM cart WHERE user_id = @user_id) = 0 THEN
        SIGNAL SQLSTATE '45001' SET MESSAGE_TEXT = 'get_cart: No items in cart';
        LEAVE proc;
    END IF;
    SELECT product_id, quantity, 'get_cart' AS 'proc' FROM cart WHERE user_id = @user_id;
END ;;
DELIMITER ;
