1 DROP SCHEMA IF EXISTS unapi CASCADE;
6 CREATE OR REPLACE FUNCTION evergreen.org_top()
7 RETURNS SETOF actor.org_unit AS $$
8 SELECT * FROM actor.org_unit WHERE parent_ou IS NULL LIMIT 1;
12 CREATE OR REPLACE FUNCTION evergreen.array_remove_item_by_value(inp ANYARRAY, el ANYELEMENT)
13 RETURNS anyarray AS $$
14 SELECT ARRAY_AGG(x.e) FROM UNNEST( $1 ) x(e) WHERE x.e <> $2;
15 $$ LANGUAGE SQL STABLE;
17 CREATE OR REPLACE FUNCTION evergreen.rank_ou(lib INT, search_lib INT, pref_lib INT DEFAULT NULL)
21 -- lib matches search_lib
22 (SELECT CASE WHEN $1 = $2 THEN -20000 END),
24 -- lib matches pref_lib
25 (SELECT CASE WHEN $1 = $3 THEN -10000 END),
28 -- pref_lib is a child of search_lib and lib is a child of pref lib.
29 -- For example, searching CONS, pref lib is SYS1,
30 -- copies at BR1 and BR2 sort to the front.
31 (SELECT distance - 5000
32 FROM actor.org_unit_descendants_distance($3)
33 WHERE id = $1 AND $3 IN (
34 SELECT id FROM actor.org_unit_descendants($2))),
36 -- lib is a child of search_lib
37 (SELECT distance FROM actor.org_unit_descendants_distance($2) WHERE id = $1),
39 -- all others pay cash
42 $$ LANGUAGE SQL STABLE;
44 -- this version exists mainly to accommodate JSON query transform limitations
45 -- (the transform argument must be an IDL field, not an entire row/object)
46 -- XXX is there another way?
47 CREATE OR REPLACE FUNCTION evergreen.rank_cp(copy_id BIGINT)
50 copy asset.copy%ROWTYPE;
52 SELECT * INTO copy FROM asset.copy WHERE id = copy_id;
53 RETURN evergreen.rank_cp(copy);
55 $$ LANGUAGE PLPGSQL STABLE;
57 CREATE OR REPLACE FUNCTION evergreen.rank_cp(copy asset.copy)
62 WITH totally_available AS (
63 SELECT id, 0 AS avail_rank
64 FROM config.copy_status
65 WHERE opac_visible IS TRUE
66 AND copy_active IS TRUE
67 AND id != 1 -- "Checked out"
68 ), almost_available AS (
69 SELECT id, 10 AS avail_rank
70 FROM config.copy_status
71 WHERE holdable IS TRUE
72 AND opac_visible IS TRUE
73 AND copy_active IS FALSE
74 OR id = 1 -- "Checked out"
77 CASE WHEN NOT copy.opac_visible THEN 100 END,
78 (SELECT avail_rank FROM totally_available WHERE copy.status IN (id)),
79 CASE WHEN copy.holdable THEN
80 (SELECT avail_rank FROM almost_available WHERE copy.status IN (id))
87 $$ LANGUAGE PLPGSQL STABLE;
89 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes(
92 depth INT DEFAULT NULL,
93 slimit HSTORE DEFAULT NULL,
94 soffset HSTORE DEFAULT NULL,
95 pref_lib INT DEFAULT NULL,
96 includes TEXT[] DEFAULT NULL::TEXT[]
97 ) RETURNS TABLE(id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT) AS $$
98 WITH RECURSIVE ou_depth AS (
103 FROM actor.org_unit_type aout
104 INNER JOIN actor.org_unit ou ON ou_type = aout.id
108 ), descendant_depth AS (
112 FROM actor.org_unit ou
113 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
114 JOIN anscestor_depth ad ON (ad.id = ou.id),
116 WHERE ad.depth = ou_depth.depth
121 FROM actor.org_unit ou
122 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
123 JOIN descendant_depth ot ON (ot.id = ou.parent_ou)
124 ), anscestor_depth AS (
128 FROM actor.org_unit ou
129 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
135 FROM actor.org_unit ou
136 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
137 JOIN anscestor_depth ot ON (ot.parent_ou = ou.id)
139 SELECT ou.* FROM actor.org_unit ou JOIN descendant_depth USING (id)
142 SELECT ua.id, ua.name, ua.label_sortkey, MIN(ua.rank) AS rank FROM (
143 SELECT acn.id, aou.name, acn.label_sortkey,
144 evergreen.rank_cp(acp),
146 FROM asset.call_number acn
147 JOIN asset.copy acp ON (acn.id = acp.call_number)
148 JOIN descendants AS aou ON (acp.circ_lib = aou.id)
149 WHERE acn.record = ANY ($1)
150 AND acn.deleted IS FALSE
151 AND acp.deleted IS FALSE
152 AND CASE WHEN ('exclude_invisible_acn' = ANY($7)) THEN
155 FROM asset.opac_visible_copies
156 WHERE copy_id = acp.id AND record = acn.record
158 GROUP BY acn.id, evergreen.rank_cp(acp), aou.name, acn.label_sortkey, aou.id
162 CASE WHEN aou.id = $2 THEN -20000 END,
163 CASE WHEN aou.id = $6 THEN -10000 END,
164 (SELECT distance - 5000
165 FROM actor.org_unit_descendants_distance($6) as x
166 WHERE x.id = aou.id AND $6 IN (
167 SELECT q.id FROM actor.org_unit_descendants($2) as q)),
168 (SELECT e.distance FROM actor.org_unit_descendants_distance($2) as e WHERE e.id = aou.id),
171 evergreen.rank_cp(acp)
174 GROUP BY ua.id, ua.name, ua.label_sortkey
175 ORDER BY rank, ua.name, ua.label_sortkey
176 LIMIT ($4 -> 'acn')::INT
177 OFFSET ($5 -> 'acn')::INT;
178 $$ LANGUAGE SQL STABLE ROWS 10;
180 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes
181 ( bibid BIGINT, ouid INT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, pref_lib INT DEFAULT NULL, includes TEXT[] DEFAULT NULL::TEXT[] )
182 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT)
183 AS $$ SELECT * FROM evergreen.ranked_volumes(ARRAY[$1],$2,$3,$4,$5,$6,$7) $$ LANGUAGE SQL STABLE;
186 CREATE OR REPLACE FUNCTION evergreen.located_uris (
189 pref_lib INT DEFAULT NULL
190 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT) AS $$
191 WITH all_orgs AS (SELECT COALESCE( enabled, FALSE ) AS flag FROM config.global_flag WHERE name = 'opac.located_uri.act_as_copy')
192 SELECT DISTINCT ON (id) * FROM (
193 SELECT acn.id, COALESCE(aou.name,aoud.name), acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
194 FROM asset.call_number acn
195 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
196 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
197 LEFT JOIN actor.org_unit_ancestors( COALESCE($3, $2) ) aou ON (acn.owning_lib = aou.id)
198 LEFT JOIN actor.org_unit_descendants( COALESCE($3, $2) ) aoud ON (acn.owning_lib = aoud.id),
200 WHERE acn.record = ANY ($1)
201 AND acn.deleted IS FALSE
202 AND auri.active IS TRUE
203 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR (all_orgs.flag AND COALESCE(aou.id,aoud.id) IS NOT NULL))
205 SELECT acn.id, COALESCE(aou.name,aoud.name) AS name, acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
206 FROM asset.call_number acn
207 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
208 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
209 LEFT JOIN actor.org_unit_ancestors( $2 ) aou ON (acn.owning_lib = aou.id)
210 LEFT JOIN actor.org_unit_descendants( $2 ) aoud ON (acn.owning_lib = aoud.id),
212 WHERE acn.record = ANY ($1)
213 AND acn.deleted IS FALSE
214 AND auri.active IS TRUE
215 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR (all_orgs.flag AND COALESCE(aou.id,aoud.id) IS NOT NULL)))x
216 ORDER BY id, pref_ou DESC;
220 CREATE OR REPLACE FUNCTION evergreen.located_uris ( bibid BIGINT, ouid INT, pref_lib INT DEFAULT NULL)
221 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT)
222 AS $$ SELECT * FROM evergreen.located_uris(ARRAY[$1],$2,$3) $$ LANGUAGE SQL STABLE;
224 CREATE TABLE unapi.bre_output_layout (
225 name TEXT PRIMARY KEY,
226 transform TEXT REFERENCES config.xml_transform (name) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
227 mime_type TEXT NOT NULL,
228 feed_top TEXT NOT NULL,
229 holdings_element TEXT,
231 description_element TEXT,
232 creator_element TEXT,
233 update_ts_element TEXT
236 INSERT INTO unapi.bre_output_layout
237 (name, transform, mime_type, holdings_element, feed_top, title_element, description_element, creator_element, update_ts_element)
239 ('holdings_xml', NULL, 'application/xml', NULL, 'hxml', NULL, NULL, NULL, NULL),
240 ('marcxml', 'marcxml', 'application/marc+xml', 'record', 'collection', NULL, NULL, NULL, NULL),
241 ('mods32', 'mods32', 'application/mods+xml', 'mods', 'modsCollection', NULL, NULL, NULL, NULL)
244 -- Dummy functions, so we can create the real ones out of order
245 CREATE OR REPLACE FUNCTION unapi.aou ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
246 CREATE OR REPLACE FUNCTION unapi.acnp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
247 CREATE OR REPLACE FUNCTION unapi.acns ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
248 CREATE OR REPLACE FUNCTION unapi.acn ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
249 CREATE OR REPLACE FUNCTION unapi.ssub ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
250 CREATE OR REPLACE FUNCTION unapi.sdist ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
251 CREATE OR REPLACE FUNCTION unapi.sstr ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
252 CREATE OR REPLACE FUNCTION unapi.sitem ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
253 CREATE OR REPLACE FUNCTION unapi.sunit ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
254 CREATE OR REPLACE FUNCTION unapi.sisum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
255 CREATE OR REPLACE FUNCTION unapi.sbsum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
256 CREATE OR REPLACE FUNCTION unapi.sssum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
257 CREATE OR REPLACE FUNCTION unapi.siss ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
258 CREATE OR REPLACE FUNCTION unapi.auri ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
259 CREATE OR REPLACE FUNCTION unapi.acp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
260 CREATE OR REPLACE FUNCTION unapi.acpn ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
261 CREATE OR REPLACE FUNCTION unapi.acl ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
262 CREATE OR REPLACE FUNCTION unapi.ccs ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
263 CREATE OR REPLACE FUNCTION unapi.ascecm ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
264 CREATE OR REPLACE FUNCTION unapi.bre (
270 depth INT DEFAULT NULL,
271 slimit HSTORE DEFAULT NULL,
272 soffset HSTORE DEFAULT NULL,
273 include_xmlns BOOL DEFAULT TRUE,
274 pref_lib INT DEFAULT NULL
276 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
277 CREATE OR REPLACE FUNCTION unapi.mmr (
283 depth INT DEFAULT NULL,
284 slimit HSTORE DEFAULT NULL,
285 soffset HSTORE DEFAULT NULL,
286 include_xmlns BOOL DEFAULT TRUE,
287 pref_lib INT DEFAULT NULL
289 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
290 CREATE OR REPLACE FUNCTION unapi.bmp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
291 CREATE OR REPLACE FUNCTION unapi.mra ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
292 CREATE OR REPLACE FUNCTION unapi.mmr_mra ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE, pref_lib INT DEFAULT NULL) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
293 CREATE OR REPLACE FUNCTION unapi.circ ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT DEFAULT '-', depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
295 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
299 depth INT DEFAULT NULL,
300 includes TEXT[] DEFAULT NULL::TEXT[],
301 slimit HSTORE DEFAULT NULL,
302 soffset HSTORE DEFAULT NULL,
303 include_xmlns BOOL DEFAULT TRUE,
304 pref_lib INT DEFAULT NULL
306 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
308 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
312 depth INT DEFAULT NULL,
313 includes TEXT[] DEFAULT NULL::TEXT[],
314 slimit HSTORE DEFAULT NULL,
315 soffset HSTORE DEFAULT NULL,
316 include_xmlns BOOL DEFAULT TRUE,
317 pref_lib INT DEFAULT NULL
319 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
321 CREATE OR REPLACE FUNCTION unapi.biblio_record_entry_feed ( id_list BIGINT[], format TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE, title TEXT DEFAULT NULL, description TEXT DEFAULT NULL, creator TEXT DEFAULT NULL, update_ts TEXT DEFAULT NULL, unapi_url TEXT DEFAULT NULL, header_xml XML DEFAULT NULL ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
323 CREATE OR REPLACE FUNCTION unapi.memoize (classname TEXT, obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
329 'id' || COALESCE(obj_id::TEXT,'') ||
330 'format' || COALESCE(format::TEXT,'') ||
331 'ename' || COALESCE(ename::TEXT,'') ||
332 'includes' || COALESCE(includes::TEXT,'{}'::TEXT[]::TEXT) ||
333 'org' || COALESCE(org::TEXT,'') ||
334 'depth' || COALESCE(depth::TEXT,'') ||
335 'slimit' || COALESCE(slimit::TEXT,'') ||
336 'soffset' || COALESCE(soffset::TEXT,'') ||
337 'include_xmlns' || COALESCE(include_xmlns::TEXT,'');
338 -- RAISE NOTICE 'memoize key: %', key;
341 -- RAISE NOTICE 'memoize hash: %', key;
343 -- XXX cache logic ... memcached? table?
345 EXECUTE $$SELECT unapi.$$ || classname || $$( $1, $2, $3, $4, $5, $6, $7, $8, $9);$$ INTO output USING obj_id, format, ename, includes, org, depth, slimit, soffset, include_xmlns;
348 $F$ LANGUAGE PLPGSQL STABLE;
350 CREATE OR REPLACE FUNCTION unapi.biblio_record_entry_feed ( id_list BIGINT[], format TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE, title TEXT DEFAULT NULL, description TEXT DEFAULT NULL, creator TEXT DEFAULT NULL, update_ts TEXT DEFAULT NULL, unapi_url TEXT DEFAULT NULL, header_xml XML DEFAULT NULL ) RETURNS XML AS $F$
352 layout unapi.bre_output_layout%ROWTYPE;
353 transform config.xml_transform%ROWTYPE;
356 xmlns_uri TEXT := 'http://open-ils.org/spec/feed-xml/v1';
361 IF org = '-' OR org IS NULL THEN
362 SELECT shortname INTO org FROM evergreen.org_top();
365 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
366 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
368 IF layout.name IS NULL THEN
372 SELECT * INTO transform FROM config.xml_transform WHERE name = layout.transform;
373 xmlns_uri := COALESCE(transform.namespace_uri,xmlns_uri);
375 -- Gather the bib xml
376 SELECT XMLAGG( unapi.bre(i, format, '', includes, org, depth, slimit, soffset, include_xmlns)) INTO tmp_xml FROM UNNEST( id_list ) i;
378 IF layout.title_element IS NOT NULL THEN
379 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.title_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, title;
382 IF layout.description_element IS NOT NULL THEN
383 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.description_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, description;
386 IF layout.creator_element IS NOT NULL THEN
387 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.creator_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, creator;
390 IF layout.update_ts_element IS NOT NULL THEN
391 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.update_ts_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, update_ts;
394 IF unapi_url IS NOT NULL THEN
395 EXECUTE $$SELECT XMLCONCAT( XMLELEMENT( name link, XMLATTRIBUTES( 'http://www.w3.org/1999/xhtml' AS xmlns, 'unapi-server' AS rel, $1 AS href, 'unapi' AS title)), $2)$$ INTO tmp_xml USING unapi_url, tmp_xml::XML;
398 IF header_xml IS NOT NULL THEN tmp_xml := XMLCONCAT(header_xml,tmp_xml::XML); END IF;
400 element_list := regexp_split_to_array(layout.feed_top,E'\\.');
401 FOR i IN REVERSE ARRAY_UPPER(element_list, 1) .. 1 LOOP
402 EXECUTE 'SELECT XMLELEMENT( name '|| quote_ident(element_list[i]) ||', XMLATTRIBUTES( $1 AS xmlns), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML;
407 $F$ LANGUAGE PLPGSQL STABLE;
409 CREATE OR REPLACE FUNCTION unapi.bre (
415 depth INT DEFAULT NULL,
416 slimit HSTORE DEFAULT NULL,
417 soffset HSTORE DEFAULT NULL,
418 include_xmlns BOOL DEFAULT TRUE,
419 pref_lib INT DEFAULT NULL
423 me biblio.record_entry%ROWTYPE;
424 layout unapi.bre_output_layout%ROWTYPE;
425 xfrm config.xml_transform%ROWTYPE;
435 IF org = '-' OR org IS NULL THEN
436 SELECT shortname INTO org FROM evergreen.org_top();
439 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
445 IF format = 'holdings_xml' THEN -- the special case
446 output := unapi.holdings_xml( obj_id, ouid, org, depth, includes, slimit, soffset, include_xmlns);
450 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
452 IF layout.name IS NULL THEN
456 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
458 SELECT * INTO me FROM biblio.record_entry WHERE id = obj_id;
460 -- grab bib_source, if any
461 IF ('cbs' = ANY (includes) AND me.source IS NOT NULL) THEN
462 source := unapi.cbs(me.source,NULL,NULL,NULL,NULL);
467 -- grab SVF if we need them
468 IF ('mra' = ANY (includes)) THEN
469 axml := unapi.mra(obj_id,NULL,NULL,NULL,NULL);
474 -- grab holdings if we need them
475 IF ('holdings_xml' = ANY (includes)) THEN
476 hxml := unapi.holdings_xml(obj_id, ouid, org, depth, evergreen.array_remove_item_by_value(includes,'holdings_xml'), slimit, soffset, include_xmlns, pref_lib);
482 -- generate our item node
485 IF format = 'marcxml' THEN
487 IF tmp_xml !~ E'<marc:' THEN -- If we're not using the prefixed namespace in this record, then remove all declarations of it
488 tmp_xml := REGEXP_REPLACE(tmp_xml, ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
491 tmp_xml := oils_xslt_process(me.marc, xfrm.xslt)::XML;
494 top_el := REGEXP_REPLACE(tmp_xml, E'^.*?<((?:\\S+:)?' || layout.holdings_element || ').*$', E'\\1');
496 IF source IS NOT NULL THEN
497 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', source || '</' || top_el || E'>\\1');
500 IF axml IS NOT NULL THEN
501 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
504 IF hxml IS NOT NULL THEN -- XXX how do we configure the holdings position?
505 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
508 IF ('bre.unapi' = ANY (includes)) THEN
509 output := REGEXP_REPLACE(
511 '</' || top_el || '>(.*?)',
515 'http://www.w3.org/1999/xhtml' AS xmlns,
517 'tag:open-ils.org:U2@bre/' || obj_id || '/' || org AS title
519 )::TEXT || '</' || top_el || E'>\\1'
525 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
528 $F$ LANGUAGE PLPGSQL STABLE;
530 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
534 depth INT DEFAULT NULL,
535 includes TEXT[] DEFAULT NULL::TEXT[],
536 slimit HSTORE DEFAULT NULL,
537 soffset HSTORE DEFAULT NULL,
538 include_xmlns BOOL DEFAULT TRUE,
539 pref_lib INT DEFAULT NULL
545 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
546 CASE WHEN ('bre' = ANY ($5)) THEN 'tag:open-ils.org:U2@bre/' || $1 || '/' || $3 ELSE NULL END AS id,
547 (SELECT record_has_holdable_copy FROM asset.record_has_holdable_copy($1)) AS has_holdable
551 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
554 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
556 FROM asset.opac_ou_record_copy_count($2, $1)
560 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
562 FROM asset.staff_ou_record_copy_count($2, $1)
566 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
568 FROM asset.opac_ou_record_copy_count($9, $1)
573 WHEN ('bmp' = ANY ($5)) THEN
575 name monograph_parts,
576 (SELECT XMLAGG(bmp) FROM (
577 SELECT unapi.bmp( id, 'xml', 'monograph_part', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'bre'), 'holdings_xml'), $3, $4, $6, $7, FALSE)
578 FROM biblio.monograph_part
579 WHERE NOT deleted AND record = $1
586 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
588 SELECT unapi.acn(y.id,'xml','volume',evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), y.rank, name, label_sortkey
589 FROM evergreen.ranked_volumes($1, $2, $4, $6, $7, $9, $5) AS y
592 SELECT unapi.acn(uris.id,'xml','volume',evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), uris.rank, name, label_sortkey
593 FROM evergreen.located_uris($1, $2, $9) AS uris
596 CASE WHEN ('ssub' = ANY ($5)) THEN
599 (SELECT XMLAGG(ssub) FROM (
600 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
601 FROM serial.subscription
602 WHERE record_entry = $1
606 CASE WHEN ('acp' = ANY ($5)) THEN
609 (SELECT XMLAGG(acp) FROM (
610 SELECT unapi.acp(p.target_copy,'xml','copy',evergreen.array_remove_item_by_value($5,'acp'), $3, $4, $6, $7, FALSE)
611 FROM biblio.peer_bib_copy_map p
612 JOIN asset.copy c ON (p.target_copy = c.id)
613 WHERE NOT c.deleted AND p.peer_record = $1
614 LIMIT ($6 -> 'acp')::INT
615 OFFSET ($7 -> 'acp')::INT
620 $F$ LANGUAGE SQL STABLE;
622 CREATE OR REPLACE FUNCTION unapi.ssub ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
626 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
627 'tag:open-ils.org:U2@ssub/' || id AS id,
628 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
629 start_date AS start, end_date AS end, expected_date_offset
632 WHEN ('sdist' = ANY ($4)) THEN
633 XMLELEMENT( name distributions,
634 (SELECT XMLAGG(sdist) FROM (
635 SELECT unapi.sdist( id, 'xml', 'distribution', evergreen.array_remove_item_by_value($4,'ssub'), $5, $6, $7, $8, FALSE)
636 FROM serial.distribution
637 WHERE subscription = ssub.id
643 FROM serial.subscription ssub
645 GROUP BY id, start_date, end_date, expected_date_offset, owning_lib;
646 $F$ LANGUAGE SQL STABLE;
648 CREATE OR REPLACE FUNCTION unapi.sdist ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
652 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
653 'tag:open-ils.org:U2@sdist/' || id AS id,
654 'tag:open-ils.org:U2@acn/' || receive_call_number AS receive_call_number,
655 'tag:open-ils.org:U2@acn/' || bind_call_number AS bind_call_number,
656 unit_label_prefix, label, unit_label_suffix, summary_method
658 unapi.aou( holding_lib, $2, 'holding_lib', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8),
659 CASE WHEN subscription IS NOT NULL AND ('ssub' = ANY ($4)) THEN unapi.ssub( subscription, 'xml', 'subscription', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE) ELSE NULL END,
661 WHEN ('sstr' = ANY ($4)) THEN
662 XMLELEMENT( name streams,
663 (SELECT XMLAGG(sstr) FROM (
664 SELECT unapi.sstr( id, 'xml', 'stream', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
666 WHERE distribution = sdist.id
671 XMLELEMENT( name summaries,
673 WHEN ('sbsum' = ANY ($4)) THEN
674 (SELECT XMLAGG(sbsum) FROM (
675 SELECT unapi.sbsum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
676 FROM serial.basic_summary
677 WHERE distribution = sdist.id
682 WHEN ('sisum' = ANY ($4)) THEN
683 (SELECT XMLAGG(sisum) FROM (
684 SELECT unapi.sisum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
685 FROM serial.index_summary
686 WHERE distribution = sdist.id
691 WHEN ('sssum' = ANY ($4)) THEN
692 (SELECT XMLAGG(sssum) FROM (
693 SELECT unapi.sssum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
694 FROM serial.supplement_summary
695 WHERE distribution = sdist.id
701 FROM serial.distribution sdist
703 GROUP BY id, label, unit_label_prefix, unit_label_suffix, holding_lib, summary_method, subscription, receive_call_number, bind_call_number;
704 $F$ LANGUAGE SQL STABLE;
706 CREATE OR REPLACE FUNCTION unapi.sstr ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
710 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
711 'tag:open-ils.org:U2@sstr/' || id AS id,
714 CASE WHEN distribution IS NOT NULL AND ('sdist' = ANY ($4)) THEN unapi.sssum( distribution, 'xml', 'distribtion', evergreen.array_remove_item_by_value($4,'sstr'), $5, $6, $7, $8, FALSE) ELSE NULL END,
716 WHEN ('sitem' = ANY ($4)) THEN
717 XMLELEMENT( name items,
718 (SELECT XMLAGG(sitem) FROM (
719 SELECT unapi.sitem( id, 'xml', 'serial_item', evergreen.array_remove_item_by_value($4,'sstr'), $5, $6, $7, $8, FALSE)
721 WHERE stream = sstr.id
727 FROM serial.stream sstr
729 GROUP BY id, routing_label, distribution;
730 $F$ LANGUAGE SQL STABLE;
732 CREATE OR REPLACE FUNCTION unapi.siss ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
736 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
737 'tag:open-ils.org:U2@siss/' || id AS id,
738 create_date, edit_date, label, date_published,
739 holding_code, holding_type, holding_link_id
741 CASE WHEN subscription IS NOT NULL AND ('ssub' = ANY ($4)) THEN unapi.ssub( subscription, 'xml', 'subscription', evergreen.array_remove_item_by_value($4,'siss'), $5, $6, $7, $8, FALSE) ELSE NULL END,
743 WHEN ('sitem' = ANY ($4)) THEN
744 XMLELEMENT( name items,
745 (SELECT XMLAGG(sitem) FROM (
746 SELECT unapi.sitem( id, 'xml', 'serial_item', evergreen.array_remove_item_by_value($4,'siss'), $5, $6, $7, $8, FALSE)
748 WHERE issuance = sstr.id
754 FROM serial.issuance sstr
756 GROUP BY id, create_date, edit_date, label, date_published, holding_code, holding_type, holding_link_id, subscription;
757 $F$ LANGUAGE SQL STABLE;
759 CREATE OR REPLACE FUNCTION unapi.sitem ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
763 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
764 'tag:open-ils.org:U2@sitem/' || id AS id,
765 'tag:open-ils.org:U2@siss/' || issuance AS issuance,
766 date_expected, date_received
768 CASE WHEN issuance IS NOT NULL AND ('siss' = ANY ($4)) THEN unapi.siss( issuance, $2, 'issuance', evergreen.array_remove_item_by_value($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
769 CASE WHEN stream IS NOT NULL AND ('sstr' = ANY ($4)) THEN unapi.sstr( stream, $2, 'stream', evergreen.array_remove_item_by_value($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
770 CASE WHEN unit IS NOT NULL AND ('sunit' = ANY ($4)) THEN unapi.sunit( unit, $2, 'serial_unit', evergreen.array_remove_item_by_value($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
771 CASE WHEN uri IS NOT NULL AND ('auri' = ANY ($4)) THEN unapi.auri( uri, $2, 'uri', evergreen.array_remove_item_by_value($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END
772 -- XMLELEMENT( name notes,
774 -- WHEN ('acpn' = ANY ($4)) THEN
775 -- (SELECT XMLAGG(acpn) FROM (
776 -- SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8)
777 -- FROM asset.copy_note
778 -- WHERE owning_copy = cp.id AND pub
784 FROM serial.item sitem
786 $F$ LANGUAGE SQL STABLE;
789 CREATE OR REPLACE FUNCTION unapi.sssum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
793 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
794 'tag:open-ils.org:U2@sbsum/' || id AS id,
795 'sssum' AS type, generated_coverage, textual_holdings, show_generated
797 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', evergreen.array_remove_item_by_value($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
799 FROM serial.supplement_summary ssum
801 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
802 $F$ LANGUAGE SQL STABLE;
804 CREATE OR REPLACE FUNCTION unapi.sbsum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
808 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
809 'tag:open-ils.org:U2@sbsum/' || id AS id,
810 'sbsum' AS type, generated_coverage, textual_holdings, show_generated
812 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', evergreen.array_remove_item_by_value($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
814 FROM serial.basic_summary ssum
816 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
817 $F$ LANGUAGE SQL STABLE;
819 CREATE OR REPLACE FUNCTION unapi.sisum ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
823 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
824 'tag:open-ils.org:U2@sbsum/' || id AS id,
825 'sisum' AS type, generated_coverage, textual_holdings, show_generated
827 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', evergreen.array_remove_item_by_value($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
829 FROM serial.index_summary ssum
831 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
832 $F$ LANGUAGE SQL STABLE;
835 CREATE OR REPLACE FUNCTION unapi.aou ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
839 IF ename = 'circlib' THEN
843 'http://open-ils.org/spec/actors/v1' AS xmlns,
848 FROM actor.org_unit aou
851 EXECUTE $$SELECT XMLELEMENT(
852 name $$ || ename || $$,
854 'http://open-ils.org/spec/actors/v1' AS xmlns,
855 'tag:open-ils.org:U2@aou/' || id AS id,
856 shortname, name, opac_visible
859 FROM actor.org_unit aou
860 WHERE id = $1 $$ INTO output USING obj_id;
866 $F$ LANGUAGE PLPGSQL STABLE;
868 CREATE OR REPLACE FUNCTION unapi.acl ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
872 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
876 label_prefix AS prefix,
877 label_suffix AS suffix
881 FROM asset.copy_location
883 $F$ LANGUAGE SQL STABLE;
885 CREATE OR REPLACE FUNCTION unapi.ccs ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
889 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
896 FROM config.copy_status
898 $F$ LANGUAGE SQL STABLE;
900 CREATE OR REPLACE FUNCTION unapi.acpn ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
904 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
912 $F$ LANGUAGE SQL STABLE;
914 CREATE OR REPLACE FUNCTION unapi.ascecm ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
918 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
924 FROM asset.stat_cat_entry asce
925 JOIN asset.stat_cat sc ON (sc.id = asce.stat_cat)
927 $F$ LANGUAGE SQL STABLE;
929 CREATE OR REPLACE FUNCTION unapi.bmp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
933 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
934 'tag:open-ils.org:U2@bmp/' || id AS id,
938 'tag:open-ils.org:U2@bre/' || record AS record
941 WHEN ('acp' = ANY ($4)) THEN
942 XMLELEMENT( name copies,
943 (SELECT XMLAGG(acp) FROM (
944 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'bmp'), $5, $6, $7, $8, FALSE)
946 JOIN asset.copy_part_map cpm ON (cpm.target_copy = cp.id)
948 AND cp.deleted IS FALSE
949 ORDER BY COALESCE(cp.copy_number,0), cp.barcode
950 LIMIT ($7 -> 'acp')::INT
951 OFFSET ($8 -> 'acp')::INT
957 CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( record, 'marcxml', 'record', evergreen.array_remove_item_by_value($4,'bmp'), $5, $6, $7, $8, FALSE) ELSE NULL END
959 FROM biblio.monograph_part
960 WHERE NOT deleted AND id = $1
961 GROUP BY id, label, label_sortkey, record;
962 $F$ LANGUAGE SQL STABLE;
964 CREATE OR REPLACE FUNCTION unapi.acp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
968 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
969 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
970 create_date, edit_date, copy_number, circulate, deposit,
971 ref, holdable, deleted, deposit_amount, price, barcode,
972 circ_modifier, circ_as_type, opac_visible, age_protect
974 unapi.ccs( status, $2, 'status', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
975 unapi.acl( location, $2, 'location', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
976 unapi.aou( circ_lib, $2, 'circ_lib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
977 unapi.aou( circ_lib, $2, 'circlib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
978 CASE WHEN ('acn' = ANY ($4)) THEN unapi.acn( call_number, $2, 'call_number', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE) ELSE NULL END,
980 WHEN ('acpn' = ANY ($4)) THEN
981 XMLELEMENT( name copy_notes,
982 (SELECT XMLAGG(acpn) FROM (
983 SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
985 WHERE owning_copy = cp.id AND pub
991 WHEN ('ascecm' = ANY ($4)) THEN
992 XMLELEMENT( name statcats,
993 (SELECT XMLAGG(ascecm) FROM (
994 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
995 FROM asset.stat_cat_entry_copy_map
996 WHERE owning_copy = cp.id
1002 WHEN ('bre' = ANY ($4)) THEN
1003 XMLELEMENT( name foreign_records,
1004 (SELECT XMLAGG(bre) FROM (
1005 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
1006 FROM biblio.peer_bib_copy_map
1007 WHERE target_copy = cp.id
1014 WHEN ('bmp' = ANY ($4)) THEN
1015 XMLELEMENT( name monograph_parts,
1016 (SELECT XMLAGG(bmp) FROM (
1017 SELECT unapi.bmp( part, 'xml', 'monograph_part', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1018 FROM asset.copy_part_map
1019 WHERE target_copy = cp.id
1025 WHEN ('circ' = ANY ($4)) THEN
1026 XMLELEMENT( name current_circulation,
1027 (SELECT XMLAGG(circ) FROM (
1028 SELECT unapi.circ( id, 'xml', 'circ', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE)
1029 FROM action.circulation
1030 WHERE target_copy = cp.id
1031 AND checkin_time IS NULL
1039 AND cp.deleted IS FALSE
1040 GROUP BY id, status, location, circ_lib, call_number, create_date,
1041 edit_date, copy_number, circulate, deposit, ref, holdable,
1042 deleted, deposit_amount, price, barcode, circ_modifier,
1043 circ_as_type, opac_visible, age_protect;
1044 $F$ LANGUAGE SQL STABLE;
1046 CREATE OR REPLACE FUNCTION unapi.sunit ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1050 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1051 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
1052 create_date, edit_date, copy_number, circulate, deposit,
1053 ref, holdable, deleted, deposit_amount, price, barcode,
1054 circ_modifier, circ_as_type, opac_visible, age_protect,
1055 status_changed_time, floating, mint_condition,
1056 detailed_contents, sort_key, summary_contents, cost
1058 unapi.ccs( status, $2, 'status', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE),
1059 unapi.acl( location, $2, 'location', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE),
1060 unapi.aou( circ_lib, $2, 'circ_lib', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8),
1061 unapi.aou( circ_lib, $2, 'circlib', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8),
1062 CASE WHEN ('acn' = ANY ($4)) THEN unapi.acn( call_number, $2, 'call_number', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE) ELSE NULL END,
1063 XMLELEMENT( name copy_notes,
1065 WHEN ('acpn' = ANY ($4)) THEN
1066 (SELECT XMLAGG(acpn) FROM (
1067 SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE)
1068 FROM asset.copy_note
1069 WHERE owning_copy = cp.id AND pub
1074 XMLELEMENT( name statcats,
1076 WHEN ('ascecm' = ANY ($4)) THEN
1077 (SELECT XMLAGG(ascecm) FROM (
1078 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1079 FROM asset.stat_cat_entry_copy_map
1080 WHERE owning_copy = cp.id
1085 XMLELEMENT( name foreign_records,
1087 WHEN ('bre' = ANY ($4)) THEN
1088 (SELECT XMLAGG(bre) FROM (
1089 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
1090 FROM biblio.peer_bib_copy_map
1091 WHERE target_copy = cp.id
1097 WHEN ('bmp' = ANY ($4)) THEN
1098 XMLELEMENT( name monograph_parts,
1099 (SELECT XMLAGG(bmp) FROM (
1100 SELECT unapi.bmp( part, 'xml', 'monograph_part', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1101 FROM asset.copy_part_map
1102 WHERE target_copy = cp.id
1108 WHEN ('circ' = ANY ($4)) THEN
1109 XMLELEMENT( name current_circulation,
1110 (SELECT XMLAGG(circ) FROM (
1111 SELECT unapi.circ( id, 'xml', 'circ', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE)
1112 FROM action.circulation
1113 WHERE target_copy = cp.id
1114 AND checkin_time IS NULL
1122 AND cp.deleted IS FALSE
1123 GROUP BY id, status, location, circ_lib, call_number, create_date,
1124 edit_date, copy_number, circulate, floating, mint_condition,
1125 deposit, ref, holdable, deleted, deposit_amount, price,
1126 barcode, circ_modifier, circ_as_type, opac_visible,
1127 status_changed_time, detailed_contents, sort_key,
1128 summary_contents, cost, age_protect;
1129 $F$ LANGUAGE SQL STABLE;
1131 CREATE OR REPLACE FUNCTION unapi.acn ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1135 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1136 'tag:open-ils.org:U2@acn/' || acn.id AS id,
1137 acn.id AS vol_id, o.shortname AS lib,
1138 o.opac_visible AS opac_visible,
1139 deleted, label, label_sortkey, label_class, record
1141 unapi.aou( owning_lib, $2, 'owning_lib', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8),
1143 WHEN ('acp' = ANY ($4)) THEN
1144 CASE WHEN $6 IS NOT NULL THEN
1145 XMLELEMENT( name copies,
1146 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1147 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1148 evergreen.rank_cp(cp) AS rank_avail
1150 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5), $6) aoud ON (cp.circ_lib = aoud.id)
1151 WHERE cp.call_number = acn.id
1152 AND cp.deleted IS FALSE
1153 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1154 LIMIT ($7 -> 'acp')::INT
1155 OFFSET ($8 -> 'acp')::INT
1159 XMLELEMENT( name copies,
1160 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1161 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1162 evergreen.rank_cp(cp) AS rank_avail
1164 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5) ) aoud ON (cp.circ_lib = aoud.id)
1165 WHERE cp.call_number = acn.id
1166 AND cp.deleted IS FALSE
1167 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1168 LIMIT ($7 -> 'acp')::INT
1169 OFFSET ($8 -> 'acp')::INT
1177 (SELECT XMLAGG(auri) FROM (SELECT unapi.auri(uri,'xml','uri', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE) FROM asset.uri_call_number_map WHERE call_number = acn.id)x)
1179 unapi.acnp( acn.prefix, 'marcxml', 'prefix', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1180 unapi.acns( acn.suffix, 'marcxml', 'suffix', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1181 CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( acn.record, 'marcxml', 'record', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE) ELSE NULL END
1183 FROM asset.call_number acn
1184 JOIN actor.org_unit o ON (o.id = acn.owning_lib)
1186 AND acn.deleted IS FALSE
1187 GROUP BY acn.id, o.shortname, o.opac_visible, deleted, label, label_sortkey, label_class, owning_lib, record, acn.prefix, acn.suffix;
1188 $F$ LANGUAGE SQL STABLE;
1190 CREATE OR REPLACE FUNCTION unapi.acnp ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1192 name call_number_prefix,
1194 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1197 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1201 FROM asset.call_number_prefix
1203 $F$ LANGUAGE SQL STABLE;
1205 CREATE OR REPLACE FUNCTION unapi.acns ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1207 name call_number_suffix,
1209 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1212 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1216 FROM asset.call_number_suffix
1218 $F$ LANGUAGE SQL STABLE;
1220 CREATE OR REPLACE FUNCTION unapi.auri ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1224 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1225 'tag:open-ils.org:U2@auri/' || uri.id AS id,
1231 WHEN ('acn' = ANY ($4)) THEN
1232 XMLELEMENT( name copies,
1233 (SELECT XMLAGG(acn) FROM (SELECT unapi.acn( call_number, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'auri'), $5, $6, $7, $8, FALSE) FROM asset.uri_call_number_map WHERE uri = uri.id)x)
1240 GROUP BY uri.id, use_restriction, href, label;
1241 $F$ LANGUAGE SQL STABLE;
1243 CREATE OR REPLACE FUNCTION unapi.cbs ( obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT, depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1247 NULL AS xmlns, -- TODO needs equivalent to http://open-ils.org/spec/holdings/v1
1255 FROM config.bib_source
1257 $F$ LANGUAGE SQL STABLE;
1259 CREATE OR REPLACE FUNCTION unapi.mra (
1265 depth INT DEFAULT NULL,
1266 slimit HSTORE DEFAULT NULL,
1267 soffset HSTORE DEFAULT NULL,
1268 include_xmlns BOOL DEFAULT TRUE
1269 ) RETURNS XML AS $F$
1273 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1274 'tag:open-ils.org:U2@mra/' || $1 AS id,
1275 'tag:open-ils.org:U2@bre/' || $1 AS record
1277 (SELECT XMLAGG(foo.y)
1283 cvm.value AS "coded-value",
1292 FROM metabib.record_attr_flat mra
1293 JOIN config.record_attr_definition rad ON (mra.attr = rad.name)
1294 LEFT JOIN config.coded_value_map cvm ON (cvm.ctype = mra.attr AND code = mra.value)
1299 $F$ LANGUAGE SQL STABLE;
1301 CREATE OR REPLACE FUNCTION unapi.circ (obj_id BIGINT, format TEXT, ename TEXT, includes TEXT[], org TEXT DEFAULT '-', depth INT DEFAULT NULL, slimit HSTORE DEFAULT NULL, soffset HSTORE DEFAULT NULL, include_xmlns BOOL DEFAULT TRUE ) RETURNS XML AS $F$
1305 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1306 'tag:open-ils.org:U2@circ/' || id AS id,
1310 CASE WHEN ('aou' = ANY ($4)) THEN unapi.aou( circ_lib, $2, 'circ_lib', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE) ELSE NULL END,
1311 CASE WHEN ('acp' = ANY ($4)) THEN unapi.acp( circ_lib, $2, 'target_copy', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE) ELSE NULL END
1313 FROM action.circulation
1315 $F$ LANGUAGE SQL STABLE;
1317 CREATE OR REPLACE FUNCTION unapi.mmr_mra (
1323 depth INT DEFAULT NULL,
1324 slimit HSTORE DEFAULT NULL,
1325 soffset HSTORE DEFAULT NULL,
1326 include_xmlns BOOL DEFAULT TRUE,
1327 pref_lib INT DEFAULT NULL
1328 ) RETURNS XML AS $F$
1332 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1333 'tag:open-ils.org:U2@mmr/' || $1 AS metarecord
1335 (SELECT XMLAGG(foo.y)
1337 WITH sourcelist AS (
1338 WITH aou AS (SELECT COALESCE(id, (evergreen.org_top()).id) AS id
1339 FROM actor.org_unit WHERE shortname = $5 LIMIT 1)
1341 FROM metabib.metarecord_source_map, aou
1342 WHERE metarecord = $1 AND (
1344 SELECT 1 FROM asset.opac_visible_copies
1345 WHERE record = source AND circ_lib IN (
1346 SELECT id FROM actor.org_unit_descendants(aou.id, $6))
1349 OR EXISTS (SELECT 1 FROM located_uris(source, aou.id, $10) LIMIT 1)
1357 cmra.value AS "coded-value",
1358 cmra.aid AS "cvmid",
1367 SELECT DISTINCT aid, attr, value
1369 SELECT v.source AS id,
1373 FROM metabib.record_attr_vector_list v
1374 JOIN config.coded_value_map c ON ( c.id = ANY( v.vlist ) )
1376 JOIN sourcelist ON (x.id = sourcelist.source)
1378 JOIN config.record_attr_definition rad ON (cmra.attr = rad.name)
1393 SELECT DISTINCT aid, attr, value
1395 SELECT v.source AS id,
1399 FROM metabib.record_attr_vector_list v
1400 JOIN metabib.uncontrolled_record_attr_value m ON ( m.id = ANY( v.vlist ) )
1402 JOIN sourcelist ON (x.id = sourcelist.source)
1404 JOIN config.record_attr_definition rad ON (umra.attr = rad.name)
1410 $F$ LANGUAGE SQL STABLE;
1412 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
1416 depth INT DEFAULT NULL,
1417 includes TEXT[] DEFAULT NULL::TEXT[],
1418 slimit HSTORE DEFAULT NULL,
1419 soffset HSTORE DEFAULT NULL,
1420 include_xmlns BOOL DEFAULT TRUE,
1421 pref_lib INT DEFAULT NULL
1427 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1428 CASE WHEN ('mmr' = ANY ($5)) THEN 'tag:open-ils.org:U2@mmr/' || $1 || '/' || $3 ELSE NULL END AS id,
1429 (SELECT metarecord_has_holdable_copy FROM asset.metarecord_has_holdable_copy($1)) AS has_holdable
1433 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
1436 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1438 FROM asset.opac_ou_metarecord_copy_count($2, $1)
1442 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1444 FROM asset.staff_ou_metarecord_copy_count($2, $1)
1448 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1450 FROM asset.opac_ou_metarecord_copy_count($9, $1)
1454 -- XXX monograph_parts and foreign_copies are skipped in MRs ... put them back some day?
1457 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
1459 SELECT unapi.acn(y.id,'xml','volume',evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), y.rank, name, label_sortkey
1460 FROM evergreen.ranked_volumes((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $4, $6, $7, $9, $5) AS y
1463 SELECT unapi.acn(uris.id,'xml','volume',evergreen.array_remove_item_by_value( evergreen.array_remove_item_by_value($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), uris.rank, name, label_sortkey
1464 FROM evergreen.located_uris((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $9) AS uris
1467 CASE WHEN ('ssub' = ANY ($5)) THEN
1470 (SELECT XMLAGG(ssub) FROM (
1471 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
1472 FROM serial.subscription
1473 WHERE record_entry IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
1478 $F$ LANGUAGE SQL STABLE;
1480 CREATE OR REPLACE FUNCTION unapi.mmr (
1486 depth INT DEFAULT NULL,
1487 slimit HSTORE DEFAULT NULL,
1488 soffset HSTORE DEFAULT NULL,
1489 include_xmlns BOOL DEFAULT TRUE,
1490 pref_lib INT DEFAULT NULL
1494 mmrec metabib.metarecord%ROWTYPE;
1495 leadrec biblio.record_entry%ROWTYPE;
1496 subrec biblio.record_entry%ROWTYPE;
1497 layout unapi.bre_output_layout%ROWTYPE;
1498 xfrm config.xml_transform%ROWTYPE;
1500 xml_buf TEXT; -- growing XML document
1501 tmp_xml TEXT; -- single-use XML string
1502 xml_frag TEXT; -- single-use XML fragment
1507 subxml XML; -- subordinate records elements
1512 -- xpath for extracting bre.marc values from subordinate records
1513 -- so they may be appended to the MARC of the master record prior
1514 -- to XSLT processing.
1515 -- subjects, isbn, issn, upc -- anything else?
1517 '//*[starts-with(@tag, "6") or @tag="020" or @tag="022" or @tag="024"]';
1519 IF org = '-' OR org IS NULL THEN
1520 SELECT shortname INTO org FROM evergreen.org_top();
1523 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
1525 IF ouid IS NULL THEN
1529 SELECT INTO mmrec * FROM metabib.metarecord WHERE id = obj_id;
1534 -- TODO: aggregate holdings from constituent records
1535 IF format = 'holdings_xml' THEN -- the special case
1536 output := unapi.mmr_holdings_xml(
1537 obj_id, ouid, org, depth,
1538 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
1539 slimit, soffset, include_xmlns, pref_lib);
1543 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
1545 IF layout.name IS NULL THEN
1549 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
1551 SELECT INTO leadrec * FROM biblio.record_entry WHERE id = mmrec.master_record;
1553 -- Grab distinct MVF for all records if requested
1554 IF ('mra' = ANY (includes)) THEN
1555 axml := unapi.mmr_mra(obj_id,NULL,NULL,NULL,org,depth,NULL,NULL,TRUE,pref_lib);
1560 xml_buf = leadrec.marc;
1563 IF ('holdings_xml' = ANY (includes)) THEN
1564 hxml := unapi.mmr_holdings_xml(
1565 obj_id, ouid, org, depth,
1566 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
1567 slimit, soffset, include_xmlns, pref_lib);
1570 subxml := NULL::XML;
1571 parts := '{}'::TEXT[];
1572 FOR subrec IN SELECT bre.* FROM biblio.record_entry bre
1573 JOIN metabib.metarecord_source_map mmsm ON (mmsm.source = bre.id)
1574 JOIN metabib.metarecord mmr ON (mmr.id = mmsm.metarecord)
1575 WHERE mmr.id = obj_id AND NOT bre.deleted
1576 ORDER BY CASE WHEN bre.id = mmr.master_record THEN 0 ELSE bre.id END
1577 LIMIT COALESCE((slimit->'bre')::INT, 5) LOOP
1579 IF subrec.id = leadrec.id THEN CONTINUE; END IF;
1580 -- Append choice data from the the non-lead records to the
1581 -- the lead record document
1583 parts := parts || xpath(sub_xpath, subrec.marc::XML)::TEXT[];
1586 SELECT ARRAY_TO_STRING( ARRAY_AGG( DISTINCT p ), '' )::XML INTO subxml FROM UNNEST(parts) p;
1588 -- append data from the subordinate records to the
1589 -- main record document before applying the XSLT
1591 IF subxml IS NOT NULL THEN
1592 xml_buf := REGEXP_REPLACE(xml_buf,
1593 '</record>(.*?)$', subxml || '</record>' || E'\\1');
1596 IF format = 'marcxml' THEN
1597 -- If we're not using the prefixed namespace in
1598 -- this record, then remove all declarations of it
1599 IF xml_buf !~ E'<marc:' THEN
1600 xml_buf := REGEXP_REPLACE(xml_buf,
1601 ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
1604 xml_buf := oils_xslt_process(xml_buf, xfrm.xslt)::XML;
1607 -- update top_el to reflect the change in xml_buf, which may
1608 -- now be a different type of document (e.g. record -> mods)
1609 top_el := REGEXP_REPLACE(xml_buf, E'^.*?<((?:\\S+:)?' ||
1610 layout.holdings_element || ').*$', E'\\1');
1612 IF axml IS NOT NULL THEN
1613 xml_buf := REGEXP_REPLACE(xml_buf,
1614 '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
1617 IF hxml IS NOT NULL THEN
1618 xml_buf := REGEXP_REPLACE(xml_buf,
1619 '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
1622 IF ('mmr.unapi' = ANY (includes)) THEN
1623 output := REGEXP_REPLACE(
1625 '</' || top_el || '>(.*?)',
1629 'http://www.w3.org/1999/xhtml' AS xmlns,
1630 'unapi-id' AS class,
1631 'tag:open-ils.org:U2@mmr/' || obj_id || '/' || org AS title
1633 )::TEXT || '</' || top_el || E'>\\1'
1639 -- remove ignorable whitesace
1640 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
1643 $F$ LANGUAGE PLPGSQL STABLE;
1648 -- Some test queries
1650 SELECT unapi.memoize( 'bre', 1,'mods32','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1651 SELECT unapi.memoize( 'bre', 1,'marcxml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1652 SELECT unapi.memoize( 'bre', 1,'holdings_xml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1654 SELECT unapi.biblio_record_entry_feed('{1}'::BIGINT[],'mods32','{holdings_xml,acp}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://c64/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1656 SELECT unapi.biblio_record_entry_feed('{7209,7394}'::BIGINT[],'marcxml','{}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://fulfillment2.esilibrary.com/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1657 EXPLAIN ANALYZE SELECT unapi.biblio_record_entry_feed('{7209,7394}'::BIGINT[],'marcxml','{}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://fulfillment2.esilibrary.com/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1658 EXPLAIN ANALYZE SELECT unapi.biblio_record_entry_feed('{7209,7394}'::BIGINT[],'marcxml','{holdings_xml}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://fulfillment2.esilibrary.com/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1659 EXPLAIN ANALYZE SELECT unapi.biblio_record_entry_feed('{7209,7394}'::BIGINT[],'mods32','{holdings_xml}'::TEXT[],'SYS1',NULL,'acn=>1',NULL, NULL,NULL,NULL,NULL,'http://fulfillment2.esilibrary.com/opac/extras/unapi', '<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">2</totalResults><startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">1</startIndex><itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">10</itemsPerPage>');
1661 SELECT unapi.biblio_record_entry_feed('{216}'::BIGINT[],'marcxml','{}'::TEXT[], 'BR1');
1662 EXPLAIN ANALYZE SELECT unapi.bre(216,'marcxml','record','{holdings_xml,bre.unapi}'::TEXT[], 'BR1');
1663 EXPLAIN ANALYZE SELECT unapi.bre(216,'holdings_xml','record','{}'::TEXT[], 'BR1');
1664 EXPLAIN ANALYZE SELECT unapi.holdings_xml(216,4,'BR1',2,'{bre}'::TEXT[]);
1665 EXPLAIN ANALYZE SELECT unapi.bre(216,'mods32','record','{}'::TEXT[], 'BR1');
1667 -- Limit to 5 call numbers, 5 copies, with a preferred library of 4 (BR1), in SYS2 at a depth of 0
1668 EXPLAIN ANALYZE SELECT unapi.bre(36,'marcxml','record','{holdings_xml,mra,acp,acnp,acns,bmp}','SYS2',0,'acn=>5,acp=>5',NULL,TRUE,4);