[Koha-patches] [PATCH] Refactor GetIssuingRule and GetLoanLength

cfouts at ptfs.com cfouts at ptfs.com
Fri Nov 5 20:51:43 CET 2010


From: Clay Fouts <cfouts at ptfs.com>

This refactors GetIssuingRule and GetLoanLength to make only a single DB query,
caching the result. This dramatically improves XSLT processing of search results
if the search returns bibs with large numbers of items.

---
 C4/Circulation.pm |  141 ++++++++++++++++-------------------------------------
 1 files changed, 43 insertions(+), 98 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 10c3c11..d0b9720 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -1071,121 +1071,66 @@ sub AddIssue {
   return ($datedue);	# not necessarily the same as when it came in!
 }
 
-=head2 GetLoanLength
+=head2 GetIssuingRule
 
-  my $loanlength = &GetLoanLength($borrowertype,$itemtype,branchcode)
+Get the issuing rule for an itemtype, a borrower type and a branch
+Returns a hashref from the issuingrules table.
 
-Get loan length for an itemtype, a borrower type and a branch
+my $irule = &GetIssuingRule($categorycode, $itemtype, $branchcode)
 
 =cut
 
-sub GetLoanLength {
-    my ( $borrowertype, $itemtype, $branchcode ) = @_;
-    my $dbh = C4::Context->dbh;
-    my $sth =
-      $dbh->prepare(
-"select issuelength from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null"
-      );
-# warn "in get loan lenght $borrowertype $itemtype $branchcode ";
-# try to find issuelength & return the 1st available.
-# check with borrowertype, itemtype and branchcode, then without one of those parameters
-    $sth->execute( $borrowertype, $itemtype, $branchcode );
-    my $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
-
-    $sth->execute( $borrowertype, "*", $branchcode );
-    $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
-
-    $sth->execute( "*", $itemtype, $branchcode );
-    $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
-
-    $sth->execute( "*", "*", $branchcode );
-    $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
-
-    $sth->execute( $borrowertype, $itemtype, "*" );
-    $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
-
-    $sth->execute( $borrowertype, "*", "*" );
-    $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
-
-    $sth->execute( "*", $itemtype, "*" );
-    $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
-
-    $sth->execute( "*", "*", "*" );
-    $loanlength = $sth->fetchrow_hashref;
-    return $loanlength->{issuelength}
-      if defined($loanlength) && $loanlength->{issuelength} ne 'NULL';
-
-    # if no rule is set => 21 days (hardcoded)
-    return 21;
-}
-
-=head2 GetIssuingRule
+our $irule_cache = undef;
 
-  my $irule = &GetIssuingRule($borrowertype,$itemtype,branchcode)
+sub _populate_irule_cache() {
+    my ($categorycode, $itemtype, $branchcode) = @_;
 
-FIXME - This is a copy-paste of GetLoanLength
-as a stop-gap.  Do not wish to change API for GetLoanLength 
-this close to release, however, Overdues::GetIssuingRules is broken.
+    $irule_cache = C4::Context->dbh->selectall_hashref(
+        'SELECT * FROM issuingrules WHERE issuelength IS NOT NULL',
+        ['categorycode', 'itemtype', 'branchcode']) or
+        die sprintf "Cannot get issuingrules: %s\n", $!;
 
-Get the issuing rule for an itemtype, a borrower type and a branch
-Returns a hashref from the issuingrules table.
-
-=cut
-
-sub GetIssuingRule {
-    my ( $borrowertype, $itemtype, $branchcode ) = @_;
-    my $dbh = C4::Context->dbh;
-    my $sth =  $dbh->prepare( "select * from issuingrules where categorycode=? and itemtype=? and branchcode=? and issuelength is not null"  );
-    my $irule;
+    return $irule_cache;
+}
 
-	$sth->execute( $borrowertype, $itemtype, $branchcode );
-    $irule = $sth->fetchrow_hashref;
-    return $irule if defined($irule) ;
+sub _clear_irule_cache {
+    return $irule_cache = undef;
+}
 
-    $sth->execute( $borrowertype, "*", $branchcode );
-    $irule = $sth->fetchrow_hashref;
-    return $irule if defined($irule) ;
+sub GetIssuingRule($$$) {
+    my ($categorycode, $itemtype, $branchcode) = @_;
+    die unless $categorycode and $itemtype and $branchcode;
+
+    $irule_cache //= _populate_irule_cache();
+    my $irule = $irule_cache->{$categorycode}{$itemtype}{$branchcode} //
+        $irule_cache->{$categorycode}{'*'}{$branchcode} //
+        $irule_cache->{'*'}{$itemtype}{$branchcode} //
+        $irule_cache->{'*'}{'*'}{$branchcode} //
+        $irule_cache->{$categorycode}{$itemtype}{'*'} //
+        $irule_cache->{$categorycode}{'*'}{'*'} //
+        $irule_cache->{'*'}{$itemtype}{'*'} //
+        $irule_cache->{'*'}{'*'}{'*'} //
+        undef;
+
+    return $irule;
+}
 
-    $sth->execute( "*", $itemtype, $branchcode );
-    $irule = $sth->fetchrow_hashref;
-    return $irule if defined($irule) ;
+=head2 GetLoanLength
 
-    $sth->execute( "*", "*", $branchcode );
-    $irule = $sth->fetchrow_hashref;
-    return $irule if defined($irule) ;
+Get loan length for an itemtype, a borrower type and a branch
 
-    $sth->execute( $borrowertype, $itemtype, "*" );
-    $irule = $sth->fetchrow_hashref;
-    return $irule if defined($irule) ;
+my $loanlength = &GetLoanLength($categorycode, $itemtype, $branchcode)
 
-    $sth->execute( $borrowertype, "*", "*" );
-    $irule = $sth->fetchrow_hashref;
-    return $irule if defined($irule) ;
+=cut
 
-    $sth->execute( "*", $itemtype, "*" );
-    $irule = $sth->fetchrow_hashref;
-    return $irule if defined($irule) ;
+sub GetLoanLength($$$) {
+    my ($categorycode, $itemtype, $branchcode) = @_;
+    my $loanlength = 21;
 
-    $sth->execute( "*", "*", "*" );
-    $irule = $sth->fetchrow_hashref;
-    return $irule if defined($irule) ;
+    my $irule = GetIssuingRule($categorycode, $itemtype, $branchcode);
+    $loanlength = $irule->{issuelength} if defined $irule;
 
-    # if no rule matches,
-    return undef;
+    return $loanlength;
 }
 
 =head2 GetBranchBorrowerCircRule
-- 
1.5.6.5



More information about the Koha-patches mailing list