From: Sarah Hoffmann Date: Thu, 24 Sep 2015 21:52:03 +0000 (+0200) Subject: streamline get_name_by_language() X-Git-Tag: v.2.5.0~25 X-Git-Url: https://git.openstreetmap.org./nominatim.git/commitdiff_plain/9fb2bf36be7edf63720a4b2514d3e8ab9173558f?ds=sidebyside;hp=--cc streamline get_name_by_language() patch by mdeweerd --- 9fb2bf36be7edf63720a4b2514d3e8ab9173558f diff --git a/sql/functions.sql b/sql/functions.sql index 3c8f16e0..bd64697a 100644 --- a/sql/functions.sql +++ b/sql/functions.sql @@ -2118,28 +2118,27 @@ BEGIN END; $$ LANGUAGE plpgsql; + CREATE OR REPLACE FUNCTION get_name_by_language(name hstore, languagepref TEXT[]) RETURNS TEXT AS $$ DECLARE - search TEXT[]; - found BOOLEAN; + result TEXT; BEGIN - IF name is null THEN RETURN null; END IF; - search := languagepref; - - FOR j IN 1..array_upper(search, 1) LOOP - IF name ? search[j] AND trim(name->search[j]) != '' THEN - return trim(name->search[j]); + FOR j IN 1..array_upper(languagepref,1) LOOP + IF name ? languagepref[j] THEN + result := trim(name->languagepref[j]); + IF result != '' THEN + return result; + END IF; END IF; END LOOP; -- anything will do as a fallback - just take the first name type thing there is - search := avals(name); - RETURN search[1]; + RETURN trim((avals(name))[1]); END; $$ LANGUAGE plpgsql IMMUTABLE;