1 CREATE OR REPLACE FUNCTION public.non_filing_normalize ( TEXT, "char" ) RETURNS TEXT AS $$
13 WHEN $2::INT NOT BETWEEN 48 AND 57 THEN 1
14 ELSE $2::TEXT::INT + 1
17 $$ LANGUAGE SQL STRICT IMMUTABLE;
19 CREATE OR REPLACE FUNCTION public.naco_normalize( TEXT, TEXT ) RETURNS TEXT AS $func$
20 use Unicode::Normalize;
26 $txt =~ s/\pM+//go; # Remove diacritics
28 $txt =~ s/\xE6/AE/go; # Convert ae digraph
29 $txt =~ s/\x{153}/OE/go;# Convert oe digraph
30 $txt =~ s/\xFE/TH/go; # Convert Icelandic thorn
32 $txt =~ tr/\x{2070}\x{2071}\x{2072}\x{2073}\x{2074}\x{2075}\x{2076}\x{2077}\x{2078}\x{2079}\x{207A}\x{207B}/0123456789+-/;# Convert superscript numbers
33 $txt =~ tr/\x{2080}\x{2081}\x{2082}\x{2083}\x{2084}\x{2085}\x{2086}\x{2087}\x{2088}\x{2089}\x{208A}\x{208B}/0123456889+-/;# Convert subscript numbers
35 $txt =~ tr/\x{0251}\x{03B1}\x{03B2}\x{0262}\x{03B3}/AABGG/; # Convert Latin and Greek
36 $txt =~ tr/\x{2113}\xF0\!\"\(\)\-\{\}\<\>\;\:\.\?\xA1\xBF\/\\\@\*\%\=\xB1\+\xAE\xA9\x{2117}\$\xA3\x{FFE1}\xB0\^\_\~\`/LD /; # Convert Misc
37 $txt =~ tr/\'\[\]\|//d; # Remove Misc
39 if ($sf && $sf =~ /^a/o) {
40 my $commapos = index($txt,',');
42 if ($commapos != length($txt) - 1) {
43 my @list = split /,/, $txt;
44 my $first = shift @list;
45 $txt = $first . ',' . join(' ', @list);
54 $txt =~ s/\s+/ /go; # Compress multiple spaces
55 $txt =~ s/^\s+//o; # Remove leading space
56 $txt =~ s/\s+$//o; # Remove trailing space
59 $func$ LANGUAGE 'plperlu' STRICT IMMUTABLE;
61 CREATE OR REPLACE FUNCTION public.naco_normalize( TEXT ) RETURNS TEXT AS $func$
62 SELECT public.naco_normalize($1,'');
63 $func$ LANGUAGE 'sql' STRICT IMMUTABLE;
65 CREATE OR REPLACE FUNCTION public.normalize_space( TEXT ) RETURNS TEXT AS $$
66 SELECT regexp_replace(regexp_replace(regexp_replace($1, E'\\n', ' ', 'g'), E'(?:^\\s+)|(\\s+$)', '', 'g'), E'\\s+', ' ', 'g');
69 CREATE OR REPLACE FUNCTION public.lowercase( TEXT ) RETURNS TEXT AS $$
73 CREATE OR REPLACE FUNCTION public.uppercase( TEXT ) RETURNS TEXT AS $$
77 CREATE OR REPLACE FUNCTION public.remove_diacritics( TEXT ) RETURNS TEXT AS $$
78 use Unicode::Normalize;
86 CREATE OR REPLACE FUNCTION public.entityize( TEXT ) RETURNS TEXT AS $$
87 use Unicode::Normalize;
90 $x =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
95 CREATE OR REPLACE FUNCTION public.call_number_dewey( TEXT ) RETURNS TEXT AS $$
98 $txt =~ s/[\[\]\{\}\(\)`'"#<>\*\?\-\+\$\\]+//o;
100 if ($txt =~ /(\d{3}(?:\.\d+)?)/o) {
103 return (split /\s+/, $txt)[0];
105 $$ LANGUAGE 'plperlu' STRICT IMMUTABLE;
107 CREATE OR REPLACE FUNCTION public.call_number_dewey( TEXT, INT ) RETURNS TEXT AS $$
108 SELECT SUBSTRING(call_number_dewey($1) FROM 1 FOR $2);
109 $$ LANGUAGE SQL STRICT IMMUTABLE;
111 CREATE OR REPLACE FUNCTION public.first_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
112 SELECT CASE WHEN $1 IS NULL THEN $2 ELSE $1 END;
113 $$ LANGUAGE SQL STABLE;
115 CREATE AGGREGATE public.first (
116 sfunc = public.first_agg,
117 basetype = anyelement,
121 CREATE OR REPLACE FUNCTION public.last_agg ( anyelement, anyelement ) RETURNS anyelement AS $$
123 $$ LANGUAGE SQL STABLE;
125 CREATE AGGREGATE public.last (
126 sfunc = public.last_agg,
127 basetype = anyelement,
131 CREATE OR REPLACE FUNCTION public.text_concat ( TEXT, TEXT ) RETURNS TEXT AS $$
139 $$ LANGUAGE SQL STABLE;
141 CREATE AGGREGATE public.agg_text (
142 sfunc = public.text_concat,
147 CREATE OR REPLACE FUNCTION public.tsvector_concat ( tsvector, tsvector ) RETURNS tsvector AS $$
155 $$ LANGUAGE SQL STABLE;
157 CREATE AGGREGATE public.agg_tsvector (
158 sfunc = public.tsvector_concat,
163 CREATE FUNCTION tableoid2name ( oid ) RETURNS TEXT AS $$
167 $$ language 'plpgsql';
170 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT ) RETURNS SETOF actor.org_unit AS $$
172 FROM connectby('actor.org_unit','id','parent_ou','name',$1,'100','.')
173 AS t(keyid text, parent_keyid text, level int, branch text,pos int)
174 JOIN actor.org_unit a ON a.id = t.keyid
175 ORDER BY CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
176 $$ LANGUAGE SQL STABLE;
178 CREATE OR REPLACE FUNCTION actor.org_unit_ancestors ( INT ) RETURNS SETOF actor.org_unit AS $$
180 FROM connectby('actor.org_unit','parent_ou','id','name',$1,'100','.')
181 AS t(keyid text, parent_keyid text, level int, branch text,pos int)
182 JOIN actor.org_unit a ON a.id = t.keyid
183 ORDER BY CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
184 $$ LANGUAGE SQL STABLE;
186 CREATE OR REPLACE FUNCTION actor.org_unit_descendants ( INT,INT ) RETURNS SETOF actor.org_unit AS $$
188 FROM connectby('actor.org_unit','id','parent_ou','name',
190 FROM actor.org_unit_ancestors($1) x
191 JOIN actor.org_unit_type y ON x.ou_type = y.id
194 AS t(keyid text, parent_keyid text, level int, branch text,pos int)
195 JOIN actor.org_unit a ON a.id = t.keyid
196 ORDER BY CASE WHEN a.parent_ou IS NULL THEN 0 ELSE 1 END, a.name;
197 $$ LANGUAGE SQL STABLE;
199 CREATE OR REPLACE FUNCTION actor.org_unit_ancestor_at_depth ( INT,INT ) RETURNS actor.org_unit AS $$
201 FROM actor.org_unit a
202 WHERE id = ( SELECT FIRST(x.id)
203 FROM actor.org_unit_ancestors($1) x
204 JOIN actor.org_unit_type y
205 ON x.ou_type = y.id AND y.depth = $2);
206 $$ LANGUAGE SQL STABLE;
208 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT ) RETURNS SETOF actor.org_unit AS $$
210 FROM actor.org_unit_ancestors($1)
213 FROM actor.org_unit_descendants($1);
214 $$ LANGUAGE SQL STABLE;
216 CREATE OR REPLACE FUNCTION actor.org_unit_full_path ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
217 SELECT * FROM actor.org_unit_full_path((actor.org_unit_ancestor_at_depth($1, $2)).id)
218 $$ LANGUAGE SQL STABLE;
220 CREATE OR REPLACE FUNCTION actor.org_unit_combined_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
222 FROM actor.org_unit_ancestors($1)
225 FROM actor.org_unit_ancestors($2);
226 $$ LANGUAGE SQL STABLE;
228 CREATE OR REPLACE FUNCTION actor.org_unit_common_ancestors ( INT, INT ) RETURNS SETOF actor.org_unit AS $$
230 FROM actor.org_unit_ancestors($1)
233 FROM actor.org_unit_ancestors($2);
234 $$ LANGUAGE SQL STABLE;
236 CREATE OR REPLACE FUNCTION actor.org_unit_proximity ( INT, INT ) RETURNS INT AS $$
237 SELECT COUNT(id)::INT FROM (
238 SELECT id FROM actor.org_unit_combined_ancestors($1, $2)
240 SELECT id FROM actor.org_unit_common_ancestors($1, $2)
242 $$ LANGUAGE SQL STABLE;
244 CREATE AGGREGATE array_accum (
245 sfunc = array_append,
246 basetype = anyelement,