Roles/Shared.pm

filter: Statementstmt 92.4% 49 / 53 Branchbran 63.6% 14 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 2 totaltotal 82.7% 67 / 81 CC 13 SCAR 20.6
CC
13
Cov
84%
connect
10.7
disconnect
4.0
BEGIN
1.0
CRAP 7.9
« Roles/Buffered.pm ↑ Summary Roles.pm »
1 # $Id: Shared.pm,v 1.4 2005/12/19 15:02:00 dk Exp $
2  
3 package DBIx::Roles::Shared;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw(%instances %dsns $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 $VERSION = '1.00';
9  
10 sub connect
11 9 ▶{
Subroutine connect: called CC=10 SCAR=23.7
Pod undocumented
12 9 my ( $self, undef, $dsn, $user, $pass, $attr) = @_;
13  
14 9 my $inst_key = "$self";
15 9 my $dsn_key = join( ' | ', $dsn, $user, $pass);
16 9 my $dbh;
17  
18 9 ▶ if ( exists $instances{$inst_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DBIx::Roles::Shared::instances{$inst_key}) { } 6 3 9
19 # apparently, connect() without disconnect --
20 # 1. find if the object was connected to another handle, and clean it up
21 6 keys %dsns; # reset each()
22 6 ▶ while ( my ( $k, $v) = each %dsns) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %DBIx::Roles::Shared::dsns) 6 0 6
23 6 my @d = grep { $_ != $self } @$v;
24 6 ▶ next if @d == @$v;
Branch: 1 branch
BranchTrueFalseTotal
if @d == @$v 0 6 6
25 # found a connection
26 6 ▶ last if $k eq $dsn_key; # yes, it is trying to reconnect
Branch: 1 branch
BranchTrueFalseTotal
if $k eq $dsn_key 6 0 6
27 # cleanup
28 0 ▶ @d ? ( @$v = @d ) : delete $dsns{$k};
Branch: 1 branch
BranchTrueFalseTotal
@d ? : 0 0 0
29 0 delete $instances{"$self"};
30 0 goto DEFAULT_CONNECT;
31 }
32 # 2. reconnect and apply the new handle to all the shared objects
33 6 eval { $dbh = $self-> super( $dsn, $user, $pass, $attr); };
34 6 my $exception = $@;
35
36 6 for my $obj (@{$dsns{$dsn_key}}) {
37 9 $obj-> dbh( $dbh);
38 }
39 6 $instances{$inst_key} = $dbh;
40  
41 6 ▶ die $exception if $exception;
Branch: 1 branch
BranchTrueFalseTotal
if $exception 4 2 6
42 } else {
43 DEFAULT_CONNECT:
44 3 ▶ if ( exists $dsns{$dsn_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DBIx::Roles::Shared::dsns{$dsn_key}) { } 1 2 3
45 # reuse an existing connection
46 1 $dbh = $dsns{$dsn_key}-> [0]-> dbh;
47 } else {
48 # new connection
49 2 $dbh = $self-> super( $dsn, $user, $pass, $attr);
50 2 ▶ return undef unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 2 2
51 2 $instances{$inst_key} = $dbh;
52 }
53 3 push @{$dsns{$dsn_key}}, $self;
54 }
55  
56 5 return $dbh;
57 }
58  
59 sub disconnect
60 3 ▶{
Subroutine disconnect: called CC=4 SCAR=14.0
Pod undocumented
61 3 my $self = $_[0];
62  
63 3 keys %dsns; # reset each()
64 3 ▶ while ( my ( $k, $v) = each %dsns) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %DBIx::Roles::Shared::dsns) 3 0 3
65 3 my @d = grep { $_ != $self } @$v;
66 3 ▶ if ( @d == @$v) {
Branch: 2 branches
BranchTrueFalseTotal
if (@d == @$v) { } 0 3 3
elsif (@d) { } 1 2 3
67 # not found
68 0 next;
69 } elsif ( @d) {
70 # remove the shared connection from the list but do not disconnect
71 1 @$v = @d;
72 1 delete $instances{"$self"};
73 1 $self-> dbh( undef); # disconnect can be called twice
74 1 return;
75 } else {
76 # that was the last reference, disconnect
77 2 delete $dsns{$k};
78 2 last;
79 }
80 }
81  
82 # also disconnect if wasn't found somehow
83 2 delete $instances{"$self"};
84 2 $self-> super();
85 }
86  
87 1 1;
88  
89 __DATA__
« Roles/Buffered.pm ↑ Summary Roles.pm »