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 CREATE OR REPLACE FUNCTION evergreen.rank_cp_status(status INT)
46 WITH totally_available AS (
47 SELECT id, 0 AS avail_rank
48 FROM config.copy_status
49 WHERE opac_visible IS TRUE
50 AND copy_active IS TRUE
51 AND id != 1 -- "Checked out"
52 ), almost_available AS (
53 SELECT id, 10 AS avail_rank
54 FROM config.copy_status
55 WHERE holdable IS TRUE
56 AND opac_visible IS TRUE
57 AND copy_active IS FALSE
58 OR id = 1 -- "Checked out"
61 (SELECT avail_rank FROM totally_available WHERE $1 IN (id)),
62 (SELECT avail_rank FROM almost_available WHERE $1 IN (id)),
65 $$ LANGUAGE SQL STABLE;
67 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes(
70 depth INT DEFAULT NULL,
71 slimit HSTORE DEFAULT NULL,
72 soffset HSTORE DEFAULT NULL,
73 pref_lib INT DEFAULT NULL,
74 includes TEXT[] DEFAULT NULL::TEXT[]
75 ) RETURNS TABLE(id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT) AS $$
76 WITH RECURSIVE ou_depth AS (
81 FROM actor.org_unit_type aout
82 INNER JOIN actor.org_unit ou ON ou_type = aout.id
86 ), descendant_depth AS (
90 FROM actor.org_unit ou
91 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
92 JOIN anscestor_depth ad ON (ad.id = ou.id),
94 WHERE ad.depth = ou_depth.depth
99 FROM actor.org_unit ou
100 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
101 JOIN descendant_depth ot ON (ot.id = ou.parent_ou)
102 ), anscestor_depth AS (
106 FROM actor.org_unit ou
107 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
113 FROM actor.org_unit ou
114 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
115 JOIN anscestor_depth ot ON (ot.parent_ou = ou.id)
117 SELECT ou.* FROM actor.org_unit ou JOIN descendant_depth USING (id)
120 SELECT ua.id, ua.name, ua.label_sortkey, MIN(ua.rank) AS rank FROM (
121 SELECT acn.id, aou.name, acn.label_sortkey,
123 FROM asset.call_number acn
124 JOIN asset.copy acp ON (acn.id = acp.call_number)
125 JOIN descendants AS aou ON (acp.circ_lib = aou.id)
126 WHERE acn.record = ANY ($1)
127 AND acn.deleted IS FALSE
128 AND acp.deleted IS FALSE
129 AND CASE WHEN ('exclude_invisible_acn' = ANY($7)) THEN
132 FROM asset.opac_visible_copies
133 WHERE copy_id = acp.id AND record = acn.record
135 GROUP BY acn.id, acp.status, aou.name, acn.label_sortkey, aou.id
139 CASE WHEN aou.id = $2 THEN -20000 END,
140 CASE WHEN aou.id = $6 THEN -10000 END,
141 (SELECT distance - 5000
142 FROM actor.org_unit_descendants_distance($6) as x
143 WHERE x.id = aou.id AND $6 IN (
144 SELECT q.id FROM actor.org_unit_descendants($2) as q)),
145 (SELECT e.distance FROM actor.org_unit_descendants_distance($2) as e WHERE e.id = aou.id),
148 evergreen.rank_cp_status(acp.status)
151 GROUP BY ua.id, ua.name, ua.label_sortkey
152 ORDER BY rank, ua.name, ua.label_sortkey
153 LIMIT ($4 -> 'acn')::INT
154 OFFSET ($5 -> 'acn')::INT;
155 $$ LANGUAGE SQL STABLE ROWS 10;
157 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes
158 ( 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[] )
159 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT)
160 AS $$ SELECT * FROM evergreen.ranked_volumes(ARRAY[$1],$2,$3,$4,$5,$6,$7) $$ LANGUAGE SQL STABLE;
163 CREATE OR REPLACE FUNCTION evergreen.located_uris (
166 pref_lib INT DEFAULT NULL
167 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT) AS $$
168 WITH all_orgs AS (SELECT COALESCE( enabled, FALSE ) AS flag FROM config.global_flag WHERE name = 'opac.located_uri.act_as_copy')
169 SELECT DISTINCT ON (id) * FROM (
170 SELECT acn.id, COALESCE(aou.name,aoud.name), acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
171 FROM asset.call_number acn
172 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
173 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
174 LEFT JOIN actor.org_unit_ancestors( COALESCE($3, $2) ) aou ON (acn.owning_lib = aou.id)
175 LEFT JOIN actor.org_unit_descendants( COALESCE($3, $2) ) aoud ON (acn.owning_lib = aoud.id),
177 WHERE acn.record = ANY ($1)
178 AND acn.deleted IS FALSE
179 AND auri.active IS TRUE
180 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR (all_orgs.flag AND COALESCE(aou.id,aoud.id) IS NOT NULL))
182 SELECT acn.id, COALESCE(aou.name,aoud.name) AS name, acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
183 FROM asset.call_number acn
184 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
185 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
186 LEFT JOIN actor.org_unit_ancestors( $2 ) aou ON (acn.owning_lib = aou.id)
187 LEFT JOIN actor.org_unit_descendants( $2 ) aoud ON (acn.owning_lib = aoud.id),
189 WHERE acn.record = ANY ($1)
190 AND acn.deleted IS FALSE
191 AND auri.active IS TRUE
192 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
193 ORDER BY id, pref_ou DESC;
197 CREATE OR REPLACE FUNCTION evergreen.located_uris ( bibid BIGINT, ouid INT, pref_lib INT DEFAULT NULL)
198 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT)
199 AS $$ SELECT * FROM evergreen.located_uris(ARRAY[$1],$2,$3) $$ LANGUAGE SQL STABLE;
201 CREATE TABLE unapi.bre_output_layout (
202 name TEXT PRIMARY KEY,
203 transform TEXT REFERENCES config.xml_transform (name) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
204 mime_type TEXT NOT NULL,
205 feed_top TEXT NOT NULL,
206 holdings_element TEXT,
208 description_element TEXT,
209 creator_element TEXT,
210 update_ts_element TEXT
213 INSERT INTO unapi.bre_output_layout
214 (name, transform, mime_type, holdings_element, feed_top, title_element, description_element, creator_element, update_ts_element)
216 ('holdings_xml', NULL, 'application/xml', NULL, 'hxml', NULL, NULL, NULL, NULL),
217 ('marcxml', 'marcxml', 'application/marc+xml', 'record', 'collection', NULL, NULL, NULL, NULL),
218 ('mods32', 'mods32', 'application/mods+xml', 'mods', 'modsCollection', NULL, NULL, NULL, NULL)
221 -- Dummy functions, so we can create the real ones out of order
222 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;
223 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;
224 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;
225 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;
226 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;
227 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;
228 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;
229 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;
230 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;
231 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;
232 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;
233 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;
234 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;
235 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;
236 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;
237 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;
238 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;
239 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;
240 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;
241 CREATE OR REPLACE FUNCTION unapi.bre (
247 depth INT DEFAULT NULL,
248 slimit HSTORE DEFAULT NULL,
249 soffset HSTORE DEFAULT NULL,
250 include_xmlns BOOL DEFAULT TRUE,
251 pref_lib INT DEFAULT NULL
253 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
254 CREATE OR REPLACE FUNCTION unapi.mmr (
260 depth INT DEFAULT NULL,
261 slimit HSTORE DEFAULT NULL,
262 soffset HSTORE DEFAULT NULL,
263 include_xmlns BOOL DEFAULT TRUE,
264 pref_lib INT DEFAULT NULL
266 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
267 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;
268 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;
269 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;
270 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;
272 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
276 depth INT DEFAULT NULL,
277 includes TEXT[] DEFAULT NULL::TEXT[],
278 slimit HSTORE DEFAULT NULL,
279 soffset HSTORE DEFAULT NULL,
280 include_xmlns BOOL DEFAULT TRUE,
281 pref_lib INT DEFAULT NULL
283 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
285 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
289 depth INT DEFAULT NULL,
290 includes TEXT[] DEFAULT NULL::TEXT[],
291 slimit HSTORE DEFAULT NULL,
292 soffset HSTORE DEFAULT NULL,
293 include_xmlns BOOL DEFAULT TRUE,
294 pref_lib INT DEFAULT NULL
296 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
298 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;
300 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$
306 'id' || COALESCE(obj_id::TEXT,'') ||
307 'format' || COALESCE(format::TEXT,'') ||
308 'ename' || COALESCE(ename::TEXT,'') ||
309 'includes' || COALESCE(includes::TEXT,'{}'::TEXT[]::TEXT) ||
310 'org' || COALESCE(org::TEXT,'') ||
311 'depth' || COALESCE(depth::TEXT,'') ||
312 'slimit' || COALESCE(slimit::TEXT,'') ||
313 'soffset' || COALESCE(soffset::TEXT,'') ||
314 'include_xmlns' || COALESCE(include_xmlns::TEXT,'');
315 -- RAISE NOTICE 'memoize key: %', key;
318 -- RAISE NOTICE 'memoize hash: %', key;
320 -- XXX cache logic ... memcached? table?
322 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;
325 $F$ LANGUAGE PLPGSQL STABLE;
327 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$
329 layout unapi.bre_output_layout%ROWTYPE;
330 transform config.xml_transform%ROWTYPE;
333 xmlns_uri TEXT := 'http://open-ils.org/spec/feed-xml/v1';
338 IF org = '-' OR org IS NULL THEN
339 SELECT shortname INTO org FROM evergreen.org_top();
342 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
343 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
345 IF layout.name IS NULL THEN
349 SELECT * INTO transform FROM config.xml_transform WHERE name = layout.transform;
350 xmlns_uri := COALESCE(transform.namespace_uri,xmlns_uri);
352 -- Gather the bib xml
353 SELECT XMLAGG( unapi.bre(i, format, '', includes, org, depth, slimit, soffset, include_xmlns)) INTO tmp_xml FROM UNNEST( id_list ) i;
355 IF layout.title_element IS NOT NULL THEN
356 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.title_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, title;
359 IF layout.description_element IS NOT NULL THEN
360 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.description_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, description;
363 IF layout.creator_element IS NOT NULL THEN
364 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.creator_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, creator;
367 IF layout.update_ts_element IS NOT NULL THEN
368 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;
371 IF unapi_url IS NOT NULL THEN
372 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;
375 IF header_xml IS NOT NULL THEN tmp_xml := XMLCONCAT(header_xml,tmp_xml::XML); END IF;
377 element_list := regexp_split_to_array(layout.feed_top,E'\\.');
378 FOR i IN REVERSE ARRAY_UPPER(element_list, 1) .. 1 LOOP
379 EXECUTE 'SELECT XMLELEMENT( name '|| quote_ident(element_list[i]) ||', XMLATTRIBUTES( $1 AS xmlns), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML;
384 $F$ LANGUAGE PLPGSQL STABLE;
386 CREATE OR REPLACE FUNCTION unapi.bre (
392 depth INT DEFAULT NULL,
393 slimit HSTORE DEFAULT NULL,
394 soffset HSTORE DEFAULT NULL,
395 include_xmlns BOOL DEFAULT TRUE,
396 pref_lib INT DEFAULT NULL
400 me biblio.record_entry%ROWTYPE;
401 layout unapi.bre_output_layout%ROWTYPE;
402 xfrm config.xml_transform%ROWTYPE;
412 IF org = '-' OR org IS NULL THEN
413 SELECT shortname INTO org FROM evergreen.org_top();
416 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
422 IF format = 'holdings_xml' THEN -- the special case
423 output := unapi.holdings_xml( obj_id, ouid, org, depth, includes, slimit, soffset, include_xmlns);
427 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
429 IF layout.name IS NULL THEN
433 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
435 SELECT * INTO me FROM biblio.record_entry WHERE id = obj_id;
437 -- grab bib_source, if any
438 IF me.source IS NOT NULL THEN
439 source := unapi.cbs(me.source,NULL,NULL,NULL,NULL);
444 -- grab SVF if we need them
445 IF ('mra' = ANY (includes)) THEN
446 axml := unapi.mra(obj_id,NULL,NULL,NULL,NULL);
451 -- grab holdings if we need them
452 IF ('holdings_xml' = ANY (includes)) THEN
453 hxml := unapi.holdings_xml(obj_id, ouid, org, depth, evergreen.array_remove_item_by_value(includes,'holdings_xml'), slimit, soffset, include_xmlns, pref_lib);
459 -- generate our item node
462 IF format = 'marcxml' THEN
464 IF tmp_xml !~ E'<marc:' THEN -- If we're not using the prefixed namespace in this record, then remove all declarations of it
465 tmp_xml := REGEXP_REPLACE(tmp_xml, ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
468 tmp_xml := oils_xslt_process(me.marc, xfrm.xslt)::XML;
471 top_el := REGEXP_REPLACE(tmp_xml, E'^.*?<((?:\\S+:)?' || layout.holdings_element || ').*$', E'\\1');
473 IF source IS NOT NULL THEN
474 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', source || '</' || top_el || E'>\\1');
477 IF axml IS NOT NULL THEN
478 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
481 IF hxml IS NOT NULL THEN -- XXX how do we configure the holdings position?
482 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
485 IF ('bre.unapi' = ANY (includes)) THEN
486 output := REGEXP_REPLACE(
488 '</' || top_el || '>(.*?)',
492 'http://www.w3.org/1999/xhtml' AS xmlns,
494 'tag:open-ils.org:U2@bre/' || obj_id || '/' || org AS title
496 )::TEXT || '</' || top_el || E'>\\1'
502 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
505 $F$ LANGUAGE PLPGSQL STABLE;
507 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
511 depth INT DEFAULT NULL,
512 includes TEXT[] DEFAULT NULL::TEXT[],
513 slimit HSTORE DEFAULT NULL,
514 soffset HSTORE DEFAULT NULL,
515 include_xmlns BOOL DEFAULT TRUE,
516 pref_lib INT DEFAULT NULL
522 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
523 CASE WHEN ('bre' = ANY ($5)) THEN 'tag:open-ils.org:U2@bre/' || $1 || '/' || $3 ELSE NULL END AS id,
524 (SELECT record_has_holdable_copy FROM asset.record_has_holdable_copy($1)) AS has_holdable
528 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
531 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
533 FROM asset.opac_ou_record_copy_count($2, $1)
537 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
539 FROM asset.staff_ou_record_copy_count($2, $1)
543 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
545 FROM asset.opac_ou_record_copy_count($9, $1)
550 WHEN ('bmp' = ANY ($5)) THEN
552 name monograph_parts,
553 (SELECT XMLAGG(bmp) FROM (
554 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)
555 FROM biblio.monograph_part
563 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
565 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
566 FROM evergreen.ranked_volumes($1, $2, $4, $6, $7, $9, $5) AS y
569 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
570 FROM evergreen.located_uris($1, $2, $9) AS uris
573 CASE WHEN ('ssub' = ANY ($5)) THEN
576 (SELECT XMLAGG(ssub) FROM (
577 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
578 FROM serial.subscription
579 WHERE record_entry = $1
583 CASE WHEN ('acp' = ANY ($5)) THEN
586 (SELECT XMLAGG(acp) FROM (
587 SELECT unapi.acp(p.target_copy,'xml','copy',evergreen.array_remove_item_by_value($5,'acp'), $3, $4, $6, $7, FALSE)
588 FROM biblio.peer_bib_copy_map p
589 JOIN asset.copy c ON (p.target_copy = c.id)
590 WHERE NOT c.deleted AND p.peer_record = $1
591 LIMIT ($6 -> 'acp')::INT
592 OFFSET ($7 -> 'acp')::INT
597 $F$ LANGUAGE SQL STABLE;
599 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$
603 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
604 'tag:open-ils.org:U2@ssub/' || id AS id,
605 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
606 start_date AS start, end_date AS end, expected_date_offset
609 WHEN ('sdist' = ANY ($4)) THEN
610 XMLELEMENT( name distributions,
611 (SELECT XMLAGG(sdist) FROM (
612 SELECT unapi.sdist( id, 'xml', 'distribution', evergreen.array_remove_item_by_value($4,'ssub'), $5, $6, $7, $8, FALSE)
613 FROM serial.distribution
614 WHERE subscription = ssub.id
620 FROM serial.subscription ssub
622 GROUP BY id, start_date, end_date, expected_date_offset, owning_lib;
623 $F$ LANGUAGE SQL STABLE;
625 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$
629 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
630 'tag:open-ils.org:U2@sdist/' || id AS id,
631 'tag:open-ils.org:U2@acn/' || receive_call_number AS receive_call_number,
632 'tag:open-ils.org:U2@acn/' || bind_call_number AS bind_call_number,
633 unit_label_prefix, label, unit_label_suffix, summary_method
635 unapi.aou( holding_lib, $2, 'holding_lib', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8),
636 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,
638 WHEN ('sstr' = ANY ($4)) THEN
639 XMLELEMENT( name streams,
640 (SELECT XMLAGG(sstr) FROM (
641 SELECT unapi.sstr( id, 'xml', 'stream', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
643 WHERE distribution = sdist.id
648 XMLELEMENT( name summaries,
650 WHEN ('sbsum' = ANY ($4)) THEN
651 (SELECT XMLAGG(sbsum) FROM (
652 SELECT unapi.sbsum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
653 FROM serial.basic_summary
654 WHERE distribution = sdist.id
659 WHEN ('sisum' = ANY ($4)) THEN
660 (SELECT XMLAGG(sisum) FROM (
661 SELECT unapi.sisum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
662 FROM serial.index_summary
663 WHERE distribution = sdist.id
668 WHEN ('sssum' = ANY ($4)) THEN
669 (SELECT XMLAGG(sssum) FROM (
670 SELECT unapi.sssum( id, 'xml', 'serial_summary', evergreen.array_remove_item_by_value($4,'sdist'), $5, $6, $7, $8, FALSE)
671 FROM serial.supplement_summary
672 WHERE distribution = sdist.id
678 FROM serial.distribution sdist
680 GROUP BY id, label, unit_label_prefix, unit_label_suffix, holding_lib, summary_method, subscription, receive_call_number, bind_call_number;
681 $F$ LANGUAGE SQL STABLE;
683 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$
687 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
688 'tag:open-ils.org:U2@sstr/' || id AS id,
691 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,
693 WHEN ('sitem' = ANY ($4)) THEN
694 XMLELEMENT( name items,
695 (SELECT XMLAGG(sitem) FROM (
696 SELECT unapi.sitem( id, 'xml', 'serial_item', evergreen.array_remove_item_by_value($4,'sstr'), $5, $6, $7, $8, FALSE)
698 WHERE stream = sstr.id
704 FROM serial.stream sstr
706 GROUP BY id, routing_label, distribution;
707 $F$ LANGUAGE SQL STABLE;
709 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$
713 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
714 'tag:open-ils.org:U2@siss/' || id AS id,
715 create_date, edit_date, label, date_published,
716 holding_code, holding_type, holding_link_id
718 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,
720 WHEN ('sitem' = ANY ($4)) THEN
721 XMLELEMENT( name items,
722 (SELECT XMLAGG(sitem) FROM (
723 SELECT unapi.sitem( id, 'xml', 'serial_item', evergreen.array_remove_item_by_value($4,'siss'), $5, $6, $7, $8, FALSE)
725 WHERE issuance = sstr.id
731 FROM serial.issuance sstr
733 GROUP BY id, create_date, edit_date, label, date_published, holding_code, holding_type, holding_link_id, subscription;
734 $F$ LANGUAGE SQL STABLE;
736 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$
740 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
741 'tag:open-ils.org:U2@sitem/' || id AS id,
742 'tag:open-ils.org:U2@siss/' || issuance AS issuance,
743 date_expected, date_received
745 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,
746 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,
747 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,
748 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
749 -- XMLELEMENT( name notes,
751 -- WHEN ('acpn' = ANY ($4)) THEN
752 -- (SELECT XMLAGG(acpn) FROM (
753 -- SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8)
754 -- FROM asset.copy_note
755 -- WHERE owning_copy = cp.id AND pub
761 FROM serial.item sitem
763 $F$ LANGUAGE SQL STABLE;
766 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$
770 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
771 'tag:open-ils.org:U2@sbsum/' || id AS id,
772 'sssum' AS type, generated_coverage, textual_holdings, show_generated
774 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
776 FROM serial.supplement_summary ssum
778 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
779 $F$ LANGUAGE SQL STABLE;
781 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$
785 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
786 'tag:open-ils.org:U2@sbsum/' || id AS id,
787 'sbsum' AS type, generated_coverage, textual_holdings, show_generated
789 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
791 FROM serial.basic_summary ssum
793 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
794 $F$ LANGUAGE SQL STABLE;
796 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$
800 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
801 'tag:open-ils.org:U2@sbsum/' || id AS id,
802 'sisum' AS type, generated_coverage, textual_holdings, show_generated
804 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
806 FROM serial.index_summary ssum
808 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
809 $F$ LANGUAGE SQL STABLE;
812 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$
816 IF ename = 'circlib' THEN
820 'http://open-ils.org/spec/actors/v1' AS xmlns,
825 FROM actor.org_unit aou
828 EXECUTE $$SELECT XMLELEMENT(
829 name $$ || ename || $$,
831 'http://open-ils.org/spec/actors/v1' AS xmlns,
832 'tag:open-ils.org:U2@aou/' || id AS id,
833 shortname, name, opac_visible
836 FROM actor.org_unit aou
837 WHERE id = $1 $$ INTO output USING obj_id;
843 $F$ LANGUAGE PLPGSQL STABLE;
845 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$
849 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
853 label_prefix AS prefix,
854 label_suffix AS suffix
858 FROM asset.copy_location
860 $F$ LANGUAGE SQL STABLE;
862 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$
866 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
873 FROM config.copy_status
875 $F$ LANGUAGE SQL STABLE;
877 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$
881 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
889 $F$ LANGUAGE SQL STABLE;
891 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$
895 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
901 FROM asset.stat_cat_entry asce
902 JOIN asset.stat_cat sc ON (sc.id = asce.stat_cat)
904 $F$ LANGUAGE SQL STABLE;
906 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$
910 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
911 'tag:open-ils.org:U2@bmp/' || id AS id,
915 'tag:open-ils.org:U2@bre/' || record AS record
918 WHEN ('acp' = ANY ($4)) THEN
919 XMLELEMENT( name copies,
920 (SELECT XMLAGG(acp) FROM (
921 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'bmp'), $5, $6, $7, $8, FALSE)
923 JOIN asset.copy_part_map cpm ON (cpm.target_copy = cp.id)
925 AND cp.deleted IS FALSE
926 ORDER BY COALESCE(cp.copy_number,0), cp.barcode
927 LIMIT ($7 -> 'acp')::INT
928 OFFSET ($8 -> 'acp')::INT
934 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
936 FROM biblio.monograph_part
938 GROUP BY id, label, label_sortkey, record;
939 $F$ LANGUAGE SQL STABLE;
941 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$
945 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
946 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
947 create_date, edit_date, copy_number, circulate, deposit,
948 ref, holdable, deleted, deposit_amount, price, barcode,
949 circ_modifier, circ_as_type, opac_visible, age_protect
951 unapi.ccs( status, $2, 'status', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
952 unapi.acl( location, $2, 'location', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE),
953 unapi.aou( circ_lib, $2, 'circ_lib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
954 unapi.aou( circ_lib, $2, 'circlib', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8),
955 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,
957 WHEN ('acpn' = ANY ($4)) THEN
958 XMLELEMENT( name copy_notes,
959 (SELECT XMLAGG(acpn) FROM (
960 SELECT unapi.acpn( id, 'xml', 'copy_note', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
962 WHERE owning_copy = cp.id AND pub
968 WHEN ('ascecm' = ANY ($4)) THEN
969 XMLELEMENT( name statcats,
970 (SELECT XMLAGG(ascecm) FROM (
971 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
972 FROM asset.stat_cat_entry_copy_map
973 WHERE owning_copy = cp.id
979 WHEN ('bre' = ANY ($4)) THEN
980 XMLELEMENT( name foreign_records,
981 (SELECT XMLAGG(bre) FROM (
982 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
983 FROM biblio.peer_bib_copy_map
984 WHERE target_copy = cp.id
991 WHEN ('bmp' = ANY ($4)) THEN
992 XMLELEMENT( name monograph_parts,
993 (SELECT XMLAGG(bmp) FROM (
994 SELECT unapi.bmp( part, 'xml', 'monograph_part', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
995 FROM asset.copy_part_map
996 WHERE target_copy = cp.id
1002 WHEN ('circ' = ANY ($4)) THEN
1003 XMLELEMENT( name current_circulation,
1004 (SELECT XMLAGG(circ) FROM (
1005 SELECT unapi.circ( id, 'xml', 'circ', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE)
1006 FROM action.circulation
1007 WHERE target_copy = cp.id
1008 AND checkin_time IS NULL
1016 AND cp.deleted IS FALSE
1017 GROUP BY id, status, location, circ_lib, call_number, create_date,
1018 edit_date, copy_number, circulate, deposit, ref, holdable,
1019 deleted, deposit_amount, price, barcode, circ_modifier,
1020 circ_as_type, opac_visible, age_protect;
1021 $F$ LANGUAGE SQL STABLE;
1023 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$
1027 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1028 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
1029 create_date, edit_date, copy_number, circulate, deposit,
1030 ref, holdable, deleted, deposit_amount, price, barcode,
1031 circ_modifier, circ_as_type, opac_visible, age_protect,
1032 status_changed_time, floating, mint_condition,
1033 detailed_contents, sort_key, summary_contents, cost
1035 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),
1036 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),
1037 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),
1038 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),
1039 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,
1040 XMLELEMENT( name copy_notes,
1042 WHEN ('acpn' = ANY ($4)) THEN
1043 (SELECT XMLAGG(acpn) FROM (
1044 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)
1045 FROM asset.copy_note
1046 WHERE owning_copy = cp.id AND pub
1051 XMLELEMENT( name statcats,
1053 WHEN ('ascecm' = ANY ($4)) THEN
1054 (SELECT XMLAGG(ascecm) FROM (
1055 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1056 FROM asset.stat_cat_entry_copy_map
1057 WHERE owning_copy = cp.id
1062 XMLELEMENT( name foreign_records,
1064 WHEN ('bre' = ANY ($4)) THEN
1065 (SELECT XMLAGG(bre) FROM (
1066 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
1067 FROM biblio.peer_bib_copy_map
1068 WHERE target_copy = cp.id
1074 WHEN ('bmp' = ANY ($4)) THEN
1075 XMLELEMENT( name monograph_parts,
1076 (SELECT XMLAGG(bmp) FROM (
1077 SELECT unapi.bmp( part, 'xml', 'monograph_part', evergreen.array_remove_item_by_value($4,'acp'), $5, $6, $7, $8, FALSE)
1078 FROM asset.copy_part_map
1079 WHERE target_copy = cp.id
1085 WHEN ('circ' = ANY ($4)) THEN
1086 XMLELEMENT( name current_circulation,
1087 (SELECT XMLAGG(circ) FROM (
1088 SELECT unapi.circ( id, 'xml', 'circ', evergreen.array_remove_item_by_value($4,'circ'), $5, $6, $7, $8, FALSE)
1089 FROM action.circulation
1090 WHERE target_copy = cp.id
1091 AND checkin_time IS NULL
1099 AND cp.deleted IS FALSE
1100 GROUP BY id, status, location, circ_lib, call_number, create_date,
1101 edit_date, copy_number, circulate, floating, mint_condition,
1102 deposit, ref, holdable, deleted, deposit_amount, price,
1103 barcode, circ_modifier, circ_as_type, opac_visible,
1104 status_changed_time, detailed_contents, sort_key,
1105 summary_contents, cost, age_protect;
1106 $F$ LANGUAGE SQL STABLE;
1108 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$
1112 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1113 'tag:open-ils.org:U2@acn/' || acn.id AS id,
1114 acn.id AS vol_id, o.shortname AS lib,
1115 o.opac_visible AS opac_visible,
1116 deleted, label, label_sortkey, label_class, record
1118 unapi.aou( owning_lib, $2, 'owning_lib', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8),
1120 WHEN ('acp' = ANY ($4)) THEN
1121 CASE WHEN $6 IS NOT NULL THEN
1122 XMLELEMENT( name copies,
1123 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1124 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1125 evergreen.rank_cp_status(cp.status) AS rank_avail
1127 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5), $6) aoud ON (cp.circ_lib = aoud.id)
1128 WHERE cp.call_number = acn.id
1129 AND cp.deleted IS FALSE
1130 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1131 LIMIT ($7 -> 'acp')::INT
1132 OFFSET ($8 -> 'acp')::INT
1136 XMLELEMENT( name copies,
1137 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1138 SELECT unapi.acp( cp.id, 'xml', 'copy', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1139 evergreen.rank_cp_status(cp.status) AS rank_avail
1141 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5) ) aoud ON (cp.circ_lib = aoud.id)
1142 WHERE cp.call_number = acn.id
1143 AND cp.deleted IS FALSE
1144 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1145 LIMIT ($7 -> 'acp')::INT
1146 OFFSET ($8 -> 'acp')::INT
1154 (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)
1156 unapi.acnp( acn.prefix, 'marcxml', 'prefix', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1157 unapi.acns( acn.suffix, 'marcxml', 'suffix', evergreen.array_remove_item_by_value($4,'acn'), $5, $6, $7, $8, FALSE),
1158 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
1160 FROM asset.call_number acn
1161 JOIN actor.org_unit o ON (o.id = acn.owning_lib)
1163 AND acn.deleted IS FALSE
1164 GROUP BY acn.id, o.shortname, o.opac_visible, deleted, label, label_sortkey, label_class, owning_lib, record, acn.prefix, acn.suffix;
1165 $F$ LANGUAGE SQL STABLE;
1167 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$
1169 name call_number_prefix,
1171 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1174 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1178 FROM asset.call_number_prefix
1180 $F$ LANGUAGE SQL STABLE;
1182 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$
1184 name call_number_suffix,
1186 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1189 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1193 FROM asset.call_number_suffix
1195 $F$ LANGUAGE SQL STABLE;
1197 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$
1201 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1202 'tag:open-ils.org:U2@auri/' || uri.id AS id,
1208 WHEN ('acn' = ANY ($4)) THEN
1209 XMLELEMENT( name copies,
1210 (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)
1217 GROUP BY uri.id, use_restriction, href, label;
1218 $F$ LANGUAGE SQL STABLE;
1220 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$
1224 NULL AS xmlns, -- TODO needs equivalent to http://open-ils.org/spec/holdings/v1
1232 FROM config.bib_source
1234 $F$ LANGUAGE SQL STABLE;
1236 CREATE OR REPLACE FUNCTION unapi.mra (
1242 depth INT DEFAULT NULL,
1243 slimit HSTORE DEFAULT NULL,
1244 soffset HSTORE DEFAULT NULL,
1245 include_xmlns BOOL DEFAULT TRUE
1246 ) RETURNS XML AS $F$
1250 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1251 'tag:open-ils.org:U2@mra/' || $1 AS id,
1252 'tag:open-ils.org:U2@bre/' || $1 AS record
1254 (SELECT XMLAGG(foo.y)
1260 cvm.value AS "coded-value",
1269 FROM metabib.record_attr_flat mra
1270 JOIN config.record_attr_definition rad ON (mra.attr = rad.name)
1271 LEFT JOIN config.coded_value_map cvm ON (cvm.ctype = mra.attr AND code = mra.value)
1276 $F$ LANGUAGE SQL STABLE;
1278 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$
1282 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1283 'tag:open-ils.org:U2@circ/' || id AS id,
1287 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,
1288 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
1290 FROM action.circulation
1292 $F$ LANGUAGE SQL STABLE;
1294 CREATE OR REPLACE FUNCTION unapi.mmr_mra (
1300 depth INT DEFAULT NULL,
1301 slimit HSTORE DEFAULT NULL,
1302 soffset HSTORE DEFAULT NULL,
1303 include_xmlns BOOL DEFAULT TRUE,
1304 pref_lib INT DEFAULT NULL
1305 ) RETURNS XML AS $F$
1309 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1310 'tag:open-ils.org:U2@mmr/' || $1 AS metarecord
1312 (SELECT XMLAGG(foo.y)
1314 WITH sourcelist AS (
1315 WITH aou AS (SELECT COALESCE(id, (evergreen.org_top()).id) AS id
1316 FROM actor.org_unit WHERE shortname = $5 LIMIT 1)
1318 FROM metabib.metarecord_source_map, aou
1319 WHERE metarecord = $1 AND (
1321 SELECT 1 FROM asset.opac_visible_copies
1322 WHERE record = source AND circ_lib IN (
1323 SELECT id FROM actor.org_unit_descendants(aou.id, $6))
1326 OR EXISTS (SELECT 1 FROM located_uris(source, aou.id, $10) LIMIT 1)
1334 cmra.value AS "coded-value",
1335 cmra.aid AS "cvmid",
1344 SELECT DISTINCT aid, attr, value
1346 SELECT v.source AS id,
1350 FROM metabib.record_attr_vector_list v
1351 JOIN config.coded_value_map c ON ( c.id = ANY( v.vlist ) )
1353 JOIN sourcelist ON (x.id = sourcelist.source)
1355 JOIN config.record_attr_definition rad ON (cmra.attr = rad.name)
1370 SELECT DISTINCT aid, attr, value
1372 SELECT v.source AS id,
1376 FROM metabib.record_attr_vector_list v
1377 JOIN metabib.uncontrolled_record_attr_value m ON ( m.id = ANY( v.vlist ) )
1379 JOIN sourcelist ON (x.id = sourcelist.source)
1381 JOIN config.record_attr_definition rad ON (umra.attr = rad.name)
1387 $F$ LANGUAGE SQL STABLE;
1389 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
1393 depth INT DEFAULT NULL,
1394 includes TEXT[] DEFAULT NULL::TEXT[],
1395 slimit HSTORE DEFAULT NULL,
1396 soffset HSTORE DEFAULT NULL,
1397 include_xmlns BOOL DEFAULT TRUE,
1398 pref_lib INT DEFAULT NULL
1404 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1405 CASE WHEN ('mmr' = ANY ($5)) THEN 'tag:open-ils.org:U2@mmr/' || $1 || '/' || $3 ELSE NULL END AS id,
1406 (SELECT metarecord_has_holdable_copy FROM asset.metarecord_has_holdable_copy($1)) AS has_holdable
1410 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
1413 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1415 FROM asset.opac_ou_metarecord_copy_count($2, $1)
1419 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1421 FROM asset.staff_ou_metarecord_copy_count($2, $1)
1425 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1427 FROM asset.opac_ou_metarecord_copy_count($9, $1)
1431 -- XXX monograph_parts and foreign_copies are skipped in MRs ... put them back some day?
1434 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
1436 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
1437 FROM evergreen.ranked_volumes((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $4, $6, $7, $9, $5) AS y
1440 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
1441 FROM evergreen.located_uris((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $9) AS uris
1444 CASE WHEN ('ssub' = ANY ($5)) THEN
1447 (SELECT XMLAGG(ssub) FROM (
1448 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
1449 FROM serial.subscription
1450 WHERE record_entry IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
1455 $F$ LANGUAGE SQL STABLE;
1457 CREATE OR REPLACE FUNCTION unapi.mmr (
1463 depth INT DEFAULT NULL,
1464 slimit HSTORE DEFAULT NULL,
1465 soffset HSTORE DEFAULT NULL,
1466 include_xmlns BOOL DEFAULT TRUE,
1467 pref_lib INT DEFAULT NULL
1471 mmrec metabib.metarecord%ROWTYPE;
1472 leadrec biblio.record_entry%ROWTYPE;
1473 subrec biblio.record_entry%ROWTYPE;
1474 layout unapi.bre_output_layout%ROWTYPE;
1475 xfrm config.xml_transform%ROWTYPE;
1477 xml_buf TEXT; -- growing XML document
1478 tmp_xml TEXT; -- single-use XML string
1479 xml_frag TEXT; -- single-use XML fragment
1484 subxml XML; -- subordinate records elements
1489 -- xpath for extracting bre.marc values from subordinate records
1490 -- so they may be appended to the MARC of the master record prior
1491 -- to XSLT processing.
1492 -- subjects, isbn, issn, upc -- anything else?
1494 '//*[starts-with(@tag, "6") or @tag="020" or @tag="022" or @tag="024"]';
1496 IF org = '-' OR org IS NULL THEN
1497 SELECT shortname INTO org FROM evergreen.org_top();
1500 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
1502 IF ouid IS NULL THEN
1506 SELECT INTO mmrec * FROM metabib.metarecord WHERE id = obj_id;
1511 -- TODO: aggregate holdings from constituent records
1512 IF format = 'holdings_xml' THEN -- the special case
1513 output := unapi.mmr_holdings_xml(
1514 obj_id, ouid, org, depth,
1515 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
1516 slimit, soffset, include_xmlns, pref_lib);
1520 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
1522 IF layout.name IS NULL THEN
1526 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
1528 SELECT INTO leadrec * FROM biblio.record_entry WHERE id = mmrec.master_record;
1530 -- Grab distinct MVF for all records if requested
1531 IF ('mra' = ANY (includes)) THEN
1532 axml := unapi.mmr_mra(obj_id,NULL,NULL,NULL,org,depth,NULL,NULL,TRUE,pref_lib);
1537 xml_buf = leadrec.marc;
1540 IF ('holdings_xml' = ANY (includes)) THEN
1541 hxml := unapi.mmr_holdings_xml(
1542 obj_id, ouid, org, depth,
1543 evergreen.array_remove_item_by_value(includes,'holdings_xml'),
1544 slimit, soffset, include_xmlns, pref_lib);
1547 subxml := NULL::XML;
1548 parts := '{}'::TEXT[];
1549 FOR subrec IN SELECT bre.* FROM biblio.record_entry bre
1550 JOIN metabib.metarecord_source_map mmsm ON (mmsm.source = bre.id)
1551 JOIN metabib.metarecord mmr ON (mmr.id = mmsm.metarecord)
1552 WHERE mmr.id = obj_id AND NOT bre.deleted
1553 ORDER BY CASE WHEN bre.id = mmr.master_record THEN 0 ELSE bre.id END
1554 LIMIT COALESCE((slimit->'bre')::INT, 5) LOOP
1556 IF subrec.id = leadrec.id THEN CONTINUE; END IF;
1557 -- Append choice data from the the non-lead records to the
1558 -- the lead record document
1560 parts := parts || xpath(sub_xpath, subrec.marc::XML)::TEXT[];
1563 SELECT ARRAY_TO_STRING( ARRAY_AGG( DISTINCT p ), '' )::XML INTO subxml FROM UNNEST(parts) p;
1565 -- append data from the subordinate records to the
1566 -- main record document before applying the XSLT
1568 IF subxml IS NOT NULL THEN
1569 xml_buf := REGEXP_REPLACE(xml_buf,
1570 '</record>(.*?)$', subxml || '</record>' || E'\\1');
1573 IF format = 'marcxml' THEN
1574 -- If we're not using the prefixed namespace in
1575 -- this record, then remove all declarations of it
1576 IF xml_buf !~ E'<marc:' THEN
1577 xml_buf := REGEXP_REPLACE(xml_buf,
1578 ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
1581 xml_buf := oils_xslt_process(xml_buf, xfrm.xslt)::XML;
1584 -- update top_el to reflect the change in xml_buf, which may
1585 -- now be a different type of document (e.g. record -> mods)
1586 top_el := REGEXP_REPLACE(xml_buf, E'^.*?<((?:\\S+:)?' ||
1587 layout.holdings_element || ').*$', E'\\1');
1589 IF axml IS NOT NULL THEN
1590 xml_buf := REGEXP_REPLACE(xml_buf,
1591 '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
1594 IF hxml IS NOT NULL THEN
1595 xml_buf := REGEXP_REPLACE(xml_buf,
1596 '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
1599 IF ('mmr.unapi' = ANY (includes)) THEN
1600 output := REGEXP_REPLACE(
1602 '</' || top_el || '>(.*?)',
1606 'http://www.w3.org/1999/xhtml' AS xmlns,
1607 'unapi-id' AS class,
1608 'tag:open-ils.org:U2@mmr/' || obj_id || '/' || org AS title
1610 )::TEXT || '</' || top_el || E'>\\1'
1616 -- remove ignorable whitesace
1617 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
1620 $F$ LANGUAGE PLPGSQL STABLE;
1625 -- Some test queries
1627 SELECT unapi.memoize( 'bre', 1,'mods32','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1628 SELECT unapi.memoize( 'bre', 1,'marcxml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1629 SELECT unapi.memoize( 'bre', 1,'holdings_xml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1631 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>');
1633 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>');
1634 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>');
1635 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>');
1636 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>');
1638 SELECT unapi.biblio_record_entry_feed('{216}'::BIGINT[],'marcxml','{}'::TEXT[], 'BR1');
1639 EXPLAIN ANALYZE SELECT unapi.bre(216,'marcxml','record','{holdings_xml,bre.unapi}'::TEXT[], 'BR1');
1640 EXPLAIN ANALYZE SELECT unapi.bre(216,'holdings_xml','record','{}'::TEXT[], 'BR1');
1641 EXPLAIN ANALYZE SELECT unapi.holdings_xml(216,4,'BR1',2,'{bre}'::TEXT[]);
1642 EXPLAIN ANALYZE SELECT unapi.bre(216,'mods32','record','{}'::TEXT[], 'BR1');
1644 -- Limit to 5 call numbers, 5 copies, with a preferred library of 4 (BR1), in SYS2 at a depth of 0
1645 EXPLAIN ANALYZE SELECT unapi.bre(36,'marcxml','record','{holdings_xml,mra,acp,acnp,acns,bmp}','SYS2',0,'acn=>5,acp=>5',NULL,TRUE,4);