[Koha-patches] [PATCH] Bug 7796 - C4/VirtualShelves.pm $dbh plack scoping
Dobrica Pavlinusic
dpavlin at rot13.org
Thu Mar 22 17:09:13 CET 2012
we get a lot of "MySQL server has gone away" error messages and
defining global $dbh isn't the best solution. Existing code
gets $dbh from context in some subs, so this change makes it always
use $dbh from context.
---
C4/VirtualShelves.pm | 15 +++++++++++++--
1 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/C4/VirtualShelves.pm b/C4/VirtualShelves.pm
index 85dbc9f..2091c7a 100644
--- a/C4/VirtualShelves.pm
+++ b/C4/VirtualShelves.pm
@@ -54,8 +54,6 @@ BEGIN {
}
-my $dbh = C4::Context->dbh;
-
=head1 NAME
C4::VirtualShelves - Functions for manipulating Koha virtual shelves
@@ -128,6 +126,7 @@ sub GetShelves {
ORDER BY vs.shelfname
LIMIT ?, ?};
+ my $dbh = C4::Context->dbh;
my $sth2 = $dbh->prepare($query);
$sth2->execute(@params);
my %shelflist;
@@ -174,6 +173,7 @@ sub GetAllShelves {
$query.='AND (allow_add=1 OR owner=?) ' if $adding_allowed;
push @params, $owner if $adding_allowed;
$query.= 'ORDER BY shelfname ASC';
+ my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare( $query );
$sth->execute(@params);
return $sth->fetchall_arrayref({});
@@ -197,6 +197,8 @@ sub GetSomeShelfNames {
push @params, $owner||0 if $adding_allowed;
$qry1.= "ORDER BY vs.lastmodified DESC LIMIT $limit";
+ my $dbh = C4::Context->dbh;
+
unless($adding_allowed && (!defined($owner) || $owner<=0)) {
#if adding items, user should be known
$pub= $dbh->selectall_arrayref($qry1,{Slice=>{}}, at params);
@@ -235,6 +237,7 @@ sub GetShelf ($) {
FROM virtualshelves
WHERE shelfnumber=?
);
+ my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($query);
$sth->execute($shelfnumber);
return $sth->fetchrow;
@@ -324,6 +327,7 @@ sub AddShelf {
(shelfname,owner,category,sortfield,allow_add,allow_delete_own,allow_delete_other)
VALUES (?,?,?,?,?,?,?));
+ my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($query);
$sth->execute(
$hashref->{shelfname},
@@ -354,6 +358,7 @@ sub AddToShelf {
FROM virtualshelfcontents
WHERE shelfnumber=? AND biblionumber=?
);
+ my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($query);
$sth->execute( $shelfnumber, $biblionumber );
@@ -390,6 +395,7 @@ sub ModShelf {
my ($shelfnumber,$hashref) = @_;
my $query= "SELECT * FROM virtualshelves WHERE shelfnumber=?";
+ my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($query);
$sth->execute($shelfnumber);
my $oldrecord= $sth->fetchrow_hashref;
@@ -465,6 +471,7 @@ sub ShelfPossibleAction {
AND sh.borrowernumber=?
WHERE vs.shelfnumber=?
/;
+ my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($query);
$sth->execute($user, $shelfnumber);
my $shelf= $sth->fetchrow_hashref;
@@ -506,6 +513,7 @@ Returns 0 if no items have been deleted.
sub DelFromShelf {
my ($bibref, $shelfnumber, $user) = @_;
my $query = qq(SELECT allow_delete_own, allow_delete_other FROM virtualshelves WHERE shelfnumber=?);
+ my $dbh = C4::Context->dbh;
my $sth= $dbh->prepare($query);
$sth->execute($shelfnumber);
my ($del_own, $del_oth)= $sth->fetchrow;
@@ -549,6 +557,7 @@ ShelfPossibleAction with manage parameter.
sub DelShelf {
my ($shelfnumber)= @_;
return unless $shelfnumber && $shelfnumber =~ /^\d+$/;
+ my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("DELETE FROM virtualshelves WHERE shelfnumber=?");
return $sth->execute($shelfnumber);
}
@@ -646,6 +655,7 @@ sub _shelf_count {
$query.='WHERE category=2';
@params= ();
}
+ my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($query);
$sth->execute(@params);
my ($total)= $sth->fetchrow;
@@ -686,6 +696,7 @@ sub _CheckShelfName {
else {
$query.= ' AND category=2';
}
+ my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare($query);
$sth->execute($cat==1? ($name, $number, $owner, $owner): ($name, $number));
return $sth->rows>0? 0: 1;
--
1.7.2.5
More information about the Koha-patches
mailing list