blib/lib/DBIO/Replicated/Storage.pm

filter: Statementstmt 71.8% 265 / 369 Branchbran 54.5% 60 / 110 Conditioncond 46.1% 42 / 91 MC/DCmcdc 21.1% 11 / 52 Subroutinesub 59.4% 41 / 69 Podpod 100% 38 / 38 totaltotal 62.6% 457 / 729 CC 151 SCAR 20.5
CC
151
Cov
64%
connect_info
26.0
_merge_replicant_connect_info
22.5
debug
12.0
CRAP 7.8
« blib/lib/DBIO/Test/Util/OverrideRequire.pm ↑ Summary blib/lib/DBIO/ResultSourceProxy.pm »
1 package DBIO::Replicated::Storage;
2 # ABSTRACT: Replicated DBI storage coordinator
3  
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.900007';
8  
9 19 ▶use base 'DBIO::Storage::DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 ▶use Scalar::Util qw/blessed reftype weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use Sub::Util 'set_subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 19 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 __PACKAGE__->mk_group_accessors(simple => qw/
18 pool_type
19 pool_args
20 balancer_type
21 balancer_args
22 backend_storage_class
23 _master_connect_info_opts
24 /);
25  
26 # ADR 0037 / ADR 0039: _exists_value_sql and _set_operation_keyword are engine
27 # facts the driver storage owns, so they have to reach the master backend
28 # exactly like sqlt_type and sql_maker do. Without the delegation a replicated
29 # setup would answer them from the generic base class and lose the driver's
30 # rendering -- and except() works here today *because* sqlt_type is delegated,
31 # so leaving the hooks out would turn this fix into a regression. For
32 # _set_operation_keyword the delegation carries the driver's *refusal* too: an
33 # engine that has no EXCEPT ALL must say so through a replicated storage as
34 # well, or the one setup that hides the driver would be the one that emits the
35 # broken SQL. _execute_rendered is the statement path results_exist runs on; it
36 # is a read, so it sits with _select.
37 #
38 # _random_function is the same shape as those two -- a static class
39 # fact of the driver, string out, no $dbh -- so it goes where they go, next to
40 # sqlt_type. Not with the readers: what rides on the reader list is the
41 # statement *path* (_select, _execute_rendered), and a spelling is not a path.
42 # rand() drops the returned function name into an order_by literal; the SELECT
43 # built around it reaches the read handler on its own.
44 #
45 # ADR 0040: its old companion _explain_sql does NOT sit here any
46 # more, and the move is the point rather than a tidy-up. While the hook only
47 # produced the *spelling* the master's driver dictates, a writer was right --
48 # execution went separately through _execute_rendered, which is a reader, so
49 # the EXPLAIN still ran on the read handler. _explain_query owns the whole
50 # operation, so the same classification would run the EXPLAIN of a READ query
51 # on the master and undo exactly that read routing. It is a read; it goes
52 # with _select and _execute_rendered. Note that adding it to @reader_methods is
53 # only half of it: the read handler is a DBIO::Replicated::Balancer, which is
54 # not a storage and forwards only the methods it names explicitly -- so
55 # DBIO::Replicated::Balancer::_explain_query exists too, routing like
56 # _execute_rendered (master inside a transaction, a replicant otherwise).
57 #
58 # The four capability entry points ride with them. A capability is a
59 # fact about the engine on the other end of a connection -- either declared by
60 # the driver storage class (_use_X) or probed from the live $dbh (_supports_X,
61 # memoised in _dbh_details). This object has neither a driver class nor a $dbh,
62 # so answering for itself could only ever produce the conservative default,
63 # which is a plausible-looking lie. Both tiers therefore go to the master: it
64 # owns DDL and the write path, and the engine whose version decides a
65 # capability is the master's. Both *setters* go with them, or a declaration
66 # made on the replicated storage would be written here and read from there.
67 2 my @writer_methods = qw(
68 sql_maker sqlt_type datetime_parser datetime_parser_type
69 build_datetime_parser last_insert_id insert update delete dbh
70 txn_begin txn_do txn_commit txn_rollback txn_scope_guard
71 deploy with_deferred_fk_checks dbh_do _prep_for_execute
72 is_datatype_numeric svp_rollback svp_begin svp_release
73 relname_to_table_alias _dbh_last_insert_id _dbi_connect_info
74 _dbio_connect_attributes auto_savepoint bind_attribute_by_data_type
75 transaction_depth deferred_rollback _throw_deferred_rollback
76 savepoints _sql_maker _sql_maker_opts _dbh_autocommit _get_dbh
77 sql_maker_class insert_bulk _insert_bulk _execute _do_query
78 _dbh_execute _server_info _get_server_version
79 _exists_value_sql _set_operation_keyword
80 _random_function
81 get_dbms_capability set_dbms_capability
82 get_use_dbms_capability set_use_dbms_capability
83 connection_schema
84 );
85  
86 # ADR 0040 section 6: _prepare_rendered is _execute_rendered
87 # stopped one verb short -- the same statement path, handing back a prepared
88 # handle instead of an executed one, for engines whose answer is an attribute
89 # of the PREPARED statement (Firebird's query plan). It rides with the path it
90 # is half of. Its one caller in the family is _explain_query, which is already
91 # a reader and therefore already runs on the replicant by the time it preps --
92 # but leaving a statement path off both lists is the failure mode ADR 0040
93 # section 4 documented and it is SILENT: the generic base class answers, the
94 # statement goes to whatever handle that finds, and nothing errors. The
95 # Balancer forwarder is not optional either, for the same reason as
96 # _explain_query's.
97 2 my @reader_methods = qw(
98 select select_single columns_info_for _dbh_columns_info_for _select
99 _execute_rendered _explain_query _prepare_rendered
100 );
101  
102  
103  
104 47 ▶sub new {
Subroutine new: called CC=9 SCAR=22.1
Pod documented
105 47 my ($class, $schema, $args) = @_;
106 47 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
443
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
107  
108 47 my $self = bless {
109 transaction_depth => 0,
110 savepoints => [],
111 }, $class;
112  
113 47 $self->schema($schema);
114 47 ▶ weaken $self->{schema} if ref $self->{schema};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"schema"} 38 9 47
115 $self->{debug} = $class->_trace_level_of($ENV{DBIO_TRACE})
116 47 ▶ if $ENV{DBIO_TRACE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DBIO_TRACE"} 1 46 47
117  
118 47 ▶ $self->pool_type($args->{pool_type} || 'DBIO::Replicated::Pool');
Condition50%1/2
$args->{'pool_type'} || 'DBIO::Replicated::Pool'
l!l
047
MC/DC0%0/1
$args->{'pool_type'} || 'DBIO::Replicated::Pool'
$args->{'pool_type'}
Truth table50%1/2
A
A= $args->{'pool_type'}
Aresult
11
00
119 47 ▶ $self->pool_args($args->{pool_args} || {});
Condition50%1/2
$args->{'pool_args'} || {}
l!l
047
MC/DC0%0/1
$args->{'pool_args'} || {}
$args->{'pool_args'}
Truth table50%1/2
A
A= $args->{'pool_args'}
Aresult
11
00
120 47 ▶ $self->balancer_type($class->_normalize_balancer_type($args->{balancer_type} || 'DBIO::Replicated::Balancer::First'));
Condition100%2/2
$args->{'balancer_type'} || 'DBIO::Replicated::Balancer::First'
l!l
2027
MC/DC100%1/1
$args->{'balancer_type'} || 'DBIO::Replicated::Balancer::First'
$args->{'balancer_type'}
Truth table100%2/2
A
A= $args->{'balancer_type'}
Aresult
11
00
121 47 ▶ $self->balancer_args($args->{balancer_args} || {});
Condition50%1/2
$args->{'balancer_args'} || {}
l!l
047
MC/DC0%0/1
$args->{'balancer_args'} || {}
$args->{'balancer_args'}
Truth table50%1/2
A
A= $args->{'balancer_args'}
Aresult
11
00
122 47 ▶ $self->backend_storage_class($args->{backend_storage_class} || 'DBIO::Storage::DBI');
Condition100%2/2
$args->{'backend_storage_class'} || 'DBIO::Storage::DBI'
l!l
443
MC/DC100%1/1
$args->{'backend_storage_class'} || 'DBIO::Storage::DBI'
$args->{'backend_storage_class'}
Truth table100%2/2
A
A= $args->{'backend_storage_class'}
Aresult
11
00
123 47 $self->_master_connect_info_opts({});
124  
125 47 return $self;
126 }
127  
128 47 ▶sub _normalize_balancer_type {
Subroutine _normalize_balancer_type: called CC=1 SCAR=0.0
129 47 my ($class, $type) = @_;
130 47 $type =~ s/\A::/DBIO::Replicated::Balancer::/;
131 47 return $type;
132 }
133  
134  
135 391 ▶sub master {
Subroutine master: called CC=3 SCAR=11.3
Pod documented
136 391 my $self = shift;
137 391 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 391 391
elsif (not $self->{'master'}) { } 45 346 391
138 0 $self->{master} = $_[0];
139 }
140 elsif (!$self->{master}) {
141 45 $self->{master} = $self->_build_master;
142 }
143 390 return $self->{master};
144 }
145  
146  
147 38 ▶sub pool {
Subroutine pool: called CC=3 SCAR=11.3
Pod documented
148 38 my $self = shift;
149 38 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 38 38
elsif (not $self->{'pool'}) { } 26 12 38
150 0 $self->{pool} = $_[0];
151 }
152 elsif (!$self->{pool}) {
153 26 $self->{pool} = $self->_build_pool;
154 }
155 38 return $self->{pool};
156 }
157  
158  
159 6 ▶sub balancer {
Subroutine balancer: called CC=3 SCAR=11.3
Pod documented
160 6 my $self = shift;
161 6 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 6 6
elsif (not $self->{'balancer'}) { } 5 1 6
162 0 $self->{balancer} = $_[0];
163 }
164 elsif (!$self->{balancer}) {
165 5 $self->{balancer} = $self->_build_balancer;
166 }
167 6 return $self->{balancer};
168 }
169  
170  
171 23 ▶sub read_handler {
Subroutine read_handler: called CC=3 SCAR=11.3
Pod documented
172 23 my $self = shift;
173 23 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 23 23
elsif (not $self->{'read_handler'}) { } 4 19 23
174 0 $self->{read_handler} = $_[0];
175 }
176 elsif (!$self->{read_handler}) {
177 4 $self->{read_handler} = $self->_build_read_handler;
178 }
179 23 return $self->{read_handler};
180 }
181  
182  
183 69 ▶sub write_handler {
Subroutine write_handler: called CC=3 SCAR=11.3
Pod documented
184 69 my $self = shift;
185 69 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 69 69
elsif (not $self->{'write_handler'}) { } 12 57 69
186 0 $self->{write_handler} = $_[0];
187 }
188 elsif (!$self->{write_handler}) {
189 12 $self->{write_handler} = $self->_build_write_handler;
190 }
191 69 return $self->{write_handler};
192 }
193  
194  
195 29 ▶sub create_pool {
Subroutine create_pool: called CC=1 SCAR=0.0
Pod documented
196 29 my ($self, %args) = @_;
197 29 my $type = $self->pool_type;
198 29 $self->ensure_class_loaded($type);
199 28 DBIO::Util::assert_class_isa(pool_type => $type, 'DBIO::Replicated::Pool');
200 27 return $type->new(%args);
201 }
202  
203 7 ▶sub create_balancer {
Subroutine create_balancer: called CC=1 SCAR=0.0
Pod documented
204 7 my ($self, %args) = @_;
205 7 my $type = $self->balancer_type;
206 7 $self->ensure_class_loaded($type);
207 7 DBIO::Util::assert_class_isa(balancer_type => $type, 'DBIO::Replicated::Balancer');
208 6 return $type->new(%args);
209 }
210  
211 45 ▶sub _build_master {
Subroutine _build_master: called CC=1 SCAR=0.0
212 45 my $self = shift;
213 45 my $storage_class = $self->backend_storage_class;
214 45 $self->ensure_class_loaded($storage_class);
215 45 DBIO::Util::assert_class_isa(
216 backend_storage_class => $storage_class, 'DBIO::Storage::DBI',
217 );
218  
219 44 my $storage = $storage_class->new($self->schema);
220  
221 44 require DBIO::Replicated::Backend::Master;
222 44 return DBIO::Replicated::Backend::Master->new(
223 storage => $storage,
224 );
225 }
226  
227 26 ▶sub _build_pool {
Subroutine _build_pool: called CC=2 SCAR=7.1
228 26 my $self = shift;
229 return $self->create_pool(
230 master => $self->master,
231 storage_class => $self->backend_storage_class,
232 26 ▶ %{ $self->pool_args || {} },
Condition50%1/2
$self->pool_args || {}
l!l
260
MC/DC0%0/1
$self->pool_args || {}
$self->pool_args
Truth table50%1/2
A
A= $self->pool_args
Aresult
11
00
233 );
234 }
235  
236 5 ▶sub _build_balancer {
Subroutine _build_balancer: called CC=2 SCAR=7.1
237 5 my $self = shift;
238 return $self->create_balancer(
239 pool => $self->pool,
240 master => $self->master,
241 5 ▶ %{ $self->balancer_args || {} },
Condition50%1/2
$self->balancer_args || {}
l!l
50
MC/DC0%0/1
$self->balancer_args || {}
$self->balancer_args
Truth table50%1/2
A
A= $self->balancer_args
Aresult
11
00
242 );
243 }
244  
245 12 ▶sub _build_write_handler { shift->master }
Subroutine _build_write_handler: called CC=1 SCAR=0.0
246 4 ▶sub _build_read_handler { shift->balancer }
Subroutine _build_read_handler: called CC=1 SCAR=0.0
247  
248 58 ▶sub connect_info {
Subroutine connect_info: called CC=12 SCAR=32.6
Pod documented
249 58 my ($self, $info, @extra) = @_;
250  
251 58 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info 0 58 58
252 'connect_info can not be retrieved from a replicated storage - accessor must be called on a specific backend instance'
253 ) unless defined $info;
254  
255 58 my ($filtered_info, $opts) = $self->_parse_connect_info($info);
256  
257 58 ▶ if (exists $opts->{pool_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"pool_type"}) 0 58 58
258 0 $self->pool_type($opts->{pool_type});
259 0 delete $self->{pool};
260 }
261  
262 58 ▶ if (exists $opts->{pool_args}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"pool_args"}) 0 58 58
263 0 ▶ $self->pool_args($self->_merge_hashrefs($opts->{pool_args}, $self->pool_args || {}));
Condition0%0/2
$self->pool_args || {}
l!l
00
MC/DC0%0/1
$self->pool_args || {}
$self->pool_args
Truth table0%0/2
A
A= $self->pool_args
Aresult
11
00
264 0 delete $self->{pool};
265 }
266  
267 58 ▶ if (exists $opts->{balancer_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"balancer_type"}) 0 58 58
268 0 $self->balancer_type($self->_normalize_balancer_type($opts->{balancer_type}));
269 0 delete $self->{balancer};
270 0 delete $self->{read_handler};
271 }
272  
273 58 ▶ if (exists $opts->{balancer_args}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"balancer_args"}) 0 58 58
274 0 ▶ $self->balancer_args($self->_merge_hashrefs($opts->{balancer_args}, $self->balancer_args || {}));
Condition0%0/2
$self->balancer_args || {}
l!l
00
MC/DC0%0/1
$self->balancer_args || {}
$self->balancer_args
Truth table0%0/2
A
A= $self->balancer_args
Aresult
11
00
275 0 delete $self->{balancer};
276 0 delete $self->{read_handler};
277 }
278  
279 58 ▶ if (exists $opts->{backend_storage_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"backend_storage_class"}) 6 52 58
280 6 $self->backend_storage_class($opts->{backend_storage_class});
281 6 delete @{$self}{qw(master pool balancer read_handler write_handler)};
282 }
283  
284 58 ▶ $self->_master_connect_info_opts($opts->{master_connect_opts} || {});
Condition50%1/2
$opts->{'master_connect_opts'} || {}
l!l
580
MC/DC0%0/1
$opts->{'master_connect_opts'} || {}
$opts->{'master_connect_opts'}
Truth table50%1/2
A
A= $opts->{'master_connect_opts'}
Aresult
11
00
285  
286 58 my $rv = $self->master->connect_info($filtered_info, @extra);
287 58 ▶ $self->master->storage->_determine_driver if $self->master->storage->can('_determine_driver');
Branch: 1 branch
BranchTrueFalseTotal
if $self->master->storage->can("_determine_driver") 58 0 58
288 58 $self->master->install_debug_proxy($self->master->storage->debugobj);
289  
290 58 ▶ if ($self->{pool}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pool"}) 0 58 58
291 0 $self->pool->master($self->master);
292 }
293  
294 58 return $rv;
295 }
296  
297  
298 27 ▶sub connect_replicants {
Subroutine connect_replicants: called CC=1 SCAR=0.0
Pod documented
299 27 my ($self, @replicants) = @_;
300 27 my @merged = map { $self->_merge_replicant_connect_info($_) } @replicants;
301 25 return $self->pool->connect_replicants($self->schema, @merged);
302 }
303  
304  
305  
306 4 ▶sub replicants {
Subroutine replicants: called CC=1 SCAR=0.0
Pod documented
307 4 my $self = shift;
308 4 return $self->pool->replicants;
309 }
310  
311 0 ▶sub has_replicants {
Subroutine has_replicants: not called CC=1 SCAR=6.9
Pod documented
312 0 my $self = shift;
313 0 return $self->pool->has_replicants;
314 }
315  
316 3 ▶sub all_storages {
Subroutine all_storages: called CC=2 SCAR=7.4
Pod documented
317 3 my $self = shift;
318 3 ▶ return grep { defined $_ && blessed $_ } ($self->master, values %{ $self->replicants });
Condition33%1/3
defined $_ && builtin::blessed($_)
!ll&&!rl&&r
007
MC/DC0%0/2
defined $_ && builtin::blessed($_)
defined $_ builtin::blessed($_)
Truth table33%1/3
A && B
A= defined $_B= builtin::blessed($_)
ABresult
0X0
100
111
319 }
320  
321 2 ▶sub execute_reliably {
Subroutine execute_reliably: called CC=2 SCAR=7.0
Pod documented
322 2 my ($self, $coderef, @args) = @_;
323 2 ▶ $self->throw_exception('Second argument must be a coderef')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $coderef eq "CODE" 0 2 2
324 unless ref $coderef eq 'CODE';
325  
326 2 local $self->{read_handler} = $self->master;
327 2 return $coderef->(@args);
328 }
329  
330  
331 # The lookup of find_or_create, find_or_new, update_or_create, update_or_new
332 # and the upsert fallback reads where its write goes (DBIO::Storage).
333 6 ▶sub _read_pool_for_read_modify_write { 'master' }
Subroutine _read_pool_for_read_modify_write: called CC=1 SCAR=0.0
334  
335 0 ▶sub set_reliable_storage {
Subroutine set_reliable_storage: not called CC=1 SCAR=6.9
Pod documented
336 0 my $self = shift;
337 0 $self->read_handler($self->write_handler);
338 }
339  
340  
341 0 ▶sub set_balanced_storage {
Subroutine set_balanced_storage: not called CC=1 SCAR=6.9
Pod documented
342 0 my $self = shift;
343 0 $self->read_handler($self->balancer);
344 }
345  
346  
347  
348 0 ▶sub connected {
Subroutine connected: not called CC=2 SCAR=17.9
Pod documented
349 0 my $self = shift;
350 0 ▶ return $self->master->connected && $self->pool->connected_replicants;
Condition0%0/3
$self->master->connected && $self->pool->connected_replicants
!ll&&!rl&&r
000
MC/DC0%0/2
$self->master->connected && $self->pool->connected_replicants
$self->master->connected $self->pool->connected_replicants
Truth table0%0/3
A && B
A= $self->master->connectedB= $self->pool->connected_replicants
ABresult
0X0
100
111
351 }
352  
353 1 ▶sub ensure_connected {
Subroutine ensure_connected: called CC=2 SCAR=6.9
Pod documented
354 1 my $self = shift;
355 1 $_->ensure_connected(@_) for $self->all_storages;
356 }
357  
358 0 ▶sub set_schema {
Subroutine set_schema: not called CC=2 SCAR=17.9
Pod documented
359 0 my ($self, @args) = @_;
360 0 $self->SUPER::set_schema(@args);
361 0 $_->set_schema(@args) for $self->all_storages;
362 }
363  
364  
365 0 ▶sub debug {
Subroutine debug: not called CC=3 SCAR=24.8
Pod documented
366 0 my $self = shift;
367 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
368 0 $_->debug(@_) for $self->all_storages;
369 }
370 0 return $self->master->debug;
371 }
372  
373 0 ▶sub debugobj {
Subroutine debugobj: not called CC=1 SCAR=6.9
Pod documented
374 0 my $self = shift;
375 0 return $self->master->debugobj(@_);
376 }
377  
378 0 ▶sub debugfh {
Subroutine debugfh: not called CC=1 SCAR=6.9
Pod documented
379 0 my $self = shift;
380 0 return $self->master->debugfh(@_);
381 }
382  
383 0 ▶sub debugcb {
Subroutine debugcb: not called CC=1 SCAR=6.9
Pod documented
384 0 my $self = shift;
385 0 return $self->master->debugcb(@_);
386 }
387  
388 1 ▶sub disconnect {
Subroutine disconnect: called CC=2 SCAR=6.9
Pod documented
389 1 my $self = shift;
390 1 $_->disconnect(@_) for $self->all_storages;
391 }
392  
393 0 ▶sub cursor_class {
Subroutine cursor_class: not called CC=3 SCAR=24.8
Pod documented
394 0 my ($self, $cursor_class) = @_;
395 0 ▶ if ($cursor_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor_class) 0 0 0
396 0 $_->cursor_class($cursor_class) for $self->all_storages;
397 }
398 0 return $self->master->cursor_class;
399 }
400  
401 0 ▶sub cursor {
Subroutine cursor: not called CC=3 SCAR=24.8
Pod documented
402 0 my ($self, $cursor_class) = @_;
403 0 ▶ if ($cursor_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cursor_class) 0 0 0
404 0 $_->cursor($cursor_class) for $self->all_storages;
405 }
406 0 return $self->master->cursor;
407 }
408  
409 0 ▶sub unsafe {
Subroutine unsafe: not called CC=3 SCAR=24.8
Pod documented
410 0 my $self = shift;
411 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
412 0 $_->unsafe(@_) for $self->all_storages;
413 }
414 0 return $self->master->unsafe;
415 }
416  
417 0 ▶sub disable_sth_caching {
Subroutine disable_sth_caching: not called CC=3 SCAR=24.8
Pod documented
418 0 my $self = shift;
419 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
420 0 $_->disable_sth_caching(@_) for $self->all_storages;
421 }
422 0 return $self->master->disable_sth_caching;
423 }
424  
425  
426 0 ▶sub lag_behind_master {
Subroutine lag_behind_master: not called CC=2 SCAR=17.9
Pod documented
427 0 my $self = shift;
428 0 my @replicants = $self->pool->all_replicants;
429 0 ▶ return unless @replicants;
Branch: 1 branch
BranchTrueFalseTotal
unless @replicants 0 0 0
430 0 return List::Util::max(map { $_->lag_behind_master } @replicants);
431 }
432  
433 0 ▶sub is_replicating {
Subroutine is_replicating: not called CC=2 SCAR=17.9
Pod documented
434 0 my $self = shift;
435 0 my @replicants = $self->pool->all_replicants;
436 0 ▶ return 0 unless @replicants;
Branch: 1 branch
BranchTrueFalseTotal
unless @replicants 0 0 0
437 0 return (grep { $_->is_replicating } @replicants) == @replicants;
438 }
439  
440 0 ▶sub connect_call_datetime_setup {
Subroutine connect_call_datetime_setup: not called CC=2 SCAR=17.9
Pod documented
441 0 my $self = shift;
442 0 $_->connect_call_datetime_setup(@_) for $self->all_storages;
443 }
444  
445 0 ▶sub connect_call_rebase_sqlmaker {
Subroutine connect_call_rebase_sqlmaker: not called CC=2 SCAR=17.9
Pod documented
446 0 my ($self, @args) = @_;
447 0 $_->connect_call_rebase_sqlmaker(@args) for $self->all_storages;
448 }
449  
450 0 ▶sub _populate_dbh {
Subroutine _populate_dbh: not called CC=2 SCAR=17.9
451 0 my $self = shift;
452 0 $_->_populate_dbh(@_) for $self->all_storages;
453 }
454  
455 0 ▶sub _connect {
Subroutine _connect: not called CC=2 SCAR=17.9
456 0 my $self = shift;
457 0 $_->_connect(@_) for $self->all_storages;
458 }
459  
460 1 ▶sub _determine_driver {
Subroutine _determine_driver: called CC=2 SCAR=6.9
461 1 my $self = shift;
462 1 $_->_determine_driver(@_) for $self->all_storages;
463  
464 # ... and record it on THIS object too. The parameterized around at the top
465 # of DBIO::Storage::DBI guards on the raw hash slot ($_[0]{_driver_determined},
466 # not the accessor) and, when it is false, calls _determine_driver and then
467 # re-enters the wrapped method with goto. Our _driver_determined accessor
468 # forwards to the backends and never touches this object's slot, so the guard
469 # stayed false forever and the wrapped method span in an unbounded goto loop
470 # (constant stack, no recursion warning) the moment it was called on a
471 # replicated storage with connect info. DBIO::Test::Storage sets the same raw
472 # slot for the same reason.
473 #
474 # Delegating a wrapped method takes it out of the loop's reach --
475 # the glob installed at the bottom of this file overrides the around, so
476 # sqlt_type, the capability entry points and friends never enter it on this
477 # invocant (the master runs its own around). The marker is still load-bearing
478 # for the methods on that list which are *not* delegated: cursor_class (every
479 # ResultSet asks the schema storage for it) and deployment_statements.
480 1 $self->{_driver_determined} = 1;
481 }
482  
483 0 ▶sub _driver_determined {
Subroutine _driver_determined: not called CC=3 SCAR=24.8
484 0 my $self = shift;
485 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
486 0 $_->_driver_determined(@_) for $self->all_storages;
487 }
488 0 return $self->master->_driver_determined;
489 }
490  
491 0 ▶sub _init {
Subroutine _init: not called CC=2 SCAR=17.9
492 0 my $self = shift;
493 0 $_->_init(@_) for $self->all_storages;
494 }
495  
496 0 ▶sub _run_connection_actions {
Subroutine _run_connection_actions: not called CC=2 SCAR=17.9
497 0 my $self = shift;
498 0 $_->_run_connection_actions(@_) for $self->all_storages;
499 }
500  
501 0 ▶sub _do_connection_actions {
Subroutine _do_connection_actions: not called CC=3 SCAR=24.8
502 0 my $self = shift;
503 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
504 0 $_->_do_connection_actions(@_) for $self->all_storages;
505 }
506 }
507  
508 59 ▶sub _parse_connect_info {
Subroutine _parse_connect_info: called CC=11 SCAR=24.2
509 59 my ($self, $info) = @_;
510  
511 59 my %replicated_opts;
512 my %master_opts;
513 59 my @filtered;
514  
515 59 ▶ foreach my $arg (@{$info || []}) {
Condition50%1/2
$info || []
l!l
590
MC/DC0%0/1
$info || []
$info
Truth table50%1/2
A
A= $info
Aresult
11
00
516 # An AccessBroker is a CredentialSource, not an options hash. It is a
517 # blessed object whose reftype happens to be HASH, so it must be passed
518 # through untouched — never copied/merged, or its guts get splatted into
519 # master_opts. The master Storage::DBI detects and consumes it downstream.
520 183 ▶ if (DBIO::Util::is_access_broker($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&DBIO::Util::is_access_broker($arg)) 9 174 183
521 9 push @filtered, $arg;
522 9 next;
523 }
524 174 ▶ if ((reftype($arg) || '') eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($arg) || '') eq 'HASH') { } 43 131 174
Condition100%2/2
builtin::reftype($arg) || ''
l!l
44130
MC/DC100%1/1
builtin::reftype($arg) || ''
builtin::reftype($arg)
Truth table100%2/2
A
A= builtin::reftype($arg)
Aresult
11
00
525 43 my %copy = %$arg;
526  
527 43 for my $key (qw(pool_type balancer_type backend_storage_class)) {
528 129 ▶ $replicated_opts{$key} = delete $copy{$key} if exists $copy{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $copy{$key} 6 123 129
529 }
530  
531 43 for my $key (qw(pool_args balancer_args)) {
532 86 ▶ if (exists $copy{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $copy{$key}) 0 86 86
533 0 ▶ $replicated_opts{$key} = $self->_merge_hashrefs(delete $copy{$key}, $replicated_opts{$key} || {});
Condition0%0/2
$replicated_opts{$key} || {}
l!l
00
MC/DC0%0/1
$replicated_opts{$key} || {}
$replicated_opts{$key}
Truth table0%0/2
A
A= $replicated_opts{$key}
Aresult
11
00
534 }
535 }
536  
537 # What is kept here is every replicant's defaults. Of the keys naming
538 # the connection itself, none that names the master's server passes
539 # on -- a replicant is a different server, and given a dsn of its own
540 # it would take the master's dbname, host and port as DBI attributes
541 # (karr k248). The master's credentials stay: a hash-form replicant
542 # naming none inherits them (_merge_replicant_connect_info decides).
543 43 %master_opts = %{ $self->_merge_hashrefs(\%copy, \%master_opts) };
544 43 my %credential = map { $_ => 1 } $self->_replicant_inherited_credential_keys;
545 43 delete @master_opts{ grep { !$credential{$_} } $self->_access_broker_identity_keys };
546  
547 43 push @filtered, \%copy;
548 }
549 else {
550 131 push @filtered, $arg;
551 }
552 }
553  
554 59 $replicated_opts{master_connect_opts} = \%master_opts;
555 59 return (\@filtered, \%replicated_opts);
556 }
557  
558 49 ▶sub _merge_replicant_connect_info {
Subroutine _merge_replicant_connect_info: called CC=22 SCAR=31.2
559 49 my ($self, $connect_info) = @_;
560 49 ▶ $connect_info = [ $connect_info ] if reftype($connect_info) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($connect_info) ne "ARRAY" 7 42 49
561  
562 # A broker-backed replicant ([ $broker ] or [ $broker, \%options ]): the
563 # broker is a CredentialSource, not an options hash, and passes through by
564 # identity -- merged into a hash, the blessed object would be shredded. The
565 # master's connect options go beside it, where the replicant Storage::DBI
566 # reads them (ADR 0030 addendum II, karr k243/k246), under the replicant's
567 # own -- the same defaults a DSN replicant gets below. Those naming the
568 # connection itself stay behind: which database, and as whom, is this
569 # broker's answer.
570 49 ▶ if (DBIO::Util::is_access_broker($connect_info->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&DBIO::Util::is_access_broker($connect_info->[0])) 19 30 49
571 19 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_access_broker_connect_info($connect_info) 2 17 19
572 'A broker-backed replicant takes [ $broker ] or [ $broker, \%options ]'
573 ) unless $self->_is_access_broker_connect_info($connect_info);
574  
575 17 ▶ my %master_opts = %{ $self->_master_connect_info_opts || {} };
Condition50%1/2
$self->_master_connect_info_opts || {}
l!l
170
MC/DC0%0/1
$self->_master_connect_info_opts || {}
$self->_master_connect_info_opts
Truth table50%1/2
A
A= $self->_master_connect_info_opts
Aresult
11
00
576 17 delete @master_opts{ $self->_access_broker_identity_keys };
577  
578 17 my $opts = $self->_merge_hashrefs($connect_info->[1], \%master_opts);
579 17 ▶ return %$opts ? [ $connect_info->[0], $opts ] : [ $connect_info->[0] ];
Branch: 1 branch
BranchTrueFalseTotal
%$opts ? : 5 12 17
580 }
581  
582 30 ▶ $self->throw_exception('coderef replicant connect_info not supported')
Branch: 1 branch
BranchTrueFalseTotal
if ref $connect_info->[0] and builtin::reftype($connect_info->[0]) eq "CODE" 0 30 30
Condition66%2/3
ref $connect_info->[0] and builtin::reftype($connect_info->[0]) eq "CODE"
!ll&&!rl&&r
2370
MC/DC0%0/2
ref $connect_info->[0] and builtin::reftype($connect_info->[0]) eq "CODE"
ref $connect_info->[0] builtin::reftype($connect_info->[0]) eq "CODE"
Truth table66%2/3
A and B
A= ref $connect_info->[0]B= builtin::reftype($connect_info->[0]) eq "CODE"
ABresult
0X0
100
111
583 if ref $connect_info->[0] && reftype($connect_info->[0]) eq 'CODE';
584  
585 30 my @copy = @$connect_info;
586 30 my $i = 0;
587 30 ▶ $i++ while $i < @copy && (reftype($copy[$i]) || '') ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
$i < @copy and (builtin::reftype($copy[$i]) || "") ne "HASH" and do { ++$i } 60 30 90
Condition100%5/5
$i < @copy and (builtin::reftype($copy[$i]) || "") ne "HASH"
!ll&&!rl&&r
111960
builtin::reftype($copy[$i]) || ''
l!l
1960
MC/DC100%2/2
$i < @copy and (builtin::reftype($copy[$i]) || "") ne "HASH"
$i < @copy builtin::reftype($copy[$i])
Truth table100%3/3
A and B
A= $i < @copyB= builtin::reftype($copy[$i])
ABresult
0X0
100
111
588 # A DBI-form replicant without an options hash gets one in the attributes
589 # position, after its dsn, user and password, given or not. Appended to a
590 # short [ $dsn ] or [ $dsn, $user ], it took the place of a credential, and
591 # the master's options -- a hash-form master's password among them -- went
592 # to DBI->connect as the replicant's user or password (karr k248).
593 30 ▶ $i = 3 if $i == @copy && $i > 0 && $i < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $i == @copy and $i > 0 and $i < 3 5 25 30
Condition83%5/6
$i == @copy and $i > 0 and $i < 3
!ll&&!rl&&r
1965
$i == @copy and $i > 0
!ll&&!rl&&r
19011
MC/DC66%2/3
$i == @copy and $i > 0 and $i < 3
$i == @copy $i > 0 $i < 3
Truth table75%3/4
A and B and C
A= $i == @copyB= $i > 0C= $i < 3
ABCresult
0XX0
10X0
1100
1111
594 30 ▶ $copy[$i] = {} unless $copy[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $copy[$i] 11 19 30
595  
596 30 my @hashes = @copy[$i .. $#copy];
597 $self->throw_exception('invalid connect_info options')
598 30 ▶ if grep { (reftype($_) || '') ne 'HASH' } @hashes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {(builtin::reftype($_) || "") ne "HASH";} @hashes 0 30 30
Condition50%1/2
builtin::reftype($_) || ''
l!l
300
MC/DC0%0/1
builtin::reftype($_) || ''
builtin::reftype($_)
Truth table50%1/2
A
A= builtin::reftype($_)
Aresult
11
00
599 30 ▶ $self->throw_exception('too many hashrefs in connect_info')
Branch: 1 branch
BranchTrueFalseTotal
if @hashes > 2 0 30 30
600 if @hashes > 2;
601  
602 30 my %opts;
603 30 for my $hash (reverse @hashes) {
604 30 %opts = %{ $self->_merge_hashrefs($hash, \%opts) };
605 }
606  
607 30 splice @copy, $i + 1, $#copy - $i, ();
608  
609 # The master's options, under the replicant's own. Nothing naming the
610 # master's server is among them (_parse_connect_info); its user and
611 # password are, and where they go depends on the replicant's form. Given
612 # in hash form ({ dsn => ... }), this hash is read for its credentials: one
613 # naming none inherits the master's -- unless it brings its own dbh_maker,
614 # which takes none. Given in DBI form ([ $dsn, $user, $pass, \%attrs ]),
615 # its credentials are positional and this hash is its DBI attributes: the
616 # master's password would reach its DBI->connect as one (karr k248).
617 30 my $positional = $i > 0;
618 30 ▶ my %master_opts = %{ $self->_master_connect_info_opts || {} };
Condition50%1/2
$self->_master_connect_info_opts || {}
l!l
300
MC/DC0%0/1
$self->_master_connect_info_opts || {}
$self->_master_connect_info_opts
Truth table50%1/2
A
A= $self->_master_connect_info_opts
Aresult
11
00
619 delete @master_opts{ $self->_replicant_inherited_credential_keys }
620 30 ▶ if $positional || exists $opts{dbh_maker};
Branch: 1 branch
BranchTrueFalseTotal
if $positional or exists $opts{"dbh_maker"} 24 6 30
Condition100%3/3
$positional or exists $opts{"dbh_maker"}
l!l&&r!l&&!r
2316
MC/DC100%2/2
$positional or exists $opts{"dbh_maker"}
$positional exists $opts{"dbh_maker"}
Truth table100%3/3
A or B
A= $positionalB= exists $opts{"dbh_maker"}
ABresult
1X1
011
000
621  
622 30 %opts = %{ $self->_merge_hashrefs(\%opts, \%master_opts) };
623 30 $copy[$i] = \%opts;
624  
625 30 return \@copy;
626 }
627  
628 # The keys naming the master's connection that a replicant may inherit: its
629 # credentials, and only into a hash-form replicant's hash. Every other key of
630 # _access_broker_identity_keys stays the master's.
631 67 ▶sub _replicant_inherited_credential_keys { qw( user password ) }
Subroutine _replicant_inherited_credential_keys: called CC=1 SCAR=0.0
632  
633 120 ▶sub _merge_hashrefs {
Subroutine _merge_hashrefs: called CC=6 SCAR=18.8
634 120 my ($self, $left, $right) = @_;
635 120 ▶ $left ||= {};
Condition100%2/2
$left ||= {}
l!l
10416
MC/DC100%1/1
$left ||= {}
$left
Truth table100%2/2
A
A= $left
Aresult
11
00
636 120 ▶ $right ||= {};
Condition50%1/2
$right ||= {}
l!l
1200
MC/DC0%0/1
$right ||= {}
$right
Truth table50%1/2
A
A= $right
Aresult
11
00
637  
638 120 my %merged = %{$right};
639 120 for my $key (keys %{$left}) {
640 127 ▶ if (ref($left->{$key}) eq 'HASH' && ref($merged{$key}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $left->{$key} eq 'HASH' and ref $merged{$key} eq 'HASH') { } 0 127 127
Condition33%1/3
ref $left->{$key} eq "HASH" and ref $merged{$key} eq "HASH"
!ll&&!rl&&r
12700
MC/DC0%0/2
ref $left->{$key} eq "HASH" and ref $merged{$key} eq "HASH"
ref $left->{$key} eq "HASH" ref $merged{$key} eq "HASH"
Truth table33%1/3
A and B
A= ref $left->{$key} eq "HASH"B= ref $merged{$key} eq "HASH"
ABresult
0X0
100
111
641 0 $merged{$key} = $self->_merge_hashrefs($left->{$key}, $merged{$key});
642 }
643 else {
644 127 $merged{$key} = $left->{$key};
645 }
646 }
647  
648 120 return \%merged;
649 }
650  
651  
652 2 for my $method (@writer_methods) {
653 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
654 106 my $orig = __PACKAGE__->can($method);
655 106 ▶ *{__PACKAGE__ . "::$method"} = set_subname $method => sub {
Subroutine txn_do: not called CC=3 SCAR=13.1
Subroutine get_dbms_capability: not called CC=3 SCAR=13.1
Subroutine _get_dbh: called CC=3 SCAR=13.1
Subroutine _server_info: called CC=3 SCAR=13.1
Subroutine sql_maker_class: called CC=3 SCAR=13.1
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
656 0 my $self = shift;
657 0 ▶ return $orig->($self, @_) if !ref $self && $orig;
Branch: 5 branches
BranchTrueFalseTotal
if not ref $self and $orig 0 0 0
if not ref $self and $orig 0 0 0
if not ref $self and $orig 0 1 1
if not ref $self and $orig 0 7 7
if not ref $self and $orig 0 61 61
Condition20%3/15
not ref $self and $orig
!ll&&!rl&&r
000
not ref $self and $orig
!ll&&!rl&&r
000
not ref $self and $orig
!ll&&!rl&&r
100
not ref $self and $orig
!ll&&!rl&&r
700
not ref $self and $orig
!ll&&!rl&&r
6100
MC/DC0%0/2
not ref $self and $orig
not ref $self $orig
MC/DC0%0/2
not ref $self and $orig
not ref $self $orig
MC/DC0%0/2
not ref $self and $orig
not ref $self $orig
MC/DC0%0/2
not ref $self and $orig
not ref $self $orig
MC/DC0%0/2
not ref $self and $orig
not ref $self $orig
Truth table0%0/3
not(A) and B
A= not ref $selfB= $orig
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not ref $selfB= $orig
ABresult
0X0
100
111
Truth table33%1/3
not(A) and B
A= not ref $selfB= $orig
ABresult
0X0
100
111
Truth table33%1/3
not(A) and B
A= not ref $selfB= $orig
ABresult
0X0
100
111
Truth table33%1/3
not(A) and B
A= not ref $selfB= $orig
ABresult
0X0
100
111
658 0 my $handler = $self->write_handler;
659 0 return $handler->$method(@_);
660 106 };
661 }
662  
663 2 for my $method (@reader_methods) {
664 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
665 16 my $orig = __PACKAGE__->can($method);
666 16 ▶ *{__PACKAGE__ . "::$method"} = set_subname $method => sub {
Subroutine _select: not called CC=3 SCAR=14.7
Subroutine __ANON__: not called CC=3 SCAR=14.7
Subroutine select: called CC=3 SCAR=14.7
Subroutine _explain_query: not called CC=3 SCAR=14.7
Pod documented
667 0 my $self = shift;
668 0 ▶ return $orig->($self, @_) if !ref $self && $orig;
Branch: 4 branches
BranchTrueFalseTotal
if not ref $self and $orig 0 0 0
if not ref $self and $orig 0 0 0
if not ref $self and $orig 0 23 23
if not ref $self and $orig 0 0 0
Condition8%1/12
not ref $self and $orig
!ll&&!rl&&r
000
not ref $self and $orig
!ll&&!rl&&r
000
not ref $self and $orig
!ll&&!rl&&r
2300
not ref $self and $orig
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $self and $orig
not ref $self $orig
MC/DC0%0/2
not ref $self and $orig
not ref $self $orig
MC/DC0%0/2
not ref $self and $orig
not ref $self $orig
MC/DC0%0/2
not ref $self and $orig
not ref $self $orig
Truth table0%0/3
not(A) and B
A= not ref $selfB= $orig
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not ref $selfB= $orig
ABresult
0X0
100
111
Truth table33%1/3
not(A) and B
A= not ref $selfB= $orig
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not ref $selfB= $orig
ABresult
0X0
100
111
669 0 my $handler = $self->read_handler;
670 0 return $handler->$method(@_);
671 16 };
672 }
673  
674 2 1;
675  
676 __END__
« blib/lib/DBIO/Test/Util/OverrideRequire.pm ↑ Summary blib/lib/DBIO/ResultSourceProxy.pm »