| 1 | package DBD::Multi; | ||||||||||||||||||||||||||||
| 2 | 12 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 3 | package DBD::Multi::dr; | ||||||||||||||||||||||||||||
| 4 | 12 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 5 | package DBD::Multi::db; | ||||||||||||||||||||||||||||
| 6 | 12 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 7 | package DBD::Multi::st; | ||||||||||||||||||||||||||||
| 8 | 12 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 9 | package DBD::Multi::Handler; | ||||||||||||||||||||||||||||
| 10 | 12 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | package DBD::Multi; | ||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 12 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 12 | DBI->setup_driver('DBD::Multi'); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 12 | ▶ | use vars qw[$err $errstr $sqlstate $drh]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 12 | $err = 0; # DBI::err | |||||||||||||||||||||||||||
| 21 | 12 | $errstr = ""; # DBI::errstr | |||||||||||||||||||||||||||
| 22 | 12 | $sqlstate = ""; # DBI::state | |||||||||||||||||||||||||||
| 23 | 12 | $drh = undef; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub driver { | ||||||||||||||||||||||||||||
| 26 | 10 | ▶ | return $drh if $drh; | ||||||||||||||||||||||||||
|
Subroutine
driver: called CC=2 SCAR=7.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 10 | my($class, $attr) = @_; | |||||||||||||||||||||||||||
| 28 | 10 | $class .= '::dr'; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 10 | $drh = DBI::_new_drh($class, { | |||||||||||||||||||||||||||
| 31 | Name => 'Multi', | ||||||||||||||||||||||||||||
| 32 | Version => $VERSION, | ||||||||||||||||||||||||||||
| 33 | Err => \$DBD::Multi::err, | ||||||||||||||||||||||||||||
| 34 | Errstr => \$DBD::Multi::errstr, | ||||||||||||||||||||||||||||
| 35 | State => \$DBD::Multi::sqlstate, | ||||||||||||||||||||||||||||
| 36 | Attribution => 'DBD::Multi, pair Networks Inc.', | ||||||||||||||||||||||||||||
| 37 | }); | ||||||||||||||||||||||||||||
| 38 | 10 | DBD::Multi::db->install_method('multi_do_all'); | |||||||||||||||||||||||||||
| 39 | 10 | return $drh; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | ####################################################################### | ||||||||||||||||||||||||||||
| 43 | package DBD::Multi::dr; | ||||||||||||||||||||||||||||
| 44 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 12 | $DBD::Multi::dr::imp_data_size = 0; | |||||||||||||||||||||||||||
| 47 | 12 | ▶ | use DBD::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub DESTROY { shift->STORE(Active => 0) } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub connect { | ||||||||||||||||||||||||||||
| 52 | 113 | ▶ | my($drh, $dbname, $user, $auth, $attr) = @_; | ||||||||||||||||||||||||||
|
Subroutine
connect: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 53 | 113 | my $dbh = DBI::_new_dbh( | |||||||||||||||||||||||||||
| 54 | $drh => { | ||||||||||||||||||||||||||||
| 55 | Name => $dbname, | ||||||||||||||||||||||||||||
| 56 | USER => $user, | ||||||||||||||||||||||||||||
| 57 | CURRENT_USER => $user, | ||||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | my @dsns = $attr->{dsns} && ref($attr->{dsns}) eq 'ARRAY' | ||||||||||||||||||||||||||||
| 61 | 113 | ▶ | ? @{$attr->{dsns}} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$attr->{"dsns"} and ref $attr->{"dsns"} eq "ARRAY"
MC/DC50%1/2
$attr->{"dsns"} and ref $attr->{"dsns"} eq "ARRAY"
$attr->{"dsns"}
ref $attr->{"dsns"} eq "ARRAY"
Truth table66%2/3
A and B
A= $attr->{"dsns"}B= ref $attr->{"dsns"} eq "ARRAY"
| |||||||||||||||||||||||||||||
| 62 | : (); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 113 | ▶ | if ( $dbname =~ /dsn=(.*)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | push @dsns, ( -1, [$1, $user, $auth] ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 113 | my $handler = DBD::Multi::Handler->new({ | |||||||||||||||||||||||||||
| 69 | dsources => [ @dsns ], | ||||||||||||||||||||||||||||
| 70 | }); | ||||||||||||||||||||||||||||
| 71 | $handler->failed_max($attr->{failed_max}) | ||||||||||||||||||||||||||||
| 72 | 113 | ▶ | if exists $attr->{failed_max}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | $handler->failed_expire($attr->{failed_expire}) | ||||||||||||||||||||||||||||
| 74 | 113 | ▶ | if exists $attr->{failed_expire}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 113 | $dbh->STORE(_handler => $handler); | |||||||||||||||||||||||||||
| 77 | 113 | $dbh->STORE(handler => $handler); # temporary | |||||||||||||||||||||||||||
| 78 | 113 | $drh->{_handler} = $handler; | |||||||||||||||||||||||||||
| 79 | 113 | $dbh->STORE(Active => 1); | |||||||||||||||||||||||||||
| 80 | 113 | return $dbh; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 6 | ▶ | sub data_sources { shift->FETCH('_handler')->all_sources } | ||||||||||||||||||||||||||
|
Subroutine
data_sources: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | ####################################################################### | ||||||||||||||||||||||||||||
| 86 | package DBD::Multi::db; | ||||||||||||||||||||||||||||
| 87 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 12 | $DBD::Multi::db::imp_data_size = 0; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub prepare { | ||||||||||||||||||||||||||||
| 92 | 415 | ▶ | my ($dbh, $statement, @attribs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # create a 'blank' sth | ||||||||||||||||||||||||||||
| 95 | 415 | my ($outer, $sth) = DBI::_new_sth($dbh, { Statement => $statement }); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 415 | my $handler = $dbh->FETCH('_handler'); | |||||||||||||||||||||||||||
| 98 | 415 | $sth->STORE(_handler => $handler); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 415 | my $_dbh = $handler->dbh; | |||||||||||||||||||||||||||
| 101 | 414 | my $_sth; | |||||||||||||||||||||||||||
| 102 | 414 | ▶ | until ( $_sth ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 436 | $_sth = $_dbh->prepare($statement, @attribs); | |||||||||||||||||||||||||||
| 104 | 436 | ▶ | unless ( $_sth ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 24 | $handler->dbh_failed; | |||||||||||||||||||||||||||
| 106 | 24 | $_dbh = $handler->dbh; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 412 | $sth->STORE(NUM_OF_PARAMS => $_sth->FETCH('NUM_OF_PARAMS')); | |||||||||||||||||||||||||||
| 111 | 412 | $sth->STORE(_dbh => $_dbh); | |||||||||||||||||||||||||||
| 112 | 412 | $sth->STORE(_sth => $_sth); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 412 | return $outer; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub multi_do_all { | ||||||||||||||||||||||||||||
| 118 | 9 | ▶ | my $dbh = shift; | ||||||||||||||||||||||||||
|
Subroutine
multi_do_all: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 119 | 9 | my $handler = $dbh->FETCH('_handler'); | |||||||||||||||||||||||||||
| 120 | 9 | return $handler->multi_do_all( @_ ); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub disconnect { | ||||||||||||||||||||||||||||
| 124 | 113 | ▶ | my ($dbh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | 113 | $dbh->STORE(Active => 0); | |||||||||||||||||||||||||||
| 126 | 113 | 1; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub commit { | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my ($dbh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
commit: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ( $dbh->FETCH('Active') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | return $dbh->FETCH('_dbh')->commit if $dbh->FETCH('_dbh'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 0 | return; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | sub rollback { | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | my ($dbh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rollback: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ( $dbh->FETCH('Active') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | return $dbh->FETCH('_dbh')->rollback if $dbh->FETCH('_dbh'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 0 | return; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub get_info { | ||||||||||||||||||||||||||||
| 146 | 5 | ▶ | my($dbh, $info_type) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_info: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # return info from current connection | ||||||||||||||||||||||||||||
| 149 | 5 | my $handler = $dbh->FETCH('_handler'); | |||||||||||||||||||||||||||
| 150 | 5 | my $_dbh = $handler->dbh; | |||||||||||||||||||||||||||
| 151 | 5 | return $_dbh->get_info($info_type); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub STORE { | ||||||||||||||||||||||||||||
| 155 | 1017 | ▶ | my ($self, $attr, $val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | 1017 | $self->{$attr} = $val; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 110 | ▶ | sub DESTROY { shift->disconnect } | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | ####################################################################### | ||||||||||||||||||||||||||||
| 162 | package DBD::Multi::st; | ||||||||||||||||||||||||||||
| 163 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 12 | $DBD::Multi::st::imp_data_size = 0; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 12 | ▶ | use vars qw[@METHODS @FIELDS]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | 12 | @METHODS = qw[ | |||||||||||||||||||||||||||
| 169 | bind_param | ||||||||||||||||||||||||||||
| 170 | bind_param_inout | ||||||||||||||||||||||||||||
| 171 | bind_param_array | ||||||||||||||||||||||||||||
| 172 | execute_array | ||||||||||||||||||||||||||||
| 173 | execute_for_fetch | ||||||||||||||||||||||||||||
| 174 | fetch | ||||||||||||||||||||||||||||
| 175 | fetchrow_arrayref | ||||||||||||||||||||||||||||
| 176 | fetchrow_array | ||||||||||||||||||||||||||||
| 177 | fetchrow_hashref | ||||||||||||||||||||||||||||
| 178 | fetchall_arrayref | ||||||||||||||||||||||||||||
| 179 | fetchall_hashref | ||||||||||||||||||||||||||||
| 180 | bind_col | ||||||||||||||||||||||||||||
| 181 | bind_columns | ||||||||||||||||||||||||||||
| 182 | dump_results | ||||||||||||||||||||||||||||
| 183 | ]; | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 12 | @FIELDS = qw[ | |||||||||||||||||||||||||||
| 186 | NUM_OF_FIELDS | ||||||||||||||||||||||||||||
| 187 | CursorName | ||||||||||||||||||||||||||||
| 188 | ParamValues | ||||||||||||||||||||||||||||
| 189 | RowsInCache | ||||||||||||||||||||||||||||
| 190 | ]; | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | sub execute { | ||||||||||||||||||||||||||||
| 193 | 410 | ▶ | my $sth = shift; | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 194 | 410 | my $_sth = $sth->FETCH('_sth'); | |||||||||||||||||||||||||||
| 195 | my $params = @_ | ||||||||||||||||||||||||||||
| 196 | ? $sth->{f_params} = [ @_ ] | ||||||||||||||||||||||||||||
| 197 | 410 | ▶ | : $sth->{f_params}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 410 | ▶ | $sth->finish if $sth->FETCH('Active'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 410 | $sth->{Active} = 1; | |||||||||||||||||||||||||||
| 201 | 410 | my $rc = $_sth->execute(@{$params}); | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 410 | for my $field ( @FIELDS ) { | |||||||||||||||||||||||||||
| 204 | 1640 | my $value = $_sth->FETCH($field); | |||||||||||||||||||||||||||
| 205 | 1640 | ▶ | $sth->STORE($field => $value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $value or defined $sth->FETCH($field)
MC/DC100%2/2
not defined $value or defined $sth->FETCH($field)
not defined $value
defined $sth->FETCH($field)
Truth table100%3/3
not(A) or B
A= not defined $valueB= defined $sth->FETCH($field)
| |||||||||||||||||||||||||||||
| 206 | unless ! defined $value | ||||||||||||||||||||||||||||
| 207 | || defined $sth->FETCH($field); | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 410 | return $rc; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | sub FETCH { | ||||||||||||||||||||||||||||
| 214 | 2451 | ▶ | my ($sth, $attrib) = @_; | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 215 | 2451 | ▶ | $sth->{'_sth'}->FETCH($attrib) || $sth->{$attrib}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | sub STORE { | ||||||||||||||||||||||||||||
| 219 | 2056 | ▶ | my ($self, $attr, $val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 220 | 2056 | $self->{$attr} = $val; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 3 | ▶ | sub rows { shift->FETCH('_sth')->rows } | ||||||||||||||||||||||||||
|
Subroutine
rows: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | sub finish { | ||||||||||||||||||||||||||||
| 226 | 402 | ▶ | my ($sth) = @_; | ||||||||||||||||||||||||||
|
Subroutine
finish: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 227 | 402 | $sth->STORE(Active => 0); | |||||||||||||||||||||||||||
| 228 | 402 | return $sth->FETCH('_sth')->finish; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 12 | foreach my $method ( @METHODS ) { | |||||||||||||||||||||||||||
| 232 | 12 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 233 | 407 | ▶ | *{$method} = sub { shift->FETCH('_sth')->$method(@_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | ####################################################################### | ||||||||||||||||||||||||||||
| 237 | package DBD::Multi::Handler; | ||||||||||||||||||||||||||||
| 238 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 12 | ▶ | use base qw[Class::Accessor::Fast]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 241 | 12 | ▶ | use Sys::SigAction qw(timeout_call); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 242 | 12 | ▶ | use List::Util qw(shuffle); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =begin ImplementationNotes | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | dsources - This thing changes from an arrayref to a hashref during construction. :( | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | Initially, when data is passed in during construction, it's an arrayref | ||||||||||||||||||||||||||||
| 249 | containing the 'dsns' param from the user's connect() call. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Later, when _configure_dsources gets called, it turns into a multi-dimension | ||||||||||||||||||||||||||||
| 252 | hashref: | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | $dsources->{$pri}->{$dsource_id} = 1; | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | The first key is the priority number, the second key is the data source index | ||||||||||||||||||||||||||||
| 257 | number. The value is always just a true value. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | nextid - A counter. Stores the index number of the next data source to be added. | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | all_dsources - A hashref. Maps index number to the connect data. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | current_dsource - The most recent chosen datasource index number. | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | used - A hashref. Keys are index numbers. Values are true when the datasource | ||||||||||||||||||||||||||||
| 266 | has been previously assigned and we want to prefer other datasources of the | ||||||||||||||||||||||||||||
| 267 | same priority (for round-robin load distribution). | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | failed - A hashref. Keys are index numbers. Values are counters indicating | ||||||||||||||||||||||||||||
| 270 | how many times the data source has failed. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | failed_last - A hashref. Keys are index number. Values are unix timestamp | ||||||||||||||||||||||||||||
| 273 | indicating the most recent time a data source failed. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | failed_max - A scalar value. Number of times a datasource may fail before we | ||||||||||||||||||||||||||||
| 276 | stop trying it. | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | failed_expire - A scalar value. Number of seconds since we stopped trying a | ||||||||||||||||||||||||||||
| 279 | datasource before we'll try it again. | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | timeout - A scalar value. Number of seconds we try to connect to a datasource | ||||||||||||||||||||||||||||
| 282 | before giving up. | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =end ImplementationNotes | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =cut | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 12 | __PACKAGE__->mk_accessors(qw[ | |||||||||||||||||||||||||||
| 289 | dsources | ||||||||||||||||||||||||||||
| 290 | nextid | ||||||||||||||||||||||||||||
| 291 | all_dsources | ||||||||||||||||||||||||||||
| 292 | current_dsource | ||||||||||||||||||||||||||||
| 293 | used | ||||||||||||||||||||||||||||
| 294 | failed | ||||||||||||||||||||||||||||
| 295 | failed_last | ||||||||||||||||||||||||||||
| 296 | failed_max | ||||||||||||||||||||||||||||
| 297 | failed_expire | ||||||||||||||||||||||||||||
| 298 | timeout | ||||||||||||||||||||||||||||
| 299 | ]); | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | sub new { | ||||||||||||||||||||||||||||
| 302 | 113 | ▶ | my ($class, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.5
| |||||||||||||||||||||||||||||
| 303 | 113 | my $self = $class->SUPER::new($args); | |||||||||||||||||||||||||||
| 304 | 113 | ▶ | $self->nextid(0) unless defined $self->nextid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 113 | $self->all_dsources({}); | |||||||||||||||||||||||||||
| 306 | 113 | $self->used({}); | |||||||||||||||||||||||||||
| 307 | 113 | $self->failed({}); | |||||||||||||||||||||||||||
| 308 | 113 | $self->failed_last({}); | |||||||||||||||||||||||||||
| 309 | 113 | ▶ | $self->failed_max(3) unless defined $self->failed_max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 113 | ▶ | $self->failed_expire(60*5) unless defined $self->failed_expire; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | 113 | ▶ | $self->timeout( 5 ) unless defined $self->timeout; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 113 | $self->_configure_dsources; | |||||||||||||||||||||||||||
| 313 | 113 | return $self; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | sub all_sources { | ||||||||||||||||||||||||||||
| 317 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
all_sources: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 318 | 6 | return values %{$self->all_dsources}; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | sub add_to_pri { | ||||||||||||||||||||||||||||
| 322 | 234 | ▶ | my ($self, $pri, $dsource) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_to_pri: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 323 | 234 | my $dsource_id = $self->nextid; | |||||||||||||||||||||||||||
| 324 | 234 | my $dsources = $self->dsources; | |||||||||||||||||||||||||||
| 325 | 234 | my $all = $self->all_dsources; | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 234 | $all->{$dsource_id} = $dsource; | |||||||||||||||||||||||||||
| 328 | 234 | $dsources->{$pri}->{$dsource_id} = 1; | |||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 234 | $self->nextid($dsource_id + 1); | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | sub dbh { | ||||||||||||||||||||||||||||
| 334 | 450 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
dbh: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 335 | 450 | my $dbh = $self->_connect_dsource; | |||||||||||||||||||||||||||
| 336 | 447 | ▶ | return $dbh if $dbh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | 6 | $self->dbh_failed; | |||||||||||||||||||||||||||
| 338 | 6 | $self->dbh; | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | sub dbh_failed { | ||||||||||||||||||||||||||||
| 342 | 30 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
dbh_failed: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 30 | my $current_dsource = $self->current_dsource; | |||||||||||||||||||||||||||
| 345 | 30 | $self->failed->{$current_dsource}++; | |||||||||||||||||||||||||||
| 346 | 30 | $self->failed_last->{$current_dsource} = time; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | sub _purge_old_failures { | ||||||||||||||||||||||||||||
| 350 | 450 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_purge_old_failures: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 351 | 450 | my $now = time; | |||||||||||||||||||||||||||
| 352 | 450 | my @all = keys %{$self->all_dsources}; | |||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 450 | foreach my $dsource ( @all ) { | |||||||||||||||||||||||||||
| 355 | 966 | ▶ | next unless $self->failed->{$dsource}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | 175 | ▶ | if ( ($now - $self->failed_last->{$dsource}) > $self->failed_expire ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | 1 | delete $self->failed->{$dsource}; | |||||||||||||||||||||||||||
| 358 | 1 | delete $self->failed_last->{$dsource}; | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | sub _pick_dsource { | ||||||||||||||||||||||||||||
| 364 | 450 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_pick_dsource: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 365 | 450 | $self->_purge_old_failures; | |||||||||||||||||||||||||||
| 366 | 450 | my $dsources = $self->dsources; | |||||||||||||||||||||||||||
| 367 | 450 | my @pri = sort { $a <=> $b } keys %{$dsources}; | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 450 | foreach my $pri ( @pri ) { | |||||||||||||||||||||||||||
| 370 | 566 | my $dsource = $self->_pick_pri_dsource($dsources->{$pri}); | |||||||||||||||||||||||||||
| 371 | 566 | ▶ | if ( defined $dsource ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 448 | $self->current_dsource($dsource); | |||||||||||||||||||||||||||
| 373 | 448 | return; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 2 | $self->used({}); | |||||||||||||||||||||||||||
| 378 | return $self->_pick_dsource | ||||||||||||||||||||||||||||
| 379 | 2 | ▶ | if (grep {$self->failed->{$_} >= $self->failed_max} keys(%{$self->failed})) < keys(%{$self->all_dsources}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | 2 | die("All data sources failed!"); | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | ### _pick_pri_dsource | ||||||||||||||||||||||||||||
| 384 | # Given a list of data sources, all of the same priority, choose one to use. | ||||||||||||||||||||||||||||
| 385 | # Passed in a structure like this: | ||||||||||||||||||||||||||||
| 386 | # _pick_pri_dsource( $self, { 3 => 1, 4 => 1, 5 => 1 } ) | ||||||||||||||||||||||||||||
| 387 | # Where the keys 3,4,5 are dsource id's, and the values are always true. | ||||||||||||||||||||||||||||
| 388 | sub _pick_pri_dsource { | ||||||||||||||||||||||||||||
| 389 | 566 | ▶ | my ($self, $dsources) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_pick_pri_dsource: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 390 | 566 | my @dsources = sort { $a <=> $b } keys %{$dsources}; # Indexes of data sources to try. | |||||||||||||||||||||||||||
| 391 | 566 | my @used = grep { exists $self->used->{$_} } @dsources; # List of data sources already used. | |||||||||||||||||||||||||||
| 392 | 566 | ▶ | my @failed = grep { exists($self->failed->{$_}) && $self->failed->{$_} >= $self->failed_max } @dsources; # List of data sources that won't be tried. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | # We've used them all and they all failed. Escallate. | ||||||||||||||||||||||||||||
| 395 | 566 | ▶ | return if @used == @dsources && @failed == @dsources; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@used == @dsources and @failed == @dsources
MC/DC100%2/2
@used == @dsources and @failed == @dsources
@used == @dsources
@failed == @dsources
Truth table100%3/3
A and B
A= @used == @dsourcesB= @failed == @dsources
| |||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | # We've used them all but some are good. Purge and reuse. | ||||||||||||||||||||||||||||
| 398 | 448 | ▶ | delete @{$self->used}{@dsources} if @used == @dsources; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 448 | foreach my $dsource ( shuffle @dsources ) { | |||||||||||||||||||||||||||
| 401 | next if $self->failed->{$dsource} | ||||||||||||||||||||||||||||
| 402 | 489 | ▶ | && $self->failed->{$dsource} >= $self->failed_max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->failed->{$dsource} and $self->failed->{$dsource} >= $self->failed_max
MC/DC100%2/2
$self->failed->{$dsource} and $self->failed->{$dsource} >= $self->failed_max
$self->failed->{$dsource}
$self->failed->{$dsource} >= $self->failed_max
Truth table100%3/3
A and B
A= $self->failed->{$dsource}B= $self->failed->{$dsource} >= $self->failed_max
| |||||||||||||||||||||||||||||
| 403 | 486 | ▶ | next if $self->used->{$dsource}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 448 | $self->used->{$dsource} = 1; | |||||||||||||||||||||||||||
| 406 | 448 | return $dsource; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | 0 | return; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | sub _configure_dsources { | ||||||||||||||||||||||||||||
| 412 | 113 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_configure_dsources: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 413 | 113 | my $dsources = $self->dsources; | |||||||||||||||||||||||||||
| 414 | 113 | $self->dsources({}); | |||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 113 | ▶ | while ( my $pri = shift @{$dsources} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | 234 | ▶ | my $dsource = shift @{$dsources} or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 418 | 234 | $self->add_to_pri($pri => $dsource); | |||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | sub _connect_dsource { | ||||||||||||||||||||||||||||
| 423 | 534 | ▶ | my ($self, $dsource) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_connect_dsource: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 424 | 534 | ▶ | unless ( $dsource ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | 450 | $self->_pick_dsource; | |||||||||||||||||||||||||||
| 426 | 448 | $dsource = $self->all_dsources->{$self->current_dsource}; | |||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | # Support ready-made handles | ||||||||||||||||||||||||||||
| 430 | 532 | ▶ | return $dsource if UNIVERSAL::isa($dsource, 'DBI::db'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | # Support code-refs which return handles | ||||||||||||||||||||||||||||
| 433 | 478 | ▶ | if (ref $dsource eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 213 | my $handle = $dsource->(); | |||||||||||||||||||||||||||
| 435 | 213 | ▶ | return $handle if UNIVERSAL::isa($handle, 'DBI::db'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | 6 | return undef; # Connect by coderef failed. | |||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 265 | my $dbh; | |||||||||||||||||||||||||||
| 440 | 265 | local $ENV{DBI_AUTOPROXY}; | |||||||||||||||||||||||||||
| 441 | 265 | ▶ | if (timeout_call( $self->timeout, sub { $dbh = DBI->connect_cached(@{$dsource}) } )) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 442 | #warn "Timeout[", $self->current_dsource, "] at ", time, "\n"; | ||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | 264 | return $dbh; | |||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | sub connect_dsource { | ||||||||||||||||||||||||||||
| 448 | 84 | ▶ | my ($self, $dsource) = @_; | ||||||||||||||||||||||||||
|
Subroutine
connect_dsource: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 449 | 84 | $self->_connect_dsource($dsource); | |||||||||||||||||||||||||||
| 450 | } | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | sub multi_do_all { | ||||||||||||||||||||||||||||
| 453 | 21 | ▶ | my ($self, $code) = @_; | ||||||||||||||||||||||||||
|
Subroutine
multi_do_all: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 21 | my @all = values %{$self->all_dsources}; | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 21 | foreach my $source ( @all ) { | |||||||||||||||||||||||||||
| 458 | 84 | my $dbh = $self->connect_dsource($source); | |||||||||||||||||||||||||||
| 459 | 84 | ▶ | next unless $dbh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 460 | 84 | ▶ | if ( $dbh->{handler} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 461 | 12 | $dbh->{handler}->multi_do_all($code, $source); | |||||||||||||||||||||||||||
| 462 | 12 | next; | |||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | 72 | $code->($dbh); | |||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 12 | 1; | |||||||||||||||||||||||||||
| 469 | __END__ | ||||||||||||||||||||||||||||