streamline get_name_by_language()

patch by mdeweerd
This commit is contained in:
Sarah Hoffmann
2015-09-24 23:52:03 +02:00
parent fe3eb40e3a
commit 9fb2bf36be

View File

@@ -2118,28 +2118,27 @@ BEGIN
END; END;
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION get_name_by_language(name hstore, languagepref TEXT[]) RETURNS TEXT CREATE OR REPLACE FUNCTION get_name_by_language(name hstore, languagepref TEXT[]) RETURNS TEXT
AS $$ AS $$
DECLARE DECLARE
search TEXT[]; result TEXT;
found BOOLEAN;
BEGIN BEGIN
IF name is null THEN IF name is null THEN
RETURN null; RETURN null;
END IF; END IF;
search := languagepref; FOR j IN 1..array_upper(languagepref,1) LOOP
IF name ? languagepref[j] THEN
FOR j IN 1..array_upper(search, 1) LOOP result := trim(name->languagepref[j]);
IF name ? search[j] AND trim(name->search[j]) != '' THEN IF result != '' THEN
return trim(name->search[j]); return result;
END IF;
END IF; END IF;
END LOOP; END LOOP;
-- anything will do as a fallback - just take the first name type thing there is -- anything will do as a fallback - just take the first name type thing there is
search := avals(name); RETURN trim((avals(name))[1]);
RETURN search[1];
END; END;
$$ $$
LANGUAGE plpgsql IMMUTABLE; LANGUAGE plpgsql IMMUTABLE;