| 1 | package Class::DBI::Plugin::MultiDatabases; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use base qw(Class::Data::Inheritable); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | $VERSION = 0.1; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | ############################################################################## | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 13 | 4 | my $me = shift; | |||||||||||||||||||||||||||
| 14 | 4 | my $class = (caller)[0]; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | unless( UNIVERSAL::isa($class, 'Class::DBI') ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 0 | croak __PACKAGE__, " can be used only by Class::DBI and its subclass."; | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | for my $sym (qw/change_db change_db set_connections db_Main | |||||||||||||||||||||||||||
| 22 | save_db_Main clear_db_Main is_imported_class effected_classes/){ | ||||||||||||||||||||||||||||
| 23 | 32 | *{"$class\::$sym"} = \&{ $sym }; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | $me->is_imported_class($class); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 4 | $class->mk_classdata('current_dbh'); | |||||||||||||||||||||||||||
| 29 | 4 | $class->mk_classdata('_dbnames'); | |||||||||||||||||||||||||||
| 30 | 4 | $class->mk_classdata('_clear_object'); | |||||||||||||||||||||||||||
| 31 | 4 | $class->mk_classdata('_DSNs'); | |||||||||||||||||||||||||||
| 32 | 4 | $class->mk_classdata('_targetClasses'); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | $class->_dbnames( {} ); | |||||||||||||||||||||||||||
| 35 | 4 | $class->_DSNs( {} ); # Data Source Name | |||||||||||||||||||||||||||
| 36 | 4 | $class->_targetClasses( {} ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 4 | ▶ | if( $class->can('clear_object_index') ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 4 | $class->_clear_object(1); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | ############################################################################## | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | { # sub is_imported_class | ||||||||||||||||||||||||||||
| 47 | 4 | my $class; | |||||||||||||||||||||||||||
| 48 | 4 | ▶ | sub is_imported_class { | ||||||||||||||||||||||||||
|
Subroutine
is_imported_class: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if($class){ return $class eq $self; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | $class = shift; # only once | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | ############################################################################## | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # See Also http://wiki.class-dbi.com/index.cgi?UsingMultipleDatabases | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | ▶ | sub set_connections { | ||||||||||||||||||||||||||
|
Subroutine
set_connections: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 63 | 4 | my %keys = @_; | |||||||||||||||||||||||||||
| 64 | 4 | $class->_DSNs(\%keys); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub change_db { | ||||||||||||||||||||||||||
|
Subroutine
change_db: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | my ($class, $dsn_key) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | unless( $class->is_imported_class ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | croak "change_db() must be called by the imported class."; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if($class->_dbnames->{$dsn_key}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # $class may not be contained in _targetClasses | ||||||||||||||||||||||||||||
| 78 | 0 | $class->current_dbh($dsn_key); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | for my $subclass ( $class->effected_classes ){ | |||||||||||||||||||||||||||
| 81 | 0 | $subclass->current_dbh($dsn_key); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else{ | ||||||||||||||||||||||||||||
| 86 | 0 | $class->_dbnames->{$dsn_key} = 1; | |||||||||||||||||||||||||||
| 87 | 0 | $class->current_dbh($dsn_key); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | unless( $class->_DSNs->{$dsn_key} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | return undef; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | my @args = @{ $class->_DSNs->{$dsn_key} }; | |||||||||||||||||||||||||||
| 94 | 0 | Ima::DBI->set_db($dsn_key, @args); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | for my $subclass ( $class->effected_classes ){ | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | $subclass->clear_object_index() if($class->_clear_object()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | return $dsn_key; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub db_Main { | ||||||||||||||||||||||||||
|
Subroutine
db_Main: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | 0 | my $proto = shift; | |||||||||||||||||||||||||||
| 107 | 0 | my $method = 'db_' . $proto->current_dbh(); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if(ref($proto) and $proto->{__db_Main}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $proto and $proto->{"__db_Main"}
MC/DC0%0/2
ref $proto and $proto->{"__db_Main"}
ref $proto
$proto->{"__db_Main"}
Truth table0%0/3
A and B
A= ref $protoB= $proto->{"__db_Main"}
| |||||||||||||||||||||||||||||
| 110 | 0 | return $proto->{__db_Main}; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if(!ref($proto)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $proto->_targetClasses->{$proto} = 1; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | $proto->$method; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub save_db_Main { | ||||||||||||||||||||||||||
|
Subroutine
save_db_Main: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 0 | my $proto = shift; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | unless(ref($proto)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | die "save_db_Main() can't be used as a clsas method."; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my $method = "db_" . $proto->current_dbh(); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | $proto->{__db_Main} = $proto->$method; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub clear_db_Main { | ||||||||||||||||||||||||||
|
Subroutine
clear_db_Main: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 0 | my $proto = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | unless(ref($proto)){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | die "save_db_Main() can't be used as a clsas method."; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | my $saved = $proto->{__db_Main}; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | $proto->{__db_Main} = undef; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | return $saved; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | sub effected_classes { | ||||||||||||||||||||||||||
|
Subroutine
effected_classes: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 150 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 151 | 0 | my @classes; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | for my $target ( keys %{ $class->_targetClasses } ){ | |||||||||||||||||||||||||||
| 154 | 0 | push @classes, $target; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | return @classes; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | ############################################################################## | ||||||||||||||||||||||||||||
| 162 | 4 | 1; | |||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||