blib/lib/DBD/Multiplex.pm

filter: Statementstmt 15.0% 40 / 266 Branchbran 0.0% 0 / 118 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 27.5% 8 / 29 Podpod 0.0% 0 / 5 totaltotal 10.0% 48 / 477 CC 79 SCAR 57.2
CC
79
Cov
10%
connect
702.0
mx_do_calls
552.0
mx_method_all
110.0
CRAP 304.2
↑ Summary
1 #########1#########2#########3#########4#########5#########6#########7#########8
2 # vim: ts=8:sw=4
3 #
4 # $Id: Multiplex.pm,v 2.11 2002/11/11 00:01:01 timbo Exp $
5 #
6 # Copyright (c) 1999,2008 Tim Bunce & Thomas Kishel
7 #
8 # You may distribute under the terms of either the GNU General Public
9 # License or the Artistic License, as specified in the Perl README file.
10  
11 { #=================================================================== DBD ===
12  
13 package DBD::Multiplex;
14  
15 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 @EXPORT = ();
18  
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use vars qw($VERSION $drh $err $errstr $sqlstate);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 $VERSION = sprintf("%d.%02d", q$Revision: 2.11 $ =~ /(\d+)\.(\d+)/o);
23  
24 1 $drh = undef; # Holds driver handle once it has been initialized.
25 1 $err = 0; # Holds error code for $DBI::err.
26 1 $errstr = ''; # Holds error string for $DBI::errstr.
27 1 $sqlstate = ''; # Holds SQL state for $DBI::state.
28  
29 #########################################
30 # The driver handle constructor.
31 #########################################
32  
33 0 ▶sub driver {
Subroutine driver: not called CC=2 SCAR=17.9
Pod undocumented
34 0 ▶ return $drh if ($drh);
Branch: 1 branch
BranchTrueFalseTotal
if $DBD::Multiplex::drh 0 0 0
35 0 my ($class, $attr) = @_;
36  
37 0 $class .= "::dr";
38
39 # $drh is not scoped with 'my',
40 # since we use it above to prevent multiple drivers.
41
42 0 ($drh) = DBI::_new_drh ($class, {
43 'Name' => 'Multiplex',
44 'Version' => $VERSION,
45 'Attribution' => 'DBD Multiplex by Tim Bunce && Thomas Kishel',
46 });
47
48 0 return $drh;
49 }
50  
51 ########################################
52 # Function for calling a method for each child handle of a parent handle.
53 # The parent handle is one of 'our' database or statement handles.
54 # Each of the child handles is a 'native' database or statement handle.
55 # -- called inside AUTOLOAD --
56 ########################################
57  
58 0 ▶sub mx_method_all {
Subroutine mx_method_all: not called CC=10 SCAR=47.0
Pod undocumented
59 # Remember that shift modifies the parameter list.
60 0 my ($method, $parent_handle) = (shift, shift);
61  
62 0 my ($exit_mode, %modes, %multiplex_options, $results, $errors);
63 0 my ($return_result, @return_results);
64
65 0 $exit_mode = $parent_handle->{'mx_exit_mode'};
66  
67 # TK Note:
68 # do() is a method of a database handle, not a statement handle.
69 0 ▶ if ($method eq 'do' or $method eq 'disconnect') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "do" or $method eq "disconnect") 0 0 0
Condition0%0/3
$method eq "do" or $method eq "disconnect"
l!l&&r!l&&!r
000
MC/DC0%0/2
$method eq "do" or $method eq "disconnect"
$method eq "do" $method eq "disconnect"
Truth table0%0/3
A or B
A= $method eq "do"B= $method eq "disconnect"
ABresult
1X1
011
000
70 0 delete $parent_handle->{'Statement'}; # PD
71 0 $parent_handle->{'Statement'} = $_[0];
72 }
73
74 # Override both the default exit_mode,
75 # and the exit_mode attribute stored in the parent handle,
76 # when multiplexing the following:
77 %modes = (
78 0 'STORE' => 'first_error',
79 'FETCH' => 'first_error',
80 'finish' => 'last_result',
81 'disconnect' => 'last_result'
82 );
83  
84 0 ▶ $exit_mode = $modes{$method} if ($modes{$method});
Branch: 1 branch
BranchTrueFalseTotal
if $modes{$method} 0 0 0
85  
86 0 %multiplex_options = ('parent_handle' => $parent_handle, 'exit_mode' => $exit_mode);
87
88 0 ($results, $errors) = &DBD::Multiplex::mx_do_calls ($method, wantarray, \%multiplex_options, @_);
89  
90 # find first defined result
91 0 for (@$results) {
92 0 $return_result = $_;
93 0 ▶ last if defined $return_result->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $return_result->[0] 0 0 0
94 }
95  
96 # return first defined result
97 0 ▶ return $return_result->[0] unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
98
99 # The context of fetchrow_array (and selectrow_array ?) is wantarray,
100 # and therefore cannot be distinguished from a multiplexed scalar context.
101 # In this case, returning an array of results from multiple handles is incorrect.
102 0 ▶ if ( $method eq 'fetchrow_array' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "fetchrow_array") 0 0 0
103 0 return @$return_result;
104 }
105
106 # find all defined results
107 0 for (@$results) {
108 0 ▶ push(@return_results, $_->[0]) if defined $_->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->[0] 0 0 0
109 }
110  
111 # return all defined results
112 0 return @return_results;
113 }
114  
115 ########################################
116 # 'Bottom-level' support function to multiplex the calls.
117 # See the documentation for information about $exit_mode.
118 # Currently the 'last_result' exit_mode is automagic.
119 ########################################
120  
121 0 ▶sub mx_do_calls {
Subroutine mx_do_calls: not called CC=23 SCAR=63.1
Pod undocumented
122 # Remember that shift modifies the parameter list.
123 0 my ($method, $wantarray, $multiplex_options) = (shift, shift, shift);
124  
125 # @errors is a sparse array paralleling $results[0..n]
126 0 my ($parent_handle, $parent_handle_list, $parent_name_list, $parent_id_list);
127 0 my ($error_proc, $exit_mode);
128 0 my ($child_handle, $child_number, @results, @errors);
129 0 my ($child_err, $child_errstr, $statement);
130  
131 0 ▶ $parent_handle = $multiplex_options->{'parent_handle'} || die;
Condition0%0/2
$multiplex_options->{'parent_handle'} || die()
l!l
00
MC/DC0%0/1
$multiplex_options->{'parent_handle'} || die()
$multiplex_options->{'parent_handle'}
Truth table0%0/2
A
A= $multiplex_options->{'parent_handle'}
Aresult
11
00
132 0 ▶ $parent_handle_list = $parent_handle->{'mx_handle_list'} || die;
Condition0%0/2
$parent_handle->{'mx_handle_list'} || die()
l!l
00
MC/DC0%0/1
$parent_handle->{'mx_handle_list'} || die()
$parent_handle->{'mx_handle_list'}
Truth table0%0/2
A
A= $parent_handle->{'mx_handle_list'}
Aresult
11
00
133 0 $parent_name_list = $parent_handle->{'mx_name_list'};
134 0 $parent_id_list = $parent_handle->{'mx_id_list'};
135 0 ▶ $exit_mode = $multiplex_options->{'exit_mode'} || 'first_error';
Condition0%0/2
$multiplex_options->{'exit_mode'} || 'first_error'
l!l
00
MC/DC0%0/1
$multiplex_options->{'exit_mode'} || 'first_error'
$multiplex_options->{'exit_mode'}
Truth table0%0/2
A
A= $multiplex_options->{'exit_mode'}
Aresult
11
00
136 0 $error_proc = $parent_handle->{'mx_error_proc'};
137  
138 0 ▶ $parent_handle->trace_msg("mx_do_calls $method for " . join(", ", map{defined $_?$_:''} @$parent_handle_list) . "\n");
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
139  
140 0 $child_number = 0;
141 0 $statement = $parent_handle->{'Statement'};
142  
143 # EP Note:
144 # If master dsn is specified, and current statement is a
145 # modification operation, make sure this is done on the master:
146 #
147 # If ($method eq 'do' || $method eq 'execute' and
148 # the above condition is wrong, because then _any_ prepare()
149 # will definitely go to second condition.
150  
151 # RR Note:
152 # Transactions are run only on the master.
153  
154 0 ▶ if ( $parent_handle->{'mx_master_id'} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_handle->{'mx_master_id'} and &mx_is_modify_statement(\$statement) || !$parent_handle->{'AutoCommit'}) { } 0 0 0
Condition0%0/6
$parent_handle->{"mx_master_id"} and &mx_is_modify_statement(\$statement) || !$parent_handle->{"AutoCommit"}
!ll&&!rl&&r
000
&mx_is_modify_statement(\$statement) or not $parent_handle->{"AutoCommit"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$parent_handle->{"mx_master_id"} and &mx_is_modify_statement(\$statement) || !$parent_handle->{"AutoCommit"}
$parent_handle->{"mx_master_id"} &mx_is_modify_statement(\$statement) not $parent_handle->{"AutoCommit"}
Truth table0%0/4
A and B or not(C)
A= $parent_handle->{"mx_master_id"}B= &mx_is_modify_statement(\$statement)C= not $parent_handle->{"AutoCommit"}
ABCresult
0XX0
1000
11X1
1011
155 ( &DBD::Multiplex::mx_is_modify_statement(\$statement) || (! $parent_handle->{'AutoCommit'}) )
156 ) {
157
158 # TK Note:
159 # Loop to find the master handle.
160 # Consider finding once and storing rather than finding each time.
161 0 for (@$parent_id_list) {
162 0 ▶ last if $_ eq $parent_handle->{'mx_master_id'};
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $parent_handle->{"mx_master_id"} 0 0 0
163 0 push @results, [undef];
164 0 $child_number++;
165 }
166
167 0 ▶ if ($statement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement) 0 0 0
168 0 $parent_handle->trace_msg("mx_do_calls $method for statement $statement against child number $child_number\n");
169 }
170  
171 0 $child_handle = $parent_handle_list->[$child_number];
172 0 ▶ $results[$child_number] = ($wantarray)
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
173 ? [ $child_handle->$method(@_) ]
174 : [ scalar $child_handle->$method(@_) ];
175  
176 0 ▶ if ($child_err = $child_handle->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_err = $child_handle->err) 0 0 0
177 0 $child_errstr = $child_handle->errstr;
178 0 $errors[$child_number] = [$child_err, $child_errstr];
179 0 ▶ if ($parent_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_handle) 0 0 0
180 0 &DBI::set_err($parent_handle, $child_err, $child_errstr);
181 }
182 0 ▶ if ($error_proc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_proc) 0 0 0
183 0 $error_proc->(${$parent_name_list}[$child_number], ${$parent_id_list}[$child_number], $child_err, $child_errstr);
184 }
185 }
186  
187 } else {
188  
189 0 foreach $child_handle (@$parent_handle_list) {
190  
191 0 ▶ if ($statement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement) 0 0 0
192 0 $parent_handle->trace_msg("mx_do_calls $method for statement $statement against child number $child_number\n") ;
193 }
194
195 # Here, the actual method being multiplexed is being called.
196 0 ▶ push @results, ($wantarray)
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
197 ? [ $child_handle->$method(@_) ]
198 : [ scalar $child_handle->$method(@_) ];
199  
200 0 ▶ if ($child_err = $child_handle->err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child_err = $child_handle->err) { } 0 0 0
201 0 $child_errstr = $child_handle->errstr;
202 0 $errors[@results - 1] = [$child_err, $child_errstr];
203 0 ▶ if ($parent_handle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_handle) 0 0 0
204 0 &DBI::set_err($parent_handle, $child_err, $child_errstr);
205 }
206 0 ▶ if ($error_proc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_proc) 0 0 0
207 0 $error_proc->(${$parent_name_list}[$child_number], ${$parent_id_list}[$child_number], $child_err, $child_errstr);
208 }
209 0 ▶ last if ($exit_mode eq 'first_error');
Branch: 1 branch
BranchTrueFalseTotal
if $exit_mode eq "first_error" 0 0 0
210 } else {
211 0 ▶ last if ($exit_mode eq 'first_success');
Branch: 1 branch
BranchTrueFalseTotal
if $exit_mode eq "first_success" 0 0 0
212 }
213  
214 0 $child_number = $child_number + 1;
215 }
216  
217 }
218
219 0 return (\@results, \@errors);
220 }
221  
222 ########################################
223 # Identify if the statement modifies data in the datasource.
224 # EP Added CREATE and DROP.
225 # MS #24220 One of these keywords must appear as the first word in the SQL. I believe this is what pgpool does.
226 # MS #24219 Support SELECTs that modify.
227 ########################################
228  
229 0 ▶sub mx_is_modify_statement {
Subroutine mx_is_modify_statement: not called CC=5 SCAR=34.0
Pod undocumented
230 0 my ($statement) = @_;
231
232 0 my ($rv) = 0;
233
234 SWITCH: {
235 0 ▶ if (! $$statement) { $rv = 0; last SWITCH; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($$statement) 0 0 0
236 0 ▶ if ($$statement =~ /^\s*INSERT\s|^\s*UPDATE\s|^\s*DELETE\s|^\s*CREATE\s|^\s*DROP\s/i) { $rv = 1; last SWITCH; }
Branch: 1 branch
BranchTrueFalseTotal
if ($$statement =~ /^\s*INSERT\s|^\s*UPDATE\s|^\s*DELETE\s|^\s*CREATE\s|^\s*DROP\s/i) 0 0 0
237 0 ▶ if ($$statement =~ /^\s*SELECT(.*?)INTO\s/i) { $rv = 1; last SWITCH; }
Branch: 1 branch
BranchTrueFalseTotal
if ($$statement =~ /^\s*SELECT(.*?)INTO\s/i) 0 0 0
238 0 ▶ if ($$statement =~ /^\s*SELECT(.*?)NEXTVAL|SETVAL\s/i) { $rv = 1; last SWITCH; }
Branch: 1 branch
BranchTrueFalseTotal
if ($$statement =~ /^\s*SELECT(.*?)NEXTVAL|SETVAL\s/i) 0 0 0
239 }
240
241 0 return $rv;
242 }
243  
244 ########################################
245 # Example error logging mechanism.
246 ########################################
247  
248 0 ▶sub mx_error_subroutine {
Subroutine mx_error_subroutine: not called CC=1 SCAR=6.9
Pod undocumented
249 0 my ($dsn, $mx_id, $error, $error_string) = @_;
250
251 0 print STDERR "DSN: $dsn\;mx_id\=$mx_id\n";
252 0 print STDERR "ERR: $error\n";
253 0 print STDERR "ERRSTR: $error_string\n";
254
255 0 return 1;
256 }
257  
258 } #=============================================================== END DBD ===
259  
260 { #================================================================ DRIVER ===
261  
262 1 package DBD::Multiplex::dr;
263 1 $imp_data_size = 0;
264  
265 ########################################
266 # The database handle constructor.
267 # This function cannot be called using mx_method_all.
268 # DW #11244
269 ########################################
270  
271 0 ▶sub connect {
Subroutine connect: not called CC=26 SCAR=65.5
272 0 my ($drh, $dsn, $user, $auth, $attr) = @_;
273  
274 0 my (@dsn_list, $dbh, @mx_dsn_list, @mx_dbh_list, $mx_id, @mx_id_list);
275 0 my ($connect_mode, $stored_print_error, $exit_mode, $error_proc, $this);
276 0 my ($dsn_count, @dsn_order, $dsn_number);
277  
278 # Retrieve the DSNs and ATTRs from the $dsn parameter.
279 0 my ($dsn_attr, $dsn_attr_mx_connect_mode, $dsn_attr_exit_mode);
280 0 ($dsn, $dsn_attr) = split (/\#/, $dsn);
281  
282 # Retrieve the DSNs from the $dsn parameter.
283 0 ▶ @dsn_list = split (/\|/, $dsn) if defined $dsn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dsn 0 0 0
284
285 # Add the DSNs from the attribute hashref parameter.
286 0 foreach (@{$attr->{'mx_dsns'}}) {
287 0 push (@dsn_list, $_);
288 }
289 0 $dsn_count = @dsn_list;
290  
291 # Retrieve valid attributes from the $dsn parameter.
292 0 ▶ if (defined $dsn_attr && $dsn_attr =~ /;?mx_connect_mode=(\w+);?/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dsn_attr and $dsn_attr =~ /;?mx_connect_mode=(\w+);?/i) 0 0 0
Condition0%0/3
defined $dsn_attr and $dsn_attr =~ /;?mx_connect_mode=(\w+);?/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dsn_attr and $dsn_attr =~ /;?mx_connect_mode=(\w+);?/i
defined $dsn_attr $dsn_attr =~ /;?mx_connect_mode=(\w+);?/i
Truth table0%0/3
A and B
A= defined $dsn_attrB= $dsn_attr =~ /;?mx_connect_mode=(\w+);?/i
ABresult
0X0
100
111
293 0 $dsn_attr_mx_connect_mode = $1;
294 0 ▶ undef $dsn_attr_mx_connect_mode if (! &mx_valid_mx_connect_mode($dsn_attr_mx_connect_mode));
Branch: 1 branch
BranchTrueFalseTotal
unless &mx_valid_mx_connect_mode($dsn_attr_mx_connect_mode) 0 0 0
295 }
296 0 ▶ if (defined $dsn_attr && $dsn_attr =~ /;?mx_exit_mode=(\w+);?/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dsn_attr and $dsn_attr =~ /;?mx_exit_mode=(\w+);?/i) 0 0 0
Condition0%0/3
defined $dsn_attr and $dsn_attr =~ /;?mx_exit_mode=(\w+);?/i
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dsn_attr and $dsn_attr =~ /;?mx_exit_mode=(\w+);?/i
defined $dsn_attr $dsn_attr =~ /;?mx_exit_mode=(\w+);?/i
Truth table0%0/3
A and B
A= defined $dsn_attrB= $dsn_attr =~ /;?mx_exit_mode=(\w+);?/i
ABresult
0X0
100
111
297 0 $dsn_attr_exit_mode = $1;
298 0 ▶ undef $dsn_attr_exit_mode if (! &mx_valid_mx_exit_mode($dsn_attr_exit_mode));
Branch: 1 branch
BranchTrueFalseTotal
unless &mx_valid_mx_exit_mode($dsn_attr_exit_mode) 0 0 0
299 }
300  
301 # Clear invalid attribute hashref parameters.
302 0 ▶ undef $attr->{'mx_connect_mode'} if (! &mx_valid_mx_connect_mode($attr->{'mx_connect_mode'}));
Branch: 1 branch
BranchTrueFalseTotal
unless &mx_valid_mx_connect_mode($attr->{"mx_connect_mode"}) 0 0 0
303 0 ▶ undef $attr->{'mx_exit_mode'} if (! &mx_valid_mx_exit_mode($attr->{'mx_exit_mode'}));
Branch: 1 branch
BranchTrueFalseTotal
unless &mx_valid_mx_exit_mode($attr->{"mx_exit_mode"}) 0 0 0
304 0 ▶ undef $attr->{'mx_error_proc'} if (! ref($attr->{'mx_error_proc'}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attr->{"mx_error_proc"} 0 0 0
305  
306 # Prefer attributes from the attribute hashref over $dsn parameters.
307 0 ▶ $attr->{'mx_connect_mode'} = ($attr->{'mx_connect_mode'} || $dsn_attr_mx_connect_mode);
Condition0%0/3
$attr->{'mx_connect_mode'} || $dsn_attr_mx_connect_mode
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr->{'mx_connect_mode'} || $dsn_attr_mx_connect_mode
$attr->{'mx_connect_mode'} $dsn_attr_mx_connect_mode
Truth table0%0/3
A || B
A= $attr->{'mx_connect_mode'}B= $dsn_attr_mx_connect_mode
ABresult
1X1
011
000
308 0 ▶ $attr->{'mx_exit_mode'} = ($attr->{'mx_exit_mode'} || $dsn_attr_exit_mode);
Condition0%0/3
$attr->{'mx_exit_mode'} || $dsn_attr_exit_mode
l!l&&r!l&&!r
000
MC/DC0%0/2
$attr->{'mx_exit_mode'} || $dsn_attr_exit_mode
$attr->{'mx_exit_mode'} $dsn_attr_exit_mode
Truth table0%0/3
A || B
A= $attr->{'mx_exit_mode'}B= $dsn_attr_exit_mode
ABresult
1X1
011
000
309  
310 # connect_mode decides what to do with DBI->connect errors.
311 # exit_mode decides when to exit the foreach loop.
312 # error_proc is a code reference to execute in case of an execute error.
313 0 ▶ $connect_mode = ($attr->{'mx_connect_mode'} || 'report_errors');
Condition0%0/2
$attr->{'mx_connect_mode'} || 'report_errors'
l!l
00
MC/DC0%0/1
$attr->{'mx_connect_mode'} || 'report_errors'
$attr->{'mx_connect_mode'}
Truth table0%0/2
A
A= $attr->{'mx_connect_mode'}
Aresult
11
00
314 0 ▶ $exit_mode = ($attr->{'mx_exit_mode'} || 'first_error');
Condition0%0/2
$attr->{'mx_exit_mode'} || 'first_error'
l!l
00
MC/DC0%0/1
$attr->{'mx_exit_mode'} || 'first_error'
$attr->{'mx_exit_mode'}
Truth table0%0/2
A
A= $attr->{'mx_exit_mode'}
Aresult
11
00
315 0 $error_proc = $attr->{'mx_error_proc'};
316  
317 # 'first_success_random' exit_mode is implemented only at connect time.
318 # Afterwards, revert to 'first_success' exit_mode.
319
320 # TK Note:
321 # Trying to implement randomness after this point fails.
322 # Consider creating a new parameter; changing parameter into a connect_mode; or rewriting randomness to work correctly at lower levels.
323 0 ▶ if ($exit_mode eq 'first_success_random') {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_mode eq 'first_success_random') { } 0 0 0
324 0 @dsn_order = &mx_rand_list($dsn_count - 1);
325 0 $attr->{'mx_exit_mode'} = 'first_success';
326 } else {
327 0 @dsn_order = (0..$dsn_count);
328 }
329
330 # Connect to each dsn in the dsn_list.
331 0 ▶ for ($dsn_number = 0; $dsn_number < $dsn_count; $dsn_number++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn_number < $dsn_count) 0 0 0
332 0 $dsn = $dsn_list[$dsn_order[$dsn_number]];
333  
334 # Retrieve the datasource id for use by the error_proc.
335 # Remove the datasource id from the driver name.
336 # There is no standard for the text following the driver name.
337 # Each driver is free to use whatever syntax it wants.
338 0 ▶ if ($dsn =~ /;?mx_id=(\w+);?/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /;?mx_id=(\w+);?/i) 0 0 0
339 0 $mx_id = $1;
340 0 $dsn =~ s/;?mx_id=$mx_id;?/;/;
341 0 $dsn =~ s/^;|;$//;
342 }
343
344 # Suppress initial warnings when ignoring errors and not explicitly printing errors.
345 0 $stored_print_error = $attr->{'PrintError'};
346 0 ▶ if (($connect_mode eq 'ignore_errors') && ($stored_print_error)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connect_mode eq "ignore_errors" and $stored_print_error) 0 0 0
Condition0%0/3
$connect_mode eq "ignore_errors" and $stored_print_error
!ll&&!rl&&r
000
MC/DC0%0/2
$connect_mode eq "ignore_errors" and $stored_print_error
$connect_mode eq "ignore_errors" $stored_print_error
Truth table0%0/3
A and B
A= $connect_mode eq "ignore_errors"B= $stored_print_error
ABresult
0X0
100
111
347 0 $attr->{'PrintError'} = 0;
348 }
349  
350 0 $dbh = DBI->connect($dsn, $user, $auth, $attr);
351 0 ▶ if ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh) { } 0 0 0
352 0 push (@mx_dsn_list, $dsn);
353 0 push (@mx_dbh_list, $dbh);
354 0 push (@mx_id_list, $mx_id);
355 0 $dbh->{'PrintError'} = $stored_print_error;
356 } else {
357 # Override the connect_mode if there is and this is the mx_master_id.
358 0 ▶ if ($attr->{'mx_master_id'} && ($attr->{'mx_master_id'} eq $mx_id)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr->{'mx_master_id'} and $attr->{'mx_master_id'} eq $mx_id) { } 0 0 0
elsif ($connect_mode eq 'ignore_errors') { } 0 0 0
Condition0%0/3
$attr->{"mx_master_id"} and $attr->{"mx_master_id"} eq $mx_id
!ll&&!rl&&r
000
MC/DC0%0/2
$attr->{"mx_master_id"} and $attr->{"mx_master_id"} eq $mx_id
$attr->{"mx_master_id"} $attr->{"mx_master_id"} eq $mx_id
Truth table0%0/3
A and B
A= $attr->{"mx_master_id"}B= $attr->{"mx_master_id"} eq $mx_id
ABresult
0X0
100
111
359 0 return &DBI::set_err($drh, $DBI::err, $DBI::errstr);
360 } elsif ($connect_mode eq 'ignore_errors') {
361 # TK Note:
362 # Implement?
363 # &DBI::set_err($drh, $DBI::err, $DBI::errstr);
364 0 ▶ if ($error_proc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_proc) 0 0 0
365 0 $error_proc->($dsn, $mx_id, $DBI::err, $DBI::errstr);
366 }
367 } else {
368 0 return &DBI::set_err($drh, $DBI::err, $DBI::errstr);
369 }
370 }
371
372 }
373
374 # Name is an array of all dsns, mx_name_list is an array of all connected dsns.
375 $this = DBI::_new_dbh ($drh, {
376 'Name' => [@dsn_list],
377 'User' => $user,
378 'mx_name_list' => [@mx_dsn_list],
379 'mx_handle_list' => [@mx_dbh_list],
380 'mx_id_list' => [@mx_id_list],
381 'mx_master_id' => $attr->{'mx_master_id'},
382 0 'mx_exit_mode' => $attr->{'mx_exit_mode'},
383 'mx_error_proc' => $error_proc,
384 });
385  
386 0 return $this;
387 }
388  
389 ########################################
390 # Required by the DBI.
391 ########################################
392  
393 0 ▶sub disconnect_all {
Subroutine disconnect_all: not called CC=1 SCAR=6.9
394 0 undef;
395 }
396  
397 ########################################
398 # Required by the DBI.
399 ########################################
400  
401 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
402 0 undef;
403 }
404  
405 ########################################
406 # A random list of numbers.
407 ########################################
408 0 ▶sub mx_rand_list {
Subroutine mx_rand_list: not called CC=2 SCAR=17.9
409 0 my (@input) = (0..$_[0]);
410 0 my (@output);
411
412 0 srand(time() ^ ($$ + ($$ << 15)));
413 0 ▶ push(@output, splice (@input, rand(@input), 1)) while (@input);
Branch: 1 branch
BranchTrueFalseTotal
@input and do { push @output, splice(@input, rand @input, 1) } 0 0 0
414
415 0 return @output;
416 }
417  
418 ########################################
419 # Parameter validation.
420 ########################################
421  
422 0 ▶sub mx_valid_mx_connect_mode {
Subroutine mx_valid_mx_connect_mode: not called CC=1 SCAR=6.9
423 0 my ($mode) = @_;
424
425 0 my (%modes);
426 0 %modes = (
427 'report_errors', => 1,
428 'ignore_errors' => 1
429 );
430
431 0 return ($modes{$mode});
432 }
433  
434 ########################################
435 # Parameter validation.
436 ########################################
437  
438 0 ▶sub mx_valid_mx_exit_mode {
Subroutine mx_valid_mx_exit_mode: not called CC=1 SCAR=6.9
439 0 my ($mode) = @_;
440
441 0 my (%modes);
442 0 %modes = (
443 'first_error', => 1,
444 'first_success' => 1,
445 'first_success_random', => 1,
446 'last_result', => 1
447 );
448
449 0 return ($modes{$mode});
450 }
451  
452 } #============================================================ END DRIVER ===
453  
454 { #============================================================== DATABASE ===
455  
456 1 package DBD::Multiplex::db;
457 1 $imp_data_size = 0;
458 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
459  
460 ########################################
461 # The statement handle constructor.
462 # This function calls mx_do_calls and therefore cannot be called using mx_method_all.
463 # TK Note:
464 # Consider the interaction between do, prepare, execute, and mx_error_proc.
465 ########################################
466  
467 0 ▶sub prepare {
Subroutine prepare: not called CC=3 SCAR=24.8
468 # Remember that shift modifies the parameter list.
469 0 my ($dbh) = shift;
470 0 my ($statement, $attr) = @_;
471  
472 0 my ($parent_name_list, $parent_id_list, $parent_master_id, $parent_error_proc, $parent_exit_mode);
473 0 my ($exit_mode, %multiplex_options, $results, $errors, $outer, $sth);
474  
475 0 $parent_name_list = $dbh->{'mx_name_list'};
476 0 $parent_id_list = $dbh->{'mx_id_list'};
477 0 $parent_master_id = $dbh->{'mx_master_id'};
478 0 $parent_exit_mode = $dbh->{'mx_exit_mode'};
479 0 $parent_error_proc = $dbh->{'mx_error_proc'};
480  
481 # The user can set the exit_mode of a new or existing database handle.
482 # Otherwise, parse the SQL statement to determine the exit_mode.
483 0 ▶ if ($parent_exit_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_exit_mode) { } 0 0 0
484 0 $exit_mode = $parent_exit_mode;
485 } else {
486 0 $exit_mode = &DBD::Multiplex::db::mx_default_statement_mode(\$statement);
487 }
488  
489 # Don't forget this!
490 0 delete $dbh->{Statement}; # PD
491 0 $dbh->{'Statement'} = $statement;
492  
493 0 %multiplex_options = ('parent_handle' => $dbh, 'exit_mode' => $exit_mode);
494  
495 0 ($results, $errors) = &DBD::Multiplex::mx_do_calls ('prepare', wantarray, \%multiplex_options, @_);
496  
497 0 ▶ return if (@$errors);
Branch: 1 branch
BranchTrueFalseTotal
if @$errors 0 0 0
498  
499 # Assign the @results of the multiple prepare calls,
500 # executed against each of the $dbh's children handles,
501 # to an array of children stored in the statement handle.
502 # $sth is a reference to the inner hash (used by the driver).
503 # $outer is a reference to the outer hash (used by the user of the DBI).
504 ($outer, $sth) = DBI::_new_sth ($dbh, {
505 'Statement' => $statement,
506 0 'mx_handle_list' => [map {$_->[0]} @$results],
507 'mx_name_list' => $parent_name_list,
508 'mx_id_list' => $parent_id_list,
509 'mx_master_id' => $parent_master_id,
510 'mx_exit_mode' => $parent_exit_mode,
511 'mx_error_proc' => $parent_error_proc,
512 });
513  
514 0 return $outer;
515 }
516  
517  
518 ########################################
519 # Some attributes are stored in the parent handle.
520 # some in each of the children handles.
521 # This function uses and therefore cannot be called using mx_method_all.
522 ########################################
523  
524 0 ▶sub STORE {
Subroutine STORE: not called CC=3 SCAR=24.8
525 0 my ($dbh, $attr, $val) = @_;
526  
527 0 ▶ if ($attr =~ /^mx_(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /^mx_(.+)/) 0 0 0
528 0 ▶ if ($1 eq uc($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq uc $1) { } 0 0 0
529 0 return $dbh->SUPER::STORE($attr, $val);
530 } else {
531 0 return $dbh->{$attr} = $val;
532 }
533 }
534  
535 # Store the attribute in each of the children handles.
536 0 return &DBD::Multiplex::mx_method_all('STORE', @_);
537 }
538  
539 ########################################
540 # Some attributes are stored in the parent handle.
541 # some in each of the children handles.
542 # This function uses and therefore cannot be called using mx_method_all.
543 ########################################
544  
545 0 ▶sub FETCH {
Subroutine FETCH: not called CC=3 SCAR=24.8
546 0 my ($dbh, $attr) = @_;
547  
548 0 ▶ if ($attr =~ /^mx_(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /^mx_(.+)/) 0 0 0
549 0 ▶ if ($1 eq uc($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq uc $1) { } 0 0 0
550 0 return $dbh->SUPER::FETCH($attr);
551 } else {
552 0 return $dbh->{$attr};
553 }
554 }
555  
556 # Fetch the attribute from one of the children handles.
557 0 return &DBD::Multiplex::mx_method_all('FETCH', @_);
558 }
559  
560 ########################################
561 # Required by the DBI.
562 ########################################
563  
564 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
565 0 undef;
566 }
567  
568 ########################################
569 # Used by AUTOLOAD below. Omit 'prepare'.
570 ########################################
571  
572 1 my %db_methods;
573 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
574 1 %db_methods = %{$DBI::DBI_methods{db}};
575 1 delete($db_methods{'prepare'});
576 }
577 1 ▶use subs keys %db_methods;
Subroutine BEGIN: called CC=1 SCAR=0.0
578  
579 ########################################
580 # Call the multiplexing code for each of the database methods listed above.
581 ########################################
582  
583 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
584 0 my ($method, @results);
585
586 0 $method = $DBD::Multiplex::db::AUTOLOAD;
587 0 $method =~ s/^DBD::Multiplex::db:://;
588
589 # Two levels down, the actual method being multiplexed is being called.
590 0 ▶ @results = (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
591 ? ( &DBD::Multiplex::mx_method_all($method, @_) )
592 : ( scalar &DBD::Multiplex::mx_method_all($method, @_) );
593  
594 0 ▶ return $results[0] unless (wantarray);
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
595 0 return @results;
596 }
597  
598 ########################################
599 # The default behaviour is to not multiplex simple select statements.
600 # The resulting statement handle then contains only one child handle,
601 # automatically resulting in subsequent methods executed against the
602 # statement handle to use 'first_success' mode.
603 ########################################
604  
605 0 ▶sub mx_default_statement_mode {
Subroutine mx_default_statement_mode: not called CC=2 SCAR=17.9
606 0 my ($statement) = @_;
607 0 my ($result);
608  
609 0 $result = '';
610  
611 0 ▶ if (! &DBD::Multiplex::mx_is_modify_statement($statement)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&DBD::Multiplex::mx_is_modify_statement($statement)) 0 0 0
612 0 $result = 'first_success';
613 }
614
615 0 return $result;
616 }
617  
618 } #========================================================== END DATABASE ===
619  
620 { #============================================================= STATEMENT ===
621  
622 1 package DBD::Multiplex::st;
623 1 $imp_data_size = 0;
624 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
625
626 ########################################
627 # Some attributes are stored in the parent handle.
628 # some in each of the children handles.
629 # This function uses and therefore cannot be called using mx_method_all.
630 ########################################
631  
632 0 ▶sub STORE {
Subroutine STORE: not called CC=4 SCAR=30.0
633 0 my ($sth, $attr, $val) = @_;
634  
635 0 ▶ if ($attr =~ /^mx_(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /^mx_(.+)/) 0 0 0
636 0 ▶ if ($1 eq uc($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq uc $1) { } 0 0 0
637 0 ▶ return $sth->SUPER::STORE($attr, $val) if ($1 eq uc($1));
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq uc $1 0 0 0
638 } else {
639 0 return $sth->{$attr} = $val;
640 }
641 }
642  
643 # Store the attribute in each of the children handles.
644 0 return &DBD::Multiplex::mx_method_all('STORE', @_);
645 }
646  
647 ########################################
648 # Some attributes are stored in the parent handle.
649 # some in each of the children handles.
650 # This function uses and therefore cannot be called using mx_method_all.
651 ########################################
652  
653 0 ▶sub FETCH {
Subroutine FETCH: not called CC=3 SCAR=24.8
654 0 my ($sth, $attr) = @_;
655  
656 0 ▶ if ($attr =~ /^mx_(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /^mx_(.+)/) 0 0 0
657 0 ▶ if ($1 eq uc($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq uc $1) { } 0 0 0
658 0 return $sth->SUPER::FETCH($attr);
659 } else {
660 0 return $sth->{$attr};
661 }
662 }
663  
664 # Fetch the attribute from each of the children handles.
665 0 return &DBD::Multiplex::mx_method_all('FETCH', @_);
666 }
667  
668 ########################################
669 # Required by the DBI.
670 ########################################
671  
672 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
673 0 undef;
674 }
675  
676 ########################################
677 # Used by AUTOLOAD below.
678 ########################################
679  
680 1 ▶use subs keys %{$DBI::DBI_methods{st}};
Subroutine BEGIN: called CC=1 SCAR=0.0
681  
682 ########################################
683 # Call the multiplexing code for each of the statement methods listed above.
684 ########################################
685  
686 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
687 0 my ($method, @results);
688
689 0 $method = $DBD::Multiplex::st::AUTOLOAD;
690 0 $method =~ s/^DBD::Multiplex::st:://;
691
692 # Two levels down, the actual method being multiplexed is being called.
693 0 ▶ @results = (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
694 ? ( &DBD::Multiplex::mx_method_all($method, @_) )
695 : ( scalar &DBD::Multiplex::mx_method_all($method, @_) );
696  
697 0 ▶ return $results[0] unless (wantarray);
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
698 0 return @results;
699 }
700  
701 } #========================================================= END STATEMENT ===
702  
703 1 1;
704  
705 __END__
↑ Summary