Files
HDB/sql/insert_url_with_custom_artist.sql
2024-11-11 20:20:49 +01:00

46 lines
1010 B
PL/PgSQL

CREATE OR REPLACE FUNCTION public.insert_url_with_custom_artist(in_url text, in_artist_name text) RETURNS integer
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 1;
END IF;
PERFORM * FROM artist a
WHERE a.name = in_artist_name;
IF FOUND THEN
RETURN 1;
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 1;
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 1;
END IF;
RETURN 0;
END;
$$;