Skip to content

Commit 9fb2bf3

Browse files
committed
streamline get_name_by_language()
patch by mdeweerd
1 parent fe3eb40 commit 9fb2bf3

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

sql/functions.sql

+9-10
Original file line numberDiff line numberDiff line change
@@ -2118,28 +2118,27 @@ BEGIN
21182118
END;
21192119
$$ LANGUAGE plpgsql;
21202120

2121+
21212122
CREATE OR REPLACE FUNCTION get_name_by_language(name hstore, languagepref TEXT[]) RETURNS TEXT
21222123
AS $$
21232124
DECLARE
2124-
search TEXT[];
2125-
found BOOLEAN;
2125+
result TEXT;
21262126
BEGIN
2127-
21282127
IF name is null THEN
21292128
RETURN null;
21302129
END IF;
21312130

2132-
search := languagepref;
2133-
2134-
FOR j IN 1..array_upper(search, 1) LOOP
2135-
IF name ? search[j] AND trim(name->search[j]) != '' THEN
2136-
return trim(name->search[j]);
2131+
FOR j IN 1..array_upper(languagepref,1) LOOP
2132+
IF name ? languagepref[j] THEN
2133+
result := trim(name->languagepref[j]);
2134+
IF result != '' THEN
2135+
return result;
2136+
END IF;
21372137
END IF;
21382138
END LOOP;
21392139

21402140
-- anything will do as a fallback - just take the first name type thing there is
2141-
search := avals(name);
2142-
RETURN search[1];
2141+
RETURN trim((avals(name))[1]);
21432142
END;
21442143
$$
21452144
LANGUAGE plpgsql IMMUTABLE;

0 commit comments

Comments
 (0)