Bio::DB::GFF::Adaptor::dbi caching_handle
Other packages in the module: Bio::DB::GFF::Adaptor::dbi::caching_handle Bio::DB::GFF::Adaptor::dbi::faux_dbh
SummaryIncluded librariesPackage variablesSynopsisDescriptionGeneral documentationMethods
Summary
Bio::DB::GFF::Adaptor::dbi::caching_handle -- Cache for database handles
Package variables
Globals (from "use vars" definitions)
$AUTOLOAD
$VERSION = 1.0
Included modules
Bio::Root::Root
DBI
Inherit
Bio::Root::Root
Synopsis
 use Bio::DB::GFF::Adaptor::dbi::caching_handle;
 $db  = Bio::DB::GFF::Adaptor::dbi::caching_handle->new('dbi:mysql:test');
 $sth = $db->prepare('select * from foo');
 @h   = $sth->fetch_rowarray;
 $sth->finish
Description
This module handles a pool of database handles. It was motivated by
the MYSQL driver's {mysql_use_result} attribute, which dramatically
improves query speed and memory usage, but forbids additional query
statements from being evaluated while an existing one is in use.
This module is a plug-in replacement for vanilla DBI. It
automatically activates the {mysql_use_result} attribute for the mysql
driver, but avoids problems with multiple active statement handlers by
creating new database handles as needed.
Methods
new
No description
Code
AUTOLOAD
No description
Code
debugDescriptionCode
prepare
No description
Code
do_queryDescriptionCode
dbh
No description
Code
disconnect
No description
Code
dbi_quoteDescriptionCode
Methods description
debugcode    nextTop
 Title   : debug
 Usage   : $debug = $db->debug([$debug])
 Function: activate debugging messages
 Returns : current state of flag
 Args    : optional new setting of flag
 Status  : public
do_querycodeprevnextTop
 Title   : do_query
 Usage   : $sth = $db->do_query($query,@args)
 Function: perform a DBI query
 Returns : a statement handler
 Args    : query string and list of bind arguments
 Status  : Public
This method performs a DBI prepare() and execute(), returning a
statement handle. You will typically call fetch() of fetchrow_array()
on the statement handle. The parsed statement handle is cached for
later use.
dbi_quotecodeprevnextTop
 Title   : dbi_quote
 Usage   : $string = $db->dbi_quote($sql,@args)
 Function: perform bind variable substitution
 Returns : query string
 Args    : the query string and bind arguments
 Status  : public
This method replaces the bind variable "?" in a SQL statement with
appropriately quoted bind arguments. It is used internally to handle
drivers that don't support argument binding.
Methods code
newdescriptionprevnextTop
sub new {
  my $class    = shift;
  my @dbi_args = @_;
  my $self = bless {
		    dbh    => [],
		    args   =>\@ dbi_args,
		    debug => 0,
		   },$class;
  $self->dbh || $self->throw("Can't connect to database: " . DBI->errstr);
  $self;
}
AUTOLOADdescriptionprevnextTop
sub AUTOLOAD {
  my($pack,$func_name) = $AUTOLOAD=~/(.+)::([^:]+)$/;
  return if $func_name eq 'DESTROY';
  my $self = shift or return DBI->$func_name(@_);
  $self->dbh->$func_name(@_);
}
debugdescriptionprevnextTop
sub debug {
  my $self = shift;
  my $d = $self->{debug};
  $self->{debug} = shift if @_;
  $d;
}
preparedescriptionprevnextTop
sub prepare {
  my $self  = shift;
  my $query = shift;

  # find a non-busy dbh
my $dbh = $self->dbh || $self->throw("Can't connect to database: " . DBI->errstr); if (my $sth = $self->{$dbh}{$query}) { warn "Using cached statement handler\n" if $self->debug; return $sth; } else { warn "Creating new statement handler\n" if $self->debug; $sth = $dbh->prepare($query) || $self->throw("Couldn't prepare query $query:\n ".DBI->errstr."\n"); return $self->{$dbh}{$query} = $sth; }
}
do_querydescriptionprevnextTop
sub do_query {
  my $self = shift;
  my ($query,@args) = @_;
  warn $self->dbi_quote($query,@args),"\n" if $self->debug;
  my $sth = $self->prepare($query);
  $sth->execute(@args) || $self->throw("Couldn't execute query $query:\n ".DBI->errstr."\n");
  $sth;
}
dbhdescriptionprevnextTop
sub dbh {
  my $self = shift;
  foreach (@{$self->{dbh}}) {
    return $_ if $_->inuse == 0;
  }
  # if we get here, we must create a new one
warn "(Re)connecting to database\n" if $self->debug; my $dbh = DBI->connect(@{$self->{args}}) or return; $dbh->{PrintError} = 0; my $wrapper = Bio::DB::GFF::Adaptor::dbi::faux_dbh->new($dbh); push @{$self->{dbh}},$wrapper; $wrapper;
}
disconnectdescriptionprevnextTop
sub disconnect {
  my $self = shift;
  $_ && $_->disconnect foreach @{$self->{dbh}};
  $self->{dbh} = [];
}
dbi_quotedescriptionprevnextTop
sub dbi_quote {
  my $self = shift;
  my ($query,@args) = @_;
  my $dbh = $self->dbh;
  $query =~ s/\?/$dbh->quote(shift @args)/eg;
  $query;
}
General documentation
USAGETop
The object constructor is
Bio::DB::GFF::Adaptor::dbi::caching_handle->new(). This is called
like DBI->connect() and takes the same arguments. The returned object
looks and acts like a conventional database handle.
In addition to all the standard DBI handle methods, this package adds
the following: