From d37987ce83ae11312bdbcc1207b7ad1d37f772f4 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 30 Aug 2023 18:56:59 +0000 Subject: [PATCH] Bug 34666: Combine queries in _Findgroupreserve The queries here are the same except for 2 differences: 1 - They check if the hold was on a particular item 2 - The latter confirms that the reserve item group matches the item's item group For 1, it doesn't matter - only 1 item can be mapped ot a reserve, itemnumber is the primary key for hold_fill_targets - so we are either matching it in the first query or the second, either way we get the same reserve - the returns are the same so we don't care which query it came from For 2, this has already been checked when the queue was built. We don't need to verify the match because it wouldn't be in the targets if they didn't match To test: 1 - Apply second unit test patch 2 - prove t/db_dependent/Reserves.t 3 - It should pass 4 - Apply this patch 5 - prove t/db_dependent/Reserves.t 6 - It continues to pass Signed-off-by: David Nind Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 72bfb416d3725bd11dc4595ac429d4c510d6d3ee) Signed-off-by: Fridolin Somers (cherry picked from commit 7aa6464c239873cb52dee11fd079280eea944b62) Signed-off-by: Matt Blenkinsop --- C4/Reserves.pm | 47 +++-------------------------------------------- 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/C4/Reserves.pm b/C4/Reserves.pm index 7efd3744ab..6824d9363c 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -1734,9 +1734,8 @@ sub _Findgroupreserve { my ( $bibitem, $biblio, $itemnumber, $lookahead, $ignore_borrowers) = @_; my $dbh = C4::Context->dbh; - # TODO: consolidate at least the SELECT portion of the first 2 queries to a common $select var. - # check for exact targeted match - my $item_level_target_query = qq{ + # check for targeted match form the holds queue + my $hold_target_query = qq{ SELECT reserves.biblionumber AS biblionumber, reserves.borrowernumber AS borrowernumber, reserves.reservedate AS reservedate, @@ -1757,13 +1756,12 @@ sub _Findgroupreserve { JOIN hold_fill_targets USING (reserve_id) WHERE found IS NULL AND priority > 0 - AND item_level_request = 1 AND hold_fill_targets.itemnumber = ? AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY) AND suspend = 0 ORDER BY priority }; - my $sth = $dbh->prepare($item_level_target_query); + my $sth = $dbh->prepare($hold_target_query); $sth->execute($itemnumber, $lookahead||0); my @results; if ( my $data = $sth->fetchrow_hashref ) { @@ -1772,45 +1770,6 @@ sub _Findgroupreserve { } return @results if @results; - # check for title-level targeted match - my $title_level_target_query = qq{ - SELECT reserves.biblionumber AS biblionumber, - reserves.borrowernumber AS borrowernumber, - reserves.reservedate AS reservedate, - reserves.branchcode AS branchcode, - reserves.cancellationdate AS cancellationdate, - reserves.found AS found, - reserves.reservenotes AS reservenotes, - reserves.priority AS priority, - reserves.timestamp AS timestamp, - biblioitems.biblioitemnumber AS biblioitemnumber, - reserves.itemnumber AS itemnumber, - reserves.reserve_id AS reserve_id, - reserves.itemtype AS itemtype, - reserves.non_priority AS non_priority, - reserves.item_group_id AS item_group_id - FROM reserves - JOIN biblioitems USING (biblionumber) - JOIN hold_fill_targets USING (reserve_id) - LEFT JOIN item_group_items ON ( item_group_items.item_id = hold_fill_targets.itemnumber ) - WHERE found IS NULL - AND priority > 0 - AND item_level_request = 0 - AND hold_fill_targets.itemnumber = ? - AND reservedate <= DATE_ADD(NOW(),INTERVAL ? DAY) - AND suspend = 0 - AND (reserves.item_group_id = item_group_items.item_group_id OR reserves.item_group_id IS NULL) - ORDER BY priority - }; - $sth = $dbh->prepare($title_level_target_query); - $sth->execute($itemnumber, $lookahead||0); - @results = (); - if ( my $data = $sth->fetchrow_hashref ) { - push( @results, $data ) - unless any{ $data->{borrowernumber} eq $_ } @$ignore_borrowers ; - } - return @results if @results; - my $query = qq{ SELECT reserves.biblionumber AS biblionumber, reserves.borrowernumber AS borrowernumber, -- 2.20.1