| 1 | package Bio::DB::GFF::Adaptor::dbi::caching_handle; | ||||||||||||||||||||
| 2 | 10 | $Bio::DB::GFF::Adaptor::dbi::caching_handle::VERSION = '1.7.4'; | |||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 10 | ▶ | use DBI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 10 | ▶ | use vars '$AUTOLOAD'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 10 | ▶ | use base qw(Bio::Root::Root); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | Bio::DB::GFF::Adaptor::dbi::caching_handle -- Cache for database handles | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | use Bio::DB::GFF::Adaptor::dbi::caching_handle; | ||||||||||||||||||||
| 15 | $db = Bio::DB::GFF::Adaptor::dbi::caching_handle->new('dbi:mysql:test'); | ||||||||||||||||||||
| 16 | $sth = $db->prepare('select * from foo'); | ||||||||||||||||||||
| 17 | @h = $sth->fetch_rowarray; | ||||||||||||||||||||
| 18 | $sth->finish | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | This module handles a pool of database handles. It was motivated by | ||||||||||||||||||||
| 23 | the MYSQL driver's {mysql_use_result} attribute, which dramatically | ||||||||||||||||||||
| 24 | improves query speed and memory usage, but forbids additional query | ||||||||||||||||||||
| 25 | statements from being evaluated while an existing one is in use. | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | This module is a plug-in replacement for vanilla DBI. It | ||||||||||||||||||||
| 28 | automatically activates the {mysql_use_result} attribute for the mysql | ||||||||||||||||||||
| 29 | driver, but avoids problems with multiple active statement handlers by | ||||||||||||||||||||
| 30 | creating new database handles as needed. | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | =head1 USAGE | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | The object constructor is | ||||||||||||||||||||
| 35 | Bio::DB::GFF::Adaptor::dbi::caching_handle-E<gt>new(). This is called | ||||||||||||||||||||
| 36 | like DBI-E<gt>connect() and takes the same arguments. The returned object | ||||||||||||||||||||
| 37 | looks and acts like a conventional database handle. | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | In addition to all the standard DBI handle methods, this package adds | ||||||||||||||||||||
| 40 | the following: | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head2 dbi_quote | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | Title : dbi_quote | ||||||||||||||||||||
| 45 | Usage : $string = $db->dbi_quote($sql,@args) | ||||||||||||||||||||
| 46 | Function: perform bind variable substitution | ||||||||||||||||||||
| 47 | Returns : query string | ||||||||||||||||||||
| 48 | Args : the query string and bind arguments | ||||||||||||||||||||
| 49 | Status : public | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | This method replaces the bind variable "?" in a SQL statement with | ||||||||||||||||||||
| 52 | appropriately quoted bind arguments. It is used internally to handle | ||||||||||||||||||||
| 53 | drivers that don't support argument binding. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head2 do_query | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | Title : do_query | ||||||||||||||||||||
| 58 | Usage : $sth = $db->do_query($query,@args) | ||||||||||||||||||||
| 59 | Function: perform a DBI query | ||||||||||||||||||||
| 60 | Returns : a statement handler | ||||||||||||||||||||
| 61 | Args : query string and list of bind arguments | ||||||||||||||||||||
| 62 | Status : Public | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | This method performs a DBI prepare() and execute(), returning a | ||||||||||||||||||||
| 65 | statement handle. You will typically call fetch() of fetchrow_array() | ||||||||||||||||||||
| 66 | on the statement handle. The parsed statement handle is cached for | ||||||||||||||||||||
| 67 | later use. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head2 debug | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | Title : debug | ||||||||||||||||||||
| 72 | Usage : $debug = $db->debug([$debug]) | ||||||||||||||||||||
| 73 | Function: activate debugging messages | ||||||||||||||||||||
| 74 | Returns : current state of flag | ||||||||||||||||||||
| 75 | Args : optional new setting of flag | ||||||||||||||||||||
| 76 | Status : public | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 81 | 0 | my $class = shift; | |||||||||||||||||||
| 82 | 0 | my @dbi_args = @_; | |||||||||||||||||||
| 83 | 0 | my $self = bless { | |||||||||||||||||||
| 84 | dbh => [], | ||||||||||||||||||||
| 85 | args => \@dbi_args, | ||||||||||||||||||||
| 86 | debug => 0, | ||||||||||||||||||||
| 87 | },$class; | ||||||||||||||||||||
| 88 | 0 | ▶ | $self->dbh || $self->throw("Can't connect to database: " . DBI->errstr); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 0 | $self; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 93 | 0 | my($pack,$func_name) = $AUTOLOAD=~/(.+)::([^:]+)$/; | |||||||||||||||||||
| 94 | 0 | ▶ | return if $func_name eq 'DESTROY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 0 | ▶ | my $self = shift or return DBI->$func_name(@_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 0 | $self->dbh->$func_name(@_); | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | ▶ | sub debug { | ||||||||||||||||||
|
Subroutine
debug: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 100 | 0 | my $self = shift; | |||||||||||||||||||
| 101 | 0 | my $d = $self->{debug}; | |||||||||||||||||||
| 102 | 0 | ▶ | $self->{debug} = shift if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | $d; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | ▶ | sub prepare { | ||||||||||||||||||
|
Subroutine
prepare: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||||||||||
| 108 | 0 | my $query = shift; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # find a non-busy dbh | ||||||||||||||||||||
| 111 | 0 | ▶ | my $dbh = $self->dbh || $self->throw("Can't connect to database: " . DBI->errstr); | ||||||||||||||||||
|
Condition0%0/3
$self->dbh || $self->throw(q[Can't connect to database: ] . 'DBI'->errstr)
MC/DC0%0/2
$self->dbh || $self->throw(q[Can't connect to database: ] . 'DBI'->errstr)
$self->dbh
$self->throw(q[Can't connect to database: ] . 'DBI'->errstr)
Truth table0%0/3
A || B
A= $self->dbhB= $self->throw(q[Can't connect to database: ] . 'DBI'->errstr)
| |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | warn "Using prepare_cache\n" if $self->debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 0 | ▶ | my $sth = $dbh->prepare_cached($query, {}, 3) || $self->throw("Couldn't prepare query $query:\n ".DBI->errstr."\n"); | ||||||||||||||||||
|
Condition0%0/3
$dbh->prepare_cached($query, {}, 3) || $self->throw("Couldn't prepare query ${query}:\n " . 'DBI'->errstr . "\n")
MC/DC0%0/2
$dbh->prepare_cached($query, {}, 3) || $self->throw("Couldn't prepare query ${query}:\n " . 'DBI'->errstr . "\n")
$dbh->prepare_cached($query, {}, 3)
$self->throw("Couldn't prepare query ${query}:\n " . 'DBI'->errstr . "\n")
Truth table0%0/3
A || B
A= $dbh->prepare_cached($query, {}, 3)B= $self->throw("Couldn't prepare query ${query}:\n " . 'DBI'->errstr . "\n")
| |||||||||||||||||||||
| 115 | 0 | return $sth; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | ▶ | sub do_query { | ||||||||||||||||||
|
Subroutine
do_query: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||||||||||
| 120 | 0 | my ($query,@args) = @_; | |||||||||||||||||||
| 121 | 0 | ▶ | warn $self->dbi_quote($query,@args),"\n" if $self->debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | 0 | my $sth = $self->prepare($query); | |||||||||||||||||||
| 123 | 0 | ▶ | $sth->execute(@args) || $self->throw("Couldn't execute query $query:\n ".DBI->errstr."\n"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 0 | $sth; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 0 | ▶ | sub dbh { | ||||||||||||||||||
|
Subroutine
dbh: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 128 | 0 | my $self = shift; | |||||||||||||||||||
| 129 | 0 | foreach (@{$self->{dbh}}) { | |||||||||||||||||||
| 130 | 0 | ▶ | return $_ if $_->inuse == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | # if we get here, we must create a new one | ||||||||||||||||||||
| 133 | 0 | ▶ | warn "(Re)connecting to database\n" if $self->debug; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | 0 | ▶ | my $dbh = DBI->connect(@{$self->{args}}) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | $dbh->{PrintError} = 0; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | # for Oracle - to retrieve LOBs, need to define the length (Jul 15, 2002) | ||||||||||||||||||||
| 139 | 0 | $dbh->{LongReadLen} = 100*65535; | |||||||||||||||||||
| 140 | 0 | $dbh->{LongTruncOk} = 0; | |||||||||||||||||||
| 141 | 0 | $dbh->{mysql_auto_reconnect} = 1; | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 0 | my $wrapper = Bio::DB::GFF::Adaptor::dbi::faux_dbh->new($dbh); | |||||||||||||||||||
| 144 | 0 | push @{$self->{dbh}},$wrapper; | |||||||||||||||||||
| 145 | 0 | $wrapper; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | # The clone method should only be called in child processes after a fork(). | ||||||||||||||||||||
| 149 | # It does two things: (1) it sets the "real" dbh's InactiveDestroy to 1, | ||||||||||||||||||||
| 150 | # thereby preventing the database connection from being destroyed in | ||||||||||||||||||||
| 151 | # the parent when the dbh's destructor is called; (2) it replaces the | ||||||||||||||||||||
| 152 | # "real" dbh with the result of dbh->clone(), so that we now have an | ||||||||||||||||||||
| 153 | # independent handle. | ||||||||||||||||||||
| 154 | 0 | ▶ | sub clone { | ||||||||||||||||||
|
Subroutine
clone: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 155 | 0 | my $self = shift; | |||||||||||||||||||
| 156 | 0 | foreach (@{$self->{dbh}}) { $_->clone }; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | =head2 attribute | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | Title : attribute | ||||||||||||||||||||
| 162 | Usage : $value = $db->attribute(AttributeName , [$newvalue]) | ||||||||||||||||||||
| 163 | Function: get/set DBI::db handle attribute | ||||||||||||||||||||
| 164 | Returns : current state of the attribute | ||||||||||||||||||||
| 165 | Args : name of the attribute and optional new setting of attribute | ||||||||||||||||||||
| 166 | Status : public | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | Under Bio::DB::GFF::Adaptor::dbi::caching_handle the DBI::db | ||||||||||||||||||||
| 169 | attributes that are usually set using hashref calls are unavailable. | ||||||||||||||||||||
| 170 | Use attribute() instead. For example, instead of: | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | $dbh->{AutoCommit} = 0; | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | use | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | $dbh->attribute(AutoCommit=>0); | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 0 | ▶ | sub attribute { | ||||||||||||||||||
|
Subroutine
attribute: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 181 | 0 | my $self = shift; | |||||||||||||||||||
| 182 | 0 | my $dbh = $self->dbh->{dbh}; | |||||||||||||||||||
| 183 | 0 | ▶ | return $dbh->{$_[0]} = $_[1] if @_ == 2; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | 0 | ▶ | return $dbh->{$_[0]} if @_ == 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | 0 | return; | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | ▶ | sub disconnect { | ||||||||||||||||||
|
Subroutine
disconnect: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 189 | 0 | my $self = shift; | |||||||||||||||||||
| 190 | 0 | ▶ | $_ && $_->disconnect foreach @{$self->{dbh}}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 191 | 0 | $self->{dbh} = []; | |||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 0 | ▶ | sub dbi_quote { | ||||||||||||||||||
|
Subroutine
dbi_quote: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 195 | 0 | my $self = shift; | |||||||||||||||||||
| 196 | 0 | my ($query,@args) = @_; | |||||||||||||||||||
| 197 | 0 | my $dbh = $self->dbh; | |||||||||||||||||||
| 198 | 0 | $query =~ s/\?/$dbh->quote(shift @args)/eg; | |||||||||||||||||||
| 199 | 0 | $query; | |||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | package Bio::DB::GFF::Adaptor::dbi::faux_dbh; | ||||||||||||||||||||
| 203 | 10 | $Bio::DB::GFF::Adaptor::dbi::faux_dbh::VERSION = '1.7.4'; | |||||||||||||||||||
| 204 | 10 | ▶ | use vars '$AUTOLOAD'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 207 | 0 | my $class = shift; | |||||||||||||||||||
| 208 | 0 | my $dbh = shift; | |||||||||||||||||||
| 209 | 0 | bless {dbh=>$dbh},$class; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 0 | ▶ | sub prepare { | ||||||||||||||||||
|
Subroutine
prepare: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 213 | 0 | my $self = shift; | |||||||||||||||||||
| 214 | 0 | ▶ | my $sth = $self->{dbh}->prepare(@_) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 215 | 0 | ▶ | $sth->{mysql_use_result} = 1 if $self->{dbh}->{Driver}{Name} eq 'mysql'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 216 | 0 | $sth; | |||||||||||||||||||
| 217 | } | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 0 | ▶ | sub prepare_delayed { | ||||||||||||||||||
|
Subroutine
prepare_delayed: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 220 | 0 | my $self = shift; | |||||||||||||||||||
| 221 | 0 | ▶ | my $sth = $self->{dbh}->prepare(@_) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 222 | 0 | $sth; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 0 | ▶ | sub inuse { | ||||||||||||||||||
|
Subroutine
inuse: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 226 | 0 | shift->{dbh}->{ActiveKids}; | |||||||||||||||||||
| 227 | } | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | # The clone method should only be called in child processes after a fork(). | ||||||||||||||||||||
| 230 | # It does two things: (1) it sets the "real" dbh's InactiveDestroy to 1, | ||||||||||||||||||||
| 231 | # thereby preventing the database connection from being destroyed in | ||||||||||||||||||||
| 232 | # the parent when the dbh's destructor is called; (2) it replaces the | ||||||||||||||||||||
| 233 | # "real" dbh with the result of dbh->clone(), so that we now have an | ||||||||||||||||||||
| 234 | # independent handle. | ||||||||||||||||||||
| 235 | 0 | ▶ | sub clone { | ||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 236 | 0 | my $self = shift; | |||||||||||||||||||
| 237 | 0 | $self->{dbh}{InactiveDestroy} = 1; | |||||||||||||||||||
| 238 | 0 | $self->{dbh} = $self->{dbh}->clone; | |||||||||||||||||||
| 239 | } | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | 0 | ▶ | sub DESTROY { } | ||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 244 | 0 | my($pack,$func_name) = $AUTOLOAD=~/(.+)::([^:]+)$/; | |||||||||||||||||||
| 245 | 0 | ▶ | return if $func_name eq 'DESTROY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 246 | 0 | my $self = shift; | |||||||||||||||||||
| 247 | 0 | ▶ | if( defined $self->{dbh} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 248 | 0 | $self->{dbh}->$func_name(@_); | |||||||||||||||||||
| 249 | } | ||||||||||||||||||||
| 250 | } | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | 10 | 1; | |||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | __END__ | ||||||||||||||||||||