line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::DBI::Plugin::MultiDatabases; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
453703
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
122
|
|
4
|
4
|
|
|
4
|
|
22
|
use Carp; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
324
|
|
5
|
4
|
|
|
4
|
|
19
|
use base qw(Class::Data::Inheritable); |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
263
|
|
6
|
4
|
|
|
4
|
|
21
|
use vars qw($VERSION); |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
424
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$VERSION = 0.1; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
############################################################################## |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub import { |
13
|
4
|
|
|
4
|
|
26
|
my $me = shift; |
14
|
4
|
|
|
|
|
14
|
my $class = (caller)[0]; |
15
|
|
|
|
|
|
|
|
16
|
4
|
50
|
|
|
|
29
|
unless( UNIVERSAL::isa($class, 'Class::DBI') ){ |
17
|
0
|
|
|
|
|
0
|
croak __PACKAGE__, " can be used only by Class::DBI and its subclass."; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
4
|
|
|
4
|
|
21
|
no strict 'refs'; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
2739
|
|
21
|
4
|
|
|
|
|
9
|
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
|
|
|
|
|
38
|
*{"$class\::$sym"} = \&{ $sym }; |
|
32
|
|
|
|
|
149
|
|
|
32
|
|
|
|
|
53
|
|
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
4
|
|
|
|
|
16
|
$me->is_imported_class($class); |
27
|
|
|
|
|
|
|
|
28
|
4
|
|
|
|
|
47
|
$class->mk_classdata('current_dbh'); |
29
|
4
|
|
|
|
|
71
|
$class->mk_classdata('_dbnames'); |
30
|
4
|
|
|
|
|
73
|
$class->mk_classdata('_clear_object'); |
31
|
4
|
|
|
|
|
73
|
$class->mk_classdata('_DSNs'); |
32
|
4
|
|
|
|
|
62
|
$class->mk_classdata('_targetClasses'); |
33
|
|
|
|
|
|
|
|
34
|
4
|
|
|
|
|
69
|
$class->_dbnames( {} ); |
35
|
4
|
|
|
|
|
48
|
$class->_DSNs( {} ); # Data Source Name |
36
|
4
|
|
|
|
|
32
|
$class->_targetClasses( {} ); |
37
|
|
|
|
|
|
|
|
38
|
4
|
50
|
|
|
|
83
|
if( $class->can('clear_object_index') ){ |
39
|
4
|
|
|
|
|
21
|
$class->_clear_object(1); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
############################################################################## |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
{ # sub is_imported_class |
47
|
|
|
|
|
|
|
my $class; |
48
|
|
|
|
|
|
|
sub is_imported_class { |
49
|
4
|
|
|
4
|
0
|
33
|
my $self = shift; |
50
|
|
|
|
|
|
|
|
51
|
4
|
50
|
|
|
|
14
|
if($class){ return $class eq $self; } |
|
0
|
|
|
|
|
0
|
|
52
|
|
|
|
|
|
|
|
53
|
4
|
|
|
|
|
9
|
$class = shift; # only once |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
############################################################################## |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# See Also http://wiki.class-dbi.com/index.cgi?UsingMultipleDatabases |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub set_connections { |
62
|
4
|
|
|
4
|
1
|
920
|
my $class = shift; |
63
|
4
|
|
|
|
|
16
|
my %keys = @_; |
64
|
4
|
|
|
|
|
22
|
$class->_DSNs(\%keys); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub change_db { |
69
|
0
|
|
|
0
|
1
|
|
my ($class, $dsn_key) = @_; |
70
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
unless( $class->is_imported_class ){ |
72
|
0
|
|
|
|
|
|
croak "change_db() must be called by the imported class."; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
if($class->_dbnames->{$dsn_key}){ |
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
|
0
|
|
|
|
|
unless( $class->_DSNs->{$dsn_key} ){ |
90
|
0
|
|
|
|
|
|
return undef; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my @args = @{ $class->_DSNs->{$dsn_key} }; |
|
0
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
Ima::DBI->set_db($dsn_key, @args); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
for my $subclass ( $class->effected_classes ){ |
98
|
0
|
0
|
|
|
|
|
$subclass->clear_object_index() if($class->_clear_object()); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
return $dsn_key; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub db_Main { |
106
|
0
|
|
|
0
|
0
|
|
my $proto = shift; |
107
|
0
|
|
|
|
|
|
my $method = 'db_' . $proto->current_dbh(); |
108
|
|
|
|
|
|
|
|
109
|
0
|
0
|
0
|
|
|
|
if(ref($proto) and $proto->{__db_Main}){ |
110
|
0
|
|
|
|
|
|
return $proto->{__db_Main}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
0
|
0
|
|
|
|
|
if(!ref($proto)){ |
114
|
0
|
|
|
|
|
|
$proto->_targetClasses->{$proto} = 1; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
$proto->$method; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub save_db_Main { |
122
|
0
|
|
|
0
|
1
|
|
my $proto = shift; |
123
|
|
|
|
|
|
|
|
124
|
0
|
0
|
|
|
|
|
unless(ref($proto)){ |
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
|
|
|
|
|
|
|
sub clear_db_Main { |
135
|
0
|
|
|
0
|
1
|
|
my $proto = shift; |
136
|
|
|
|
|
|
|
|
137
|
0
|
0
|
|
|
|
|
unless(ref($proto)){ |
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
|
|
|
|
|
|
|
sub effected_classes { |
150
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
151
|
0
|
|
|
|
|
|
my @classes; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
for my $target ( keys %{ $class->_targetClasses } ){ |
|
0
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
push @classes, $target; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
return @classes; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
############################################################################## |
162
|
|
|
|
|
|
|
1; |
163
|
|
|
|
|
|
|
__END__ |