CREATE OR REPLACE FUNCTION public.insert_url(in_url text) RETURNS TABLE (out_url text, out_website_name text)
	LANGUAGE plpgsql STRICT
	AS $$
DECLARE
	ret_value integer;
BEGIN
	PERFORM * FROM artist_website aw
	WHERE aw.url = in_url;

    IF FOUND THEN
        RETURN;
    END IF;

	INSERT INTO artist_website (url) VALUES (in_url);
	
	SELECT * INTO ret_value FROM set_artist_name(in_url);
	IF ret_value <> 0 THEN
		DELETE FROM artist_website WHERE url = in_url;
		RETURN;
	END IF;
	
	SELECT * INTO ret_value FROM set_website_name(in_url);
	IF ret_value <> 0 THEN
		DELETE FROM artist_website WHERE url = in_url;
		RETURN;
	END IF;
	
	SELECT * INTO ret_value FROM set_folder_path(in_url);
	IF ret_value <> 0 THEN
		DELETE FROM artist_website WHERE url = in_url;
		RETURN;
	END IF;

	RETURN QUERY SELECT aw.url, aw.folder_path
				  FROM artist_website aw
                  WHERE  aw.url ILIKE in_url;
END;
$$;
