Index: database.sql ================================================================== --- database.sql +++ database.sql @@ -1,21 +1,43 @@ CREATE PROCEDURAL LANGUAGE plpgsql; -- general array sorting functions -- sorts array -CREATE FUNCTION sort(original anyarray) RETURNS anyarray +CREATE or replace FUNCTION sort(original anyarray) RETURNS anyarray LANGUAGE sql IMMUTABLE STRICT AS $_$ select array_agg(item) as result from (select unnest($1) as item order by item) a; $_$; -- sorts array and removes duplicates -CREATE FUNCTION usort(original anyarray) RETURNS anyarray +CREATE or replace FUNCTION usort(original anyarray) RETURNS anyarray LANGUAGE sql IMMUTABLE STRICT AS $_$ select array_agg(item) as result from (select distinct unnest($1) as item order by item) a; $_$; + +-- this functions returns id of site +create or replace function get_site(my_site text[]) returns integer + language plpgsql strict + as $$ +declare + site_id integer; +begin + select id_site from site where my_site = site into site_id; + if not found then + insert into site (site) values (my_site); + select id_site from site where my_site = site into site_id; + end if; + return site_id; +end; +$$; + +create or replace function get_site(domain text) returns integer + language sql immutable strict + as $$ +select get_site(tripdomain($1)) as result; +$$; -- this function adds tag to domain CREATE or replace FUNCTION mark(domain text, new_tag text) RETURNS void LANGUAGE sql immutable STRICT AS $$ @@ -60,34 +82,12 @@ end if; return tag_id; end; $$; --- this functions returns id of site -create or replace function get_site(domain text) returns integer - language sql immutable strict - as $$ -select get_site(tripdomain($1)) as result; -$$; - -create or replace function get_site(my_site text[]) returns integer - language plpgsql strict - as $$ -declare - site_id integer; -begin - select id_site from site where my_site = site into site_id; - if not found then - insert into site (site) values (my_site); - select id_site from site where my_site = site into site_id; - end if; - return site_id; -end; -$$; - -- transforms domain into ordered array for indexing -CREATE FUNCTION tripdomain(url text) RETURNS text[] +CREATE or replace FUNCTION tripdomain(url text) RETURNS text[] LANGUAGE plpgsql IMMUTABLE STRICT AS $_$ declare result text[]; splitted text[]; @@ -104,11 +104,11 @@ end loop; return result; end;$_$; -- transforms ordered array into domain -create function untrip(site text[]) returns text +create or replace function untrip(site text[]) returns text language plpgsql immutable strict as $_$ declare x integer; splitted text[];