1 DROP SCHEMA IF EXISTS unapi CASCADE;
6 CREATE OR REPLACE FUNCTION evergreen.org_top()
7 RETURNS actor.org_unit AS $$
8 SELECT * FROM actor.org_unit WHERE parent_ou IS NULL LIMIT 1;
9 $$ LANGUAGE SQL STABLE;
11 CREATE OR REPLACE FUNCTION evergreen.rank_ou(lib INT, search_lib INT, pref_lib INT DEFAULT NULL)
15 -- lib matches search_lib
16 (SELECT CASE WHEN $1 = $2 THEN -20000 END),
18 -- lib matches pref_lib
19 (SELECT CASE WHEN $1 = $3 THEN -10000 END),
22 -- pref_lib is a child of search_lib and lib is a child of pref lib.
23 -- For example, searching CONS, pref lib is SYS1,
24 -- copies at BR1 and BR2 sort to the front.
25 (SELECT distance - 5000
26 FROM actor.org_unit_descendants_distance($3)
27 WHERE id = $1 AND $3 IN (
28 SELECT id FROM actor.org_unit_descendants($2))),
30 -- lib is a child of search_lib
31 (SELECT distance FROM actor.org_unit_descendants_distance($2) WHERE id = $1),
33 -- all others pay cash
36 $$ LANGUAGE SQL STABLE;
38 -- geolocation-aware variant
39 CREATE OR REPLACE FUNCTION evergreen.rank_ou(lib INT, search_lib INT, pref_lib INT, plat FLOAT, plon FLOAT)
43 -- lib matches search_lib
44 (SELECT CASE WHEN $1 = $2 THEN -20000 END),
46 -- lib matches pref_lib
47 (SELECT CASE WHEN $1 = $3 THEN -10000 END),
50 -- pref_lib is a child of search_lib and lib is a child of pref lib.
51 -- For example, searching CONS, pref lib is SYS1,
52 -- copies at BR1 and BR2 sort to the front.
53 (SELECT distance - 5000
54 FROM actor.org_unit_descendants_distance($3)
55 WHERE id = $1 AND $3 IN (
56 SELECT id FROM actor.org_unit_descendants($2))),
58 -- lib is a child of search_lib
59 (SELECT distance FROM actor.org_unit_descendants_distance($2) WHERE id = $1),
61 -- all others pay cash
63 ) + ((SELECT CASE WHEN addr.latitude IS NULL THEN 0 ELSE -20038 END) + (earth_distance( -- shortest GC distance is returned, only half the circumfrence is needed
65 COALESCE(addr.latitude,plat), -- if the org has no coords, we just
66 COALESCE(addr.longitude,plon) -- force 0 distance and let the above tie-break
67 ),ll_to_earth(plat,plon)
68 ) / 1000)::INT ) -- earth_distance is in meters, convert to kilometers and subtract from largest distance
69 FROM actor.org_unit org
70 LEFT JOIN actor.org_address addr ON (org.billing_address = addr.id)
72 $$ LANGUAGE SQL STABLE;
74 -- this version exists mainly to accommodate JSON query transform limitations
75 -- (the transform argument must be an IDL field, not an entire row/object)
76 -- XXX is there another way?
77 CREATE OR REPLACE FUNCTION evergreen.rank_cp(copy_id BIGINT)
80 copy asset.copy%ROWTYPE;
82 SELECT * INTO copy FROM asset.copy WHERE id = copy_id;
83 RETURN evergreen.rank_cp(copy);
85 $$ LANGUAGE PLPGSQL STABLE;
87 CREATE OR REPLACE FUNCTION evergreen.rank_cp(copy asset.copy)
92 WITH totally_available AS (
93 SELECT id, 0 AS avail_rank
94 FROM config.copy_status
95 WHERE opac_visible IS TRUE
96 AND copy_active IS TRUE
97 AND id != 1 -- "Checked out"
98 ), almost_available AS (
99 SELECT id, 10 AS avail_rank
100 FROM config.copy_status
101 WHERE holdable IS TRUE
102 AND opac_visible IS TRUE
103 AND copy_active IS FALSE
104 OR id = 1 -- "Checked out"
107 CASE WHEN NOT copy.opac_visible THEN 100 END,
108 (SELECT avail_rank FROM totally_available WHERE copy.status IN (id)),
109 CASE WHEN copy.holdable THEN
110 (SELECT avail_rank FROM almost_available WHERE copy.status IN (id))
117 $$ LANGUAGE PLPGSQL STABLE;
119 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes(
122 depth INT DEFAULT NULL,
123 slimit HSTORE DEFAULT NULL,
124 soffset HSTORE DEFAULT NULL,
125 pref_lib INT DEFAULT NULL,
126 includes TEXT[] DEFAULT NULL::TEXT[]
127 ) RETURNS TABLE(id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT) AS $$
128 WITH RECURSIVE ou_depth AS (
133 FROM actor.org_unit_type aout
134 INNER JOIN actor.org_unit ou ON ou_type = aout.id
138 ), descendant_depth AS (
142 FROM actor.org_unit ou
143 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
144 JOIN anscestor_depth ad ON (ad.id = ou.id),
146 WHERE ad.depth = ou_depth.depth
151 FROM actor.org_unit ou
152 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
153 JOIN descendant_depth ot ON (ot.id = ou.parent_ou)
154 ), anscestor_depth AS (
158 FROM actor.org_unit ou
159 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
165 FROM actor.org_unit ou
166 JOIN actor.org_unit_type out ON (out.id = ou.ou_type)
167 JOIN anscestor_depth ot ON (ot.parent_ou = ou.id)
169 SELECT ou.* FROM actor.org_unit ou JOIN descendant_depth USING (id)
172 SELECT ua.id, ua.name, ua.label_sortkey, MIN(ua.rank) AS rank FROM (
173 SELECT acn.id, owning_lib.name, acn.label_sortkey,
174 evergreen.rank_cp(acp),
176 FROM asset.call_number acn
177 JOIN asset.copy acp ON (acn.id = acp.call_number)
178 JOIN descendants AS aou ON (acp.circ_lib = aou.id)
179 JOIN actor.org_unit AS owning_lib ON (acn.owning_lib = owning_lib.id)
180 WHERE acn.record = ANY ($1)
181 AND acn.deleted IS FALSE
182 AND acp.deleted IS FALSE
183 AND CASE WHEN ('exclude_invisible_acn' = ANY($7)) THEN
185 WITH basevm AS (SELECT c_attrs FROM asset.patron_default_visibility_mask()),
186 circvm AS (SELECT search.calculate_visibility_attribute_test('circ_lib', ARRAY[acp.circ_lib]) AS mask)
188 FROM basevm, circvm, asset.copy_vis_attr_cache acvac
189 WHERE acvac.vis_attr_vector @@ (basevm.c_attrs || '&' || circvm.mask)::query_int
190 AND acvac.target_copy = acp.id
191 AND acvac.record = acn.record
193 GROUP BY acn.id, evergreen.rank_cp(acp), owning_lib.name, acn.label_sortkey, aou.id
197 CASE WHEN aou.id = $2 THEN -20000 END,
198 CASE WHEN aou.id = $6 THEN -10000 END,
199 (SELECT distance - 5000
200 FROM actor.org_unit_descendants_distance($6) as x
201 WHERE x.id = aou.id AND $6 IN (
202 SELECT q.id FROM actor.org_unit_descendants($2) as q)),
203 (SELECT e.distance FROM actor.org_unit_descendants_distance($2) as e WHERE e.id = aou.id),
206 evergreen.rank_cp(acp)
209 GROUP BY ua.id, ua.name, ua.label_sortkey
210 ORDER BY rank, ua.name, ua.label_sortkey
211 LIMIT ($4 -> 'acn')::INT
212 OFFSET ($5 -> 'acn')::INT;
213 $$ LANGUAGE SQL STABLE ROWS 10;
215 CREATE OR REPLACE FUNCTION evergreen.ranked_volumes
216 ( 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[] )
217 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank BIGINT)
218 AS $$ SELECT * FROM evergreen.ranked_volumes(ARRAY[$1],$2,$3,$4,$5,$6,$7) $$ LANGUAGE SQL STABLE;
221 CREATE OR REPLACE FUNCTION evergreen.located_uris (
224 pref_lib INT DEFAULT NULL
225 ) RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT) AS $$
226 WITH all_orgs AS (SELECT COALESCE( enabled, FALSE ) AS flag FROM config.global_flag WHERE name = 'opac.located_uri.act_as_copy')
227 SELECT DISTINCT ON (id) * FROM (
228 SELECT acn.id, COALESCE(aou.name,aoud.name), acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
229 FROM asset.call_number acn
230 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
231 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
232 LEFT JOIN actor.org_unit_ancestors( COALESCE($3, $2) ) aou ON (acn.owning_lib = aou.id)
233 LEFT JOIN actor.org_unit_descendants( COALESCE($3, $2) ) aoud ON (acn.owning_lib = aoud.id),
235 WHERE acn.record = ANY ($1)
236 AND acn.deleted IS FALSE
237 AND auri.active IS TRUE
238 AND ((NOT all_orgs.flag AND aou.id IS NOT NULL) OR (all_orgs.flag AND COALESCE(aou.id,aoud.id) IS NOT NULL))
240 SELECT acn.id, COALESCE(aou.name,aoud.name) AS name, acn.label_sortkey, evergreen.rank_ou(aou.id, $2, $3) AS pref_ou
241 FROM asset.call_number acn
242 INNER JOIN asset.uri_call_number_map auricnm ON acn.id = auricnm.call_number
243 INNER JOIN asset.uri auri ON auri.id = auricnm.uri
244 LEFT JOIN actor.org_unit_ancestors( $2 ) aou ON (acn.owning_lib = aou.id)
245 LEFT JOIN actor.org_unit_descendants( $2 ) aoud ON (acn.owning_lib = aoud.id),
247 WHERE acn.record = ANY ($1)
248 AND acn.deleted IS FALSE
249 AND auri.active IS TRUE
250 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
251 ORDER BY id, pref_ou DESC;
255 CREATE OR REPLACE FUNCTION evergreen.located_uris ( bibid BIGINT, ouid INT, pref_lib INT DEFAULT NULL)
256 RETURNS TABLE (id BIGINT, name TEXT, label_sortkey TEXT, rank INT)
257 AS $$ SELECT * FROM evergreen.located_uris(ARRAY[$1],$2,$3) $$ LANGUAGE SQL STABLE;
259 CREATE TABLE unapi.bre_output_layout (
260 name TEXT PRIMARY KEY,
261 transform TEXT REFERENCES config.xml_transform (name) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED,
262 mime_type TEXT NOT NULL,
263 feed_top TEXT NOT NULL,
264 holdings_element TEXT,
266 description_element TEXT,
267 creator_element TEXT,
268 update_ts_element TEXT
271 INSERT INTO unapi.bre_output_layout
272 (name, transform, mime_type, holdings_element, feed_top, title_element, description_element, creator_element, update_ts_element)
274 ('holdings_xml', NULL, 'application/xml', NULL, 'hxml', NULL, NULL, NULL, NULL),
275 ('marcxml', 'marcxml', 'application/marc+xml', 'record', 'collection', NULL, NULL, NULL, NULL),
276 ('mods32', 'mods32', 'application/mods+xml', 'mods', 'modsCollection', NULL, NULL, NULL, NULL)
279 -- Dummy functions, so we can create the real ones out of order
280 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;
281 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;
282 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;
283 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;
284 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;
285 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;
286 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;
287 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;
288 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;
289 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;
290 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;
291 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;
292 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;
293 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;
294 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;
295 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;
296 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;
297 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;
298 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;
299 CREATE OR REPLACE FUNCTION unapi.bre (
305 depth INT DEFAULT NULL,
306 slimit HSTORE DEFAULT NULL,
307 soffset HSTORE DEFAULT NULL,
308 include_xmlns BOOL DEFAULT TRUE,
309 pref_lib INT DEFAULT NULL
311 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
312 CREATE OR REPLACE FUNCTION unapi.mmr (
318 depth INT DEFAULT NULL,
319 slimit HSTORE DEFAULT NULL,
320 soffset HSTORE DEFAULT NULL,
321 include_xmlns BOOL DEFAULT TRUE,
322 pref_lib INT DEFAULT NULL
324 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
325 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;
326 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;
327 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;
328 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;
330 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
334 depth INT DEFAULT NULL,
335 includes TEXT[] DEFAULT NULL::TEXT[],
336 slimit HSTORE DEFAULT NULL,
337 soffset HSTORE DEFAULT NULL,
338 include_xmlns BOOL DEFAULT TRUE,
339 pref_lib INT DEFAULT NULL
341 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
343 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
347 depth INT DEFAULT NULL,
348 includes TEXT[] DEFAULT NULL::TEXT[],
349 slimit HSTORE DEFAULT NULL,
350 soffset HSTORE DEFAULT NULL,
351 include_xmlns BOOL DEFAULT TRUE,
352 pref_lib INT DEFAULT NULL
354 RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
356 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, pref_lib INT DEFAULT NULL ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
358 CREATE OR REPLACE FUNCTION unapi.metabib_virtual_record_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, pref_lib INT DEFAULT NULL ) RETURNS XML AS $F$ SELECT NULL::XML $F$ LANGUAGE SQL STABLE;
360 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$
366 'id' || COALESCE(obj_id::TEXT,'') ||
367 'format' || COALESCE(format::TEXT,'') ||
368 'ename' || COALESCE(ename::TEXT,'') ||
369 'includes' || COALESCE(includes::TEXT,'{}'::TEXT[]::TEXT) ||
370 'org' || COALESCE(org::TEXT,'') ||
371 'depth' || COALESCE(depth::TEXT,'') ||
372 'slimit' || COALESCE(slimit::TEXT,'') ||
373 'soffset' || COALESCE(soffset::TEXT,'') ||
374 'include_xmlns' || COALESCE(include_xmlns::TEXT,'');
375 -- RAISE NOTICE 'memoize key: %', key;
378 -- RAISE NOTICE 'memoize hash: %', key;
380 -- XXX cache logic ... memcached? table?
382 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;
385 $F$ LANGUAGE PLPGSQL STABLE;
387 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, pref_lib INT DEFAULT NULL ) RETURNS XML AS $F$
389 layout unapi.bre_output_layout%ROWTYPE;
390 transform config.xml_transform%ROWTYPE;
393 xmlns_uri TEXT := 'http://open-ils.org/spec/feed-xml/v1';
398 IF org = '-' OR org IS NULL THEN
399 SELECT shortname INTO org FROM evergreen.org_top();
402 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
403 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
405 IF layout.name IS NULL THEN
409 SELECT * INTO transform FROM config.xml_transform WHERE name = layout.transform;
410 xmlns_uri := COALESCE(transform.namespace_uri,xmlns_uri);
412 -- Gather the bib xml
413 SELECT XMLAGG( unapi.bre(i, format, '', includes, org, depth, slimit, soffset, include_xmlns, pref_lib)) INTO tmp_xml FROM UNNEST( id_list ) i;
415 IF layout.title_element IS NOT NULL THEN
416 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.title_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, title;
419 IF layout.description_element IS NOT NULL THEN
420 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.description_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, description;
423 IF layout.creator_element IS NOT NULL THEN
424 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.creator_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, creator;
427 IF layout.update_ts_element IS NOT NULL THEN
428 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;
431 IF unapi_url IS NOT NULL THEN
432 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;
435 IF header_xml IS NOT NULL THEN tmp_xml := XMLCONCAT(header_xml,tmp_xml::XML); END IF;
437 element_list := regexp_split_to_array(layout.feed_top,E'\\.');
438 FOR i IN REVERSE ARRAY_UPPER(element_list, 1) .. 1 LOOP
439 EXECUTE 'SELECT XMLELEMENT( name '|| quote_ident(element_list[i]) ||', XMLATTRIBUTES( $1 AS xmlns), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML;
444 $F$ LANGUAGE PLPGSQL STABLE;
446 CREATE OR REPLACE FUNCTION unapi.metabib_virtual_record_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, pref_lib INT DEFAULT NULL ) RETURNS XML AS $F$
448 layout unapi.bre_output_layout%ROWTYPE;
449 transform config.xml_transform%ROWTYPE;
452 xmlns_uri TEXT := 'http://open-ils.org/spec/feed-xml/v1';
457 IF org = '-' OR org IS NULL THEN
458 SELECT shortname INTO org FROM evergreen.org_top();
461 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
462 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
464 IF layout.name IS NULL THEN
468 SELECT * INTO transform FROM config.xml_transform WHERE name = layout.transform;
469 xmlns_uri := COALESCE(transform.namespace_uri,xmlns_uri);
471 -- Gather the bib xml
472 SELECT XMLAGG( unapi.mmr(i, format, '', includes, org, depth, slimit, soffset, include_xmlns, pref_lib)) INTO tmp_xml FROM UNNEST( id_list ) i;
474 IF layout.title_element IS NOT NULL THEN
475 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.title_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, title;
478 IF layout.description_element IS NOT NULL THEN
479 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.description_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, description;
482 IF layout.creator_element IS NOT NULL THEN
483 EXECUTE 'SELECT XMLCONCAT( XMLELEMENT( name '|| layout.creator_element ||', XMLATTRIBUTES( $1 AS xmlns), $3), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML, creator;
486 IF layout.update_ts_element IS NOT NULL THEN
487 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;
490 IF unapi_url IS NOT NULL THEN
491 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;
494 IF header_xml IS NOT NULL THEN tmp_xml := XMLCONCAT(header_xml,tmp_xml::XML); END IF;
496 element_list := regexp_split_to_array(layout.feed_top,E'\\.');
497 FOR i IN REVERSE ARRAY_UPPER(element_list, 1) .. 1 LOOP
498 EXECUTE 'SELECT XMLELEMENT( name '|| quote_ident(element_list[i]) ||', XMLATTRIBUTES( $1 AS xmlns), $2)' INTO tmp_xml USING xmlns_uri, tmp_xml::XML;
503 $F$ LANGUAGE PLPGSQL STABLE;
505 CREATE OR REPLACE FUNCTION unapi.bre (
511 depth INT DEFAULT NULL,
512 slimit HSTORE DEFAULT NULL,
513 soffset HSTORE DEFAULT NULL,
514 include_xmlns BOOL DEFAULT TRUE,
515 pref_lib INT DEFAULT NULL
519 me biblio.record_entry%ROWTYPE;
520 layout unapi.bre_output_layout%ROWTYPE;
521 xfrm config.xml_transform%ROWTYPE;
531 IF org = '-' OR org IS NULL THEN
532 SELECT shortname INTO org FROM evergreen.org_top();
535 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
541 IF format = 'holdings_xml' THEN -- the special case
542 output := unapi.holdings_xml( obj_id, ouid, org, depth, includes, slimit, soffset, include_xmlns);
546 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
548 IF layout.name IS NULL THEN
552 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
554 SELECT * INTO me FROM biblio.record_entry WHERE id = obj_id;
556 -- grab bib_source, if any
557 IF ('cbs' = ANY (includes) AND me.source IS NOT NULL) THEN
558 source := unapi.cbs(me.source,NULL,NULL,NULL,NULL);
563 -- grab SVF if we need them
564 IF ('mra' = ANY (includes)) THEN
565 axml := unapi.mra(obj_id,NULL,NULL,NULL,NULL);
570 -- grab holdings if we need them
571 IF ('holdings_xml' = ANY (includes)) THEN
572 hxml := unapi.holdings_xml(obj_id, ouid, org, depth, array_remove(includes,'holdings_xml'), slimit, soffset, include_xmlns, pref_lib);
578 -- generate our item node
581 IF format = 'marcxml' THEN
583 IF tmp_xml !~ E'<marc:' THEN -- If we're not using the prefixed namespace in this record, then remove all declarations of it
584 tmp_xml := REGEXP_REPLACE(tmp_xml, ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
587 tmp_xml := oils_xslt_process(me.marc, xfrm.xslt)::XML;
590 top_el := REGEXP_REPLACE(tmp_xml, E'^.*?<((?:\\S+:)?' || layout.holdings_element || ').*$', E'\\1');
592 IF source IS NOT NULL THEN
593 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', source || '</' || top_el || E'>\\1');
596 IF axml IS NOT NULL THEN
597 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
600 IF hxml IS NOT NULL THEN -- XXX how do we configure the holdings position?
601 tmp_xml := REGEXP_REPLACE(tmp_xml, '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
604 IF ('bre.unapi' = ANY (includes)) THEN
605 output := REGEXP_REPLACE(
607 '</' || top_el || '>(.*?)',
611 'http://www.w3.org/1999/xhtml' AS xmlns,
613 'tag:open-ils.org:U2@bre/' || obj_id || '/' || org AS title
615 )::TEXT || '</' || top_el || E'>\\1'
621 IF ('bre.extern' = ANY (includes)) THEN
622 output := REGEXP_REPLACE(
624 '</' || top_el || '>(.*?)',
628 'http://open-ils.org/spec/biblio/v1' AS xmlns,
629 me.creator AS creator,
631 me.create_date AS create_date,
632 me.edit_date AS edit_date,
633 me.quality AS quality,
634 me.fingerprint AS fingerprint,
635 me.tcn_source AS tcn_source,
636 me.tcn_value AS tcn_value,
638 me.share_depth AS share_depth,
640 me.deleted AS deleted
642 )::TEXT || '</' || top_el || E'>\\1'
648 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
651 $F$ LANGUAGE PLPGSQL STABLE;
653 CREATE OR REPLACE FUNCTION unapi.holdings_xml (
657 depth INT DEFAULT NULL,
658 includes TEXT[] DEFAULT NULL::TEXT[],
659 slimit HSTORE DEFAULT NULL,
660 soffset HSTORE DEFAULT NULL,
661 include_xmlns BOOL DEFAULT TRUE,
662 pref_lib INT DEFAULT NULL
668 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
669 CASE WHEN ('bre' = ANY ($5)) THEN 'tag:open-ils.org:U2@bre/' || $1 || '/' || $3 ELSE NULL END AS id,
670 (SELECT record_has_holdable_copy FROM asset.record_has_holdable_copy($1)) AS has_holdable
674 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
677 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
679 FROM asset.opac_ou_record_copy_count($2, $1)
683 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
685 FROM asset.staff_ou_record_copy_count($2, $1)
689 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
691 FROM asset.opac_ou_record_copy_count($9, $1)
696 WHEN ('bmp' = ANY ($5)) THEN
698 name monograph_parts,
699 (SELECT XMLAGG(bmp) FROM (
700 SELECT unapi.bmp( id, 'xml', 'monograph_part', array_remove( array_remove($5,'bre'), 'holdings_xml'), $3, $4, $6, $7, FALSE)
701 FROM biblio.monograph_part
702 WHERE NOT deleted AND record = $1
709 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
711 SELECT unapi.acn(y.id,'xml','volume',array_remove( array_remove($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), y.rank, name, label_sortkey
712 FROM evergreen.ranked_volumes($1, $2, $4, $6, $7, $9, $5) AS y
715 SELECT unapi.acn(uris.id,'xml','volume',array_remove( array_remove($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), uris.rank, name, label_sortkey
716 FROM evergreen.located_uris($1, $2, $9) AS uris
719 CASE WHEN ('ssub' = ANY ($5)) THEN
722 (SELECT XMLAGG(ssub) FROM (
723 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
724 FROM serial.subscription
725 WHERE record_entry = $1
729 CASE WHEN ('acp' = ANY ($5)) THEN
732 (SELECT XMLAGG(acp) FROM (
733 SELECT unapi.acp(p.target_copy,'xml','copy',array_remove($5,'acp'), $3, $4, $6, $7, FALSE)
734 FROM biblio.peer_bib_copy_map p
735 JOIN asset.copy c ON (p.target_copy = c.id)
736 WHERE NOT c.deleted AND p.peer_record = $1
737 LIMIT ($6 -> 'acp')::INT
738 OFFSET ($7 -> 'acp')::INT
743 $F$ LANGUAGE SQL STABLE;
745 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$
749 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
750 'tag:open-ils.org:U2@ssub/' || id AS id,
751 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
752 start_date AS start, end_date AS end, expected_date_offset
755 WHEN ('sdist' = ANY ($4)) THEN
756 XMLELEMENT( name distributions,
757 (SELECT XMLAGG(sdist) FROM (
758 SELECT unapi.sdist( id, 'xml', 'distribution', array_remove($4,'ssub'), $5, $6, $7, $8, FALSE)
759 FROM serial.distribution
760 WHERE subscription = ssub.id
766 FROM serial.subscription ssub
768 GROUP BY id, start_date, end_date, expected_date_offset, owning_lib;
769 $F$ LANGUAGE SQL STABLE;
771 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$
775 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
776 'tag:open-ils.org:U2@sdist/' || id AS id,
777 'tag:open-ils.org:U2@acn/' || receive_call_number AS receive_call_number,
778 'tag:open-ils.org:U2@acn/' || bind_call_number AS bind_call_number,
779 unit_label_prefix, label, unit_label_suffix, summary_method
781 unapi.aou( holding_lib, $2, 'holding_lib', array_remove($4,'sdist'), $5, $6, $7, $8),
782 CASE WHEN subscription IS NOT NULL AND ('ssub' = ANY ($4)) THEN unapi.ssub( subscription, 'xml', 'subscription', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE) ELSE NULL END,
784 WHEN ('sstr' = ANY ($4)) THEN
785 XMLELEMENT( name streams,
786 (SELECT XMLAGG(sstr) FROM (
787 SELECT unapi.sstr( id, 'xml', 'stream', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE)
789 WHERE distribution = sdist.id
794 XMLELEMENT( name summaries,
796 WHEN ('sbsum' = ANY ($4)) THEN
797 (SELECT XMLAGG(sbsum) FROM (
798 SELECT unapi.sbsum( id, 'xml', 'serial_summary', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE)
799 FROM serial.basic_summary
800 WHERE distribution = sdist.id
805 WHEN ('sisum' = ANY ($4)) THEN
806 (SELECT XMLAGG(sisum) FROM (
807 SELECT unapi.sisum( id, 'xml', 'serial_summary', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE)
808 FROM serial.index_summary
809 WHERE distribution = sdist.id
814 WHEN ('sssum' = ANY ($4)) THEN
815 (SELECT XMLAGG(sssum) FROM (
816 SELECT unapi.sssum( id, 'xml', 'serial_summary', array_remove($4,'sdist'), $5, $6, $7, $8, FALSE)
817 FROM serial.supplement_summary
818 WHERE distribution = sdist.id
824 FROM serial.distribution sdist
826 GROUP BY id, label, unit_label_prefix, unit_label_suffix, holding_lib, summary_method, subscription, receive_call_number, bind_call_number;
827 $F$ LANGUAGE SQL STABLE;
829 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$
833 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
834 'tag:open-ils.org:U2@sstr/' || id AS id,
837 CASE WHEN distribution IS NOT NULL AND ('sdist' = ANY ($4)) THEN unapi.sssum( distribution, 'xml', 'distribtion', array_remove($4,'sstr'), $5, $6, $7, $8, FALSE) ELSE NULL END,
839 WHEN ('sitem' = ANY ($4)) THEN
840 XMLELEMENT( name items,
841 (SELECT XMLAGG(sitem) FROM (
842 SELECT unapi.sitem( id, 'xml', 'serial_item', array_remove($4,'sstr'), $5, $6, $7, $8, FALSE)
844 WHERE stream = sstr.id
850 FROM serial.stream sstr
852 GROUP BY id, routing_label, distribution;
853 $F$ LANGUAGE SQL STABLE;
855 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$
859 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
860 'tag:open-ils.org:U2@siss/' || id AS id,
861 create_date, edit_date, label, date_published,
862 holding_code, holding_type, holding_link_id
864 CASE WHEN subscription IS NOT NULL AND ('ssub' = ANY ($4)) THEN unapi.ssub( subscription, 'xml', 'subscription', array_remove($4,'siss'), $5, $6, $7, $8, FALSE) ELSE NULL END,
866 WHEN ('sitem' = ANY ($4)) THEN
867 XMLELEMENT( name items,
868 (SELECT XMLAGG(sitem) FROM (
869 SELECT unapi.sitem( id, 'xml', 'serial_item', array_remove($4,'siss'), $5, $6, $7, $8, FALSE)
871 WHERE issuance = sstr.id
877 FROM serial.issuance sstr
879 GROUP BY id, create_date, edit_date, label, date_published, holding_code, holding_type, holding_link_id, subscription;
880 $F$ LANGUAGE SQL STABLE;
882 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$
886 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
887 'tag:open-ils.org:U2@sitem/' || id AS id,
888 'tag:open-ils.org:U2@siss/' || issuance AS issuance,
889 date_expected, date_received
891 CASE WHEN issuance IS NOT NULL AND ('siss' = ANY ($4)) THEN unapi.siss( issuance, $2, 'issuance', array_remove($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
892 CASE WHEN stream IS NOT NULL AND ('sstr' = ANY ($4)) THEN unapi.sstr( stream, $2, 'stream', array_remove($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
893 CASE WHEN unit IS NOT NULL AND ('sunit' = ANY ($4)) THEN unapi.sunit( unit, $2, 'serial_unit', array_remove($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END,
894 CASE WHEN uri IS NOT NULL AND ('auri' = ANY ($4)) THEN unapi.auri( uri, $2, 'uri', array_remove($4,'sitem'), $5, $6, $7, $8, FALSE) ELSE NULL END
895 -- XMLELEMENT( name notes,
897 -- WHEN ('acpn' = ANY ($4)) THEN
898 -- (SELECT XMLAGG(acpn) FROM (
899 -- SELECT unapi.acpn( id, 'xml', 'copy_note', array_remove($4,'acp'), $5, $6, $7, $8)
900 -- FROM asset.copy_note
901 -- WHERE owning_copy = cp.id AND pub
907 FROM serial.item sitem
909 $F$ LANGUAGE SQL STABLE;
912 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$
916 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
917 'tag:open-ils.org:U2@sbsum/' || id AS id,
918 'sssum' AS type, generated_coverage, textual_holdings, show_generated
920 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', array_remove($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
922 FROM serial.supplement_summary ssum
924 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
925 $F$ LANGUAGE SQL STABLE;
927 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$
931 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
932 'tag:open-ils.org:U2@sbsum/' || id AS id,
933 'sbsum' AS type, generated_coverage, textual_holdings, show_generated
935 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', array_remove($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
937 FROM serial.basic_summary ssum
939 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
940 $F$ LANGUAGE SQL STABLE;
942 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$
946 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
947 'tag:open-ils.org:U2@sbsum/' || id AS id,
948 'sisum' AS type, generated_coverage, textual_holdings, show_generated
950 CASE WHEN ('sdist' = ANY ($4)) THEN unapi.sdist( distribution, 'xml', 'distribtion', array_remove($4,'ssum'), $5, $6, $7, $8, FALSE) ELSE NULL END
952 FROM serial.index_summary ssum
954 GROUP BY id, generated_coverage, textual_holdings, distribution, show_generated;
955 $F$ LANGUAGE SQL STABLE;
958 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$
962 IF ename = 'circlib' THEN
966 'http://open-ils.org/spec/actors/v1' AS xmlns,
971 FROM actor.org_unit aou
974 EXECUTE $$SELECT XMLELEMENT(
975 name $$ || ename || $$,
977 'http://open-ils.org/spec/actors/v1' AS xmlns,
978 'tag:open-ils.org:U2@aou/' || id AS id,
979 shortname, name, opac_visible
982 FROM actor.org_unit aou
983 WHERE id = $1 $$ INTO output USING obj_id;
989 $F$ LANGUAGE PLPGSQL STABLE;
991 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$
995 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
999 label_prefix AS prefix,
1000 label_suffix AS suffix
1004 FROM asset.copy_location
1006 $F$ LANGUAGE SQL STABLE;
1008 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$
1012 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1019 FROM config.copy_status
1021 $F$ LANGUAGE SQL STABLE;
1023 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$
1027 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1028 create_date AS date,
1033 FROM asset.copy_note
1035 $F$ LANGUAGE SQL STABLE;
1037 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$
1041 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1047 FROM asset.stat_cat_entry asce
1048 JOIN asset.stat_cat sc ON (sc.id = asce.stat_cat)
1050 $F$ LANGUAGE SQL STABLE;
1052 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$
1054 name monograph_part,
1056 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1057 'tag:open-ils.org:U2@bmp/' || id AS id,
1061 'tag:open-ils.org:U2@bre/' || record AS record
1064 WHEN ('acp' = ANY ($4)) THEN
1065 XMLELEMENT( name copies,
1066 (SELECT XMLAGG(acp) FROM (
1067 SELECT unapi.acp( cp.id, 'xml', 'copy', array_remove($4,'bmp'), $5, $6, $7, $8, FALSE)
1069 JOIN asset.copy_part_map cpm ON (cpm.target_copy = cp.id)
1071 AND cp.deleted IS FALSE
1072 ORDER BY COALESCE(cp.copy_number,0), cp.barcode
1073 LIMIT ($7 -> 'acp')::INT
1074 OFFSET ($8 -> 'acp')::INT
1080 CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( record, 'marcxml', 'record', array_remove($4,'bmp'), $5, $6, $7, $8, FALSE) ELSE NULL END
1082 FROM biblio.monograph_part
1083 WHERE NOT deleted AND id = $1
1084 GROUP BY id, label, label_sortkey, record;
1085 $F$ LANGUAGE SQL STABLE;
1087 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$
1091 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1092 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
1093 create_date, edit_date, copy_number, circulate, deposit,
1094 ref, holdable, deleted, deposit_amount, price, barcode,
1095 circ_modifier, circ_as_type, opac_visible, age_protect
1097 unapi.ccs( status, $2, 'status', array_remove($4,'acp'), $5, $6, $7, $8, FALSE),
1098 unapi.acl( location, $2, 'location', array_remove($4,'acp'), $5, $6, $7, $8, FALSE),
1099 unapi.aou( circ_lib, $2, 'circ_lib', array_remove($4,'acp'), $5, $6, $7, $8),
1100 unapi.aou( circ_lib, $2, 'circlib', array_remove($4,'acp'), $5, $6, $7, $8),
1101 CASE WHEN ('acn' = ANY ($4)) THEN unapi.acn( call_number, $2, 'call_number', array_remove($4,'acp'), $5, $6, $7, $8, FALSE) ELSE NULL END,
1103 WHEN ('acpn' = ANY ($4)) THEN
1104 XMLELEMENT( name copy_notes,
1105 (SELECT XMLAGG(acpn) FROM (
1106 SELECT unapi.acpn( id, 'xml', 'copy_note', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
1107 FROM asset.copy_note
1108 WHERE owning_copy = cp.id AND pub
1114 WHEN ('ascecm' = ANY ($4)) THEN
1115 XMLELEMENT( name statcats,
1116 (SELECT XMLAGG(ascecm) FROM (
1117 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
1118 FROM asset.stat_cat_entry_copy_map
1119 WHERE owning_copy = cp.id
1125 WHEN ('bre' = ANY ($4)) THEN
1126 XMLELEMENT( name foreign_records,
1127 (SELECT XMLAGG(bre) FROM (
1128 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
1129 FROM biblio.peer_bib_copy_map
1130 WHERE target_copy = cp.id
1137 WHEN ('bmp' = ANY ($4)) THEN
1138 XMLELEMENT( name monograph_parts,
1139 (SELECT XMLAGG(bmp) FROM (
1140 SELECT unapi.bmp( part, 'xml', 'monograph_part', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
1141 FROM asset.copy_part_map
1142 WHERE target_copy = cp.id
1148 WHEN ('circ' = ANY ($4)) THEN
1149 XMLELEMENT( name current_circulation,
1150 (SELECT XMLAGG(circ) FROM (
1151 SELECT unapi.circ( id, 'xml', 'circ', array_remove($4,'circ'), $5, $6, $7, $8, FALSE)
1152 FROM action.circulation
1153 WHERE target_copy = cp.id
1154 AND checkin_time IS NULL
1162 AND cp.deleted IS FALSE
1163 GROUP BY id, status, location, circ_lib, call_number, create_date,
1164 edit_date, copy_number, circulate, deposit, ref, holdable,
1165 deleted, deposit_amount, price, barcode, circ_modifier,
1166 circ_as_type, opac_visible, age_protect;
1167 $F$ LANGUAGE SQL STABLE;
1169 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$
1173 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1174 'tag:open-ils.org:U2@acp/' || id AS id, id AS copy_id,
1175 create_date, edit_date, copy_number, circulate, deposit,
1176 ref, holdable, deleted, deposit_amount, price, barcode,
1177 circ_modifier, circ_as_type, opac_visible, age_protect,
1178 status_changed_time, floating, mint_condition,
1179 detailed_contents, sort_key, summary_contents, cost
1181 unapi.ccs( status, $2, 'status', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE),
1182 unapi.acl( location, $2, 'location', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE),
1183 unapi.aou( circ_lib, $2, 'circ_lib', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8),
1184 unapi.aou( circ_lib, $2, 'circlib', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8),
1185 CASE WHEN ('acn' = ANY ($4)) THEN unapi.acn( call_number, $2, 'call_number', array_remove($4,'acp'), $5, $6, $7, $8, FALSE) ELSE NULL END,
1186 XMLELEMENT( name copy_notes,
1188 WHEN ('acpn' = ANY ($4)) THEN
1189 (SELECT XMLAGG(acpn) FROM (
1190 SELECT unapi.acpn( id, 'xml', 'copy_note', array_remove( array_remove($4,'acp'),'sunit'), $5, $6, $7, $8, FALSE)
1191 FROM asset.copy_note
1192 WHERE owning_copy = cp.id AND pub
1197 XMLELEMENT( name statcats,
1199 WHEN ('ascecm' = ANY ($4)) THEN
1200 (SELECT XMLAGG(ascecm) FROM (
1201 SELECT unapi.ascecm( stat_cat_entry, 'xml', 'statcat', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
1202 FROM asset.stat_cat_entry_copy_map
1203 WHERE owning_copy = cp.id
1208 XMLELEMENT( name foreign_records,
1210 WHEN ('bre' = ANY ($4)) THEN
1211 (SELECT XMLAGG(bre) FROM (
1212 SELECT unapi.bre(peer_record,'marcxml','record','{}'::TEXT[], $5, $6, $7, $8, FALSE)
1213 FROM biblio.peer_bib_copy_map
1214 WHERE target_copy = cp.id
1220 WHEN ('bmp' = ANY ($4)) THEN
1221 XMLELEMENT( name monograph_parts,
1222 (SELECT XMLAGG(bmp) FROM (
1223 SELECT unapi.bmp( part, 'xml', 'monograph_part', array_remove($4,'acp'), $5, $6, $7, $8, FALSE)
1224 FROM asset.copy_part_map
1225 WHERE target_copy = cp.id
1231 WHEN ('circ' = ANY ($4)) THEN
1232 XMLELEMENT( name current_circulation,
1233 (SELECT XMLAGG(circ) FROM (
1234 SELECT unapi.circ( id, 'xml', 'circ', array_remove($4,'circ'), $5, $6, $7, $8, FALSE)
1235 FROM action.circulation
1236 WHERE target_copy = cp.id
1237 AND checkin_time IS NULL
1245 AND cp.deleted IS FALSE
1246 GROUP BY id, status, location, circ_lib, call_number, create_date,
1247 edit_date, copy_number, circulate, floating, mint_condition,
1248 deposit, ref, holdable, deleted, deposit_amount, price,
1249 barcode, circ_modifier, circ_as_type, opac_visible,
1250 status_changed_time, detailed_contents, sort_key,
1251 summary_contents, cost, age_protect;
1252 $F$ LANGUAGE SQL STABLE;
1254 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$
1258 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1259 'tag:open-ils.org:U2@acn/' || acn.id AS id,
1260 acn.id AS vol_id, o.shortname AS lib,
1261 o.opac_visible AS opac_visible,
1262 deleted, label, label_sortkey, label_class, record
1264 unapi.aou( owning_lib, $2, 'owning_lib', array_remove($4,'acn'), $5, $6, $7, $8),
1266 WHEN ('acp' = ANY ($4)) THEN
1267 CASE WHEN $6 IS NOT NULL THEN
1268 XMLELEMENT( name copies,
1269 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1270 SELECT unapi.acp( cp.id, 'xml', 'copy', array_remove($4,'acn'), $5, $6, $7, $8, FALSE),
1271 evergreen.rank_cp(cp) AS rank_avail
1273 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5), $6) aoud ON (cp.circ_lib = aoud.id)
1274 WHERE cp.call_number = acn.id
1275 AND cp.deleted IS FALSE
1276 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1277 LIMIT ($7 -> 'acp')::INT
1278 OFFSET ($8 -> 'acp')::INT
1282 XMLELEMENT( name copies,
1283 (SELECT XMLAGG(acp ORDER BY rank_avail) FROM (
1284 SELECT unapi.acp( cp.id, 'xml', 'copy', array_remove($4,'acn'), $5, $6, $7, $8, FALSE),
1285 evergreen.rank_cp(cp) AS rank_avail
1287 JOIN actor.org_unit_descendants( (SELECT id FROM actor.org_unit WHERE shortname = $5) ) aoud ON (cp.circ_lib = aoud.id)
1288 WHERE cp.call_number = acn.id
1289 AND cp.deleted IS FALSE
1290 ORDER BY rank_avail, COALESCE(cp.copy_number,0), cp.barcode
1291 LIMIT ($7 -> 'acp')::INT
1292 OFFSET ($8 -> 'acp')::INT
1300 (SELECT XMLAGG(auri) FROM (SELECT unapi.auri(uri,'xml','uri', array_remove($4,'acn'), $5, $6, $7, $8, FALSE) FROM asset.uri_call_number_map WHERE call_number = acn.id)x)
1302 unapi.acnp( acn.prefix, 'marcxml', 'prefix', array_remove($4,'acn'), $5, $6, $7, $8, FALSE),
1303 unapi.acns( acn.suffix, 'marcxml', 'suffix', array_remove($4,'acn'), $5, $6, $7, $8, FALSE),
1304 CASE WHEN ('bre' = ANY ($4)) THEN unapi.bre( acn.record, 'marcxml', 'record', array_remove($4,'acn'), $5, $6, $7, $8, FALSE) ELSE NULL END
1306 FROM asset.call_number acn
1307 JOIN actor.org_unit o ON (o.id = acn.owning_lib)
1309 AND acn.deleted IS FALSE
1310 GROUP BY acn.id, o.shortname, o.opac_visible, deleted, label, label_sortkey, label_class, owning_lib, record, acn.prefix, acn.suffix;
1311 $F$ LANGUAGE SQL STABLE;
1313 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$
1315 name call_number_prefix,
1317 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1320 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1324 FROM asset.call_number_prefix
1326 $F$ LANGUAGE SQL STABLE;
1328 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$
1330 name call_number_suffix,
1332 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1335 'tag:open-ils.org:U2@aou/' || owning_lib AS owning_lib,
1339 FROM asset.call_number_suffix
1341 $F$ LANGUAGE SQL STABLE;
1343 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$
1347 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1348 'tag:open-ils.org:U2@auri/' || uri.id AS id,
1354 WHEN ('acn' = ANY ($4)) THEN
1355 XMLELEMENT( name copies,
1356 (SELECT XMLAGG(acn) FROM (SELECT unapi.acn( call_number, 'xml', 'copy', array_remove($4,'auri'), $5, $6, $7, $8, FALSE) FROM asset.uri_call_number_map WHERE uri = uri.id)x)
1363 GROUP BY uri.id, use_restriction, href, label;
1364 $F$ LANGUAGE SQL STABLE;
1366 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$
1370 NULL AS xmlns, -- TODO needs equivalent to http://open-ils.org/spec/holdings/v1
1378 FROM config.bib_source
1380 $F$ LANGUAGE SQL STABLE;
1382 CREATE OR REPLACE FUNCTION unapi.mra (
1388 depth INT DEFAULT NULL,
1389 slimit HSTORE DEFAULT NULL,
1390 soffset HSTORE DEFAULT NULL,
1391 include_xmlns BOOL DEFAULT TRUE
1392 ) RETURNS XML AS $F$
1396 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1397 'tag:open-ils.org:U2@mra/' || $1 AS id,
1398 'tag:open-ils.org:U2@bre/' || $1 AS record
1400 (SELECT XMLAGG(foo.y)
1406 cvm.value AS "coded-value",
1415 FROM metabib.record_attr_flat mra
1416 JOIN config.record_attr_definition rad ON (mra.attr = rad.name)
1417 LEFT JOIN config.coded_value_map cvm ON (cvm.ctype = mra.attr AND code = mra.value)
1422 $F$ LANGUAGE SQL STABLE;
1424 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$
1428 CASE WHEN $9 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1429 'tag:open-ils.org:U2@circ/' || id AS id,
1433 CASE WHEN ('aou' = ANY ($4)) THEN unapi.aou( circ_lib, $2, 'circ_lib', array_remove($4,'circ'), $5, $6, $7, $8, FALSE) ELSE NULL END,
1434 CASE WHEN ('acp' = ANY ($4)) THEN unapi.acp( circ_lib, $2, 'target_copy', array_remove($4,'circ'), $5, $6, $7, $8, FALSE) ELSE NULL END
1436 FROM action.circulation
1438 $F$ LANGUAGE SQL STABLE;
1440 CREATE OR REPLACE FUNCTION unapi.mmr_mra (
1446 depth INT DEFAULT NULL,
1447 slimit HSTORE DEFAULT NULL,
1448 soffset HSTORE DEFAULT NULL,
1449 include_xmlns BOOL DEFAULT TRUE,
1450 pref_lib INT DEFAULT NULL
1451 ) RETURNS XML AS $F$
1455 CASE WHEN $9 THEN 'http://open-ils.org/spec/indexing/v1' ELSE NULL END AS xmlns,
1456 'tag:open-ils.org:U2@mmr/' || $1 AS metarecord
1458 (SELECT XMLAGG(foo.y)
1460 WITH sourcelist AS (
1461 WITH aou AS (SELECT COALESCE(id, (evergreen.org_top()).id) AS id FROM actor.org_unit WHERE shortname = $5 LIMIT 1),
1462 basevm AS (SELECT c_attrs FROM asset.patron_default_visibility_mask()),
1463 circvm AS (SELECT search.calculate_visibility_attribute_test('circ_lib', ARRAY_AGG(aoud.id)) AS mask
1464 FROM aou, LATERAL actor.org_unit_descendants(aou.id, $6) aoud)
1466 FROM aou, circvm, basevm, metabib.metarecord_source_map mmsm
1467 WHERE mmsm.metarecord = $1 AND (
1470 FROM circvm, basevm, asset.copy_vis_attr_cache acvac
1471 WHERE acvac.vis_attr_vector @@ (basevm.c_attrs || '&' || circvm.mask)::query_int
1472 AND acvac.record = mmsm.source
1474 OR EXISTS (SELECT 1 FROM evergreen.located_uris(source, aou.id, $10) LIMIT 1)
1475 OR EXISTS (SELECT 1 FROM biblio.record_entry b JOIN config.bib_source src ON (b.source = src.id) WHERE src.transcendant AND b.id = mmsm.source)
1483 cmra.value AS "coded-value",
1484 cmra.aid AS "cvmid",
1494 SELECT DISTINCT aid, attr, value, STRING_AGG(x.id::TEXT, ',') AS source_list
1496 SELECT v.source AS id,
1500 FROM metabib.record_attr_vector_list v
1501 JOIN config.coded_value_map c ON ( c.id = ANY( v.vlist ) )
1503 JOIN sourcelist ON (x.id = sourcelist.source)
1506 JOIN config.record_attr_definition rad ON (cmra.attr = rad.name)
1521 SELECT DISTINCT aid, attr, value
1523 SELECT v.source AS id,
1527 FROM metabib.record_attr_vector_list v
1528 JOIN metabib.uncontrolled_record_attr_value m ON ( m.id = ANY( v.vlist ) )
1530 JOIN sourcelist ON (x.id = sourcelist.source)
1532 JOIN config.record_attr_definition rad ON (umra.attr = rad.name)
1538 $F$ LANGUAGE SQL STABLE;
1540 CREATE OR REPLACE FUNCTION unapi.mmr_holdings_xml (
1544 depth INT DEFAULT NULL,
1545 includes TEXT[] DEFAULT NULL::TEXT[],
1546 slimit HSTORE DEFAULT NULL,
1547 soffset HSTORE DEFAULT NULL,
1548 include_xmlns BOOL DEFAULT TRUE,
1549 pref_lib INT DEFAULT NULL
1555 CASE WHEN $8 THEN 'http://open-ils.org/spec/holdings/v1' ELSE NULL END AS xmlns,
1556 CASE WHEN ('mmr' = ANY ($5)) THEN 'tag:open-ils.org:U2@mmr/' || $1 || '/' || $3 ELSE NULL END AS id,
1557 (SELECT metarecord_has_holdable_copy FROM asset.metarecord_has_holdable_copy($1)) AS has_holdable
1561 (SELECT XMLAGG(XMLELEMENT::XML) FROM (
1564 XMLATTRIBUTES('public' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1566 FROM asset.opac_ou_metarecord_copy_count($2, $1)
1570 XMLATTRIBUTES('staff' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1572 FROM asset.staff_ou_metarecord_copy_count($2, $1)
1576 XMLATTRIBUTES('pref_lib' as type, depth, org_unit, coalesce(transcendant,0) as transcendant, available, visible as count, unshadow)
1578 FROM asset.opac_ou_metarecord_copy_count($9, $1)
1582 -- XXX monograph_parts and foreign_copies are skipped in MRs ... put them back some day?
1585 (SELECT XMLAGG(acn ORDER BY rank, name, label_sortkey) FROM (
1587 SELECT unapi.acn(y.id,'xml','volume',array_remove( array_remove($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), y.rank, name, label_sortkey
1588 FROM evergreen.ranked_volumes((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $4, $6, $7, $9, $5) AS y
1591 SELECT unapi.acn(uris.id,'xml','volume',array_remove( array_remove($5,'holdings_xml'),'bre'), $3, $4, $6, $7, FALSE), uris.rank, name, label_sortkey
1592 FROM evergreen.located_uris((SELECT ARRAY_AGG(source) FROM metabib.metarecord_source_map WHERE metarecord = $1), $2, $9) AS uris
1595 CASE WHEN ('ssub' = ANY ($5)) THEN
1598 (SELECT XMLAGG(ssub) FROM (
1599 SELECT unapi.ssub(id,'xml','subscription','{}'::TEXT[], $3, $4, $6, $7, FALSE)
1600 FROM serial.subscription
1601 WHERE record_entry IN (SELECT source FROM metabib.metarecord_source_map WHERE metarecord = $1)
1606 $F$ LANGUAGE SQL STABLE;
1608 CREATE OR REPLACE FUNCTION unapi.mmr (
1614 depth INT DEFAULT NULL,
1615 slimit HSTORE DEFAULT NULL,
1616 soffset HSTORE DEFAULT NULL,
1617 include_xmlns BOOL DEFAULT TRUE,
1618 pref_lib INT DEFAULT NULL
1622 mmrec metabib.metarecord%ROWTYPE;
1623 leadrec biblio.record_entry%ROWTYPE;
1624 subrec biblio.record_entry%ROWTYPE;
1625 layout unapi.bre_output_layout%ROWTYPE;
1626 xfrm config.xml_transform%ROWTYPE;
1628 xml_buf TEXT; -- growing XML document
1629 tmp_xml TEXT; -- single-use XML string
1630 xml_frag TEXT; -- single-use XML fragment
1635 subxml XML; -- subordinate records elements
1640 -- xpath for extracting bre.marc values from subordinate records
1641 -- so they may be appended to the MARC of the master record prior
1642 -- to XSLT processing.
1643 -- subjects, isbn, issn, upc -- anything else?
1645 '//*[starts-with(@tag, "6") or @tag="020" or @tag="022" or @tag="024"]';
1647 IF org = '-' OR org IS NULL THEN
1648 SELECT shortname INTO org FROM evergreen.org_top();
1651 SELECT id INTO ouid FROM actor.org_unit WHERE shortname = org;
1653 IF ouid IS NULL THEN
1657 SELECT INTO mmrec * FROM metabib.metarecord WHERE id = obj_id;
1662 -- TODO: aggregate holdings from constituent records
1663 IF format = 'holdings_xml' THEN -- the special case
1664 output := unapi.mmr_holdings_xml(
1665 obj_id, ouid, org, depth,
1666 array_remove(includes,'holdings_xml'),
1667 slimit, soffset, include_xmlns, pref_lib);
1671 SELECT * INTO layout FROM unapi.bre_output_layout WHERE name = format;
1673 IF layout.name IS NULL THEN
1677 SELECT * INTO xfrm FROM config.xml_transform WHERE name = layout.transform;
1679 SELECT INTO leadrec * FROM biblio.record_entry WHERE id = mmrec.master_record;
1681 -- Grab distinct MVF for all records if requested
1682 IF ('mra' = ANY (includes)) THEN
1683 axml := unapi.mmr_mra(obj_id,NULL,NULL,NULL,org,depth,NULL,NULL,TRUE,pref_lib);
1688 xml_buf = leadrec.marc;
1691 IF ('holdings_xml' = ANY (includes)) THEN
1692 hxml := unapi.mmr_holdings_xml(
1693 obj_id, ouid, org, depth,
1694 array_remove(includes,'holdings_xml'),
1695 slimit, soffset, include_xmlns, pref_lib);
1698 subxml := NULL::XML;
1699 parts := '{}'::TEXT[];
1700 FOR subrec IN SELECT bre.* FROM biblio.record_entry bre
1701 JOIN metabib.metarecord_source_map mmsm ON (mmsm.source = bre.id)
1702 JOIN metabib.metarecord mmr ON (mmr.id = mmsm.metarecord)
1703 WHERE mmr.id = obj_id AND NOT bre.deleted
1704 ORDER BY CASE WHEN bre.id = mmr.master_record THEN 0 ELSE bre.id END
1705 LIMIT COALESCE((slimit->'bre')::INT, 5) LOOP
1707 IF subrec.id = leadrec.id THEN CONTINUE; END IF;
1708 -- Append choice data from the the non-lead records to the
1709 -- the lead record document
1711 parts := parts || xpath(sub_xpath, subrec.marc::XML)::TEXT[];
1714 SELECT STRING_AGG( DISTINCT p , '' )::XML INTO subxml FROM UNNEST(parts) p;
1716 -- append data from the subordinate records to the
1717 -- main record document before applying the XSLT
1719 IF subxml IS NOT NULL THEN
1720 xml_buf := REGEXP_REPLACE(xml_buf,
1721 '</record>(.*?)$', subxml || '</record>' || E'\\1');
1724 IF format = 'marcxml' THEN
1725 -- If we're not using the prefixed namespace in
1726 -- this record, then remove all declarations of it
1727 IF xml_buf !~ E'<marc:' THEN
1728 xml_buf := REGEXP_REPLACE(xml_buf,
1729 ' xmlns:marc="http://www.loc.gov/MARC21/slim"', '', 'g');
1732 xml_buf := oils_xslt_process(xml_buf, xfrm.xslt)::XML;
1735 -- update top_el to reflect the change in xml_buf, which may
1736 -- now be a different type of document (e.g. record -> mods)
1737 top_el := REGEXP_REPLACE(xml_buf, E'^.*?<((?:\\S+:)?' ||
1738 layout.holdings_element || ').*$', E'\\1');
1740 IF axml IS NOT NULL THEN
1741 xml_buf := REGEXP_REPLACE(xml_buf,
1742 '</' || top_el || '>(.*?)$', axml || '</' || top_el || E'>\\1');
1745 IF hxml IS NOT NULL THEN
1746 xml_buf := REGEXP_REPLACE(xml_buf,
1747 '</' || top_el || '>(.*?)$', hxml || '</' || top_el || E'>\\1');
1750 IF ('mmr.unapi' = ANY (includes)) THEN
1751 output := REGEXP_REPLACE(
1753 '</' || top_el || '>(.*?)',
1757 'http://www.w3.org/1999/xhtml' AS xmlns,
1758 'unapi-id' AS class,
1759 'tag:open-ils.org:U2@mmr/' || obj_id || '/' || org AS title
1761 )::TEXT || '</' || top_el || E'>\\1'
1767 -- remove ignorable whitesace
1768 output := REGEXP_REPLACE(output::TEXT,E'>\\s+<','><','gs')::XML;
1771 $F$ LANGUAGE PLPGSQL STABLE;
1773 -- note not adding a companion version which takes an int[] as
1774 -- the first arument, similar to evergreen.located_uris(), because
1775 -- json_query is unable to distinguish between the type of paramaters,
1776 -- leading to 'Could not choose a best candidate function' errors.
1777 CREATE OR REPLACE FUNCTION evergreen.located_uris_as_uris
1778 (bibid BIGINT, ouid INT, pref_lib INT DEFAULT NULL)
1779 RETURNS SETOF asset.uri AS $FUNK$
1780 /* Maps a bib directly to its scoped asset.uri's */
1783 FROM evergreen.located_uris($1, $2, $3) located_uri
1784 JOIN asset.uri_call_number_map map ON (map.call_number = located_uri.id)
1785 JOIN asset.uri uri ON (uri.id = map.uri)
1786 $FUNK$ LANGUAGE SQL STABLE;
1791 -- Some test queries
1793 SELECT unapi.memoize( 'bre', 1,'mods32','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1794 SELECT unapi.memoize( 'bre', 1,'marcxml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1795 SELECT unapi.memoize( 'bre', 1,'holdings_xml','','{holdings_xml,acp}'::TEXT[], 'SYS1');
1797 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>');
1799 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>');
1800 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>');
1801 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>');
1802 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>');
1804 SELECT unapi.biblio_record_entry_feed('{216}'::BIGINT[],'marcxml','{}'::TEXT[], 'BR1');
1805 EXPLAIN ANALYZE SELECT unapi.bre(216,'marcxml','record','{holdings_xml,bre.unapi}'::TEXT[], 'BR1');
1806 EXPLAIN ANALYZE SELECT unapi.bre(216,'holdings_xml','record','{}'::TEXT[], 'BR1');
1807 EXPLAIN ANALYZE SELECT unapi.holdings_xml(216,4,'BR1',2,'{bre}'::TEXT[]);
1808 EXPLAIN ANALYZE SELECT unapi.bre(216,'mods32','record','{}'::TEXT[], 'BR1');
1810 -- Limit to 5 call numbers, 5 copies, with a preferred library of 4 (BR1), in SYS2 at a depth of 0
1811 EXPLAIN ANALYZE SELECT unapi.bre(36,'marcxml','record','{holdings_xml,mra,acp,acnp,acns,bmp}','SYS2',0,'acn=>5,acp=>5',NULL,TRUE,4);