v_oldest timestamptz := NUll;
v_c_oldest timestamptz := NULL;
v_key_count INT8;
+ v_circ_libs_size INT4;
BEGIN
IF array_length(p_circ_libs,1) > 0 THEN
SELECT ARRAY_AGG(id) INTO a_circ_libs FROM actor.org_unit;
END IF;
+ v_circ_libs_size := array_length(a_circ_libs, 1);
+
FOREACH v_circ_lib IN ARRAY a_circ_libs LOOP
v_found := 0;
v_seen := '';
v_c_oldest := NULL;
+
+ -- If there is only one circ lib in the array then we do not need
+ -- to get the key count, so set v_key_count to 0 to force second
+ -- query.
+ IF v_circ_libs_size > 1 THEN
+ SELECT COUNT(keys) FROM skeys(v_results) AS keys INTO v_key_count;
+ ELSE
+ v_key_count = 0;
+ END IF;
+
-- If the number of keys in v_results is greater than or equal to
-- the number of results we need for the current page, then only look
-- for records that are newer than items currently in v_results
- SELECT COUNT(keys) FROM skeys(v_results) AS keys INTO v_key_count;
IF v_key_count >= p_limit + p_offset THEN
open v_cursor NO SCROLL FOR
SELECT c.call_number, c.create_date, c.status, c.location
CLOSE v_cursor;
-- Update oldest information based on oldest row added in current loop
- IF v_oldest IS NULL OR v_oldest < v_c_oldest THEN
+ -- only if we have more than one circ lib
+ IF (v_oldest IS NULL OR v_oldest < v_c_oldest) AND v_circ_libs_size > 1 THEN
v_oldest := v_c_oldest;
END IF;
v_oldest timestamptz := NUll;
v_c_oldest timestamptz := NULL;
v_key_count INT8;
+ v_circ_libs_size INT4;
BEGIN
IF array_length(p_circ_libs,1) > 0 THEN
SELECT ARRAY_AGG(id) INTO a_circ_libs FROM actor.org_unit;
END IF;
+ v_circ_libs_size := array_length(a_circ_libs, 1);
+
FOREACH v_circ_lib IN ARRAY a_circ_libs LOOP
v_found := 0;
v_seen := '';
v_c_oldest := NULL;
+
+ -- If there is only one circ lib in the array then we do not need
+ -- to get the key count, so set v_key_count to 0 to force second
+ -- query.
+ IF v_circ_libs_size > 1 THEN
+ SELECT COUNT(keys) FROM skeys(v_results) AS keys INTO v_key_count;
+ ELSE
+ v_key_count = 0;
+ END IF;
+
-- If the number of keys in v_results is greater than or equal to
-- the number of results we need for the current page, then only look
-- for records that are newer than items currently in v_results
- SELECT COUNT(keys) FROM skeys(v_results) AS keys INTO v_key_count;
IF v_key_count >= p_limit + p_offset THEN
open v_cursor NO SCROLL FOR
SELECT c.call_number, c.create_date, c.status, c.location
CLOSE v_cursor;
-- Update oldest information based on oldest row added in current loop
- IF v_oldest IS NULL OR v_oldest < v_c_oldest THEN
+ -- only if we have more than one circ lib
+ IF (v_oldest IS NULL OR v_oldest < v_c_oldest) AND v_circ_libs_size > 1 THEN
v_oldest := v_c_oldest;
END IF;