Files
HDB/sql/insert_url_with_custom_artist.sql
2024-12-11 00:24:25 +01:00

48 lines
1.1 KiB
PL/PgSQL

CREATE OR REPLACE FUNCTION public.insert_url_with_custom_artist(in_url text, in_artist_name text) RETURNS TABLE (out_url text, out_website_name text)
LANGUAGE plpgsql STRICT
AS $$
DECLARE
ret_value integer;
temp_artist_id integer;
BEGIN
PERFORM * FROM artist_website aw
WHERE aw.url = in_url;
IF FOUND THEN
RETURN;
END IF;
PERFORM * FROM artist a
WHERE a.name = in_artist_name;
IF FOUND THEN
RETURN;
END IF;
INSERT INTO artist_website (url) VALUES (in_url);
INSERT INTO artist (name) VALUES (in_artist_name);
SELECT artist.id INTO temp_artist_id FROM artist
WHERE artist.name = in_artist_name;
UPDATE artist_website SET artist_id = temp_artist_id
WHERE url = in_url;
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;
$$;