DBIx/Class/ResultSet.pm

filter: Statementstmt 92.3% 984 / 1066 Branchbran 82.7% 531 / 642 Conditioncond 72.4% 329 / 454 MC/DCmcdc 45.9% 118 / 257 Subroutinesub 92.2% 83 / 90 Podpod 95.5% 43 / 45 totaltotal 81.7% 2088 / 2554 CC 537 SCAR 36.4
CC
537
Cov
85%
populate
100.5
_resolved_attrs
74.4
_construct_results
72.0
CRAP 38.1
« DBIx/Class/Storage/DBIHacks.pm ↑ Summary DBIx/Class/Schema/Versioned.pm »
1 package DBIx::Class::ResultSet;
2  
3 379 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 379 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 379 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 379 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 379 ▶use DBIx::Class::ResultSetColumn;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 379 ▶use Scalar::Util qw/blessed weaken reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 379 ▶use DBIx::Class::_Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 fail_on_internal_wantarray fail_on_internal_call UNRESOLVABLE_CONDITION
11 379 );
12 379 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 379 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 # De-duplication in _merge_attr() is disabled, but left in for reference
16 # (the merger is used for other things that ought not to be de-duped)
17 379 *__HM_DEDUP = sub () { 0 };
18 }
19  
20 # FIXME - get rid of this
21 379 ▶use Hash::Merge ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 379 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 use overload
26 379 ▶ '0+' => "count",
Subroutine BEGIN: called CC=1 SCAR=0.0
27 'bool' => "_bool",
28 379 fallback => 1;
29  
30 # this is real - CDBICompat overrides it with insanity
31 # yes, prototype won't matter, but that's for now ;)
32 sub _bool () { 1 }
33  
34 379 __PACKAGE__->mk_group_accessors('simple' => qw/_result_class result_source/);
35  
36 =head1 NAME
37  
38 DBIx::Class::ResultSet - Represents a query used for fetching a set of results.
39  
40 =head1 SYNOPSIS
41  
42 my $users_rs = $schema->resultset('User');
43 while( $user = $users_rs->next) {
44 print $user->username;
45 }
46  
47 my $registered_users_rs = $schema->resultset('User')->search({ registered => 1 });
48 my @cds_in_2005 = $schema->resultset('CD')->search({ year => 2005 })->all();
49  
50 =head1 DESCRIPTION
51  
52 A ResultSet is an object which stores a set of conditions representing
53 a query. It is the backbone of DBIx::Class (i.e. the really
54 important/useful bit).
55  
56 No SQL is executed on the database when a ResultSet is created, it
57 just stores all the conditions needed to create the query.
58  
59 A basic ResultSet representing the data of an entire table is returned
60 by calling C<resultset> on a L<DBIx::Class::Schema> and passing in a
61 L<Source|DBIx::Class::Manual::Glossary/ResultSource> name.
62  
63 my $users_rs = $schema->resultset('User');
64  
65 A new ResultSet is returned from calling L</search> on an existing
66 ResultSet. The new one will contain all the conditions of the
67 original, plus any new conditions added in the C<search> call.
68  
69 A ResultSet also incorporates an implicit iterator. L</next> and L</reset>
70 can be used to walk through all the L<DBIx::Class::Row>s the ResultSet
71 represents.
72  
73 The query that the ResultSet represents is B<only> executed against
74 the database when these methods are called:
75 L</find>, L</next>, L</all>, L</first>, L</single>, L</count>.
76  
77 If a resultset is used in a numeric context it returns the L</count>.
78 However, if it is used in a boolean context it is B<always> true. So if
79 you want to check if a resultset has any results, you must use C<if $rs
80 != 0>.
81  
82 =head1 EXAMPLES
83  
84 =head2 Chaining resultsets
85  
86 Let's say you've got a query that needs to be run to return some data
87 to the user. But, you have an authorization system in place that
88 prevents certain users from seeing certain information. So, you want
89 to construct the basic query in one method, but add constraints to it in
90 another.
91  
92 sub get_data {
93 my $self = shift;
94 my $request = $self->get_request; # Get a request object somehow.
95 my $schema = $self->result_source->schema;
96  
97 my $cd_rs = $schema->resultset('CD')->search({
98 title => $request->param('title'),
99 year => $request->param('year'),
100 });
101  
102 $cd_rs = $self->apply_security_policy( $cd_rs );
103  
104 return $cd_rs->all();
105 }
106  
107 sub apply_security_policy {
108 my $self = shift;
109 my ($rs) = @_;
110  
111 return $rs->search({
112 subversive => 0,
113 });
114 }
115  
116 =head3 Resolving conditions and attributes
117  
118 When a resultset is chained from another resultset (e.g.:
119 C<< my $new_rs = $old_rs->search(\%extra_cond, \%attrs) >>), conditions
120 and attributes with the same keys need resolving.
121  
122 If any of L</columns>, L</select>, L</as> are present, they reset the
123 original selection, and start the selection "clean".
124  
125 The L</join>, L</prefetch>, L</+columns>, L</+select>, L</+as> attributes
126 are merged into the existing ones from the original resultset.
127  
128 The L</where> and L</having> attributes, and any search conditions, are
129 merged with an SQL C<AND> to the existing condition from the original
130 resultset.
131  
132 All other attributes are overridden by any new ones supplied in the
133 search attributes.
134  
135 =head2 Multiple queries
136  
137 Since a resultset just defines a query, you can do all sorts of
138 things with it with the same object.
139  
140 # Don't hit the DB yet.
141 my $cd_rs = $schema->resultset('CD')->search({
142 title => 'something',
143 year => 2009,
144 });
145  
146 # Each of these hits the DB individually.
147 my $count = $cd_rs->count;
148 my $most_recent = $cd_rs->get_column('date_released')->max();
149 my @records = $cd_rs->all;
150  
151 And it's not just limited to SELECT statements.
152  
153 $cd_rs->delete();
154  
155 This is even cooler:
156  
157 $cd_rs->create({ artist => 'Fred' });
158  
159 Which is the same as:
160  
161 $schema->resultset('CD')->create({
162 title => 'something',
163 year => 2009,
164 artist => 'Fred'
165 });
166  
167 See: L</search>, L</count>, L</get_column>, L</all>, L</create>.
168  
169 =head2 Custom ResultSet classes
170  
171 To add methods to your resultsets, you can subclass L<DBIx::Class::ResultSet>, similar to:
172  
173 package MyApp::Schema::ResultSet::User;
174  
175 use strict;
176 use warnings;
177  
178 use base 'DBIx::Class::ResultSet';
179  
180 sub active {
181 my $self = shift;
182 $self->search({ $self->current_source_alias . '.active' => 1 });
183 }
184  
185 sub unverified {
186 my $self = shift;
187 $self->search({ $self->current_source_alias . '.verified' => 0 });
188 }
189  
190 sub created_n_days_ago {
191 my ($self, $days_ago) = @_;
192 $self->search({
193 $self->current_source_alias . '.create_date' => {
194 '<=',
195 $self->result_source->schema->storage->datetime_parser->format_datetime(
196 DateTime->now( time_zone => 'UTC' )->subtract( days => $days_ago )
197 )}
198 });
199 }
200  
201 sub users_to_warn { shift->active->unverified->created_n_days_ago(7) }
202  
203 1;
204  
205 See L<DBIx::Class::Schema/load_namespaces> on how DBIC can discover and
206 automatically attach L<Result|DBIx::Class::Manual::ResultClass>-specific
207 L<ResulSet|DBIx::Class::ResultSet> classes.
208  
209 =head3 ResultSet subclassing with Moose and similar constructor-providers
210  
211 Using L<Moose> or L<Moo> in your ResultSet classes is usually overkill, but
212 you may find it useful if your ResultSets contain a lot of business logic
213 (e.g. C<has xml_parser>, C<has json>, etc) or if you just prefer to organize
214 your code via roles.
215  
216 In order to write custom ResultSet classes with L<Moo> you need to use the
217 following template. The L<BUILDARGS|Moo/BUILDARGS> is necessary due to the
218 unusual signature of the L<constructor provided by DBIC
219 |DBIx::Class::ResultSet/new> C<< ->new($source, \%args) >>.
220  
221 use Moo;
222 extends 'DBIx::Class::ResultSet';
223 sub BUILDARGS { $_[2] || {} } # ::RS::new() expects my ($class, $rsrc, $args) = @_
224  
225 ...your code...
226  
227 1;
228  
229 If you want to build your custom ResultSet classes with L<Moose>, you need
230 a similar, though a little more elaborate template in order to interface the
231 inlining of the L<Moose>-provided
232 L<object constructor|Moose::Manual::Construction/WHERE'S THE CONSTRUCTOR?>,
233 with the DBIC one.
234  
235 package MyApp::Schema::ResultSet::User;
236  
237 use Moose;
238 use MooseX::NonMoose;
239 extends 'DBIx::Class::ResultSet';
240  
241 sub BUILDARGS { $_[2] || {} } # ::RS::new() expects my ($class, $rsrc, $args) = @_
242  
243 ...your code...
244  
245 __PACKAGE__->meta->make_immutable;
246  
247 1;
248  
249 The L<MooseX::NonMoose> is necessary so that the L<Moose> constructor does not
250 entirely overwrite the DBIC one (in contrast L<Moo> does this automatically).
251 Alternatively, you can skip L<MooseX::NonMoose> and get by with just L<Moose>
252 instead by doing:
253  
254 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
255  
256 =head1 METHODS
257  
258 =head2 new
259  
260 =over 4
261  
262 =item Arguments: L<$source|DBIx::Class::ResultSource>, L<\%attrs?|/ATTRIBUTES>
263  
264 =item Return Value: L<$resultset|/search>
265  
266 =back
267  
268 The resultset constructor. Takes a source object (usually a
269 L<DBIx::Class::ResultSourceProxy::Table>) and an attribute hash (see
270 L</ATTRIBUTES> below). Does not perform any queries -- these are
271 executed as needed by the other methods.
272  
273 Generally you never construct a resultset manually. Instead you get one
274 from e.g. a
275 C<< $schema->L<resultset|DBIx::Class::Schema/resultset>('$source_name') >>
276 or C<< $another_resultset->L<search|/search>(...) >> (the later called in
277 scalar context):
278  
279 my $rs = $schema->resultset('CD')->search({ title => '100th Window' });
280  
281 =over
282  
283 =item WARNING
284  
285 If called on an object, proxies to L</new_result> instead, so
286  
287 my $cd = $schema->resultset('CD')->new({ title => 'Spoon' });
288  
289 will return a CD object, not a ResultSet, and is equivalent to:
290  
291 my $cd = $schema->resultset('CD')->new_result({ title => 'Spoon' });
292  
293 Please also keep in mind that many internals call L</new_result> directly,
294 so overloading this method with the idea of intercepting new result object
295 creation B<will not work>. See also warning pertaining to L</create>.
296  
297 =back
298  
299 =cut
300  
301 32977 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
302 32977 my $class = shift;
303  
304 32977 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 21 32956 32977
305 21 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call;
306 21 return $class->new_result(@_);
307 }
308  
309 32956 my ($source, $attrs) = @_;
310 32956 ▶ $source = $source->resolve
Branch: 1 branch
BranchTrueFalseTotal
if $source->isa("DBIx::Class::ResultSourceHandle") 1 32955 32956
311 if $source->isa('DBIx::Class::ResultSourceHandle');
312  
313 32956 ▶ $attrs = { %{$attrs||{}} };
Condition100%2/2
$attrs || {}
l!l
329551
MC/DC100%1/1
$attrs || {}
$attrs
Truth table100%2/2
A
A= $attrs
Aresult
11
00
314 32956 delete @{$attrs}{qw(_last_sqlmaker_alias_map _simple_passthrough_construction)};
315  
316 32956 ▶ if ($attrs->{page}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"page"}) 26 32930 32956
317 26 ▶ $attrs->{rows} ||= 10;
Condition100%2/2
$attrs->{"rows"} ||= 10
l!l
215
MC/DC100%1/1
$attrs->{"rows"} ||= 10
$attrs->{"rows"}
Truth table100%2/2
A
A= $attrs->{"rows"}
Aresult
11
00
318 }
319  
320 32956 ▶ $attrs->{alias} ||= 'me';
Condition100%2/2
$attrs->{"alias"} ||= "me"
l!l
1839214564
MC/DC100%1/1
$attrs->{"alias"} ||= "me"
$attrs->{"alias"}
Truth table100%2/2
A
A= $attrs->{"alias"}
Aresult
11
00
321  
322 my $self = bless {
323 result_source => $source,
324 cond => $attrs->{where},
325 32956 pager => undef,
326 attrs => $attrs,
327 }, $class;
328  
329 # if there is a dark selector, this means we are already in a
330 # chain and the cleanup/sanification was taken care of by
331 # _search_rs already
332 $self->_normalize_selection($attrs)
333 32956 ▶ unless $attrs->{_dark_selector};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs->{"_dark_selector"} 32929 27 32956
334  
335 $self->result_class(
336 32956 ▶ $attrs->{result_class} || $source->result_class
Condition66%2/3
$attrs->{'result_class'} || $source->result_class
l!l&&r!l&&!r
1512314440
MC/DC0%0/2
$attrs->{'result_class'} || $source->result_class
$attrs->{'result_class'} $source->result_class
Truth table66%2/3
A || B
A= $attrs->{'result_class'}B= $source->result_class
ABresult
1X1
011
000
337 );
338  
339 32954 $self;
340 }
341  
342 =head2 search
343  
344 =over 4
345  
346 =item Arguments: L<$cond|DBIx::Class::SQLMaker> | undef, L<\%attrs?|/ATTRIBUTES>
347  
348 =item Return Value: $resultset (scalar context) | L<@result_objs|DBIx::Class::Manual::ResultClass> (list context)
349  
350 =back
351  
352 my @cds = $cd_rs->search({ year => 2001 }); # "... WHERE year = 2001"
353 my $new_rs = $cd_rs->search({ year => 2005 });
354  
355 my $new_rs = $cd_rs->search([ { year => 2005 }, { year => 2004 } ]);
356 # year = 2005 OR year = 2004
357  
358 In list context, C<< ->all() >> is called implicitly on the resultset, thus
359 returning a list of L<result|DBIx::Class::Manual::ResultClass> objects instead.
360 To avoid that, use L</search_rs>.
361  
362 If you need to pass in additional attributes but no additional condition,
363 call it as C<search(undef, \%attrs)>.
364  
365 # "SELECT name, artistid FROM $artist_table"
366 my @all_artists = $schema->resultset('Artist')->search(undef, {
367 columns => [qw/name artistid/],
368 });
369  
370 For a list of attributes that can be passed to C<search>, see
371 L</ATTRIBUTES>. For more examples of using this function, see
372 L<Searching|DBIx::Class::Manual::Cookbook/SEARCHING>. For a complete
373 documentation for the first argument, see
374 L<SQL::Abstract::Classic/"WHERE CLAUSES"> and its extension
375 L<DBIx::Class::SQLMaker>.
376  
377 For more help on using joins with search, see L<DBIx::Class::Manual::Joining>.
378  
379 =head3 CAVEAT
380  
381 Note that L</search> does not process/deflate any of the values passed in the
382 L<SQL::Abstract::Classic>-compatible search condition structure. This is unlike
383 other condition-bound methods L</new_result>, L</create> and L</find>. The user
384 must ensure manually that any value passed to this method will stringify to
385 something the RDBMS knows how to deal with. A notable example is the handling
386 of L<DateTime> objects, for more info see:
387 L<DBIx::Class::Manual::Cookbook/Formatting DateTime objects in queries>.
388  
389 =cut
390  
391 16525 ▶sub search {
Subroutine search: called CC=4 SCAR=14.0
Pod documented
392 16525 my $self = shift;
393 16525 my $rs = $self->search_rs( @_ );
394  
395 16517 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 85 16432 16517
elsif (defined wantarray) { } 16431 1 16432
396 85 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = fail_on_internal_wantarray;
397 85 return $rs->all;
398 }
399 elsif (defined wantarray) {
400 16431 return $rs;
401 }
402 else {
403 # we can be called by a relationship helper, which in
404 # turn may be called in void context due to some braindead
405 # overload or whatever else the user decided to be clever
406 # at this particular day. Thus limit the exception to
407 # external code calls only
408 1 ▶ $self->throw_exception ('->search is *not* a mutator, calling it in void context makes no sense')
Branch: 1 branch
BranchTrueFalseTotal
unless (caller)[0] =~ /^DBIx\:\:Class\:\:/ 1 0 1
409 if (caller)[0] !~ /^\QDBIx::Class::/;
410  
411 0 return ();
412 }
413 }
414  
415 =head2 search_rs
416  
417 =over 4
418  
419 =item Arguments: L<$cond|DBIx::Class::SQLMaker>, L<\%attrs?|/ATTRIBUTES>
420  
421 =item Return Value: L<$resultset|/search>
422  
423 =back
424  
425 This method does the same exact thing as search() except it will
426 always return a resultset, even in list context.
427  
428 =cut
429  
430 17515 ▶sub search_rs {
Subroutine search_rs: called CC=36 SCAR=36.0
Pod documented
431 17515 my $self = shift;
432  
433 17515 my $rsrc = $self->result_source;
434 17515 my ($call_cond, $call_attrs);
435  
436 # Special-case handling for (undef, undef) or (undef)
437 # Note that (foo => undef) is valid deprecated syntax
438 17515 ▶ @_ = () if not scalar grep { defined $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar grep({defined $_;} @_) 3442 14073 17515
439  
440 # just a cond
441 17515 ▶ if (@_ == 1) {
Branch: 4 branches
BranchTrueFalseTotal
if (@_ == 1) { } 5602 11913 17515
elsif (@_ == 2 and !defined($_[0]) || ref $_[0] ne '') { } 8464 3449 11913
elsif (@_ % 2) { } 6 3443 3449
elsif (@_) { } 1 3442 3443
442 5602 $call_cond = shift;
443 }
444 # fish out attrs in the ($condref, $attr) case
445 ▶ elsif (@_ == 2 and ( ! defined $_[0] or (ref $_[0]) ne '') ) {
Condition100%6/6
@_ == 2 and !defined($_[0]) || ref $_[0] ne ""
!ll&&!rl&&r
344818464
not defined $_[0] or ref $_[0] ne ""
l!l&&r!l&&!r
145570091
MC/DC100%3/3
@_ == 2 and !defined($_[0]) || ref $_[0] ne ""
@_ == 2 not defined $_[0] ref $_[0] ne ""
Truth table100%4/4
A and not(B) or C
A= @_ == 2B= not defined $_[0]C= ref $_[0] ne ""
ABCresult
0XX0
1000
11X1
1011
446 8464 ($call_cond, $call_attrs) = @_;
447 }
448 elsif (@_ % 2) {
449 6 $self->throw_exception('Odd number of arguments to search')
450 }
451 # legacy search
452 elsif (@_) {
453 1 ▶ carp_unique 'search( %condition ) is deprecated, use search( \%condition ) instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $rsrc->result_class->isa("DBIx::Class::CDBICompat") 1 0 1
454 unless $rsrc->result_class->isa('DBIx::Class::CDBICompat');
455  
456 1 for my $i (0 .. $#_) {
457 2 ▶ next if $i % 2;
Branch: 1 branch
BranchTrueFalseTotal
if $i % 2 1 1 2
458 1 ▶ $self->throw_exception ('All keys in condition key/value pairs must be plain scalars')
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[$i] or ref $_[$i] ne "" 0 1 1
Condition33%1/3
not defined $_[$i] or ref $_[$i] ne ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $_[$i] or ref $_[$i] ne ""
not defined $_[$i] ref $_[$i] ne ""
Truth table33%1/3
not(A) or B
A= not defined $_[$i]B= ref $_[$i] ne ""
ABresult
1X1
011
000
459 if (! defined $_[$i] or ref $_[$i] ne '');
460 }
461  
462 1 $call_cond = { @_ };
463 }
464  
465 # see if we can keep the cache (no $rs changes)
466 17509 my $cache;
467 17509 my %safe = (alias => 1, cache => 1);
468 17509 ▶ if ( ! grep { !$safe{$_} } keys %$call_attrs and (
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({not $safe{$_};} keys %$call_attrs) and !defined($call_cond) || ref $call_cond eq "HASH" && !keys(%$call_cond) || ref $call_cond eq "ARRAY" && !@$call_cond) 3638 13871 17509
Condition86%13/15
not grep({not $safe{$_};} keys %$call_attrs) and !defined($call_cond) || ref $call_cond eq "HASH" && !keys(%$call_cond) || ref $call_cond eq "ARRAY" && !@$call_cond
!ll&&!rl&&r
831055613638
not defined $call_cond or ref $call_cond eq "HASH" and not keys %$call_cond or ref $call_cond eq "ARRAY" and not @$call_cond
l!l&&r!l&&!r
363805561
not defined $call_cond or ref $call_cond eq "HASH" and not keys %$call_cond
l!l&&r!l&&!r
34771615561
ref $call_cond eq "HASH" and not keys %$call_cond
!ll&&!rl&&r
195542161
ref $call_cond eq "ARRAY" and not @$call_cond
!ll&&!rl&&r
5546150
MC/DC66%4/6
not grep({not $safe{$_};} keys %$call_attrs) and !defined($call_cond) || ref $call_cond eq "HASH" && !keys(%$call_cond) || ref $call_cond eq "ARRAY" && !@$call_cond
not grep({not $safe{$_};} keys %$call_attrs) not defined $call_cond ref $call_cond eq "HASH" not keys %$call_cond ref $call_cond eq "ARRAY" not @$call_cond
Truth table66%6/9
not(A) and not(B) or C and not(D) or E and not(F)
A= not grep({not $safe{$_};} keys %$call_attrs)B= not defined $call_condC= ref $call_cond eq "HASH"D= not keys %$call_condE= ref $call_cond eq "ARRAY"F= not @$call_cond
ABCDEFresult
0XXXXX0
100X0X0
100X100
10100X0
1010100
11XXXX1
1011XX1
100X111
1010111
469 ! defined $call_cond
470 or
471 ref $call_cond eq 'HASH' && ! keys %$call_cond
472 or
473 ref $call_cond eq 'ARRAY' && ! @$call_cond
474 )) {
475 3638 $cache = $self->get_cache;
476 }
477  
478 17509 my $old_attrs = { %{$self->{attrs}} };
479 17509 my ($old_having, $old_where) = delete @{$old_attrs}{qw(having where)};
480  
481 17509 my $new_attrs = { %$old_attrs };
482  
483 # take care of call attrs (only if anything is changing)
484 17509 ▶ if ($call_attrs and keys %$call_attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call_attrs and keys %$call_attrs) 8459 9050 17509
Condition66%2/3
$call_attrs and keys %$call_attrs
!ll&&!rl&&r
090508459
MC/DC50%1/2
$call_attrs and keys %$call_attrs
$call_attrs keys %$call_attrs
Truth table66%2/3
A and B
A= $call_attrsB= keys %$call_attrs
ABresult
0X0
100
111
485  
486 # copy for _normalize_selection
487 8459 $call_attrs = { %$call_attrs };
488  
489 8459 my @selector_attrs = qw/select as columns cols +select +as +columns include_columns/;
490  
491 # reset the current selector list if new selectors are supplied
492 1649 delete @{$old_attrs}{(@selector_attrs, '_dark_selector')}
493 8459 ▶ if grep { exists $call_attrs->{$_} } qw(columns cols select as);
Branch: 1 branch
BranchTrueFalseTotal
if grep {exists $call_attrs->{$_};} "columns", "cols", "select", "as" 1649 6810 8459
494  
495 # Normalize the new selector list (operates on the passed-in attr structure)
496 # Need to do it on every chain instead of only once on _resolved_attrs, in
497 # order to allow detection of empty vs partial 'as'
498 $call_attrs->{_dark_selector} = $old_attrs->{_dark_selector}
499 8459 ▶ if $old_attrs->{_dark_selector};
Branch: 1 branch
BranchTrueFalseTotal
if $old_attrs->{"_dark_selector"} 8 8451 8459
500 8459 $self->_normalize_selection ($call_attrs);
501  
502 # start with blind overwriting merge, exclude selector attrs
503 8457 $new_attrs = { %{$old_attrs}, %{$call_attrs} };
504 8457 delete @{$new_attrs}{@selector_attrs};
505  
506 8457 for (@selector_attrs) {
507 $new_attrs->{$_} = $self->_merge_attr($old_attrs->{$_}, $call_attrs->{$_})
508 67656 ▶ if ( exists $old_attrs->{$_} or exists $call_attrs->{$_} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $old_attrs->{$_} or exists $call_attrs->{$_} 2549 65107 67656
Condition100%3/3
exists $old_attrs->{$_} or exists $call_attrs->{$_}
l!l&&r!l&&!r
264228565107
MC/DC100%2/2
exists $old_attrs->{$_} or exists $call_attrs->{$_}
exists $old_attrs->{$_} exists $call_attrs->{$_}
Truth table100%3/3
A or B
A= exists $old_attrs->{$_}B= exists $call_attrs->{$_}
ABresult
1X1
011
000
509 }
510  
511 # older deprecated name, use only if {columns} is not there
512 8457 ▶ if (my $c = delete $new_attrs->{cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = delete $new_attrs->{"cols"}) 1 8456 8457
513 1 carp_unique( "Resultset attribute 'cols' is deprecated, use 'columns' instead" );
514 1 ▶ if ($new_attrs->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_attrs->{'columns'}) { } 0 1 1
515 0 carp "Resultset specifies both the 'columns' and the legacy 'cols' attributes - ignoring 'cols'";
516 }
517 else {
518 1 $new_attrs->{columns} = $c;
519 }
520 }
521  
522  
523 # join/prefetch use their own crazy merging heuristics
524 8457 foreach my $key (qw/join prefetch/) {
525 $new_attrs->{$key} = $self->_merge_joinpref_attr($old_attrs->{$key}, $call_attrs->{$key})
526 16914 ▶ if exists $call_attrs->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $call_attrs->{$key} 885 16029 16914
527 }
528  
529 # stack binds together
530 8457 ▶ $new_attrs->{bind} = [ @{ $old_attrs->{bind} || [] }, @{ $call_attrs->{bind} || [] } ];
Condition100%4/4
$old_attrs->{'bind'} || []
l!l
18626595
$call_attrs->{'bind'} || []
l!l
1868271
MC/DC100%1/1
$old_attrs->{'bind'} || []
$old_attrs->{'bind'}
MC/DC100%1/1
$call_attrs->{'bind'} || []
$call_attrs->{'bind'}
Truth table100%2/2
A
A= $old_attrs->{'bind'}
Aresult
11
00
Truth table100%2/2
A
A= $call_attrs->{'bind'}
Aresult
11
00
531 }
532  
533  
534 17507 for ($old_where, $call_cond) {
535 35014 ▶ if (defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 21746 13268 35014
536 $new_attrs->{where} = $self->_stack_cond (
537 $_, $new_attrs->{where}
538 21746 );
539 }
540 }
541  
542 17507 ▶ if (defined $old_having) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_having) 27 17480 17507
543 $new_attrs->{having} = $self->_stack_cond (
544 $old_having, $new_attrs->{having}
545 )
546 27 }
547  
548 17507 my $rs = (ref $self)->new($rsrc, $new_attrs);
549  
550 17505 ▶ $rs->set_cache($cache) if ($cache);
Branch: 1 branch
BranchTrueFalseTotal
if $cache 155 17350 17505
551  
552 17505 return $rs;
553 }
554  
555 379 my $dark_sel_dumper;
556 41388 ▶sub _normalize_selection {
Subroutine _normalize_selection: called CC=20 SCAR=30.3
557 41388 my ($self, $attrs) = @_;
558  
559 # legacy syntax
560 41388 ▶ if ( exists $attrs->{include_columns} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attrs->{"include_columns"}) 1 41387 41388
561 1 carp_unique( "Resultset attribute 'include_columns' is deprecated, use '+columns' instead" );
562 $attrs->{'+columns'} = $self->_merge_attr(
563 $attrs->{'+columns'}, delete $attrs->{include_columns}
564 1 );
565 }
566  
567 # columns are always placed first, however
568  
569 # Keep the X vs +X separation until _resolved_attrs time - this allows to
570 # delay the decision on whether to use a default select list ($rsrc->columns)
571 # allowing stuff like the remove_columns helper to work
572 #
573 # select/as +select/+as pairs need special handling - the amount of select/as
574 # elements in each pair does *not* have to be equal (think multicolumn
575 # selectors like distinct(foo, bar) ). If the selector is bare (no 'as'
576 # supplied at all) - try to infer the alias, either from the -as parameter
577 # of the selector spec, or use the parameter whole if it looks like a column
578 # name (ugly legacy heuristic). If all fails - leave the selector bare (which
579 # is ok as well), but make sure no more additions to the 'as' chain take place
580 41388 for my $pref ('', '+') {
581  
582 my ($sel, $as) = map {
583 82776 my $key = "${pref}${_}";
584  
585 my $val = [ ref $attrs->{$key} eq 'ARRAY'
586 2359 ▶ ? @{$attrs->{$key}}
Condition66%2/3
$attrs->{$key} or ()
l!l&&r!l&&!r
15181616750
MC/DC0%0/2
$attrs->{$key} or ()
$attrs->{$key} ()
Truth table66%2/3
A or B
A= $attrs->{$key}B= ()
ABresult
1X1
011
000
587 165552 ▶ : $attrs->{$key} || ()
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs->{$key} eq 'ARRAY' ? : 2359 163193 165552
588 ];
589 165552 delete $attrs->{$key};
590 165552 $val;
591 } qw/select as/;
592  
593 82776 ▶ if (! @$as and ! @$sel ) {
Branch: 5 branches
BranchTrueFalseTotal
if (not @$as || @$sel) { } 80539 2237 82776
elsif (@$as and not @$sel) { } 0 2237 2237
elsif (not @$as) { } 598 1639 2237
elsif (@$as < @$sel) { } 0 1639 1639
elsif ($pref and $attrs->{'_dark_selector'}) { } 2 3187 3189
Condition100%3/3
@$as || @$sel
l!l&&r!l&&!r
163959880539
MC/DC100%2/2
@$as || @$sel
@$as @$sel
Truth table100%3/3
A || B
A= @$asB= @$sel
ABresult
1X1
011
000
594 80539 next;
595 }
596 ▶ elsif (@$as and ! @$sel) {
Condition66%2/3
@$as and not @$sel
!ll&&!rl&&r
59816390
MC/DC0%0/2
@$as and not @$sel
@$as not @$sel
Truth table66%2/3
A and not(B)
A= @$asB= not @$sel
ABresult
0X0
100
111
597 0 $self->throw_exception(
598 "Unable to handle ${pref}as specification (@$as) without a corresponding ${pref}select"
599 );
600 }
601 elsif( ! @$as ) {
602 # no as part supplied at all - try to deduce (unless explicit end of named selection is declared)
603 # if any @$as has been supplied we assume the user knows what (s)he is doing
604 # and blindly keep stacking up pieces
605 598 ▶ unless ($attrs->{_dark_selector}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attrs->{"_dark_selector"}) 593 5 598
606 SELECTOR:
607 593 for (@$sel) {
608 658 ▶ if ( ref $_ eq 'HASH' and exists $_->{-as} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'HASH' and exists $_->{'-as'}) { } 5 653 658
elsif (not ref $_ and $_ =~ /^ [^\s\(\)]+ $/x) { } 632 21 653
Condition100%3/3
ref $_ eq "HASH" and exists $_->{"-as"}
!ll&&!rl&&r
65035
MC/DC100%2/2
ref $_ eq "HASH" and exists $_->{"-as"}
ref $_ eq "HASH" exists $_->{"-as"}
Truth table100%3/3
A and B
A= ref $_ eq "HASH"B= exists $_->{"-as"}
ABresult
0X0
100
111
609 5 push @$as, $_->{-as};
610 }
611 # assume any plain no-space, no-parenthesis string to be a column spec
612 # FIXME - this is retarded but is necessary to support shit like 'count(foo)'
613 ▶ elsif ( ! ref $_ and $_ =~ /^ [^\s\(\)]+ $/x) {
Condition66%2/3
not ref $_ and $_ =~ /^ [^\s\(\)]+ $/x
!ll&&!rl&&r
210632
MC/DC50%1/2
not ref $_ and $_ =~ /^ [^\s\(\)]+ $/x
not ref $_ $_ =~ /^ [^\s\(\)]+ $/x
Truth table66%2/3
not(A) and B
A= not ref $_B= $_ =~ /^ [^\s\(\)]+ $/x
ABresult
0X0
100
111
614 632 push @$as, $_;
615 }
616 # if all else fails - raise a flag that no more aliasing will be allowed
617 else {
618 $attrs->{_dark_selector} = {
619 plus_stage => $pref,
620 21 ▶ string => ($dark_sel_dumper ||= do {
Condition66%2/3
$dark_sel_dumper ||= do { require Data::Dumper::Concise; Data::Dumper::Concise::DumperObject()->Indent(0) }
l!l&&r!l&&!r
1290
MC/DC0%0/2
$dark_sel_dumper ||= do { require Data::Dumper::Concise; Data::Dumper::Concise::DumperObject()->Indent(0) }
$dark_sel_dumper do { require Data::Dumper::Concise; Data::Dumper::Concise::DumperObject()->Indent(0) }
Truth table66%2/3
A ||= B
A= $dark_sel_dumperB= do { require Data::Dumper::Concise; Data::Dumper::Concise::DumperObject()->Indent(0) }
ABresult
1X1
011
000
621 9 require Data::Dumper::Concise;
622 9 Data::Dumper::Concise::DumperObject()->Indent(0);
623 })->Values([$_])->Dump
624 ,
625 };
626 21 last SELECTOR;
627 }
628 }
629 }
630 }
631 elsif (@$as < @$sel) {
632 0 $self->throw_exception(
633 "Unable to handle an ${pref}as specification (@$as) with less elements than the corresponding ${pref}select"
634 );
635 }
636 ▶ elsif ($pref and $attrs->{_dark_selector}) {
Condition100%3/3
$pref and $attrs->{"_dark_selector"}
!ll&&!rl&&r
1550872
MC/DC100%2/2
$pref and $attrs->{"_dark_selector"}
$pref $attrs->{"_dark_selector"}
Truth table100%3/3
A and B
A= $prefB= $attrs->{"_dark_selector"}
ABresult
0X0
100
111
637 2 $self->throw_exception(
638 "Unable to process named '+select', resultset contains an unnamed selector $attrs->{_dark_selector}{string}"
639 );
640 }
641  
642  
643 # merge result
644 2235 $attrs->{"${pref}select"} = $self->_merge_attr($attrs->{"${pref}select"}, $sel);
645 2235 $attrs->{"${pref}as"} = $self->_merge_attr($attrs->{"${pref}as"}, $as);
646 }
647 }
648  
649 21773 ▶sub _stack_cond {
Subroutine _stack_cond: called CC=10 SCAR=23.1
650 21773 my ($self, $left, $right) = @_;
651  
652 (
653 (ref $_ eq 'ARRAY' and !@$_)
654 or
655 (ref $_ eq 'HASH' and ! keys %$_)
656 21773 ▶ ) and $_ = undef for ($left, $right);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq "ARRAY" && !@$_ || ref $_ eq "HASH" && !keys(%$_) and $_ = undef 2412 41134 43546
Condition77%7/9
ref $_ eq "ARRAY" and not @$_ or ref $_ eq "HASH" and not keys %$_
l!l&&r!l&&!r
0241241134
ref $_ eq "ARRAY" and not @$_
!ll&&!rl&&r
4244611000
ref $_ eq "HASH" and not keys %$_
!ll&&!rl&&r
17653234812412
MC/DC50%2/4
ref $_ eq "ARRAY" and not @$_ or ref $_ eq "HASH" and not keys %$_
ref $_ eq "ARRAY" not @$_ ref $_ eq "HASH" not keys %$_
Truth table57%4/7
A and not(B) or C and not(D)
A= ref $_ eq "ARRAY"B= not @$_C= ref $_ eq "HASH"D= not keys %$_
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
657  
658 # either one of the two undef
659 21773 ▶ if ( (defined $left) xor (defined $right) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $left xor defined $right) { } 16874 4899 21773
elsif (not defined $left) { } 969 3930 4899
Condition100%4/4
defined $left xor defined $right
l&&rl&&!r!l&&r!l&&!r
3930154311443969
MC/DC100%2/2
defined $left xor defined $right
defined $left defined $right
Truth table100%4/4
A xor B
A= defined $leftB= defined $right
ABresult
110
101
011
000
660 16874 ▶ return defined $left ? $left : $right;
Branch: 1 branch
BranchTrueFalseTotal
defined $left ? : 15431 1443 16874
661 }
662 # both undef
663 elsif ( ! defined $left ) {
664 return undef
665 969 }
666 else {
667 3930 return $self->result_source->schema->storage->_collapse_cond({ -and => [$left, $right] });
668 }
669 }
670  
671 =head2 search_literal
672  
673 B<CAVEAT>: C<search_literal> is provided for Class::DBI compatibility and
674 should only be used in that context. C<search_literal> is a convenience
675 method. It is equivalent to calling C<< $schema->search(\[]) >>, but if you
676 want to ensure columns are bound correctly, use L</search>.
677  
678 See L<DBIx::Class::Manual::Cookbook/SEARCHING> and
679 L<DBIx::Class::Manual::FAQ/Searching> for searching techniques that do not
680 require C<search_literal>.
681  
682 =over 4
683  
684 =item Arguments: $sql_fragment, @standalone_bind_values
685  
686 =item Return Value: L<$resultset|/search> (scalar context) | L<@result_objs|DBIx::Class::Manual::ResultClass> (list context)
687  
688 =back
689  
690 my @cds = $cd_rs->search_literal('year = ? AND title = ?', qw/2001 Reload/);
691 my $newrs = $artist_rs->search_literal('name = ?', 'Metallica');
692  
693 Pass a literal chunk of SQL to be added to the conditional part of the
694 resultset query.
695  
696 Example of how to use C<search> instead of C<search_literal>
697  
698 my @cds = $cd_rs->search_literal('cdid = ? AND (artist = ? OR artist = ?)', (2, 1, 2));
699 my @cds = $cd_rs->search(\[ 'cdid = ? AND (artist = ? OR artist = ?)', [ 'cdid', 2 ], [ 'artist', 1 ], [ 'artist', 2 ] ]);
700  
701 =cut
702  
703 1 ▶sub search_literal {
Subroutine search_literal: called CC=4 SCAR=17.2
Pod documented
704 1 my ($self, $sql, @bind) = @_;
705 1 my $attr;
706 1 ▶ if ( @bind && ref($bind[-1]) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bind and ref $bind[-1] eq "HASH") 0 1 1
Condition33%1/3
@bind and ref $bind[-1] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@bind and ref $bind[-1] eq "HASH"
@bind ref $bind[-1] eq "HASH"
Truth table33%1/3
A and B
A= @bindB= ref $bind[-1] eq "HASH"
ABresult
0X0
100
111
707 0 $attr = pop @bind;
708 }
709 1 ▶ return $self->search(\[ $sql, map [ {} => $_ ], @bind ], ($attr || () ));
Condition33%1/3
$attr || ()
l!l&&r!l&&!r
010
MC/DC0%0/2
$attr || ()
$attr ()
Truth table33%1/3
A || B
A= $attrB= ()
ABresult
1X1
011
000
710 }
711  
712 =head2 find
713  
714 =over 4
715  
716 =item Arguments: \%columns_values | @pk_values, { key => $unique_constraint, L<%attrs|/ATTRIBUTES> }?
717  
718 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass> | undef
719  
720 =back
721  
722 Finds and returns a single row based on supplied criteria. Takes either a
723 hashref with the same format as L</create> (including inference of foreign
724 keys from related objects), or a list of primary key values in the same
725 order as the L<primary columns|DBIx::Class::ResultSource/primary_columns>
726 declaration on the L</result_source>.
727  
728 In either case an attempt is made to combine conditions already existing on
729 the resultset with the condition passed to this method.
730  
731 To aid with preparing the correct query for the storage you may supply the
732 C<key> attribute, which is the name of a
733 L<unique constraint|DBIx::Class::ResultSource/add_unique_constraint> (the
734 unique constraint corresponding to the
735 L<primary columns|DBIx::Class::ResultSource/primary_columns> is always named
736 C<primary>). If the C<key> attribute has been supplied, and DBIC is unable
737 to construct a query that satisfies the named unique constraint fully (
738 non-NULL values for each column member of the constraint) an exception is
739 thrown.
740  
741 If no C<key> is specified, the search is carried over all unique constraints
742 which are fully defined by the available condition.
743  
744 If no such constraint is found, C<find> currently defaults to a simple
745 C<< search->(\%column_values) >> which may or may not do what you expect.
746 Note that this fallback behavior may be deprecated in further versions. If
747 you need to search with arbitrary conditions - use L</search>. If the query
748 resulting from this fallback produces more than one row, a warning to the
749 effect is issued, though only the first row is constructed and returned as
750 C<$result_object>.
751  
752 In addition to C<key>, L</find> recognizes and applies standard
753 L<resultset attributes|/ATTRIBUTES> in the same way as L</search> does.
754  
755 Note that if you have extra concerns about the correctness of the resulting
756 query you need to specify the C<key> attribute and supply the entire condition
757 as an argument to find (since it is not always possible to perform the
758 combination of the resultset condition with the supplied one, especially if
759 the resultset condition contains literal sql).
760  
761 For example, to find a row by its primary key:
762  
763 my $cd = $schema->resultset('CD')->find(5);
764  
765 You can also find a row by a specific unique constraint:
766  
767 my $cd = $schema->resultset('CD')->find(
768 {
769 artist => 'Massive Attack',
770 title => 'Mezzanine',
771 },
772 { key => 'cd_artist_title' }
773 );
774  
775 See also L</find_or_create> and L</update_or_create>.
776  
777 =cut
778  
779 1393 ▶sub find {
Subroutine find: called CC=27 SCAR=33.1
Pod documented
780 1393 my $self = shift;
781 1393 ▶ my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 375 1018 1393
Condition100%3/3
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
10126375
MC/DC100%2/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table100%3/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
782  
783 1393 my $rsrc = $self->result_source;
784  
785 1393 my $constraint_name;
786 1393 ▶ if (exists $attrs->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attrs->{"key"}) 18 1375 1393
787 $constraint_name = defined $attrs->{key}
788 ? $attrs->{key}
789 18 ▶ : $self->throw_exception("An undefined 'key' resultset attribute makes no sense")
Branch: 1 branch
BranchTrueFalseTotal
defined $attrs->{'key'} ? : 18 0 18
790 ;
791 }
792  
793 # Parse out the condition from input
794 1393 my $call_cond;
795  
796 1393 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 1177 216 1393
797 1177 $call_cond = { %{$_[0]} };
798 }
799 else {
800 # if only values are supplied we need to default to 'primary'
801 216 ▶ $constraint_name = 'primary' unless defined $constraint_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $constraint_name 215 1 216
802  
803 216 my @c_cols = $rsrc->unique_constraint_columns($constraint_name);
804  
805 216 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless @c_cols 0 216 216
806 "No constraint columns, maybe a malformed '$constraint_name' constraint?"
807 ) unless @c_cols;
808  
809 216 ▶ $self->throw_exception (
Branch: 1 branch
BranchTrueFalseTotal
unless @c_cols == @_ 1 215 216
810 'find() expects either a column/value hashref, or a list of values '
811 . "corresponding to the columns of the specified unique constraint '$constraint_name'"
812 ) unless @c_cols == @_;
813  
814 215 @{$call_cond}{@c_cols} = @_;
815 }
816  
817 # process relationship data if any
818 1392 for my $key (keys %$call_cond) {
819 1322 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $call_cond->{$key} and my $relinfo = $rsrc->relationship_info($key) and ref(my $val = delete $call_cond->{$key}) ne "ARRAY") 19 1303 1322
Condition100%6/6
length ref $call_cond->{$key} and my $relinfo = $rsrc->relationship_info($key) and ref(my $val = delete $call_cond->{$key}) ne "ARRAY"
!ll&&!rl&&r
12891419
length ref $call_cond->{$key} and my $relinfo = $rsrc->relationship_info($key)
!ll&&!rl&&r
1283633
MC/DC100%3/3
length ref $call_cond->{$key} and my $relinfo = $rsrc->relationship_info($key) and ref(my $val = delete $call_cond->{$key}) ne "ARRAY"
length ref $call_cond->{$key} my $relinfo = $rsrc->relationship_info($key) ref(my $val = delete $call_cond->{$key}) ne "ARRAY"
Truth table100%4/4
A and B and C
A= length ref $call_cond->{$key}B= my $relinfo = $rsrc->relationship_info($key)C= ref(my $val = delete $call_cond->{$key}) ne "ARRAY"
ABCresult
0XX0
10X0
1100
1111
820 length ref($call_cond->{$key})
821 and
822 my $relinfo = $rsrc->relationship_info($key)
823 and
824 # implicitly skip has_many's (likely MC)
825 (ref (my $val = delete $call_cond->{$key}) ne 'ARRAY' )
826 ) {
827 my ($rel_cond, $crosstable) = $rsrc->_resolve_condition(
828 19 $relinfo->{cond}, $val, $key, $key
829 );
830  
831 18 ▶ $self->throw_exception("Complex condition via relationship '$key' is unsupported in find()")
Branch: 1 branch
BranchTrueFalseTotal
if $crosstable or ref $rel_cond ne "HASH" 0 18 18
Condition33%1/3
$crosstable or ref $rel_cond ne "HASH"
l!l&&r!l&&!r
0018
MC/DC0%0/2
$crosstable or ref $rel_cond ne "HASH"
$crosstable ref $rel_cond ne "HASH"
Truth table33%1/3
A or B
A= $crosstableB= ref $rel_cond ne "HASH"
ABresult
1X1
011
000
832 if $crosstable or ref($rel_cond) ne 'HASH';
833  
834 # supplement condition
835 # relationship conditions take precedence (?)
836 18 @{$call_cond}{keys %$rel_cond} = values %$rel_cond;
837 }
838 }
839  
840 1391 ▶ my $alias = exists $attrs->{alias} ? $attrs->{alias} : $self->{attrs}{alias};
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs->{'alias'} ? : 0 1391 1391
841 1391 my $final_cond;
842 1391 ▶ if (defined $constraint_name) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $constraint_name) { } 232 1159 1391
elsif ($self->{'attrs'}{'accessor'} and $self->{'attrs'}{'accessor'} eq 'single') { } 8 1151 1159
843 232 $final_cond = $self->_qualify_cond_columns (
844  
845 $self->result_source->_minimal_valueset_satisfying_constraint(
846 constraint_name => $constraint_name,
847 values => ($self->_merge_with_rscond($call_cond))[0],
848 carp_on_nulls => 1,
849 ),
850  
851 $alias,
852 );
853 }
854 ▶ elsif ($self->{attrs}{accessor} and $self->{attrs}{accessor} eq 'single') {
Condition100%3/3
$self->{"attrs"}{"accessor"} and $self->{"attrs"}{"accessor"} eq "single"
!ll&&!rl&&r
8183338
MC/DC100%2/2
$self->{"attrs"}{"accessor"} and $self->{"attrs"}{"accessor"} eq "single"
$self->{"attrs"}{"accessor"} $self->{"attrs"}{"accessor"} eq "single"
Truth table100%3/3
A and B
A= $self->{"attrs"}{"accessor"}B= $self->{"attrs"}{"accessor"} eq "single"
ABresult
0X0
100
111
855 # This means that we got here after a merger of relationship conditions
856 # in ::Relationship::Base::search_related (the row method), and furthermore
857 # the relationship is of the 'single' type. This means that the condition
858 # provided by the relationship (already attached to $self) is sufficient,
859 # as there can be only one row in the database that would satisfy the
860 # relationship
861 }
862 else {
863 1151 my (@unique_queries, %seen_column_combinations, $ci, @fc_exceptions);
864  
865 # no key was specified - fall down to heuristics mode:
866 # run through all unique queries registered on the resultset, and
867 # 'OR' all qualifying queries together
868 #
869 # always start from 'primary' if it exists at all
870 1151 for my $c_name ( sort {
871 4034 ▶ $a eq 'primary' ? -1
Branch: 2 branches
BranchTrueFalseTotal
$a eq 'primary' ? : 944 3090 4034
$b eq 'primary' ? : 888 2202 3090
872 : $b eq 'primary' ? 1
873 : $a cmp $b
874 } $rsrc->unique_constraint_names) {
875  
876 next if $seen_column_combinations{
877 3809 ▶ join "\x00", sort $rsrc->unique_constraint_columns($c_name)
Branch: 1 branch
BranchTrueFalseTotal
if $seen_column_combinations{join "\0", sort($rsrc->unique_constraint_columns($c_name))}++ 817 2992 3809
878 }++;
879  
880 2992 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=7.2
881 2992 ▶ push @unique_queries, $self->_qualify_cond_columns(
Condition66%2/3
$ci ||= $self->result_source->columns_info
l!l&&r!l&&!r
184111510
MC/DC0%0/2
$ci ||= $self->result_source->columns_info
$ci $self->result_source->columns_info
Truth table66%2/3
A ||= B
A= $ciB= $self->result_source->columns_info
ABresult
1X1
011
000
882 $self->result_source->_minimal_valueset_satisfying_constraint(
883 constraint_name => $c_name,
884 values => ($self->_merge_with_rscond($call_cond))[0],
885 columns_info => ($ci ||= $self->result_source->columns_info),
886 ),
887 $alias
888 );
889 }
890 1949 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=6.9
891 1949 ▶ push @fc_exceptions, $_ if $_ =~ /\bFilterColumn\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $_ =~ /\bFilterColumn\b/ 1 1948 1949
892 2992 };
893 }
894  
895 $final_cond =
896 @unique_queries ? \@unique_queries
897 1151 ▶ : @fc_exceptions ? $self->throw_exception(join "; ", map { $_ =~ /(.*) at .+ line \d+$/s } @fc_exceptions )
Branch: 2 branches
BranchTrueFalseTotal
@unique_queries ? : 1018 133 1151
@fc_exceptions ? : 1 132 133
898 : $self->_non_unique_find_fallback ($call_cond, $attrs)
899 ;
900 }
901  
902 # Run the query, passing the result_class since it should propagate for find
903 1386 my $rs = $self->search ($final_cond, {result_class => $self->result_class, %$attrs});
904 1386 ▶ if ($rs->_resolved_attrs->{collapse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rs->_resolved_attrs->{'collapse'}) { } 11 1375 1386
905 11 my $row = $rs->next;
906 11 ▶ carp "Query returned more than one row" if $rs->next;
Branch: 1 branch
BranchTrueFalseTotal
if $rs->next 1 10 11
907 11 return $row;
908 }
909 else {
910 1375 return $rs->single;
911 }
912 }
913  
914 # This is a stop-gap method as agreed during the discussion on find() cleanup:
915 # http://lists.scsys.co.uk/pipermail/dbix-class/2010-October/009535.html
916 #
917 # It is invoked when find() is called in legacy-mode with insufficiently-unique
918 # condition. It is provided for overrides until a saner way forward is devised
919 #
920 # *NOTE* This is not a public method, and it's *GUARANTEED* to disappear down
921 # the road. Please adjust your tests accordingly to catch this situation early
922 # DBIx::Class::ResultSet->can('_non_unique_find_fallback') is reasonable
923 #
924 # The method will not be removed without an adequately complete replacement
925 # for strict-mode enforcement
926 132 ▶sub _non_unique_find_fallback {
Subroutine _non_unique_find_fallback: called CC=2 SCAR=7.2
927 132 my ($self, $cond, $attrs) = @_;
928  
929 return $self->_qualify_cond_columns(
930 $cond,
931 exists $attrs->{alias}
932 ? $attrs->{alias}
933 : $self->{attrs}{alias}
934 132 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
exists $attrs->{'alias'} ? : 0 132 132
935 }
936  
937  
938 1403 ▶sub _qualify_cond_columns {
Subroutine _qualify_cond_columns: called CC=3 SCAR=11.1
939 1403 my ($self, $cond, $alias) = @_;
940  
941 1403 my %aliased = %$cond;
942 1403 for (keys %aliased) {
943 1573 ▶ $aliased{"$alias.$_"} = delete $aliased{$_}
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /\./ 1573 0 1573
944 if $_ !~ /\./;
945 }
946  
947 1403 return \%aliased;
948 }
949  
950 0 ▶sub _build_unique_cond {
Subroutine _build_unique_cond: not called CC=1 SCAR=6.9
951 0 carp_unique sprintf
952 '_build_unique_cond is a private method, and moreover is about to go '
953 . 'away. Please contact the development team at %s if you believe you '
954 . 'have a genuine use for this method, in order to discuss alternatives.',
955 DBIx::Class::_ENV_::HELP_URL,
956 ;
957  
958 0 my ($self, $constraint_name, $cond, $croak_on_null) = @_;
959  
960 0 $self->result_source->_minimal_valueset_satisfying_constraint(
961 constraint_name => $constraint_name,
962 values => $cond,
963 carp_on_nulls => !$croak_on_null
964 );
965 }
966  
967 =head2 search_related
968  
969 =over 4
970  
971 =item Arguments: $rel_name, $cond?, L<\%attrs?|/ATTRIBUTES>
972  
973 =item Return Value: L<$resultset|/search> (scalar context) | L<@result_objs|DBIx::Class::Manual::ResultClass> (list context)
974  
975 =back
976  
977 $new_rs = $cd_rs->search_related('artist', {
978 name => 'Emo-R-Us',
979 });
980  
981 Searches the specified relationship, optionally specifying a condition and
982 attributes for matching records. See L</ATTRIBUTES> for more information.
983  
984 In list context, C<< ->all() >> is called implicitly on the resultset, thus
985 returning a list of result objects instead. To avoid that, use L</search_related_rs>.
986  
987 See also L</search_related_rs>.
988  
989 =cut
990  
991 214 ▶sub search_related {
Subroutine search_related: called CC=1 SCAR=0.0
Pod documented
992 214 return shift->related_resultset(shift)->search(@_);
993 }
994  
995 =head2 search_related_rs
996  
997 This method works exactly the same as search_related, except that
998 it guarantees a resultset, even in list context.
999  
1000 =cut
1001  
1002 1 ▶sub search_related_rs {
Subroutine search_related_rs: called CC=1 SCAR=0.0
Pod documented
1003 1 return shift->related_resultset(shift)->search_rs(@_);
1004 }
1005  
1006 =head2 cursor
1007  
1008 =over 4
1009  
1010 =item Arguments: none
1011  
1012 =item Return Value: L<$cursor|DBIx::Class::Cursor>
1013  
1014 =back
1015  
1016 Returns a storage-driven cursor to the given resultset. See
1017 L<DBIx::Class::Cursor> for more information.
1018  
1019 =cut
1020  
1021 10720 ▶sub cursor {
Subroutine cursor: called CC=2 SCAR=7.0
Pod documented
1022 10720 my $self = shift;
1023  
1024 10720 ▶ return $self->{cursor} ||= do {
Condition66%2/3
$self->{"cursor"} ||= do { my $attrs = $self->_resolved_attrs; $self->result_source->storage->select($attrs->{"from"}, $attrs->{"select"}, $attrs->{"where"}, $attrs) }
l!l&&r!l&&!r
694037800
MC/DC0%0/2
$self->{"cursor"} ||= do { my $attrs = $self->_resolved_attrs; $self->result_source->storage->select($attrs->{"from"}, $attrs->{"select"}, $attrs->{"where"}, $attrs) }
$self->{"cursor"} do { my $attrs = $self->_resolved_attrs; $self->result_source->storage->select($attrs->{"from"}, $attrs->{"select"}, $attrs->{"where"}, $attrs) }
Truth table66%2/3
A ||= B
A= $self->{"cursor"}B= do { my $attrs = $self->_resolved_attrs; $self->result_source->storage->select($attrs->{"from"}, $attrs->{"select"}, $attrs->{"where"}, $attrs) }
ABresult
1X1
011
000
1025 3780 my $attrs = $self->_resolved_attrs;
1026 $self->result_source->storage->select(
1027 3780 $attrs->{from}, $attrs->{select}, $attrs->{where}, $attrs
1028 );
1029 };
1030 }
1031  
1032 =head2 single
1033  
1034 =over 4
1035  
1036 =item Arguments: L<$cond?|DBIx::Class::SQLMaker>
1037  
1038 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass> | undef
1039  
1040 =back
1041  
1042 my $cd = $schema->resultset('CD')->single({ year => 2001 });
1043  
1044 Inflates the first result without creating a cursor if the resultset has
1045 any records in it; if not returns C<undef>. Used by L</find> as a lean version
1046 of L</search>.
1047  
1048 While this method can take an optional search condition (just like L</search>)
1049 being a fast-code-path it does not recognize search attributes. If you need to
1050 add extra joins or similar, call L</search> and then chain-call L</single> on the
1051 L<DBIx::Class::ResultSet> returned.
1052  
1053 =over
1054  
1055 =item B<Note>
1056  
1057 As of 0.08100, this method enforces the assumption that the preceding
1058 query returns only one row. If more than one row is returned, you will receive
1059 a warning:
1060  
1061 Query returned more than one row
1062  
1063 In this case, you should be using L</next> or L</find> instead, or if you really
1064 know what you are doing, use the L</rows> attribute to explicitly limit the size
1065 of the resultset.
1066  
1067 This method will also throw an exception if it is called on a resultset prefetching
1068 has_many, as such a prefetch implies fetching multiple rows from the database in
1069 order to assemble the resulting object.
1070  
1071 =back
1072  
1073 =cut
1074  
1075 2716 ▶sub single {
Subroutine single: called CC=7 SCAR=20.6
Pod documented
1076 2716 my ($self, $where) = @_;
1077 2716 ▶ if(@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 0 2716 2716
1078 0 $self->throw_exception('single() only takes search conditions, no attributes. You want ->search( $cond, $attrs )->single()');
1079 }
1080  
1081 2716 my $attrs = { %{$self->_resolved_attrs} };
1082  
1083 $self->throw_exception(
1084 'single() can not be used on resultsets collapsing a has_many. Use find( \%cond ) or next() instead'
1085 2716 ▶ ) if $attrs->{collapse};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"collapse"} 1 2715 2716
1086  
1087 2715 ▶ if ($where) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where) 9 2706 2715
1088 9 ▶ if (defined $attrs->{where}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attrs->{'where'}) { } 0 9 9
1089 $attrs->{where} = {
1090 '-and' =>
1091 0 ▶ [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
1092 0 $where, delete $attrs->{where} ]
1093 };
1094 } else {
1095 9 $attrs->{where} = $where;
1096 }
1097 }
1098  
1099 my $data = [ $self->result_source->storage->select_single(
1100 $attrs->{from}, $attrs->{select},
1101 2715 $attrs->{where}, $attrs
1102 )];
1103  
1104 2714 ▶ return undef unless @$data;
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 294 2420 2714
1105 2420 $self->{_stashed_rows} = [ $data ];
1106 2420 $self->_construct_results->[0];
1107 }
1108  
1109 =head2 get_column
1110  
1111 =over 4
1112  
1113 =item Arguments: L<$cond?|DBIx::Class::SQLMaker>
1114  
1115 =item Return Value: L<$resultsetcolumn|DBIx::Class::ResultSetColumn>
1116  
1117 =back
1118  
1119 my $max_length = $rs->get_column('length')->max;
1120  
1121 Returns a L<DBIx::Class::ResultSetColumn> instance for a column of the ResultSet.
1122  
1123 =cut
1124  
1125 713 ▶sub get_column {
Subroutine get_column: called CC=1 SCAR=0.0
Pod documented
1126 713 my ($self, $column) = @_;
1127 713 my $new = DBIx::Class::ResultSetColumn->new($self, $column);
1128 713 return $new;
1129 }
1130  
1131 =head2 search_like
1132  
1133 =over 4
1134  
1135 =item Arguments: L<$cond|DBIx::Class::SQLMaker>, L<\%attrs?|/ATTRIBUTES>
1136  
1137 =item Return Value: L<$resultset|/search> (scalar context) | L<@result_objs|DBIx::Class::Manual::ResultClass> (list context)
1138  
1139 =back
1140  
1141 # WHERE title LIKE '%blue%'
1142 $cd_rs = $rs->search_like({ title => '%blue%'});
1143  
1144 Performs a search, but uses C<LIKE> instead of C<=> as the condition. Note
1145 that this is simply a convenience method retained for ex Class::DBI users.
1146 You most likely want to use L</search> with specific operators.
1147  
1148 For more information, see L<DBIx::Class::Manual::Cookbook>.
1149  
1150 This method is deprecated and will be removed in 0.09. Use L<search()|/search>
1151 instead. An example conversion is:
1152  
1153 ->search_like({ foo => 'bar' });
1154  
1155 # Becomes
1156  
1157 ->search({ foo => { like => 'bar' } });
1158  
1159 =cut
1160  
1161 0 ▶sub search_like {
Subroutine search_like: not called CC=5 SCAR=34.0
Pod documented
1162 0 my $class = shift;
1163 0 carp_unique (
1164 'search_like() is deprecated and will be removed in DBIC version 0.09.'
1165 .' Instead use ->search({ x => { -like => "y%" } })'
1166 .' (note the outer pair of {}s - they are important!)'
1167 );
1168 0 ▶ my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table0%0/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
1169 0 ▶ my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
1170 0 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
1171 0 return $class->search($query, { %$attrs });
1172 }
1173  
1174 =head2 slice
1175  
1176 =over 4
1177  
1178 =item Arguments: $first, $last
1179  
1180 =item Return Value: L<$resultset|/search> (scalar context) | L<@result_objs|DBIx::Class::Manual::ResultClass> (list context)
1181  
1182 =back
1183  
1184 Returns a resultset or object list representing a subset of elements from the
1185 resultset slice is called on. Indexes are from 0, i.e., to get the first
1186 three records, call:
1187  
1188 my ($one, $two, $three) = $rs->slice(0, 2);
1189  
1190 =cut
1191  
1192 14 ▶sub slice {
Subroutine slice: called CC=3 SCAR=11.0
Pod documented
1193 14 my ($self, $min, $max) = @_;
1194 14 my $attrs = {}; # = { %{ $self->{attrs} || {} } };
1195 14 ▶ $attrs->{offset} = $self->{attrs}{offset} || 0;
Condition50%1/2
$self->{'attrs'}{'offset'} || 0
l!l
014
MC/DC0%0/1
$self->{'attrs'}{'offset'} || 0
$self->{'attrs'}{'offset'}
Truth table50%1/2
A
A= $self->{'attrs'}{'offset'}
Aresult
11
00
1196 14 $attrs->{offset} += $min;
1197 14 ▶ $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
Branch: 1 branch
BranchTrueFalseTotal
$max ? : 13 1 14
1198 14 return $self->search(undef, $attrs);
1199 }
1200  
1201 =head2 next
1202  
1203 =over 4
1204  
1205 =item Arguments: none
1206  
1207 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass> | undef
1208  
1209 =back
1210  
1211 Returns the next element in the resultset (C<undef> is there is none).
1212  
1213 Can be used to efficiently iterate over records in the resultset:
1214  
1215 my $rs = $schema->resultset('CD')->search;
1216 while (my $cd = $rs->next) {
1217 print $cd->title;
1218 }
1219  
1220 Note that you need to store the resultset object, and call C<next> on it.
1221 Calling C<< resultset('Table')->next >> repeatedly will always return the
1222 first record from the resultset.
1223  
1224 =cut
1225  
1226 4887 ▶sub next {
Subroutine next: called CC=7 SCAR=19.5
Pod documented
1227 4887 my ($self) = @_;
1228  
1229 4887 ▶ if (my $cache = $self->get_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cache = $self->get_cache) 65 4822 4887
1230 65 ▶ $self->{all_cache_position} ||= 0;
Condition100%2/2
$self->{"all_cache_position"} ||= 0
l!l
1352
MC/DC100%1/1
$self->{"all_cache_position"} ||= 0
$self->{"all_cache_position"}
Truth table100%2/2
A
A= $self->{"all_cache_position"}
Aresult
11
00
1231 65 return $cache->[$self->{all_cache_position}++];
1232 }
1233  
1234 4822 ▶ if ($self->{attrs}{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attrs"}{"cache"}) 2 4820 4822
1235 2 delete $self->{pager};
1236 2 $self->{all_cache_position} = 1;
1237 2 return ($self->all)[0];
1238 }
1239  
1240 4820 ▶ return shift(@{$self->{_stashed_results}}) if @{ $self->{_stashed_results}||[] };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_stashed_results"} or [];} 11 4809 4820
Condition100%2/2
$self->{'_stashed_results'} || []
l!l
31891631
MC/DC100%1/1
$self->{'_stashed_results'} || []
$self->{'_stashed_results'}
Truth table100%2/2
A
A= $self->{'_stashed_results'}
Aresult
11
00
1241  
1242 4809 ▶ $self->{_stashed_results} = $self->_construct_results
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_stashed_results"} = $self->_construct_results or return undef 4069 704 4773
1243 or return undef;
1244  
1245 4069 return shift @{$self->{_stashed_results}};
1246 }
1247  
1248 # Constructs as many results as it can in one pass while respecting
1249 # cursor laziness. Several modes of operation:
1250 #
1251 # * Always builds everything present in @{$self->{_stashed_rows}}
1252 # * If called with $fetch_all true - pulls everything off the cursor and
1253 # builds all result structures (or objects) in one pass
1254 # * If $self->_resolved_attrs->{collapse} is true, checks the order_by
1255 # and if the resultset is ordered properly by the left side:
1256 # * Fetches stuff off the cursor until the "master object" changes,
1257 # and saves the last extra row (if any) in @{$self->{_stashed_rows}}
1258 # OR
1259 # * Just fetches, and collapses/constructs everything as if $fetch_all
1260 # was requested (there is no other way to collapse except for an
1261 # eager cursor)
1262 # * If no collapse is requested - just get the next row, construct and
1263 # return
1264 8840 ▶sub _construct_results {
Subroutine _construct_results: called CC=69 SCAR=42.8
1265 8840 my ($self, $fetch_all) = @_;
1266  
1267 8840 my $rsrc = $self->result_source;
1268 8840 my $attrs = $self->_resolved_attrs;
1269  
1270 8839 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $fetch_all || $attrs->{"order_by"} and $attrs->{"collapse"} and my(@pcols) = $rsrc->primary_columns) 34 8805 8839
Condition88%8/9
not $fetch_all || $attrs->{"order_by"} and $attrs->{"collapse"} and my(@pcols) = $rsrc->primary_columns
!ll&&!rl&&r
8805034
not $fetch_all || $attrs->{"order_by"} and $attrs->{"collapse"}
!ll&&!rl&&r
6828197734
$fetch_all || $attrs->{'order_by'}
l!l&&r!l&&!r
161152172011
MC/DC75%3/4
not $fetch_all || $attrs->{"order_by"} and $attrs->{"collapse"} and my(@pcols) = $rsrc->primary_columns
$fetch_all $attrs->{'order_by'} $attrs->{"collapse"} my(@pcols) = $rsrc->primary_columns
Truth table80%4/5
not(A || B) and C and D
A= $fetch_allB= $attrs->{'order_by'}C= $attrs->{"collapse"}D= my(@pcols) = $rsrc->primary_columns
ABCDresult
1XXX0
01XX0
000X0
00100
00111
1271 ! $fetch_all
1272 and
1273 ! $attrs->{order_by}
1274 and
1275 $attrs->{collapse}
1276 and
1277 my @pcols = $rsrc->primary_columns
1278 ) {
1279 # default order for collapsing unless the user asked for something
1280 34 $attrs->{order_by} = [ map { join '.', $attrs->{alias}, $_} @pcols ];
1281 34 $attrs->{_ordered_for_collapse} = 1;
1282 34 $attrs->{_order_is_artificial} = 1;
1283 }
1284  
1285 # this will be used as both initial raw-row collector AND as a RV of
1286 # _construct_results. Not regrowing the array twice matters a lot...
1287 # a surprising amount actually
1288 8839 my $rows = delete $self->{_stashed_rows};
1289  
1290 8839 my $cursor; # we may not need one at all
1291  
1292 8839 my $did_fetch_all = $fetch_all;
1293  
1294 8839 ▶ if ($fetch_all) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fetch_all) { } 1611 7228 8839
elsif ($attrs->{'collapse'}) { } 109 7119 7228
1295 # FIXME SUBOPTIMAL - we can do better, cursor->next/all (well diff. methods) should return a ref
1296 1611 ▶ $rows = [ ($rows ? @$rows : ()), $self->cursor->all ];
Branch: 1 branch
BranchTrueFalseTotal
$rows ? : 0 1611 1611
1297 }
1298 elsif( $attrs->{collapse} ) {
1299  
1300 # a cursor will need to be closed over in case of collapse
1301 109 $cursor = $self->cursor;
1302  
1303 $attrs->{_ordered_for_collapse} = (
1304 (
1305 $attrs->{order_by}
1306 and
1307 $rsrc->schema
1308 ->storage
1309 ->_extract_colinfo_of_stable_main_source_order_by_portion($attrs)
1310 ) ? 1 : 0
1311 109 ▶ ) unless defined $attrs->{_ordered_for_collapse};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $attrs->{"_ordered_for_collapse"} 19 90 109
$attrs->{'order_by'} && $rsrc->schema->storage->_extract_colinfo_of_stable_main_source_order_by_portion($attrs) ? : 11 8 19
Condition66%2/3
$attrs->{"order_by"} and $rsrc->schema->storage->_extract_colinfo_of_stable_main_source_order_by_portion($attrs)
!ll&&!rl&&r
0811
MC/DC50%1/2
$attrs->{"order_by"} and $rsrc->schema->storage->_extract_colinfo_of_stable_main_source_order_by_portion($attrs)
$attrs->{"order_by"} $rsrc->schema->storage->_extract_colinfo_of_stable_main_source_order_by_portion($attrs)
Truth table66%2/3
A and B
A= $attrs->{"order_by"}B= $rsrc->schema->storage->_extract_colinfo_of_stable_main_source_order_by_portion($attrs)
ABresult
0X0
100
111
1312  
1313 109 ▶ if (! $attrs->{_ordered_for_collapse}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attrs->{"_ordered_for_collapse"}) 13 96 109
1314 13 $did_fetch_all = 1;
1315  
1316 # instead of looping over ->next, use ->all in stealth mode
1317 # *without* calling a ->reset afterwards
1318 # FIXME ENCAPSULATION - encapsulation breach, cursor method additions pending
1319 13 ▶ if (! $cursor->{_done}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cursor->{"_done"}) 8 5 13
1320 8 ▶ $rows = [ ($rows ? @$rows : ()), $cursor->all ];
Branch: 1 branch
BranchTrueFalseTotal
$rows ? : 0 8 8
1321 7 $cursor->{_done} = 1;
1322 }
1323 }
1324 }
1325  
1326 8830 ▶ if (! $did_fetch_all and ! @{$rows||[]} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($did_fetch_all or @{$rows or [];}) 4771 4059 8830
Condition100%5/5
$did_fetch_all or @{$rows or [];}
l!l&&r!l&&!r
161524444771
$rows || []
l!l
24544761
MC/DC100%2/2
$did_fetch_all or @{$rows or [];}
$did_fetch_all @{$rows or [];}
MC/DC100%1/1
$rows || []
$rows
Truth table100%3/3
A or B
A= $did_fetch_allB= @{$rows or [];}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $rows
Aresult
11
00
1327 # FIXME SUBOPTIMAL - we can do better, cursor->next/all (well diff. methods) should return a ref
1328 4771 ▶ $cursor ||= $self->cursor;
Condition66%2/3
$cursor ||= $self->cursor
l!l&&r!l&&!r
7246990
MC/DC0%0/2
$cursor ||= $self->cursor
$cursor $self->cursor
Truth table66%2/3
A ||= B
A= $cursorB= $self->cursor
ABresult
1X1
011
000
1329 4771 ▶ if (scalar (my @r = $cursor->next) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(my(@r) = $cursor->next)) 4065 699 4764
1330 4065 $rows = [ \@r ];
1331 }
1332 }
1333  
1334 8823 ▶ return undef unless @{$rows||[]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$rows or [];} 989 7834 8823
Condition100%2/2
$rows || []
l!l
8129694
MC/DC100%1/1
$rows || []
$rows
Truth table100%2/2
A
A= $rows
Aresult
11
00
1335  
1336 # sanity check - people are too clever for their own good
1337 7834 ▶ if ($attrs->{collapse} and my $aliastypes = $attrs->{_last_sqlmaker_alias_map} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"collapse"} and my $aliastypes = $attrs->{"_last_sqlmaker_alias_map"}) 226 7608 7834
Condition66%2/3
$attrs->{"collapse"} and my $aliastypes = $attrs->{"_last_sqlmaker_alias_map"}
!ll&&!rl&&r
76080226
MC/DC50%1/2
$attrs->{"collapse"} and my $aliastypes = $attrs->{"_last_sqlmaker_alias_map"}
$attrs->{"collapse"} my $aliastypes = $attrs->{"_last_sqlmaker_alias_map"}
Truth table66%2/3
A and B
A= $attrs->{"collapse"}B= my $aliastypes = $attrs->{"_last_sqlmaker_alias_map"}
ABresult
0X0
100
111
1338  
1339 226 my $multiplied_selectors;
1340 226 for my $sel_alias ( grep { $_ ne $attrs->{alias} } keys %{ $aliastypes->{selecting} } ) {
1341 423 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($aliastypes->{"multiplying"}{$sel_alias} or $aliastypes->{"premultiplied"}{$sel_alias}) 341 82 423
Condition100%3/3
$aliastypes->{"multiplying"}{$sel_alias} or $aliastypes->{"premultiplied"}{$sel_alias}
l!l&&r!l&&!r
2934882
MC/DC100%2/2
$aliastypes->{"multiplying"}{$sel_alias} or $aliastypes->{"premultiplied"}{$sel_alias}
$aliastypes->{"multiplying"}{$sel_alias} $aliastypes->{"premultiplied"}{$sel_alias}
Truth table100%3/3
A or B
A= $aliastypes->{"multiplying"}{$sel_alias}B= $aliastypes->{"premultiplied"}{$sel_alias}
ABresult
1X1
011
000
1342 $aliastypes->{multiplying}{$sel_alias}
1343 or
1344 $aliastypes->{premultiplied}{$sel_alias}
1345 ) {
1346 341 $multiplied_selectors->{$_} = 1 for values %{$aliastypes->{selecting}{$sel_alias}{-seen_columns}}
1347 }
1348 }
1349  
1350 226 for my $i (0 .. $#{$attrs->{as}} ) {
1351 2167 my $sel = $attrs->{select}[$i];
1352  
1353 2167 ▶ if (ref $sel eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $sel eq 'SCALAR') { } 3 2164 2167
elsif (ref $sel eq 'REF' and ref $$sel eq 'ARRAY') { } 6 4316 4322
1354 3 $sel = $$sel;
1355 }
1356 ▶ elsif( ref $sel eq 'REF' and ref $$sel eq 'ARRAY' ) {
Condition66%2/3
ref $sel eq "REF" and ref $$sel eq "ARRAY"
!ll&&!rl&&r
215806
MC/DC50%1/2
ref $sel eq "REF" and ref $$sel eq "ARRAY"
ref $sel eq "REF" ref $$sel eq "ARRAY"
Truth table66%2/3
A and B
A= ref $sel eq "REF"B= ref $$sel eq "ARRAY"
ABresult
0X0
100
111
1357 6 $sel = $$sel->[0];
1358 }
1359  
1360 $self->throw_exception(
1361 'Result collapse not possible - selection from a has_many source redirected to the main object'
1362 2167 ▶ ) if ($multiplied_selectors->{$sel} and $attrs->{as}[$i] !~ /\./);
Branch: 1 branch
BranchTrueFalseTotal
if $multiplied_selectors->{$sel} and not $attrs->{"as"}[$i] =~ /\./ 36 2131 2167
Condition100%3/3
$multiplied_selectors->{$sel} and not $attrs->{"as"}[$i] =~ /\./
!ll&&!rl&&r
993113836
MC/DC100%2/2
$multiplied_selectors->{$sel} and not $attrs->{"as"}[$i] =~ /\./
$multiplied_selectors->{$sel} not $attrs->{"as"}[$i] =~ /\./
Truth table100%3/3
A and not(B)
A= $multiplied_selectors->{$sel}B= not $attrs->{"as"}[$i] =~ /\./
ABresult
0X0
100
111
1363 }
1364 }
1365  
1366 # hotspot - skip the setter
1367 7798 my $res_class = $self->_result_class;
1368  
1369 7798 ▶ my $inflator_cref = $self->{_result_inflator}{cref} ||= do {
Condition66%2/3
$self->{"_result_inflator"}{"cref"} ||= do { $res_class->can("inflate_result") or $self->throw_exception("Inflator $res_class does not provide an inflate_result() method") }
l!l&&r!l&&!r
315446440
MC/DC0%0/2
$self->{"_result_inflator"}{"cref"} ||= do { $res_class->can("inflate_result") or $self->throw_exception("Inflator $res_class does not provide an inflate_result() method") }
$self->{"_result_inflator"}{"cref"} do { $res_class->can("inflate_result") or $self->throw_exception("Inflator $res_class does not provide an inflate_result() method") }
Truth table66%2/3
A ||= B
A= $self->{"_result_inflator"}{"cref"}B= do { $res_class->can("inflate_result") or $self->throw_exception("Inflator $res_class does not provide an inflate_result() method") }
ABresult
1X1
011
000
1370 4644 ▶ $res_class->can ('inflate_result')
Condition66%2/3
$res_class->can('inflate_result') || $self->throw_exception("Inflator $res_class does not provide an inflate_result() method")
l!l&&r!l&&!r
464310
MC/DC0%0/2
$res_class->can('inflate_result') || $self->throw_exception("Inflator $res_class does not provide an inflate_result() method")
$res_class->can('inflate_result') $self->throw_exception("Inflator $res_class does not provide an inflate_result() method")
Truth table33%1/3
A || B
A= $res_class->can('inflate_result')B= $self->throw_exception("Inflator $res_class does not provide an inflate_result() method")
ABresult
1X1
011
000
1371 or $self->throw_exception("Inflator $res_class does not provide an inflate_result() method");
1372 };
1373  
1374 7797 my $infmap = $attrs->{as};
1375  
1376 $self->{_result_inflator}{is_core_row} = ( (
1377 $inflator_cref
1378 ==
1379 ( \&DBIx::Class::Row::inflate_result || die "No ::Row::inflate_result() - can't happen" )
1380 7797 ▶ ) ? 1 : 0 ) unless defined $self->{_result_inflator}{is_core_row};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->{"_result_inflator"}{"is_core_row"} 4638 3159 7797
$inflator_cref == (\&DBIx::Class::Row::inflate_result || die(q[No ::Row::inflate_result() - can't happen])) ? : 4529 109 4638
Condition50%1/2
\&DBIx::Class::Row::inflate_result || die(q[No ::Row::inflate_result() - can't happen])
l!l
46380
MC/DC0%0/1
\&DBIx::Class::Row::inflate_result || die(q[No ::Row::inflate_result() - can't happen])
\&DBIx::Class::Row::inflate_result
Truth table50%1/2
A
A= \&DBIx::Class::Row::inflate_result
Aresult
11
00
1381  
1382 $self->{_result_inflator}{is_hri} = ( (
1383 ! $self->{_result_inflator}{is_core_row}
1384 and
1385 $inflator_cref == (
1386 require DBIx::Class::ResultClass::HashRefInflator
1387 &&
1388 DBIx::Class::ResultClass::HashRefInflator->can('inflate_result')
1389 )
1390 7797 ▶ ) ? 1 : 0 ) unless defined $self->{_result_inflator}{is_hri};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->{"_result_inflator"}{"is_hri"} 4643 3154 7797
!$self->{'_result_inflator'}{'is_core_row'} && $inflator_cref == (require DBIx::Class::ResultClass::HashRefInflator && 'DBIx::Class::ResultClass::HashRefInflator'->can('inflate_result')) ? : 96 4547 4643
Condition66%4/6
not $self->{"_result_inflator"}{"is_core_row"} and $inflator_cref == (require DBIx::Class::ResultClass::HashRefInflator && "DBIx::Class::ResultClass::HashRefInflator"->can("inflate_result"))
!ll&&!rl&&r
45341396
require DBIx::Class::ResultClass::HashRefInflator && 'DBIx::Class::ResultClass::HashRefInflator'->can('inflate_result')
!ll&&!rl&&r
00109
MC/DC100%2/2
not $self->{"_result_inflator"}{"is_core_row"} and $inflator_cref == (require DBIx::Class::ResultClass::HashRefInflator && "DBIx::Class::ResultClass::HashRefInflator"->can("inflate_result"))
not $self->{"_result_inflator"}{"is_core_row"} $inflator_cref == (require DBIx::Class::ResultClass::HashRefInflator && "DBIx::Class::ResultClass::HashRefInflator"->can("inflate_result"))
MC/DC0%0/2
require DBIx::Class::ResultClass::HashRefInflator && 'DBIx::Class::ResultClass::HashRefInflator'->can('inflate_result')
require DBIx::Class::ResultClass::HashRefInflator 'DBIx::Class::ResultClass::HashRefInflator'->can('inflate_result')
Truth table100%3/3
not(A) and B
A= not $self->{"_result_inflator"}{"is_core_row"}B= $inflator_cref == (require DBIx::Class::ResultClass::HashRefInflator && "DBIx::Class::ResultClass::HashRefInflator"->can("inflate_result"))
ABresult
0X0
100
111
Truth table33%1/3
A && B
A= require DBIx::Class::ResultClass::HashRefInflatorB= 'DBIx::Class::ResultClass::HashRefInflator'->can('inflate_result')
ABresult
0X0
100
111
1391  
1392  
1393 7797 ▶ if ($attrs->{_simple_passthrough_construction}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{'_simple_passthrough_construction'}) { } 7531 266 7797
1394 # construct a much simpler array->hash folder for the one-table HRI cases right here
1395 7531 ▶ if ($self->{_result_inflator}{is_hri}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_result_inflator'}{'is_hri'}) { } 51 7480 7531
elsif ($self->{'_result_inflator'}{'is_core_row'} and @$rows < 60) { } 7476 4 7480
1396 51 for my $r (@$rows) {
1397 142 $r = { map { $infmap->[$_] => $r->[$_] } 0..$#$infmap };
1398 }
1399 }
1400 # FIXME SUBOPTIMAL this is a very very very hot spot
1401 # while rather optimal we can *still* do much better, by
1402 # building a smarter Row::inflate_result(), and
1403 # switch to feeding it data via a much leaner interface
1404 #
1405 # crude unscientific benchmarking indicated the shortcut eval is not worth it for
1406 # this particular resultset size
1407 ▶ elsif ( $self->{_result_inflator}{is_core_row} and @$rows < 60 ) {
Condition66%2/3
$self->{"_result_inflator"}{"is_core_row"} and @$rows < 60
!ll&&!rl&&r
407476
MC/DC50%1/2
$self->{"_result_inflator"}{"is_core_row"} and @$rows < 60
$self->{"_result_inflator"}{"is_core_row"} @$rows < 60
Truth table66%2/3
A and B
A= $self->{"_result_inflator"}{"is_core_row"}B= @$rows < 60
ABresult
0X0
100
111
1408 7476 for my $r (@$rows) {
1409 8719 $r = $inflator_cref->($res_class, $rsrc, { map { $infmap->[$_] => $r->[$_] } (0..$#$infmap) } );
1410 }
1411 }
1412 else {
1413 eval sprintf (
1414 ( $self->{_result_inflator}{is_core_row}
1415 ? '$_ = $inflator_cref->($res_class, $rsrc, { %s }) for @$rows'
1416 # a custom inflator may be a multiplier/reductor - put it in direct list ctx
1417 : '@$rows = map { $inflator_cref->($res_class, $rsrc, { %s } ) } @$rows'
1418 ),
1419 4 ▶ ( join (', ', map { "\$infmap->[$_] => \$_->[$_]" } 0..$#$infmap ) )
Branch: 2 branches
BranchTrueFalseTotal
eval sprintf($self->{"_result_inflator"}{"is_core_row"} ? "\$_ = \$inflator_cref->(\$res_class, \$rsrc, { %s }) for \@\$rows" : "\@\$rows = map { \$inflator_cref->(\$res_class, \$rsrc, { %s } ) } \@\$rows", join(", ", map({"\$infmap->[$_] => \$_->[$_]";} 0 .. $infmap->$#*))) . '; 1' or die 3 1 4
$self->{'_result_inflator'}{'is_core_row'} ? : 0 4 4
1420 ) . '; 1' or die;
1421 }
1422 }
1423 else {
1424 my $parser_type =
1425 $self->{_result_inflator}{is_hri} ? 'hri'
1426 266 ▶ : $self->{_result_inflator}{is_core_row} ? 'classic_pruning'
Branch: 2 branches
BranchTrueFalseTotal
$self->{'_result_inflator'}{'is_hri'} ? : 57 209 266
$self->{'_result_inflator'}{'is_core_row'} ? : 200 9 209
1427 : 'classic_nonpruning'
1428 ;
1429  
1430 # $args and $attrs to _mk_row_parser are separated to delineate what is
1431 # core collapser stuff and what is dbic $rs specific
1432 223 @{$self->{_row_parser}{$parser_type}}{qw(cref nullcheck)} = $rsrc->_mk_row_parser({
1433 eval => 1,
1434 inflate_map => $infmap,
1435 collapse => $attrs->{collapse},
1436 premultiplied => $attrs->{_main_source_premultiplied},
1437 hri_style => $self->{_result_inflator}{is_hri},
1438 prune_null_branches => $self->{_result_inflator}{is_hri} || $self->{_result_inflator}{is_core_row},
1439 266 ▶ }, $attrs) unless $self->{_row_parser}{$parser_type}{cref};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_row_parser"}{$parser_type}{"cref"} 223 43 266
Condition100%3/3
$self->{'_result_inflator'}{'is_hri'} || $self->{'_result_inflator'}{'is_core_row'}
l!l&&r!l&&!r
511639
MC/DC100%2/2
$self->{'_result_inflator'}{'is_hri'} || $self->{'_result_inflator'}{'is_core_row'}
$self->{'_result_inflator'}{'is_hri'} $self->{'_result_inflator'}{'is_core_row'}
Truth table100%3/3
A || B
A= $self->{'_result_inflator'}{'is_hri'}B= $self->{'_result_inflator'}{'is_core_row'}
ABresult
1X1
011
000
1440  
1441 # column_info metadata historically hasn't been too reliable.
1442 # We need to start fixing this somehow (the collapse resolver
1443 # can't work without it). Add an explicit check for the *main*
1444 # result, hopefully this will gradually weed out such errors
1445 #
1446 # FIXME - this is a temporary kludge that reduces performance
1447 # It is however necessary for the time being
1448 266 my ($unrolled_non_null_cols_to_check, $err);
1449  
1450 266 ▶ if (my $check_non_null_cols = $self->{_row_parser}{$parser_type}{nullcheck} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $check_non_null_cols = $self->{"_row_parser"}{$parser_type}{"nullcheck"}) 168 98 266
1451  
1452 168 $err =
1453 'Collapse aborted due to invalid ResultSource metadata - the following '
1454 . 'selections are declared non-nullable but NULLs were retrieved: '
1455 ;
1456  
1457 168 my @violating_idx;
1458 168 COL: for my $i (@$check_non_null_cols) {
1459 174 ▶ ! defined $_->[$i] and push @violating_idx, $i and next COL for @$rows;
Branch: 1 branch
BranchTrueFalseTotal
not defined $_->[$i] and push @violating_idx, $i and next COL 0 989 989
Condition33%1/3
not defined $_->[$i] and push @violating_idx, $i
!ll&&!rl&&r
98900
MC/DC0%0/2
not defined $_->[$i] and push @violating_idx, $i
not defined $_->[$i] push @violating_idx, $i
Truth table33%1/3
not(A) and B
A= not defined $_->[$i]B= push @violating_idx, $i
ABresult
0X0
100
111
1460 }
1461  
1462 0 ▶ $self->throw_exception( $err . join (', ', map { "'$infmap->[$_]'" } @violating_idx ) )
Branch: 1 branch
BranchTrueFalseTotal
if @violating_idx 0 168 168
1463 if @violating_idx;
1464  
1465 168 $unrolled_non_null_cols_to_check = join (',', @$check_non_null_cols);
1466  
1467 168 utf8::upgrade($unrolled_non_null_cols_to_check)
1468 if DBIx::Class::_ENV_::STRESSTEST_UTF8_UPGRADE_GENERATED_COLLAPSER_SOURCE;
1469 }
1470  
1471 my $next_cref =
1472 ($did_fetch_all or ! $attrs->{collapse}) ? undef
1473 : defined $unrolled_non_null_cols_to_check ? eval sprintf <<'EOS', $unrolled_non_null_cols_to_check
1474 sub {
1475 # FIXME SUBOPTIMAL - we can do better, cursor->next/all (well diff. methods) should return a ref
1476 my @r = $cursor->next or return;
1477 if (my @violating_idx = grep { ! defined $r[$_] } (%s) ) {
1478 $self->throw_exception( $err . join (', ', map { "'$infmap->[$_]'" } @violating_idx ) )
1479 }
1480 \@r
1481 }
1482 EOS
1483 90 ▶ : sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1484 # FIXME SUBOPTIMAL - we can do better, cursor->next/all (well diff. methods) should return a ref
1485 90 ▶ my @r = $cursor->next or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@r) = $cursor->next or return 88 2 90
1486 \@r
1487 88 }
1488 266 ▶ ;
Branch: 2 branches
BranchTrueFalseTotal
$did_fetch_all || !$attrs->{'collapse'} ? : 210 56 266
defined $unrolled_non_null_cols_to_check ? : 44 12 56
Condition100%3/3
$did_fetch_all or not $attrs->{"collapse"}
l!l&&r!l&&!r
1842656
MC/DC100%2/2
$did_fetch_all or not $attrs->{"collapse"}
$did_fetch_all not $attrs->{"collapse"}
Truth table100%3/3
A or not(B)
A= $did_fetch_allB= not $attrs->{"collapse"}
ABresult
1X1
011
000
1489  
1490 $self->{_row_parser}{$parser_type}{cref}->(
1491 $rows,
1492 266 ▶ $next_cref ? ( $next_cref, $self->{_stashed_rows} = [] ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$next_cref ? : 56 210 266
1493 );
1494  
1495 # simple in-place substitution, does not regrow $rows
1496 266 ▶ if ($self->{_result_inflator}{is_core_row}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_result_inflator'}{'is_core_row'}) { } 200 66 266
elsif (not $self->{'_result_inflator'}{'is_hri'}) { } 9 57 66
1497 200 $_ = $inflator_cref->($res_class, $rsrc, @$_) for @$rows
1498 }
1499 # Special-case multi-object HRI - there is no $inflator_cref pass at all
1500 elsif ( ! $self->{_result_inflator}{is_hri} ) {
1501 # the inflator may be a multiplier/reductor - put it in list ctx
1502 9 @$rows = map { $inflator_cref->($res_class, $rsrc, @$_) } @$rows;
1503 }
1504 }
1505  
1506 # The @$rows check seems odd at first - why wouldn't we want to warn
1507 # regardless? The issue is things like find() etc, where the user
1508 # *knows* only one result will come back. In these cases the ->all
1509 # is not a pessimization, but rather something we actually want
1510 7794 ▶ carp_unique(
Branch: 1 branch
BranchTrueFalseTotal
if not $fetch_all and @$rows > 1 3 7791 7794
Condition100%3/3
not $fetch_all and @$rows > 1
!ll&&!rl&&r
130564863
MC/DC100%2/2
not $fetch_all and @$rows > 1
not $fetch_all @$rows > 1
Truth table100%3/3
not(A) and B
A= not $fetch_allB= @$rows > 1
ABresult
0X0
100
111
1511 'Unable to properly collapse has_many results in iterator mode due '
1512 . 'to order criteria - performed an eager cursor slurp underneath. '
1513 . 'Consider using ->all() instead'
1514 ) if ( ! $fetch_all and @$rows > 1 );
1515  
1516 7794 return $rows;
1517 }
1518  
1519 =head2 result_source
1520  
1521 =over 4
1522  
1523 =item Arguments: L<$result_source?|DBIx::Class::ResultSource>
1524  
1525 =item Return Value: L<$result_source|DBIx::Class::ResultSource>
1526  
1527 =back
1528  
1529 An accessor for the primary ResultSource object from which this ResultSet
1530 is derived.
1531  
1532 =head2 result_class
1533  
1534 =over 4
1535  
1536 =item Arguments: $result_class?
1537  
1538 =item Return Value: $result_class
1539  
1540 =back
1541  
1542 An accessor for the class to use when creating result objects. Defaults to
1543 C<< result_source->result_class >> - which in most cases is the name of the
1544 L<"table"|DBIx::Class::Manual::Glossary/"ResultSource"> class.
1545  
1546 Note that changing the result_class will also remove any components
1547 that were originally loaded in the source class via
1548 L<load_components|Class::C3::Componentised/load_components( @comps )>.
1549 Any overloaded methods in the original source class will not run.
1550  
1551 =cut
1552  
1553 36991 ▶sub result_class {
Subroutine result_class: called CC=6 SCAR=17.9
Pod documented
1554 36991 my ($self, $result_class) = @_;
1555 36991 ▶ if ($result_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_class) 32974 4017 36991
1556  
1557 # don't fire this for an object
1558 32974 ▶ $self->ensure_class_loaded($result_class)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result_class 32974 0 32974
1559 unless ref($result_class);
1560  
1561 32970 ▶ if ($self->get_cache) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_cache) { } 1 32969 32970
elsif ($self->{'cursor'} and $self->{'cursor'}{'_pos'}) { } 1 65935 65936
1562 1 carp_unique('Changing the result_class of a ResultSet instance with cached results is a noop - the cache contents will not be altered');
1563 }
1564 # FIXME ENCAPSULATION - encapsulation breach, cursor method additions pending
1565 ▶ elsif ($self->{cursor} && $self->{cursor}{_pos}) {
Condition100%3/3
$self->{"cursor"} and $self->{"cursor"}{"_pos"}
!ll&&!rl&&r
3296711
MC/DC100%2/2
$self->{"cursor"} and $self->{"cursor"}{"_pos"}
$self->{"cursor"} $self->{"cursor"}{"_pos"}
Truth table100%3/3
A and B
A= $self->{"cursor"}B= $self->{"cursor"}{"_pos"}
ABresult
0X0
100
111
1566 1 $self->throw_exception('Changing the result_class of a ResultSet instance with an active cursor is not supported');
1567 }
1568  
1569 32969 $self->_result_class($result_class);
1570  
1571 32969 delete $self->{_result_inflator};
1572 }
1573 36986 $self->_result_class;
1574 }
1575  
1576 =head2 count
1577  
1578 =over 4
1579  
1580 =item Arguments: L<$cond|DBIx::Class::SQLMaker>, L<\%attrs?|/ATTRIBUTES>
1581  
1582 =item Return Value: $count
1583  
1584 =back
1585  
1586 Performs an SQL C<COUNT> with the same query as the resultset was built
1587 with to find the number of elements. Passing arguments is equivalent to
1588 C<< $rs->search ($cond, \%attrs)->count >>
1589  
1590 =cut
1591  
1592 621 ▶sub count {
Subroutine count: called CC=9 SCAR=22.0
Pod documented
1593 621 my $self = shift;
1594 621 ▶ return $self->search(@_)->count if @_ and defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if @_ and defined $_[0] 15 606 621
Condition100%3/3
@_ and defined $_[0]
!ll&&!rl&&r
5654115
MC/DC100%2/2
@_ and defined $_[0]
@_ defined $_[0]
Truth table100%3/3
A and B
A= @_B= defined $_[0]
ABresult
0X0
100
111
1595 606 ▶ return scalar @{ $self->get_cache } if $self->get_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_cache 58 548 606
1596  
1597 548 my $attrs = { %{ $self->_resolved_attrs } };
1598  
1599 # this is a little optimization - it is faster to do the limit
1600 # adjustments in software, instead of a subquery
1601 548 my ($rows, $offset) = delete @{$attrs}{qw/rows offset/};
1602  
1603 548 my $crs;
1604 548 ▶ if ($self->_has_resolved_attr (qw/collapse group_by/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_resolved_attr('collapse', 'group_by')) { } 65 483 548
1605 65 $crs = $self->_count_subq_rs ($attrs);
1606 }
1607 else {
1608 483 $crs = $self->_count_rs ($attrs);
1609 }
1610 548 my $count = $crs->next;
1611  
1612 545 ▶ $count -= $offset if $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $offset 22 523 545
1613 545 ▶ $count = $rows if $rows and $rows < $count;
Branch: 1 branch
BranchTrueFalseTotal
if $rows and $rows < $count 13 532 545
Condition100%3/3
$rows and $rows < $count
!ll&&!rl&&r
5082413
MC/DC100%2/2
$rows and $rows < $count
$rows $rows < $count
Truth table100%3/3
A and B
A= $rowsB= $rows < $count
ABresult
0X0
100
111
1614 545 ▶ $count = 0 if ($count < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $count < 0 1 544 545
1615  
1616 545 return $count;
1617 }
1618  
1619 =head2 count_rs
1620  
1621 =over 4
1622  
1623 =item Arguments: L<$cond|DBIx::Class::SQLMaker>, L<\%attrs?|/ATTRIBUTES>
1624  
1625 =item Return Value: L<$count_rs|DBIx::Class::ResultSetColumn>
1626  
1627 =back
1628  
1629 Same as L</count> but returns a L<DBIx::Class::ResultSetColumn> object.
1630 This can be very handy for subqueries:
1631  
1632 ->search( { amount => $some_rs->count_rs->as_query } )
1633  
1634 As with regular resultsets the SQL query will be executed only after
1635 the resultset is accessed via L</next> or L</all>. That would return
1636 the same single value obtainable via L</count>.
1637  
1638 =cut
1639  
1640 70 ▶sub count_rs {
Subroutine count_rs: called CC=3 SCAR=11.0
Pod documented
1641 70 my $self = shift;
1642 70 ▶ return $self->search(@_)->count_rs if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 10 60 70
1643  
1644 # this may look like a lack of abstraction (count() does about the same)
1645 # but in fact an _rs *must* use a subquery for the limits, as the
1646 # software based limiting can not be ported if this $rs is to be used
1647 # in a subquery itself (i.e. ->as_query)
1648 60 ▶ if ($self->_has_resolved_attr (qw/collapse group_by offset rows/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_resolved_attr('collapse', 'group_by', 'offset', 'rows')) { } 28 32 60
1649 28 return $self->_count_subq_rs($self->{_attrs});
1650 }
1651 else {
1652 32 return $self->_count_rs($self->{_attrs});
1653 }
1654 }
1655  
1656 #
1657 # returns a ResultSetColumn object tied to the count query
1658 #
1659 515 ▶sub _count_rs {
Subroutine _count_rs: called CC=1 SCAR=0.0
1660 515 my ($self, $attrs) = @_;
1661  
1662 515 my $rsrc = $self->result_source;
1663  
1664 515 my $tmp_attrs = { %$attrs };
1665 # take off any limits, record_filter is cdbi, and no point of ordering nor locking a count
1666 515 delete @{$tmp_attrs}{qw/rows offset order_by record_filter for/};
1667  
1668 # overwrite the selector (supplied by the storage)
1669 515 $rsrc->resultset_class->new($rsrc, {
1670 %$tmp_attrs,
1671 select => $rsrc->storage->_count_select ($rsrc, $attrs),
1672 as => 'count',
1673 })->get_column ('count');
1674 }
1675  
1676 #
1677 # same as above but uses a subquery
1678 #
1679 93 ▶sub _count_subq_rs {
Subroutine _count_subq_rs: called CC=19 SCAR=29.9
1680 93 my ($self, $attrs) = @_;
1681  
1682 93 my $rsrc = $self->result_source;
1683  
1684 93 my $sub_attrs = { %$attrs };
1685 # extra selectors do not go in the subquery and there is no point of ordering it, nor locking it
1686 93 delete @{$sub_attrs}{qw/collapse columns as select order_by for/};
1687  
1688 # if we multi-prefetch we group_by something unique, as this is what we would
1689 # get out of the rs via ->next/->all. We *DO WANT* to clobber old group_by regardless
1690 93 ▶ if ( $attrs->{collapse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"collapse"}) 31 62 93
1691 31 $sub_attrs->{group_by} = [ map { "$attrs->{alias}.$_" } @{
1692 31 ▶ $rsrc->_identifying_column_set || $self->throw_exception(
Condition33%1/3
$rsrc->_identifying_column_set || $self->throw_exception('Unable to construct a unique group_by criteria properly collapsing the has_many prefetch before count()')
l!l&&r!l&&!r
3100
MC/DC0%0/2
$rsrc->_identifying_column_set || $self->throw_exception('Unable to construct a unique group_by criteria properly collapsing the has_many prefetch before count()')
$rsrc->_identifying_column_set $self->throw_exception('Unable to construct a unique group_by criteria properly collapsing the has_many prefetch before count()')
Truth table33%1/3
A || B
A= $rsrc->_identifying_column_setB= $self->throw_exception('Unable to construct a unique group_by criteria properly collapsing the has_many prefetch before count()')
ABresult
1X1
011
000
1693 'Unable to construct a unique group_by criteria properly collapsing the '
1694 . 'has_many prefetch before count()'
1695 );
1696 } ]
1697 }
1698  
1699 # Calculate subquery selector
1700 93 ▶ if (my $g = $sub_attrs->{group_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $sub_attrs->{'group_by'}) { } 90 3 93
1701  
1702 90 my $sql_maker = $rsrc->storage->sql_maker;
1703  
1704 # necessary as the group_by may refer to aliased functions
1705 90 my $sel_index;
1706 90 for my $sel (@{$attrs->{select}}) {
1707 $sel_index->{$sel->{-as}} = $sel
1708 544 ▶ if (ref $sel eq 'HASH' and $sel->{-as});
Branch: 1 branch
BranchTrueFalseTotal
if ref $sel eq "HASH" and $sel->{"-as"} 11 533 544
Condition100%3/3
ref $sel eq "HASH" and $sel->{"-as"}
!ll&&!rl&&r
525811
MC/DC100%2/2
ref $sel eq "HASH" and $sel->{"-as"}
ref $sel eq "HASH" $sel->{"-as"}
Truth table100%3/3
A and B
A= ref $sel eq "HASH"B= $sel->{"-as"}
ABresult
0X0
100
111
1709 }
1710  
1711 # anything from the original select mentioned on the group-by needs to make it to the inner selector
1712 # also look for named aggregates referred in the having clause
1713 # having often contains scalarrefs - thus parse it out entirely
1714 90 my @parts = @$g;
1715 90 ▶ if ($attrs->{having}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"having"}) 3 87 90
1716 3 local $sql_maker->{having_bind};
1717 3 local $sql_maker->{quote_char} = $sql_maker->{quote_char};
1718 3 local $sql_maker->{name_sep} = $sql_maker->{name_sep};
1719 3 ▶ unless (defined $sql_maker->{quote_char} and length $sql_maker->{quote_char}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sql_maker->{"quote_char"} and length $sql_maker->{"quote_char"}) 3 0 3
Condition33%1/3
defined $sql_maker->{"quote_char"} and length $sql_maker->{"quote_char"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $sql_maker->{"quote_char"} and length $sql_maker->{"quote_char"}
defined $sql_maker->{"quote_char"} length $sql_maker->{"quote_char"}
Truth table33%1/3
A and B
A= defined $sql_maker->{"quote_char"}B= length $sql_maker->{"quote_char"}
ABresult
0X0
100
111
1720 3 $sql_maker->{quote_char} = [ "\x00", "\xFF" ];
1721 # if we don't unset it we screw up retarded but unfortunately working
1722 # 'MAX(foo.bar)' => { '>', 3 }
1723 3 $sql_maker->{name_sep} = '';
1724 }
1725  
1726 3 my ($lquote, $rquote, $sep) = map { quotemeta $_ } ($sql_maker->_quote_chars, $sql_maker->name_sep);
1727  
1728 3 my $having_sql = $sql_maker->_parse_rs_attrs ({ having => $attrs->{having} });
1729 3 my %seen_having;
1730  
1731 # search for both a proper quoted qualified string, for a naive unquoted scalarref
1732 # and if all fails for an utterly naive quoted scalar-with-function
1733 3 ▶ while ($having_sql =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($having_sql =~ / $rquote $sep $lquote (.+?) $rquote | [\s,] \w+ \. (\w+) [\s,] | [\s,] $lquote (.+?) $rquote [\s,] /gx) 3 3 6
1734 $rquote $sep $lquote (.+?) $rquote
1735 |
1736 [\s,] \w+ \. (\w+) [\s,]
1737 |
1738 [\s,] $lquote (.+?) $rquote [\s,]
1739 /gx) {
1740 3 ▶ my $part = $1 || $2 || $3; # one of them matched if we got here
Condition33%2/6
$1 || $2 || $3
l!l&&r!l&&!r
030
$1 or $2
l!l&&r!l&&!r
003
MC/DC0%0/3
$1 || $2 || $3
$1 $2 $3
Truth table25%1/4
A or B || C
A= $1B= $2C= $3
ABCresult
1XX1
01X1
0011
0000
1741 3 ▶ unless ($seen_having{$part}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_having{$part}++) 2 1 3
1742 2 push @parts, $part;
1743 }
1744 }
1745 }
1746  
1747 90 for (@parts) {
1748 140 ▶ my $colpiece = $sel_index->{$_} || $_;
Condition66%2/3
$sel_index->{$_} || $_
l!l&&r!l&&!r
31370
MC/DC0%0/2
$sel_index->{$_} || $_
$sel_index->{$_} $_
Truth table66%2/3
A || B
A= $sel_index->{$_}B= $_
ABresult
1X1
011
000
1749  
1750 # unqualify join-based group_by's. Arcane but possible query
1751 # also horrible horrible hack to alias a column (not a func.)
1752 140 ▶ if ($colpiece =~ /\./ && $colpiece !~ /^$attrs->{alias}\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($colpiece =~ /\./ and not $colpiece =~ /^$attrs->{"alias"}\./) 3 137 140
Condition100%3/3
$colpiece =~ /\./ and not $colpiece =~ /^$attrs->{"alias"}\./
!ll&&!rl&&r
39983
MC/DC100%2/2
$colpiece =~ /\./ and not $colpiece =~ /^$attrs->{"alias"}\./
$colpiece =~ /\./ not $colpiece =~ /^$attrs->{"alias"}\./
Truth table100%3/3
A and not(B)
A= $colpiece =~ /\./B= not $colpiece =~ /^$attrs->{"alias"}\./
ABresult
0X0
100
111
1753 3 my $as = $colpiece;
1754 3 $as =~ s/\./__/;
1755 3 $colpiece = \ sprintf ('%s AS %s', map { $sql_maker->_quote ($_) } ($colpiece, $as) );
1756 }
1757 140 push @{$sub_attrs->{select}}, $colpiece;
1758 }
1759 }
1760 else {
1761 3 my @pcols = map { "$attrs->{alias}.$_" } ($rsrc->primary_columns);
1762 3 ▶ $sub_attrs->{select} = @pcols ? \@pcols : [ 1 ];
Branch: 1 branch
BranchTrueFalseTotal
@pcols ? : 3 0 3
1763 }
1764  
1765 93 return $rsrc->resultset_class
1766 ->new ($rsrc, $sub_attrs)
1767 ->as_subselect_rs
1768 ->search ({}, { columns => { count => $rsrc->storage->_count_select ($rsrc, $attrs) } })
1769 ->get_column ('count');
1770 }
1771  
1772  
1773 =head2 count_literal
1774  
1775 B<CAVEAT>: C<count_literal> is provided for Class::DBI compatibility and
1776 should only be used in that context. See L</search_literal> for further info.
1777  
1778 =over 4
1779  
1780 =item Arguments: $sql_fragment, @standalone_bind_values
1781  
1782 =item Return Value: $count
1783  
1784 =back
1785  
1786 Counts the results in a literal query. Equivalent to calling L</search_literal>
1787 with the passed arguments, then L</count>.
1788  
1789 =cut
1790  
1791 0 ▶sub count_literal { shift->search_literal(@_)->count; }
Subroutine count_literal: not called CC=1 SCAR=6.9
Pod documented
1792  
1793 =head2 all
1794  
1795 =over 4
1796  
1797 =item Arguments: none
1798  
1799 =item Return Value: L<@result_objs|DBIx::Class::Manual::ResultClass>
1800  
1801 =back
1802  
1803 Returns all elements in the resultset.
1804  
1805 =cut
1806  
1807 1677 ▶sub all {
Subroutine all: called CC=5 SCAR=16.2
Pod documented
1808 1677 my $self = shift;
1809 1677 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1677 1677
1810 0 $self->throw_exception("all() doesn't take any arguments, you probably wanted ->search(...)->all()");
1811 }
1812  
1813 1677 delete @{$self}{qw/_stashed_rows _stashed_results/};
1814  
1815 1677 ▶ if (my $c = $self->get_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->get_cache) 66 1611 1677
1816 66 return @$c;
1817 }
1818  
1819 1611 $self->cursor->reset;
1820  
1821 1611 ▶ my $objs = $self->_construct_results('fetch_all') || [];
Condition100%2/2
$self->_construct_results('fetch_all') || []
l!l
1305285
MC/DC100%1/1
$self->_construct_results('fetch_all') || []
$self->_construct_results('fetch_all')
Truth table100%2/2
A
A= $self->_construct_results('fetch_all')
Aresult
11
00
1822  
1823 1590 ▶ $self->set_cache($objs) if $self->{attrs}{cache};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attrs"}{"cache"} 12 1578 1590
1824  
1825 1590 return @$objs;
1826 }
1827  
1828 =head2 reset
1829  
1830 =over 4
1831  
1832 =item Arguments: none
1833  
1834 =item Return Value: $self
1835  
1836 =back
1837  
1838 Resets the resultset's cursor, so you can iterate through the elements again.
1839 Implicitly resets the storage cursor, so a subsequent L</next> will trigger
1840 another query.
1841  
1842 =cut
1843  
1844 1523 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
1845 1523 my ($self) = @_;
1846  
1847 1523 delete @{$self}{qw/_stashed_rows _stashed_results/};
1848 1523 $self->{all_cache_position} = 0;
1849 1523 $self->cursor->reset;
1850 1523 return $self;
1851 }
1852  
1853 =head2 first
1854  
1855 =over 4
1856  
1857 =item Arguments: none
1858  
1859 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass> | undef
1860  
1861 =back
1862  
1863 L<Resets|/reset> the resultset (causing a fresh query to storage) and returns
1864 an object for the first result (or C<undef> if the resultset is empty).
1865  
1866 =cut
1867  
1868 884 ▶sub first {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
1869 884 return $_[0]->reset->next;
1870 }
1871  
1872  
1873 # _rs_update_delete
1874 #
1875 # Determines whether and what type of subquery is required for the $rs operation.
1876 # If grouping is necessary either supplies its own, or verifies the current one
1877 # After all is done delegates to the proper storage method.
1878  
1879 604 ▶sub _rs_update_delete {
Subroutine _rs_update_delete: called CC=23 SCAR=31.6
1880 604 my ($self, $op, $values) = @_;
1881  
1882 604 my $rsrc = $self->result_source;
1883 604 my $storage = $rsrc->schema->storage;
1884  
1885 604 my $attrs = { %{$self->_resolved_attrs} };
1886  
1887 604 my $join_classifications;
1888 604 my ($existing_group_by) = delete @{$attrs}{qw(group_by _grouped_by_distinct)};
1889  
1890 # do we need a subquery for any reason?
1891 my $needs_subq = (
1892 defined $existing_group_by
1893 or
1894 # if {from} is unparseable wrap a subq
1895 604 ▶ ref($attrs->{from}) ne 'ARRAY'
Condition83%5/6
defined $existing_group_by || ref $attrs->{'from'} ne 'ARRAY' || $self->_has_resolved_attr('rows', 'offset')
l!l&&r!l&&!r
23599
defined $existing_group_by or ref $attrs->{"from"} ne "ARRAY"
l!l&&r!l&&!r
20602
MC/DC66%2/3
defined $existing_group_by || ref $attrs->{'from'} ne 'ARRAY' || $self->_has_resolved_attr('rows', 'offset')
defined $existing_group_by ref $attrs->{"from"} ne "ARRAY" $self->_has_resolved_attr('rows', 'offset')
Truth table75%3/4
A or B || C
A= defined $existing_group_byB= ref $attrs->{"from"} ne "ARRAY"C= $self->_has_resolved_attr('rows', 'offset')
ABCresult
1XX1
01X1
0011
0000
1896 or
1897 # limits call for a subq
1898 $self->_has_resolved_attr(qw/rows offset/)
1899 );
1900  
1901 # simplify the joinmap, so we can further decide if a subq is necessary
1902 604 ▶ if (!$needs_subq and @{$attrs->{from}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $needs_subq and @{$attrs->{"from"};} > 1) 21 583 604
Condition100%3/3
not $needs_subq and @{$attrs->{"from"};} > 1
!ll&&!rl&&r
557821
MC/DC100%2/2
not $needs_subq and @{$attrs->{"from"};} > 1
not $needs_subq @{$attrs->{"from"};} > 1
Truth table100%3/3
not(A) and B
A= not $needs_subqB= @{$attrs->{"from"};} > 1
ABresult
0X0
100
111
1903  
1904 21 ($attrs->{from}, $join_classifications) =
1905 $storage->_prune_unused_joins ($attrs);
1906  
1907 # any non-pruneable non-local restricting joins imply subq
1908 21 ▶ $needs_subq = grep { $_ ne $attrs->{alias} } keys %{ $join_classifications->{restricting} || {} };
Condition50%1/2
$join_classifications->{'restricting'} || {}
l!l
210
MC/DC0%0/1
$join_classifications->{'restricting'} || {}
$join_classifications->{'restricting'}
Truth table50%1/2
A
A= $join_classifications->{'restricting'}
Aresult
11
00
1909 }
1910  
1911 # check if the head is composite (by now all joins are thrown out unless $needs_subq)
1912 $needs_subq ||= (
1913 (ref $attrs->{from}[0]) ne 'HASH'
1914 or
1915 ref $attrs->{from}[0]{ $attrs->{from}[0]{-alias} }
1916 604 ▶ );
Condition83%5/6
$needs_subq ||= ref $attrs->{"from"}[0] ne "HASH" || ref $attrs->{"from"}[0]{$attrs->{"from"}[0]{"-alias"}}
l!l&&r!l&&!r
2310571
ref $attrs->{'from'}[0] ne 'HASH' || ref $attrs->{'from'}[0]{$attrs->{'from'}[0]{'-alias'}}
l!l&&r!l&&!r
010571
MC/DC0%0/3
$needs_subq ||= ref $attrs->{"from"}[0] ne "HASH" || ref $attrs->{"from"}[0]{$attrs->{"from"}[0]{"-alias"}}
$needs_subq ref $attrs->{'from'}[0] ne 'HASH' ref $attrs->{'from'}[0]{$attrs->{'from'}[0]{'-alias'}}
Truth table0%0/4
A ||= B || C
A= $needs_subqB= ref $attrs->{'from'}[0] ne 'HASH'C= ref $attrs->{'from'}[0]{$attrs->{'from'}[0]{'-alias'}}
ABCresult
1XX1
01X1
0011
0000
1917  
1918 604 my ($cond, $guard);
1919 # do we need anything like a subquery?
1920 604 ▶ if (! $needs_subq) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $needs_subq) { } 571 33 604
1921 # Most databases do not allow aliasing of tables in UPDATE/DELETE. Thus
1922 # a condition containing 'me' or other table prefixes will not work
1923 # at all. Tell SQLMaker to dequalify idents via a gross hack.
1924 571 $cond = do {
1925 571 my $sqla = $rsrc->storage->sql_maker;
1926 571 local $sqla->{_dequalify_idents} = 1;
1927 571 \[ $sqla->_recurse_where($self->{cond}) ];
1928 };
1929 }
1930 else {
1931 # we got this far - means it is time to wrap a subquery
1932 33 ▶ my $idcols = $rsrc->_identifying_column_set || $self->throw_exception(
Condition33%1/3
$rsrc->_identifying_column_set || $self->throw_exception(sprintf(q[Unable to perform complex resultset %s() without an identifying set of columns on source '%s'], $op, $rsrc->source_name))
l!l&&r!l&&!r
3300
MC/DC0%0/2
$rsrc->_identifying_column_set || $self->throw_exception(sprintf(q[Unable to perform complex resultset %s() without an identifying set of columns on source '%s'], $op, $rsrc->source_name))
$rsrc->_identifying_column_set $self->throw_exception(sprintf(q[Unable to perform complex resultset %s() without an identifying set of columns on source '%s'], $op, $rsrc->source_name))
Truth table33%1/3
A || B
A= $rsrc->_identifying_column_setB= $self->throw_exception(sprintf(q[Unable to perform complex resultset %s() without an identifying set of columns on source '%s'], $op, $rsrc->source_name))
ABresult
1X1
011
000
1933 sprintf(
1934 "Unable to perform complex resultset %s() without an identifying set of columns on source '%s'",
1935 $op,
1936 $rsrc->source_name,
1937 )
1938 );
1939  
1940 # make a new $rs selecting only the PKs (that's all we really need for the subq)
1941 33 delete $attrs->{$_} for qw/select as collapse/;
1942 33 $attrs->{columns} = [ map { "$attrs->{alias}.$_" } @$idcols ];
1943  
1944 # this will be consumed by the pruner waaaaay down the stack
1945 33 $attrs->{_force_prune_multiplying_joins} = 1;
1946  
1947 33 my $subrs = (ref $self)->new($rsrc, $attrs);
1948  
1949 33 ▶ if (@$idcols == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$idcols == 1) { } 20 13 33
elsif ($storage->_use_multicolumn_in) { } 9 4 13
1950 20 $cond = { $idcols->[0] => { -in => $subrs->as_query } };
1951 }
1952 elsif ($storage->_use_multicolumn_in) {
1953 # no syntax for calling this properly yet
1954 # !!! EXPERIMENTAL API !!! WILL CHANGE !!!
1955 9 $cond = $storage->sql_maker->_where_op_multicolumn_in (
1956 $idcols, # how do I convey a list of idents...? can binds reside on lhs?
1957 $subrs->as_query
1958 ),
1959 }
1960 else {
1961 # if all else fails - get all primary keys and operate over a ORed set
1962 # wrap in a transaction for consistency
1963 # this is where the group_by/multiplication starts to matter
1964 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_group_by or keys %{$join_classifications->{"multiplying"} or {};}) 4 0 4
Condition66%2/3
$existing_group_by or keys %{$join_classifications->{"multiplying"} or {};}
l!l&&r!l&&!r
130
MC/DC0%0/2
$existing_group_by or keys %{$join_classifications->{"multiplying"} or {};}
$existing_group_by keys %{$join_classifications->{"multiplying"} or {};}
Truth table66%2/3
A or B
A= $existing_group_byB= keys %{$join_classifications->{"multiplying"} or {};}
ABresult
1X1
011
000
1965 $existing_group_by
1966 or
1967 # we do not need to check pre-multipliers, since if the premulti is there, its
1968 # parent (who is multi) will be there too
1969 3 ▶ keys %{ $join_classifications->{multiplying} || {} }
Condition50%1/2
$join_classifications->{'multiplying'} || {}
l!l
30
MC/DC0%0/1
$join_classifications->{'multiplying'} || {}
$join_classifications->{'multiplying'}
Truth table50%1/2
A
A= $join_classifications->{'multiplying'}
Aresult
11
00
1970 ) {
1971 # make sure if there is a supplied group_by it matches the columns compiled above
1972 # perfectly. Anything else can not be sanely executed on most databases so croak
1973 # right then and there
1974 4 ▶ if ($existing_group_by) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_group_by) 1 3 4
1975 my @current_group_by = map
1976 1 ▶ { $_ =~ /\./ ? $_ : "$attrs->{alias}.$_" }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /\./ ? : 8 0 8
1977 @$existing_group_by
1978 ;
1979  
1980 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (join("\0", sort(@current_group_by)) ne join("\0", sort(@{$attrs->{"columns"};}))) 1 0 1
1981 join ("\x00", sort @current_group_by)
1982 ne
1983 1 join ("\x00", sort @{$attrs->{columns}} )
1984 ) {
1985 1 $self->throw_exception (
1986 "You have just attempted a $op operation on a resultset which does group_by"
1987 . ' on columns other than the primary keys, while DBIC internally needs to retrieve'
1988 . ' the primary keys in a subselect. All sane RDBMS engines do not support this'
1989 . ' kind of queries. Please retry the operation with a modified group_by or'
1990 . ' without using one at all.'
1991 );
1992 }
1993 }
1994  
1995 3 $subrs = $subrs->search({}, { group_by => $attrs->{columns} });
1996 }
1997  
1998 3 $guard = $storage->txn_scope_guard;
1999  
2000 3 for my $row ($subrs->cursor->all) {
2001 push @$cond, { map
2002 4 { $idcols->[$_] => $row->[$_] }
2003 (0 .. $#$idcols)
2004 };
2005 }
2006 }
2007 }
2008  
2009 603 ▶ my $res = $cond ? $storage->$op (
Branch: 2 branches
BranchTrueFalseTotal
$cond ? : 602 1 603
$op eq 'update' ? : 497 105 602
2010 $rsrc,
2011 $op eq 'update' ? $values : (),
2012 $cond,
2013 ) : '0E0';
2014  
2015 599 ▶ $guard->commit if $guard;
Branch: 1 branch
BranchTrueFalseTotal
if $guard 3 596 599
2016  
2017 599 return $res;
2018 }
2019  
2020 =head2 update
2021  
2022 =over 4
2023  
2024 =item Arguments: \%values
2025  
2026 =item Return Value: $underlying_storage_rv
2027  
2028 =back
2029  
2030 Sets the specified columns in the resultset to the supplied values in a
2031 single query. Note that this will not run any accessor/set_column/update
2032 triggers, nor will it update any result object instances derived from this
2033 resultset (this includes the contents of the L<resultset cache|/set_cache>
2034 if any). See L</update_all> if you need to execute any on-update
2035 triggers or cascades defined either by you or a
2036 L<result component|DBIx::Class::Manual::Component/WHAT IS A COMPONENT>.
2037  
2038 The return value is a pass through of what the underlying
2039 storage backend returned, and may vary. See L<DBI/execute> for the most
2040 common case.
2041  
2042 =head3 CAVEAT
2043  
2044 Note that L</update> does not process/deflate any of the values passed in.
2045 This is unlike the corresponding L<DBIx::Class::Row/update>. The user must
2046 ensure manually that any value passed to this method will stringify to
2047 something the RDBMS knows how to deal with. A notable example is the
2048 handling of L<DateTime> objects, for more info see:
2049 L<DBIx::Class::Manual::Cookbook/Formatting DateTime objects in queries>.
2050  
2051 =cut
2052  
2053 497 ▶sub update {
Subroutine update: called CC=2 SCAR=7.1
Pod documented
2054 497 my ($self, $values) = @_;
2055 497 ▶ $self->throw_exception('Values for update must be a hash')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values eq "HASH" 0 497 497
2056 unless ref $values eq 'HASH';
2057  
2058 497 return $self->_rs_update_delete ('update', $values);
2059 }
2060  
2061 =head2 update_all
2062  
2063 =over 4
2064  
2065 =item Arguments: \%values
2066  
2067 =item Return Value: 1
2068  
2069 =back
2070  
2071 Fetches all objects and updates them one at a time via
2072 L<DBIx::Class::Row/update>. Note that C<update_all> will run DBIC defined
2073 triggers, while L</update> will not.
2074  
2075 =cut
2076  
2077 1 ▶sub update_all {
Subroutine update_all: called CC=3 SCAR=11.0
Pod documented
2078 1 my ($self, $values) = @_;
2079 1 ▶ $self->throw_exception('Values for update_all must be a hash')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values eq "HASH" 0 1 1
2080 unless ref $values eq 'HASH';
2081  
2082 1 my $guard = $self->result_source->schema->txn_scope_guard;
2083 1 $_->update({%$values}) for $self->all; # shallow copy - update will mangle it
2084 1 $guard->commit;
2085 1 return 1;
2086 }
2087  
2088 =head2 delete
2089  
2090 =over 4
2091  
2092 =item Arguments: none
2093  
2094 =item Return Value: $underlying_storage_rv
2095  
2096 =back
2097  
2098 Deletes the rows matching this resultset in a single query. Note that this
2099 will not run any delete triggers, nor will it alter the
2100 L<in_storage|DBIx::Class::Row/in_storage> status of any result object instances
2101 derived from this resultset (this includes the contents of the
2102 L<resultset cache|/set_cache> if any). See L</delete_all> if you need to
2103 execute any on-delete triggers or cascades defined either by you or a
2104 L<result component|DBIx::Class::Manual::Component/WHAT IS A COMPONENT>.
2105  
2106 The return value is a pass through of what the underlying storage backend
2107 returned, and may vary. See L<DBI/execute> for the most common case.
2108  
2109 =cut
2110  
2111 107 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.1
Pod documented
2112 107 my $self = shift;
2113 107 ▶ $self->throw_exception('delete does not accept any arguments')
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 107 107
2114 if @_;
2115  
2116 107 return $self->_rs_update_delete ('delete');
2117 }
2118  
2119 =head2 delete_all
2120  
2121 =over 4
2122  
2123 =item Arguments: none
2124  
2125 =item Return Value: 1
2126  
2127 =back
2128  
2129 Fetches all objects and deletes them one at a time via
2130 L<DBIx::Class::Row/delete>. Note that C<delete_all> will run DBIC defined
2131 triggers, while L</delete> will not.
2132  
2133 =cut
2134  
2135 270 ▶sub delete_all {
Subroutine delete_all: called CC=3 SCAR=11.0
Pod documented
2136 270 my $self = shift;
2137 270 ▶ $self->throw_exception('delete_all does not accept any arguments')
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 270 270
2138 if @_;
2139  
2140 270 my $guard = $self->result_source->schema->txn_scope_guard;
2141 270 $_->delete for $self->all;
2142 270 $guard->commit;
2143 270 return 1;
2144 }
2145  
2146 =head2 populate
2147  
2148 =over 4
2149  
2150 =item Arguments: [ \@column_list, \@row_values+ ] | [ \%col_data+ ]
2151  
2152 =item Return Value: L<\@result_objects|DBIx::Class::Manual::ResultClass> (scalar context) | L<@result_objects|DBIx::Class::Manual::ResultClass> (list context)
2153  
2154 =back
2155  
2156 Accepts either an arrayref of hashrefs or alternatively an arrayref of
2157 arrayrefs.
2158  
2159 =over
2160  
2161 =item NOTE
2162  
2163 The context of this method call has an important effect on what is
2164 submitted to storage. In void context data is fed directly to fastpath
2165 insertion routines provided by the underlying storage (most often
2166 L<DBI/execute_for_fetch>), bypassing the L<new|DBIx::Class::Row/new> and
2167 L<insert|DBIx::Class::Row/insert> calls on the
2168 L<Result|DBIx::Class::Manual::ResultClass> class, including any
2169 augmentation of these methods provided by components. For example if you
2170 are using something like L<DBIx::Class::UUIDColumns> to create primary
2171 keys for you, you will find that your PKs are empty. In this case you
2172 will have to explicitly force scalar or list context in order to create
2173 those values.
2174  
2175 =back
2176  
2177 In non-void (scalar or list) context, this method is simply a wrapper
2178 for L</create>. Depending on list or scalar context either a list of
2179 L<Result|DBIx::Class::Manual::ResultClass> objects or an arrayref
2180 containing these objects is returned.
2181  
2182 When supplying data in "arrayref of arrayrefs" invocation style, the
2183 first element should be a list of column names and each subsequent
2184 element should be a data value in the earlier specified column order.
2185 For example:
2186  
2187 $schema->resultset("Artist")->populate([
2188 [ qw( artistid name ) ],
2189 [ 100, 'A Formally Unknown Singer' ],
2190 [ 101, 'A singer that jumped the shark two albums ago' ],
2191 [ 102, 'An actually cool singer' ],
2192 ]);
2193  
2194 For the arrayref of hashrefs style each hashref should be a structure
2195 suitable for passing to L</create>. Multi-create is also permitted with
2196 this syntax.
2197  
2198 $schema->resultset("Artist")->populate([
2199 { artistid => 4, name => 'Manufactured Crap', cds => [
2200 { title => 'My First CD', year => 2006 },
2201 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
2202 ],
2203 },
2204 { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
2205 { title => 'My parents sold me to a record company', year => 2005 },
2206 { title => 'Why Am I So Ugly?', year => 2006 },
2207 { title => 'I Got Surgery and am now Popular', year => 2007 }
2208 ],
2209 },
2210 ]);
2211  
2212 If you attempt a void-context multi-create as in the example above (each
2213 Artist also has the related list of CDs), and B<do not> supply the
2214 necessary autoinc foreign key information, this method will proxy to the
2215 less efficient L</create>, and then throw the Result objects away. In this
2216 case there are obviously no benefits to using this method over L</create>.
2217  
2218 =cut
2219  
2220 7682 ▶sub populate {
Subroutine populate: called CC=77 SCAR=46.1
Pod documented
2221 7682 my $self = shift;
2222  
2223 # this is naive and just a quick check
2224 # the types will need to be checked more thoroughly when the
2225 # multi-source populate gets added
2226 my $data = (
2227 ref $_[0] eq 'ARRAY'
2228 and
2229 7682 ▶ ( @{$_[0]} or return )
Branch: 1 branch
BranchTrueFalseTotal
my $data = ref $_[0] eq "ARRAY" && (@{$_[0];} || (return)) && (ref $_[0][0] eq "HASH" || ref $_[0][0] eq "ARRAY") && $_[0] or $self->throw_exception("Populate expects an arrayref of hashrefs or arrayref of arrayrefs") 7675 0 7675
Condition57%8/14
ref $_[0] eq 'ARRAY' && (@{$_[0];} || (return)) && (ref $_[0][0] eq 'HASH' || ref $_[0][0] eq 'ARRAY') && $_[0]
!ll&&!rl&&r
007675
ref $_[0] eq "ARRAY" and @{$_[0];} || (return) and ref $_[0][0] eq "HASH" || ref $_[0][0] eq "ARRAY"
!ll&&!rl&&r
007675
ref $_[0] eq "ARRAY" and @{$_[0];} || (return)
!ll&&!rl&&r
017681
@{$_[0];} or return
l!l
76757
ref $_[0][0] eq "HASH" or ref $_[0][0] eq "ARRAY"
l!l&&r!l&&!r
6476110
MC/DC0%0/5
ref $_[0] eq 'ARRAY' && (@{$_[0];} || (return)) && (ref $_[0][0] eq 'HASH' || ref $_[0][0] eq 'ARRAY') && $_[0]
ref $_[0] eq "ARRAY" @{$_[0];} ref $_[0][0] eq "HASH" ref $_[0][0] eq "ARRAY" $_[0]
Truth table28%2/7
A and B and C or D && E
A= ref $_[0] eq "ARRAY"B= @{$_[0];}C= ref $_[0][0] eq "HASH"D= ref $_[0][0] eq "ARRAY"E= $_[0]
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
2230 and
2231 ( ref $_[0][0] eq 'HASH' or ref $_[0][0] eq 'ARRAY' )
2232 and
2233 $_[0]
2234 ) or $self->throw_exception('Populate expects an arrayref of hashrefs or arrayref of arrayrefs');
2235  
2236 # FIXME - no cref handling
2237 # At this point assume either hashes or arrays
2238  
2239 7675 ▶ if(defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 41 7634 7675
2240 41 my (@results, $guard);
2241  
2242 41 ▶ if (ref $data->[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->[0] eq 'ARRAY') { } 21 20 41
2243 # column names only, nothing to do
2244 21 ▶ return if @$data == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$data == 1 4 17 21
2245  
2246 17 ▶ $guard = $self->result_source->schema->storage->txn_scope_guard
Branch: 1 branch
BranchTrueFalseTotal
if @$data > 2 12 5 17
2247 if @$data > 2;
2248  
2249 @results = map
2250 46 { my $vals = $_; $self->new_result({ map { $data->[0][$_] => $vals->[$_] } 0..$#{$data->[0]} })->insert }
2251 17 @{$data}[1 .. $#$data]
2252 ;
2253 }
2254 else {
2255  
2256 20 ▶ $guard = $self->result_source->schema->storage->txn_scope_guard
Branch: 1 branch
BranchTrueFalseTotal
if @$data > 1 15 5 20
2257 if @$data > 1;
2258  
2259 20 @results = map { $self->new_result($_)->insert } @$data;
2260 }
2261  
2262 37 ▶ $guard->commit if $guard;
Branch: 1 branch
BranchTrueFalseTotal
if $guard 27 10 37
2263 37 ▶ return wantarray ? @results : \@results;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 22 15 37
2264 }
2265  
2266 # we have to deal with *possibly incomplete* related data
2267 # this means we have to walk the data structure twice
2268 # whether we want this or not
2269 # jnap, I hate you ;)
2270 7634 my $rsrc = $self->result_source;
2271 7634 my $rel_info = { map { $_ => $rsrc->relationship_info($_) } $rsrc->relationships };
2272  
2273 7634 my ($colinfo, $colnames, $slices_with_rels);
2274 7634 my $data_start = 0;
2275  
2276 DATA_SLICE:
2277 7634 for my $i (0 .. $#$data) {
2278  
2279 44397 my $current_slice_seen_rel_infos;
2280  
2281 ### Determine/Supplement collists
2282 ### BEWARE - This is a hot piece of code, a lot of weird idioms were used
2283 44397 ▶ if( ref $data->[$i] eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data->[$i] eq 'ARRAY') { } 44291 106 44397
elsif (ref $data->[$i] eq 'HASH') { } 106 0 106
2284  
2285 # positional(!) explicit column list
2286 44291 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 7590 36701 44291
2287 # column names only, nothing to do
2288 7590 ▶ return if @$data == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$data == 1 2 7588 7590
2289  
2290 $colinfo->{$data->[0][$_]} = { pos => $_, name => $data->[0][$_] } and push @$colnames, $data->[0][$_]
2291 7588 ▶ for 0 .. $#{$data->[0]};
Branch: 1 branch
BranchTrueFalseTotal
$$colinfo{$data->[0][$_]} = {"pos", $_, "name", $data->[0][$_]} and push @$colnames, $data->[0][$_] 21762 0 21762
2292  
2293 7588 $data_start = 1;
2294  
2295 7588 next DATA_SLICE;
2296 }
2297 else {
2298 36701 for (values %$colinfo) {
2299 102705 ▶ if ($_->{is_rel} ||= (
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{"is_rel"} ||= $$rel_info{$_->{"name"}} && (ref $data->[$i][$_->{"pos"}] eq "ARRAY" || ref $data->[$i][$_->{"pos"}] eq "HASH" || defined builtin::blessed($data->[$i][$_->{"pos"}]) && $data->[$i][$_->{"pos"}]->isa("DBIx::Class::Row")) && 1) 4 102701 102705
Condition76%13/17
$_->{"is_rel"} ||= $$rel_info{$_->{"name"}} && (ref $data->[$i][$_->{"pos"}] eq "ARRAY" || ref $data->[$i][$_->{"pos"}] eq "HASH" || defined builtin::blessed($data->[$i][$_->{"pos"}]) && $data->[$i][$_->{"pos"}]->isa("DBIx::Class::Row")) && 1
l!l&&r!l&&!r
31102701
$$rel_info{$_->{'name'}} && (ref $data->[$i][$_->{'pos'}] eq 'ARRAY' || ref $data->[$i][$_->{'pos'}] eq 'HASH' || defined builtin::blessed($data->[$i][$_->{'pos'}]) && $data->[$i][$_->{'pos'}]->isa('DBIx::Class::Row')) && 1
!ll
1027011
$$rel_info{$_->{"name"}} and ref $data->[$i][$_->{"pos"}] eq "ARRAY" || ref $data->[$i][$_->{"pos"}] eq "HASH" || defined builtin::blessed($data->[$i][$_->{"pos"}]) && $data->[$i][$_->{"pos"}]->isa("DBIx::Class::Row")
!ll&&!rl&&r
81628210731
ref $data->[$i][$_->{"pos"}] eq "ARRAY" or ref $data->[$i][$_->{"pos"}] eq "HASH" or defined builtin::blessed($data->[$i][$_->{"pos"}]) and $data->[$i][$_->{"pos"}]->isa("DBIx::Class::Row")
l!l&&r!l&&!r
1021073
ref $data->[$i][$_->{"pos"}] eq "ARRAY" or ref $data->[$i][$_->{"pos"}] eq "HASH"
l!l&&r!l&&!r
1021073
defined builtin::blessed($data->[$i][$_->{"pos"}]) and $data->[$i][$_->{"pos"}]->isa("DBIx::Class::Row")
!ll&&!rl&&r
2107300
MC/DC0%0/6
$_->{"is_rel"} ||= $$rel_info{$_->{"name"}} && (ref $data->[$i][$_->{"pos"}] eq "ARRAY" || ref $data->[$i][$_->{"pos"}] eq "HASH" || defined builtin::blessed($data->[$i][$_->{"pos"}]) && $data->[$i][$_->{"pos"}]->isa("DBIx::Class::Row")) && 1
$_->{"is_rel"} $$rel_info{$_->{"name"}} ref $data->[$i][$_->{"pos"}] eq "ARRAY" ref $data->[$i][$_->{"pos"}] eq "HASH" defined builtin::blessed($data->[$i][$_->{"pos"}]) $data->[$i][$_->{"pos"}]->isa("DBIx::Class::Row")
Truth table0%0/7
A ||= B and C or D or E and F
A= $_->{"is_rel"}B= $$rel_info{$_->{"name"}}C= ref $data->[$i][$_->{"pos"}] eq "ARRAY"D= ref $data->[$i][$_->{"pos"}] eq "HASH"E= defined builtin::blessed($data->[$i][$_->{"pos"}])F= $data->[$i][$_->{"pos"}]->isa("DBIx::Class::Row")
ABCDEFresult
1XXXXX1
011XXX1
0101XX1
0100111
00XXXX0
01000X0
0100100
2300 $rel_info->{$_->{name}}
2301 and
2302 (
2303 ref $data->[$i][$_->{pos}] eq 'ARRAY'
2304 or
2305 ref $data->[$i][$_->{pos}] eq 'HASH'
2306 or
2307 ( defined blessed $data->[$i][$_->{pos}] and $data->[$i][$_->{pos}]->isa('DBIx::Class::Row') )
2308 )
2309 and
2310 1
2311 )) {
2312  
2313 # moar sanity check... sigh
2314 4 ▶ for ( ref $data->[$i][$_->{pos}] eq 'ARRAY' ? @{$data->[$i][$_->{pos}]} : $data->[$i][$_->{pos}] ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $data->[$i][$_->{'pos'}] eq 'ARRAY' ? : 4 0 4
2315 5 ▶ if ( defined blessed $_ and $_->isa('DBIx::Class::Row' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($_) and $_->isa("DBIx::Class::Row")) 0 5 5
Condition33%1/3
defined builtin::blessed($_) and $_->isa("DBIx::Class::Row")
!ll&&!rl&&r
500
MC/DC0%0/2
defined builtin::blessed($_) and $_->isa("DBIx::Class::Row")
defined builtin::blessed($_) $_->isa("DBIx::Class::Row")
Truth table33%1/3
A and B
A= defined builtin::blessed($_)B= $_->isa("DBIx::Class::Row")
ABresult
0X0
100
111
2316 0 carp_unique("Fast-path populate() with supplied related objects is not possible - falling back to regular create()");
2317 0 return my $throwaway = $self->populate(@_);
2318 }
2319 }
2320  
2321 4 push @$current_slice_seen_rel_infos, $rel_info->{$_->{name}};
2322 }
2323 }
2324 }
2325  
2326 36701 ▶ if ($current_slice_seen_rel_infos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_slice_seen_rel_infos) 4 36697 36701
2327 4 push @$slices_with_rels, { map { $colnames->[$_] => $data->[$i][$_] } 0 .. $#$colnames };
2328  
2329 # this is needed further down to decide whether or not to fallback to create()
2330 $colinfo->{$colnames->[$_]}{seen_null} ||= ! defined $data->[$i][$_]
2331 4 ▶ for 0 .. $#$colnames;
Condition66%2/3
$$colinfo{$colnames->[$_]}{"seen_null"} ||= !defined($data->[$i][$_])
l!l&&r!l&&!r
017
MC/DC50%1/2
$$colinfo{$colnames->[$_]}{"seen_null"} ||= !defined($data->[$i][$_])
$$colinfo{$colnames->[$_]}{"seen_null"} !defined($data->[$i][$_])
Truth table66%2/3
A ||= not(B)
A= $$colinfo{$colnames->[$_]}{"seen_null"}B= !defined($data->[$i][$_])
ABresult
1X1
011
000
2332 }
2333 }
2334 elsif( ref $data->[$i] eq 'HASH' ) {
2335  
2336 106 for ( sort keys %{$data->[$i]} ) {
2337  
2338 224 ▶ $colinfo->{$_} ||= do {
Condition66%2/3
$colinfo->{$_} ||= do { $self->throw_exception("Column '${_}' must be present in supplied explicit column list") if $data_start; push @$colnames, $_; +{"pos", $colnames->$#*, "name", $_} }
l!l&&r!l&&!r
127970
MC/DC0%0/2
$colinfo->{$_} ||= do { $self->throw_exception("Column '${_}' must be present in supplied explicit column list") if $data_start; push @$colnames, $_; +{"pos", $colnames->$#*, "name", $_} }
$colinfo->{$_} do { $self->throw_exception("Column '${_}' must be present in supplied explicit column list") if $data_start; push @$colnames, $_; +{"pos", $colnames->$#*, "name", $_} }
Truth table66%2/3
A ||= B
A= $colinfo->{$_}B= do { $self->throw_exception("Column '${_}' must be present in supplied explicit column list") if $data_start; push @$colnames, $_; +{"pos", $colnames->$#*, "name", $_} }
ABresult
1X1
011
000
2339  
2340 97 ▶ $self->throw_exception("Column '$_' must be present in supplied explicit column list")
Branch: 1 branch
BranchTrueFalseTotal
if $data_start 0 97 97
2341 if $data_start; # it will be 0 on AoH, 1 on AoA
2342  
2343 97 push @$colnames, $_;
2344  
2345 # RV
2346 97 { pos => $#$colnames, name => $_ }
2347 };
2348  
2349 224 ▶ if ($colinfo->{$_}{is_rel} ||= (
Branch: 1 branch
BranchTrueFalseTotal
if ($colinfo->{$_}{"is_rel"} ||= $rel_info->{$_} && (ref $data->[$i]{$_} eq "ARRAY" || ref $data->[$i]{$_} eq "HASH" || defined builtin::blessed($data->[$i]{$_}) && $data->[$i]{$_}->isa("DBIx::Class::Row")) && 1) 12 212 224
Condition82%14/17
$colinfo->{$_}{"is_rel"} ||= $rel_info->{$_} && (ref $data->[$i]{$_} eq "ARRAY" || ref $data->[$i]{$_} eq "HASH" || defined builtin::blessed($data->[$i]{$_}) && $data->[$i]{$_}->isa("DBIx::Class::Row")) && 1
l!l&&r!l&&!r
57212
$rel_info->{$_} && (ref $data->[$i]{$_} eq 'ARRAY' || ref $data->[$i]{$_} eq 'HASH' || defined builtin::blessed($data->[$i]{$_}) && $data->[$i]{$_}->isa('DBIx::Class::Row')) && 1
!ll
2127
$rel_info->{$_} and ref $data->[$i]{$_} eq "ARRAY" || ref $data->[$i]{$_} eq "HASH" || defined builtin::blessed($data->[$i]{$_}) && $data->[$i]{$_}->isa("DBIx::Class::Row")
!ll&&!rl&&r
20397
ref $data->[$i]{$_} eq "ARRAY" or ref $data->[$i]{$_} eq "HASH" or defined builtin::blessed($data->[$i]{$_}) and $data->[$i]{$_}->isa("DBIx::Class::Row")
l!l&&r!l&&!r
709
ref $data->[$i]{$_} eq "ARRAY" or ref $data->[$i]{$_} eq "HASH"
l!l&&r!l&&!r
529
defined builtin::blessed($data->[$i]{$_}) and $data->[$i]{$_}->isa("DBIx::Class::Row")
!ll&&!rl&&r
900
MC/DC0%0/6
$colinfo->{$_}{"is_rel"} ||= $rel_info->{$_} && (ref $data->[$i]{$_} eq "ARRAY" || ref $data->[$i]{$_} eq "HASH" || defined builtin::blessed($data->[$i]{$_}) && $data->[$i]{$_}->isa("DBIx::Class::Row")) && 1
$colinfo->{$_}{"is_rel"} $rel_info->{$_} ref $data->[$i]{$_} eq "ARRAY" ref $data->[$i]{$_} eq "HASH" defined builtin::blessed($data->[$i]{$_}) $data->[$i]{$_}->isa("DBIx::Class::Row")
Truth table0%0/7
A ||= B and C or D or E and F
A= $colinfo->{$_}{"is_rel"}B= $rel_info->{$_}C= ref $data->[$i]{$_} eq "ARRAY"D= ref $data->[$i]{$_} eq "HASH"E= defined builtin::blessed($data->[$i]{$_})F= $data->[$i]{$_}->isa("DBIx::Class::Row")
ABCDEFresult
1XXXXX1
011XXX1
0101XX1
0100111
00XXXX0
01000X0
0100100
2350 $rel_info->{$_}
2351 and
2352 (
2353 ref $data->[$i]{$_} eq 'ARRAY'
2354 or
2355 ref $data->[$i]{$_} eq 'HASH'
2356 or
2357 ( defined blessed $data->[$i]{$_} and $data->[$i]{$_}->isa('DBIx::Class::Row') )
2358 )
2359 and
2360 1
2361 )) {
2362  
2363 # moar sanity check... sigh
2364 12 ▶ for ( ref $data->[$i]{$_} eq 'ARRAY' ? @{$data->[$i]{$_}} : $data->[$i]{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $data->[$i]{$_} eq 'ARRAY' ? : 10 2 12
2365 18 ▶ if ( defined blessed $_ and $_->isa('DBIx::Class::Row' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($_) and $_->isa("DBIx::Class::Row")) 1 17 18
Condition66%2/3
defined builtin::blessed($_) and $_->isa("DBIx::Class::Row")
!ll&&!rl&&r
1701
MC/DC50%1/2
defined builtin::blessed($_) and $_->isa("DBIx::Class::Row")
defined builtin::blessed($_) $_->isa("DBIx::Class::Row")
Truth table66%2/3
A and B
A= defined builtin::blessed($_)B= $_->isa("DBIx::Class::Row")
ABresult
0X0
100
111
2366 1 carp_unique("Fast-path populate() with supplied related objects is not possible - falling back to regular create()");
2367 1 return my $throwaway = $self->populate(@_);
2368 }
2369 }
2370  
2371 11 push @$current_slice_seen_rel_infos, $rel_info->{$_};
2372 }
2373 }
2374  
2375 105 ▶ if ($current_slice_seen_rel_infos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_slice_seen_rel_infos) 11 94 105
2376 11 push @$slices_with_rels, $data->[$i];
2377  
2378 # this is needed further down to decide whether or not to fallback to create()
2379 $colinfo->{$_}{seen_null} ||= ! defined $data->[$i]{$_}
2380 11 ▶ for keys %{$data->[$i]};
Condition66%2/3
$colinfo->{$_}{"seen_null"} ||= !defined($data->[$i]{$_})
l!l&&r!l&&!r
0131
MC/DC50%1/2
$colinfo->{$_}{"seen_null"} ||= !defined($data->[$i]{$_})
$colinfo->{$_}{"seen_null"} !defined($data->[$i]{$_})
Truth table66%2/3
A ||= not(B)
A= $colinfo->{$_}{"seen_null"}B= !defined($data->[$i]{$_})
ABresult
1X1
011
000
2381 }
2382 }
2383 else {
2384 0 $self->throw_exception('Unexpected populate() data structure member type: ' . ref $data->[$i] );
2385 }
2386  
2387 36806 ▶ if ( grep
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->{"attrs"}{"is_depends_on"};} @{$current_slice_seen_rel_infos or [];}) 2 36804 36806
2388 15 { $_->{attrs}{is_depends_on} }
2389 36806 ▶ @{ $current_slice_seen_rel_infos || [] }
Condition100%2/2
$current_slice_seen_rel_infos || []
l!l
1536791
MC/DC100%1/1
$current_slice_seen_rel_infos || []
$current_slice_seen_rel_infos
Truth table100%2/2
A
A= $current_slice_seen_rel_infos
Aresult
11
00
2390 ) {
2391 2 carp_unique("Fast-path populate() of belongs_to relationship data is not possible - falling back to regular create()");
2392 2 return my $throwaway = $self->populate(@_);
2393 }
2394 }
2395  
2396 7629 ▶ if( $slices_with_rels ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slices_with_rels) 5 7624 7629
2397  
2398 # need to exclude the rel "columns"
2399 5 $colnames = [ grep { ! $colinfo->{$_}{is_rel} } @$colnames ];
2400  
2401 # extra sanity check - ensure the main source is in fact identifiable
2402 # the localizing of nullability is insane, but oh well... the use-case is legit
2403 5 my $ci = $rsrc->columns_info($colnames);
2404  
2405 8 $ci->{$_} = { %{$ci->{$_}}, is_nullable => 0 }
2406 5 for grep { ! $colinfo->{$_}{seen_null} } keys %$ci;
2407  
2408 5 ▶ unless( $rsrc->_identifying_column_set($ci) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rsrc->_identifying_column_set($ci)) 1 4 5
2409 1 carp_unique("Fast-path populate() of non-uniquely identifiable rows with related data is not possible - falling back to regular create()");
2410 1 return my $throwaway = $self->populate(@_);
2411 }
2412 }
2413  
2414 ### inherit the data locked in the conditions of the resultset
2415 7628 my ($rs_data) = $self->_merge_with_rscond({});
2416 7628 delete @{$rs_data}{@$colnames}; # passed-in stuff takes precedence
2417  
2418 # if anything left - decompose rs_data
2419 7628 my $rs_data_vals;
2420 7628 ▶ if (keys %$rs_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$rs_data) 9 7619 7628
2421 push @$rs_data_vals, $rs_data->{$_}
2422 9 for sort keys %$rs_data;
2423 }
2424  
2425 ### start work
2426 7628 my $guard;
2427 7628 ▶ $guard = $rsrc->schema->storage->txn_scope_guard
Branch: 1 branch
BranchTrueFalseTotal
if $slices_with_rels 4 7624 7628
2428 if $slices_with_rels;
2429  
2430 ### main source data
2431 # FIXME - need to switch entirely to a coderef-based thing,
2432 # so that large sets aren't copied several times... I think
2433 $rsrc->storage->_insert_bulk(
2434 $rsrc,
2435 [ @$colnames, sort keys %$rs_data ],
2436 [ map {
2437 7628 ref $data->[$_] eq 'ARRAY'
2438 ? (
2439 0 ▶ $slices_with_rels ? [ @{$data->[$_]}[0..$#$colnames], @{$rs_data_vals||[]} ] # the collist changed
Branch: 1 branch
BranchTrueFalseTotal
$rs_data_vals ? : 0 36697 36697
Condition0%0/2
$rs_data_vals || []
l!l
00
MC/DC0%0/1
$rs_data_vals || []
$rs_data_vals
Truth table0%0/2
A
A= $rs_data_vals
Aresult
11
00
2440 0 : $rs_data_vals ? [ @{$data->[$_]}, @$rs_data_vals ]
2441 : $data->[$_]
2442 )
2443 36800 ▶ : [ @{$data->[$_]}{@$colnames}, @{$rs_data_vals||[]} ]
Branch: 2 branches
BranchTrueFalseTotal
ref $data->[$_] eq 'ARRAY' ? : 36697 103 36800
$slices_with_rels ? : 0 36697 36697
Condition100%2/2
$rs_data_vals || []
l!l
1588
MC/DC100%1/1
$rs_data_vals || []
$rs_data_vals
Truth table100%2/2
A
A= $rs_data_vals
Aresult
11
00
2444 } $data_start .. $#$data ],
2445 );
2446  
2447 ### do the children relationships
2448 7621 ▶ if ( $slices_with_rels ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slices_with_rels) 4 7617 7621
2449 4 ▶ my @rels = grep { $colinfo->{$_}{is_rel} } keys %$colinfo
Branch: 1 branch
BranchTrueFalseTotal
my(@rels) = grep({$colinfo->{$_}{"is_rel"};} keys %$colinfo) or die "wtf... please report a bug with DBIC_TRACE=1 output (stacktrace)" 4 0 4
2450 or die 'wtf... please report a bug with DBIC_TRACE=1 output (stacktrace)';
2451  
2452 4 for my $sl (@$slices_with_rels) {
2453  
2454 9 my ($main_proto, $main_proto_rs);
2455 9 for my $rel (@rels) {
2456 9 ▶ next unless defined $sl->{$rel};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sl->{$rel} 0 9 9
2457  
2458 $main_proto ||= {
2459 %$rs_data,
2460 9 ▶ (map { $_ => $sl->{$_} } @$colnames),
Condition50%1/2
$main_proto ||= {%$rs_data, map({$_, $sl->{$_};} @$colnames)}
l!l
09
MC/DC0%0/1
$main_proto ||= {%$rs_data, map({$_, $sl->{$_};} @$colnames)}
$main_proto
Truth table50%1/2
A
A= $main_proto
Aresult
11
00
2461 };
2462  
2463 9 ▶ unless (defined $colinfo->{$rel}{rs}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $colinfo->{$rel}{"rs"}) 4 5 9
2464  
2465 4 $colinfo->{$rel}{rs} = $rsrc->related_source($rel)->resultset;
2466  
2467 4 $colinfo->{$rel}{fk_map} = { reverse %{ $rsrc->_resolve_relationship_condition(
2468 rel_name => $rel,
2469 self_alias => "\xFE", # irrelevant
2470 foreign_alias => "\xFF", # irrelevant
2471 4 ▶ )->{identity_map} || {} } };
Condition50%1/2
$rsrc->_resolve_relationship_condition('rel_name', $rel, 'self_alias', "\376", 'foreign_alias', "\377")->{'identity_map'} || {}
l!l
40
MC/DC0%0/1
$rsrc->_resolve_relationship_condition('rel_name', $rel, 'self_alias', "\376", 'foreign_alias', "\377")->{'identity_map'} || {}
$rsrc->_resolve_relationship_condition('rel_name', $rel, 'self_alias', "\376", 'foreign_alias', "\377")->{'identity_map'}
Truth table50%1/2
A
A= $rsrc->_resolve_relationship_condition('rel_name', $rel, 'self_alias', "\376", 'foreign_alias', "\377")->{'identity_map'}
Aresult
11
00
2472  
2473 }
2474  
2475 $colinfo->{$rel}{rs}->search({ map # only so that we inherit them values properly, no actual search
2476 {
2477 $_ => { '=' =>
2478 ( $main_proto_rs ||= $rsrc->resultset->search($main_proto) )
2479 10 ▶ ->get_column( $colinfo->{$rel}{fk_map}{$_} )
Condition66%2/3
$main_proto_rs ||= $rsrc->resultset->search($main_proto)
l!l&&r!l&&!r
190
MC/DC0%0/2
$main_proto_rs ||= $rsrc->resultset->search($main_proto)
$main_proto_rs $rsrc->resultset->search($main_proto)
Truth table66%2/3
A ||= B
A= $main_proto_rsB= $rsrc->resultset->search($main_proto)
ABresult
1X1
011
000
2480 ->as_query
2481 }
2482 }
2483 9 ▶ keys %{$colinfo->{$rel}{fk_map}}
Branch: 1 branch
BranchTrueFalseTotal
ref $sl->{$rel} eq 'ARRAY' ? : 9 0 9
2484 9 })->populate( ref $sl->{$rel} eq 'ARRAY' ? $sl->{$rel} : [ $sl->{$rel} ] );
2485  
2486 9 1;
2487 }
2488 }
2489 }
2490  
2491 7621 ▶ $guard->commit if $guard;
Branch: 1 branch
BranchTrueFalseTotal
if $guard 4 7617 7621
2492 }
2493  
2494 =head2 pager
2495  
2496 =over 4
2497  
2498 =item Arguments: none
2499  
2500 =item Return Value: L<$pager|DBIx::Class::ResultSet::Pager>
2501  
2502 =back
2503  
2504 Returns a L<DBIx::Class::ResultSet::Pager> object tied to the current
2505 resultset. Requires the C<page> attribute to have been previously set on
2506 the resultset object, usually via a call to L</page>.
2507  
2508 To get the full count of entries for a paged resultset, call
2509 L<total_entries|DBIx::Class::ResultSet::Pager/total_entries> on the pager
2510 object.
2511  
2512 =cut
2513  
2514 28 ▶sub pager {
Subroutine pager: called CC=5 SCAR=16.7
Pod documented
2515 28 my ($self) = @_;
2516  
2517 28 ▶ return $self->{pager} if $self->{pager};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pager"} 12 16 28
2518  
2519 16 my $attrs = $self->{attrs};
2520 16 ▶ if (!defined $attrs->{page}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $attrs->{'page'}) { } 0 16 16
elsif ($attrs->{'page'} <= 0) { } 0 16 16
2521 0 $self->throw_exception("Can't create pager for non-paged rs, you need to call page(\$num) first");
2522 }
2523 elsif ($attrs->{page} <= 0) {
2524 0 $self->throw_exception('Invalid page number (page-numbers are 1-based)');
2525 }
2526 16 ▶ $attrs->{rows} ||= 10;
Condition50%1/2
$attrs->{"rows"} ||= 10
l!l
160
MC/DC0%0/1
$attrs->{"rows"} ||= 10
$attrs->{"rows"}
Truth table50%1/2
A
A= $attrs->{"rows"}
Aresult
11
00
2527  
2528 # throw away the paging flags and re-run the count (possibly
2529 # with a subselect) to get the real total count
2530 16 my $count_attrs = { %$attrs };
2531 16 delete @{$count_attrs}{qw/rows offset page pager/};
2532  
2533 16 my $total_rs = (ref $self)->new($self->result_source, $count_attrs);
2534  
2535 16 require DBIx::Class::ResultSet::Pager;
2536 return $self->{pager} = DBIx::Class::ResultSet::Pager->new(
2537 10 ▶ sub { $total_rs->count }, #lazy-get the total
Subroutine __ANON__: called CC=1 SCAR=0.0
2538 $attrs->{rows},
2539 $self->{attrs}{page},
2540 16 );
2541 }
2542  
2543 =head2 page
2544  
2545 =over 4
2546  
2547 =item Arguments: $page_number
2548  
2549 =item Return Value: L<$resultset|/search>
2550  
2551 =back
2552  
2553 Returns a resultset for the $page_number page of the resultset on which page
2554 is called, where each page contains a number of rows equal to the 'rows'
2555 attribute set on the resultset (10 by default).
2556  
2557 =cut
2558  
2559 12 ▶sub page {
Subroutine page: called CC=1 SCAR=0.0
Pod documented
2560 12 my ($self, $page) = @_;
2561 12 return (ref $self)->new($self->result_source, { %{$self->{attrs}}, page => $page });
2562 }
2563  
2564 =head2 new_result
2565  
2566 =over 4
2567  
2568 =item Arguments: \%col_data
2569  
2570 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
2571  
2572 =back
2573  
2574 Creates a new result object in the resultset's result class and returns
2575 it. The row is not inserted into the database at this point, call
2576 L<DBIx::Class::Row/insert> to do that. Calling L<DBIx::Class::Row/in_storage>
2577 will tell you whether the result object has been inserted or not.
2578  
2579 Passes the hashref of input on to L<DBIx::Class::Row/new>.
2580  
2581 =cut
2582  
2583 1491 ▶sub new_result {
Subroutine new_result: called CC=7 SCAR=22.4
Pod documented
2584 1491 my ($self, $values) = @_;
2585  
2586 1491 ▶ $self->throw_exception( "new_result takes only one argument - a hashref of values" )
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 1491 1491
2587 if @_ > 2;
2588  
2589 1491 ▶ $self->throw_exception( "Result object instantiation requires a hashref as argument" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values eq "HASH" 0 1491 1491
2590 unless (ref $values eq 'HASH');
2591  
2592 1491 my ($merged_cond, $cols_from_relations) = $self->_merge_with_rscond($values);
2593  
2594 1491 ▶ my $new = $self->result_class->new({
Branch: 1 branch
BranchTrueFalseTotal
@$cols_from_relations ? : 6 1485 1491
2595 %$merged_cond,
2596 ( @$cols_from_relations
2597 ? (-cols_from_relations => $cols_from_relations)
2598 : ()
2599 ),
2600 -result_source => $self->result_source, # DO NOT REMOVE THIS, REQUIRED
2601 });
2602  
2603 1489 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($new) eq "HASH" and not keys %$new and builtin::blessed($new)) 0 1489 1489
Condition33%2/6
builtin::reftype($new) eq "HASH" and not keys %$new and builtin::blessed($new)
!ll&&!rl&&r
148900
builtin::reftype($new) eq "HASH" and not keys %$new
!ll&&!rl&&r
014890
MC/DC0%0/3
builtin::reftype($new) eq "HASH" and not keys %$new and builtin::blessed($new)
builtin::reftype($new) eq "HASH" not keys %$new builtin::blessed($new)
Truth table25%1/4
A and not(B) and C
A= builtin::reftype($new) eq "HASH"B= not keys %$newC= builtin::blessed($new)
ABCresult
0XX0
10X0
1100
1111
2604 reftype($new) eq 'HASH'
2605 and
2606 ! keys %$new
2607 and
2608 blessed($new)
2609 ) {
2610 0 carp_unique (sprintf (
2611 "%s->new returned a blessed empty hashref - a strong indicator something is wrong with its inheritance chain",
2612 $self->result_class,
2613 ));
2614 }
2615  
2616 1489 $new;
2617 }
2618  
2619 # _merge_with_rscond
2620 #
2621 # Takes a simple hash of K/V data and returns its copy merged with the
2622 # condition already present on the resultset. Additionally returns an
2623 # arrayref of value/condition names, which were inferred from related
2624 # objects (this is needed for in-memory related objects)
2625 12343 ▶sub _merge_with_rscond {
Subroutine _merge_with_rscond: called CC=7 SCAR=19.5
2626 12343 my ($self, $data) = @_;
2627  
2628 12343 my ($implied_data, @cols_from_relations);
2629  
2630 12343 my $alias = $self->{attrs}{alias};
2631  
2632 12343 ▶ if (! defined $self->{cond}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'cond'}) { } 10277 2066 12343
elsif ($self->{'cond'} eq \'1 = 0') { } 6 2060 2066
2633 # just massage $data below
2634 }
2635 elsif ($self->{cond} eq UNRESOLVABLE_CONDITION) {
2636 6 $implied_data = $self->{attrs}{related_objects}; # nothing might have been inserted yet
2637 6 ▶ @cols_from_relations = keys %{ $implied_data || {} };
Condition50%1/2
$implied_data || {}
l!l
60
MC/DC0%0/1
$implied_data || {}
$implied_data
Truth table50%1/2
A
A= $implied_data
Aresult
11
00
2638 }
2639 else {
2640 2060 my $eqs = $self->result_source->schema->storage->_extract_fixed_condition_columns($self->{cond}, 'consider_nulls');
2641 $implied_data = { map {
2642 2060 ▶ ( ($eqs->{$_}||'') eq UNRESOLVABLE_CONDITION ) ? () : ( $_ => $eqs->{$_} )
Branch: 1 branch
BranchTrueFalseTotal
($eqs->{$_} || '') eq \'1 = 0' ? : 0 2106 2106
Condition100%2/2
$eqs->{$_} || ''
l!l
20997
MC/DC100%1/1
$eqs->{$_} || ''
$eqs->{$_}
Truth table100%2/2
A
A= $eqs->{$_}
Aresult
11
00
2643 } keys %$eqs };
2644 }
2645  
2646 return (
2647 { map
2648 12343 ▶ { %{ $self->_remove_alias($_, $alias) } }
Condition100%3/3
$implied_data || ()
l!l&&r!l&&!r
206616138664
MC/DC100%2/2
$implied_data || ()
$implied_data ()
Truth table100%3/3
A || B
A= $implied_dataB= ()
ABresult
1X1
011
000
2649 # precedence must be given to passed values over values inherited from
2650 # the cond, so the order here is important.
2651 ( $implied_data||(), $data)
2652 },
2653 \@cols_from_relations
2654 );
2655 }
2656  
2657 # _has_resolved_attr
2658 #
2659 # determines if the resultset defines at least one
2660 # of the attributes supplied
2661 #
2662 # used to determine if a subquery is necessary
2663 #
2664 # supports some virtual attributes:
2665 # -join
2666 # This will scan for any joins being present on the resultset.
2667 # It is not a mere key-search but a deep inspection of {from}
2668 #
2669  
2670 1426 ▶sub _has_resolved_attr {
Subroutine _has_resolved_attr: called CC=12 SCAR=29.3
2671 1426 my ($self, @attr_names) = @_;
2672  
2673 1426 my $attrs = $self->_resolved_attrs;
2674  
2675 1426 my %extra_checks;
2676  
2677 1426 for my $n (@attr_names) {
2678 3306 ▶ if (grep { $n eq $_ } (qw/-join/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$n eq $_;} "-join") 0 3306 3306
2679 0 $extra_checks{$n}++;
2680 0 next;
2681 }
2682  
2683 3306 my $attr = $attrs->{$n};
2684  
2685 3306 ▶ next if not defined $attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr 3181 125 3306
2686  
2687 125 ▶ if (ref $attr eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $attr eq 'HASH') { } 0 125 125
elsif (ref $attr eq 'ARRAY') { } 64 61 125
2688 0 ▶ return 1 if keys %$attr;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$attr 0 0 0
2689 }
2690 elsif (ref $attr eq 'ARRAY') {
2691 64 ▶ return 1 if @$attr;
Branch: 1 branch
BranchTrueFalseTotal
if @$attr 64 0 64
2692 }
2693 else {
2694 61 ▶ return 1 if $attr;
Branch: 1 branch
BranchTrueFalseTotal
if $attr 41 20 61
2695 }
2696 }
2697  
2698 # a resolved join is expressed as a multi-level from
2699 return 1 if (
2700 $extra_checks{-join}
2701 and
2702 ref $attrs->{from} eq 'ARRAY'
2703 and
2704 0 ▶ @{$attrs->{from}} > 1
Branch: 1 branch
BranchTrueFalseTotal
if $extra_checks{"-join"} and ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1 0 1321 1321
Condition33%2/6
$extra_checks{"-join"} and ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1
!ll&&!rl&&r
132100
$extra_checks{"-join"} and ref $attrs->{"from"} eq "ARRAY"
!ll&&!rl&&r
132100
MC/DC0%0/3
$extra_checks{"-join"} and ref $attrs->{"from"} eq "ARRAY" and @{$attrs->{"from"};} > 1
$extra_checks{"-join"} ref $attrs->{"from"} eq "ARRAY" @{$attrs->{"from"};} > 1
Truth table25%1/4
A and B and C
A= $extra_checks{"-join"}B= ref $attrs->{"from"} eq "ARRAY"C= @{$attrs->{"from"};} > 1
ABCresult
0XX0
10X0
1100
1111
2705 );
2706  
2707 1321 return 0;
2708 }
2709  
2710 # _remove_alias
2711 #
2712 # Remove the specified alias from the specified query hash. A copy is made so
2713 # the original query is not modified.
2714  
2715 14409 ▶sub _remove_alias {
Subroutine _remove_alias: called CC=5 SCAR=16.1
2716 14409 my ($self, $query, $alias) = @_;
2717  
2718 14409 ▶ my %orig = %{ $query || {} };
Condition50%1/2
$query || {}
l!l
144090
MC/DC0%0/1
$query || {}
$query
Truth table50%1/2
A
A= $query
Aresult
11
00
2719 14409 my %unaliased;
2720  
2721 14409 foreach my $key (keys %orig) {
2722 8342 ▶ if ($key !~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /\./) 6687 1655 8342
2723 6687 $unaliased{$key} = $orig{$key};
2724 6687 next;
2725 }
2726 1655 ▶ $unaliased{$1} = $orig{$key}
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^(?:\Q$alias\E\.)?([^.]+)$/ 1640 15 1655
2727 if $key =~ m/^(?:\Q$alias\E\.)?([^.]+)$/;
2728 }
2729  
2730 14409 return \%unaliased;
2731 }
2732  
2733 =head2 as_query
2734  
2735 =over 4
2736  
2737 =item Arguments: none
2738  
2739 =item Return Value: \[ $sql, L<@bind_values|/DBIC BIND VALUES> ]
2740  
2741 =back
2742  
2743 Returns the SQL query and bind vars associated with the invocant.
2744  
2745 This is generally used as the RHS for a subquery.
2746  
2747 =cut
2748  
2749 674 ▶sub as_query {
Subroutine as_query: called CC=1 SCAR=0.0
Pod documented
2750 674 my $self = shift;
2751  
2752 674 my $attrs = { %{ $self->_resolved_attrs } };
2753  
2754 my $aq = $self->result_source->storage->_select_args_to_query (
2755 674 $attrs->{from}, $attrs->{select}, $attrs->{where}, $attrs
2756 );
2757  
2758 670 $aq;
2759 }
2760  
2761 =head2 find_or_new
2762  
2763 =over 4
2764  
2765 =item Arguments: \%col_data, { key => $unique_constraint, L<%attrs|/ATTRIBUTES> }?
2766  
2767 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
2768  
2769 =back
2770  
2771 my $artist = $schema->resultset('Artist')->find_or_new(
2772 { artist => 'fred' }, { key => 'artists' });
2773  
2774 $cd->cd_to_producer->find_or_new({ producer => $producer },
2775 { key => 'primary' });
2776  
2777 Find an existing record from this resultset using L</find>. if none exists,
2778 instantiate a new result object and return it. The object will not be saved
2779 into your storage until you call L<DBIx::Class::Row/insert> on it.
2780  
2781 You most likely want this method when looking for existing rows using a unique
2782 constraint that is not the primary key, or looking for related rows.
2783  
2784 If you want objects to be saved immediately, use L</find_or_create> instead.
2785  
2786 B<Note>: Make sure to read the documentation of L</find> and understand the
2787 significance of the C<key> attribute, as its lack may skew your search, and
2788 subsequently result in spurious new objects.
2789  
2790 B<Note>: Take care when using C<find_or_new> with a table having
2791 columns with default values that you intend to be automatically
2792 supplied by the database (e.g. an auto_increment primary key column).
2793 In normal usage, the value of such columns should NOT be included at
2794 all in the call to C<find_or_new>, even when set to C<undef>.
2795  
2796 =cut
2797  
2798 4 ▶sub find_or_new {
Subroutine find_or_new: called CC=6 SCAR=18.2
Pod documented
2799 4 my $self = shift;
2800 4 ▶ my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 1 3 4
Condition66%2/3
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
301
MC/DC50%1/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table66%2/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
2801 4 ▶ my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 4 0 4
2802 4 ▶ if (keys %$hash and my $row = $self->find($hash, $attrs) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$hash and my $row = $self->find($hash, $attrs)) 2 2 4
Condition66%2/3
keys %$hash and my $row = $self->find($hash, $attrs)
!ll&&!rl&&r
022
MC/DC50%1/2
keys %$hash and my $row = $self->find($hash, $attrs)
keys %$hash my $row = $self->find($hash, $attrs)
Truth table66%2/3
A and B
A= keys %$hashB= my $row = $self->find($hash, $attrs)
ABresult
0X0
100
111
2803 2 return $row;
2804 }
2805 2 return $self->new_result($hash);
2806 }
2807  
2808 =head2 create
2809  
2810 =over 4
2811  
2812 =item Arguments: \%col_data
2813  
2814 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
2815  
2816 =back
2817  
2818 Attempt to create a single new row or a row with multiple related rows
2819 in the table represented by the resultset (and related tables). This
2820 will not check for duplicate rows before inserting, use
2821 L</find_or_create> to do that.
2822  
2823 To create one row for this resultset, pass a hashref of key/value
2824 pairs representing the columns of the table and the values you wish to
2825 store. If the appropriate relationships are set up, foreign key fields
2826 can also be passed an object representing the foreign row, and the
2827 value will be set to its primary key.
2828  
2829 To create related objects, pass a hashref of related-object column values
2830 B<keyed on the relationship name>. If the relationship is of type C<multi>
2831 (L<DBIx::Class::Relationship/has_many>) - pass an arrayref of hashrefs.
2832 The process will correctly identify columns holding foreign keys, and will
2833 transparently populate them from the keys of the corresponding relation.
2834 This can be applied recursively, and will work correctly for a structure
2835 with an arbitrary depth and width, as long as the relationships actually
2836 exists and the correct column data has been supplied.
2837  
2838 Instead of hashrefs of plain related data (key/value pairs), you may
2839 also pass new or inserted objects. New objects (not inserted yet, see
2840 L</new_result>), will be inserted into their appropriate tables.
2841  
2842 Effectively a shortcut for C<< ->new_result(\%col_data)->insert >>.
2843  
2844 Example of creating a new row.
2845  
2846 $person_rs->create({
2847 name=>"Some Person",
2848 email=>"somebody@someplace.com"
2849 });
2850  
2851 Example of creating a new row and also creating rows in a related C<has_many>
2852 or C<has_one> resultset. Note Arrayref.
2853  
2854 $artist_rs->create(
2855 { artistid => 4, name => 'Manufactured Crap', cds => [
2856 { title => 'My First CD', year => 2006 },
2857 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
2858 ],
2859 },
2860 );
2861  
2862 Example of creating a new row and also creating a row in a related
2863 C<belongs_to> resultset. Note Hashref.
2864  
2865 $cd_rs->create({
2866 title=>"Music for Silly Walks",
2867 year=>2000,
2868 artist => {
2869 name=>"Silly Musician",
2870 }
2871 });
2872  
2873 =over
2874  
2875 =item WARNING
2876  
2877 When subclassing ResultSet never attempt to override this method. Since
2878 it is a simple shortcut for C<< $self->new_result($attrs)->insert >>, a
2879 lot of the internals simply never call it, so your override will be
2880 bypassed more often than not. Override either L<DBIx::Class::Row/new>
2881 or L<DBIx::Class::Row/insert> depending on how early in the
2882 L</create> process you need to intervene. See also warning pertaining to
2883 L</new>.
2884  
2885 =back
2886  
2887 =cut
2888  
2889 386 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
2890 #my ($self, $col_data) = @_;
2891 386 DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call;
2892 386 return shift->new_result(shift)->insert;
2893 }
2894  
2895 =head2 find_or_create
2896  
2897 =over 4
2898  
2899 =item Arguments: \%col_data, { key => $unique_constraint, L<%attrs|/ATTRIBUTES> }?
2900  
2901 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
2902  
2903 =back
2904  
2905 $cd->cd_to_producer->find_or_create({ producer => $producer },
2906 { key => 'primary' });
2907  
2908 Tries to find a record based on its primary key or unique constraints; if none
2909 is found, creates one and returns that instead.
2910  
2911 my $cd = $schema->resultset('CD')->find_or_create({
2912 cdid => 5,
2913 artist => 'Massive Attack',
2914 title => 'Mezzanine',
2915 year => 2005,
2916 });
2917  
2918 Also takes an optional C<key> attribute, to search by a specific key or unique
2919 constraint. For example:
2920  
2921 my $cd = $schema->resultset('CD')->find_or_create(
2922 {
2923 artist => 'Massive Attack',
2924 title => 'Mezzanine',
2925 },
2926 { key => 'cd_artist_title' }
2927 );
2928  
2929 B<Note>: Make sure to read the documentation of L</find> and understand the
2930 significance of the C<key> attribute, as its lack may skew your search, and
2931 subsequently result in spurious row creation.
2932  
2933 B<Note>: Because find_or_create() reads from the database and then
2934 possibly inserts based on the result, this method is subject to a race
2935 condition. Another process could create a record in the table after
2936 the find has completed and before the create has started. To avoid
2937 this problem, use find_or_create() inside a transaction.
2938  
2939 B<Note>: Take care when using C<find_or_create> with a table having
2940 columns with default values that you intend to be automatically
2941 supplied by the database (e.g. an auto_increment primary key column).
2942 In normal usage, the value of such columns should NOT be included at
2943 all in the call to C<find_or_create>, even when set to C<undef>.
2944  
2945 See also L</find> and L</update_or_create>. For information on how to declare
2946 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
2947  
2948 If you need to know if an existing row was found or a new one created use
2949 L</find_or_new> and L<DBIx::Class::Row/in_storage> instead. Don't forget
2950 to call L<DBIx::Class::Row/insert> to save the newly created row to the
2951 database!
2952  
2953 my $cd = $schema->resultset('CD')->find_or_new({
2954 cdid => 5,
2955 artist => 'Massive Attack',
2956 title => 'Mezzanine',
2957 year => 2005,
2958 });
2959  
2960 if( !$cd->in_storage ) {
2961 # do some stuff
2962 $cd->insert;
2963 }
2964  
2965 =cut
2966  
2967 30 ▶sub find_or_create {
Subroutine find_or_create: called CC=6 SCAR=18.2
Pod documented
2968 30 my $self = shift;
2969 30 ▶ my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 5 25 30
Condition66%2/3
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
2505
MC/DC50%1/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table66%2/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
2970 30 ▶ my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 30 0 30
2971 30 ▶ if (keys %$hash and my $row = $self->find($hash, $attrs) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$hash and my $row = $self->find($hash, $attrs)) 7 21 28
Condition66%2/3
keys %$hash and my $row = $self->find($hash, $attrs)
!ll&&!rl&&r
0237
MC/DC50%1/2
keys %$hash and my $row = $self->find($hash, $attrs)
keys %$hash my $row = $self->find($hash, $attrs)
Truth table66%2/3
A and B
A= keys %$hashB= my $row = $self->find($hash, $attrs)
ABresult
0X0
100
111
2972 7 return $row;
2973 }
2974 21 return $self->new_result($hash)->insert;
2975 }
2976  
2977 =head2 update_or_create
2978  
2979 =over 4
2980  
2981 =item Arguments: \%col_data, { key => $unique_constraint, L<%attrs|/ATTRIBUTES> }?
2982  
2983 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
2984  
2985 =back
2986  
2987 $resultset->update_or_create({ col => $val, ... });
2988  
2989 Like L</find_or_create>, but if a row is found it is immediately updated via
2990 C<< $found_row->update (\%col_data) >>.
2991  
2992  
2993 Takes an optional C<key> attribute to search on a specific unique constraint.
2994 For example:
2995  
2996 # In your application
2997 my $cd = $schema->resultset('CD')->update_or_create(
2998 {
2999 artist => 'Massive Attack',
3000 title => 'Mezzanine',
3001 year => 1998,
3002 },
3003 { key => 'cd_artist_title' }
3004 );
3005  
3006 $cd->cd_to_producer->update_or_create({
3007 producer => $producer,
3008 name => 'harry',
3009 }, {
3010 key => 'primary',
3011 });
3012  
3013 B<Note>: Make sure to read the documentation of L</find> and understand the
3014 significance of the C<key> attribute, as its lack may skew your search, and
3015 subsequently result in spurious row creation.
3016  
3017 B<Note>: Take care when using C<update_or_create> with a table having
3018 columns with default values that you intend to be automatically
3019 supplied by the database (e.g. an auto_increment primary key column).
3020 In normal usage, the value of such columns should NOT be included at
3021 all in the call to C<update_or_create>, even when set to C<undef>.
3022  
3023 See also L</find> and L</find_or_create>. For information on how to declare
3024 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
3025  
3026 If you need to know if an existing row was updated or a new one created use
3027 L</update_or_new> and L<DBIx::Class::Row/in_storage> instead. Don't forget
3028 to call L<DBIx::Class::Row/insert> to save the newly created row to the
3029 database!
3030  
3031 =cut
3032  
3033 13 ▶sub update_or_create {
Subroutine update_or_create: called CC=5 SCAR=16.2
Pod documented
3034 13 my $self = shift;
3035 13 ▶ my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 4 9 13
Condition66%2/3
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
904
MC/DC50%1/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table66%2/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
3036 13 ▶ my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 13 0 13
3037  
3038 13 my $row = $self->find($cond, $attrs);
3039 13 ▶ if (defined $row) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row) 8 5 13
3040 8 $row->update($cond);
3041 8 return $row;
3042 }
3043  
3044 5 return $self->new_result($cond)->insert;
3045 }
3046  
3047 =head2 update_or_new
3048  
3049 =over 4
3050  
3051 =item Arguments: \%col_data, { key => $unique_constraint, L<%attrs|/ATTRIBUTES> }?
3052  
3053 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
3054  
3055 =back
3056  
3057 $resultset->update_or_new({ col => $val, ... });
3058  
3059 Like L</find_or_new> but if a row is found it is immediately updated via
3060 C<< $found_row->update (\%col_data) >>.
3061  
3062 For example:
3063  
3064 # In your application
3065 my $cd = $schema->resultset('CD')->update_or_new(
3066 {
3067 artist => 'Massive Attack',
3068 title => 'Mezzanine',
3069 year => 1998,
3070 },
3071 { key => 'cd_artist_title' }
3072 );
3073  
3074 if ($cd->in_storage) {
3075 # the cd was updated
3076 }
3077 else {
3078 # the cd is not yet in the database, let's insert it
3079 $cd->insert;
3080 }
3081  
3082 B<Note>: Make sure to read the documentation of L</find> and understand the
3083 significance of the C<key> attribute, as its lack may skew your search, and
3084 subsequently result in spurious new objects.
3085  
3086 B<Note>: Take care when using C<update_or_new> with a table having
3087 columns with default values that you intend to be automatically
3088 supplied by the database (e.g. an auto_increment primary key column).
3089 In normal usage, the value of such columns should NOT be included at
3090 all in the call to C<update_or_new>, even when set to C<undef>.
3091  
3092 See also L</find>, L</find_or_create> and L</find_or_new>.
3093  
3094 =cut
3095  
3096 2 ▶sub update_or_new {
Subroutine update_or_new: called CC=5 SCAR=16.7
Pod documented
3097 2 my $self = shift;
3098 2 ▶ my $attrs = ( @_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {} );
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && ref $_[$#_] eq 'HASH' ? : 2 0 2
Condition33%1/3
@_ > 1 and ref $_[$#_] eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
@_ > 1 and ref $_[$#_] eq "HASH"
@_ > 1 ref $_[$#_] eq "HASH"
Truth table33%1/3
A and B
A= @_ > 1B= ref $_[$#_] eq "HASH"
ABresult
0X0
100
111
3099 2 ▶ my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 2 0 2
3100  
3101 2 my $row = $self->find( $cond, $attrs );
3102 2 ▶ if ( defined $row ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $row) 1 1 2
3103 1 $row->update($cond);
3104 1 return $row;
3105 }
3106  
3107 1 return $self->new_result($cond);
3108 }
3109  
3110 =head2 get_cache
3111  
3112 =over 4
3113  
3114 =item Arguments: none
3115  
3116 =item Return Value: L<\@result_objs|DBIx::Class::Manual::ResultClass> | undef
3117  
3118 =back
3119  
3120 Gets the contents of the cache for the resultset, if the cache is set.
3121  
3122 The cache is populated either by using the L</prefetch> attribute to
3123 L</search> or by calling L</set_cache>.
3124  
3125 =cut
3126  
3127 44105 ▶sub get_cache {
Subroutine get_cache: called CC=1 SCAR=0.0
Pod documented
3128 44105 shift->{all_cache};
3129 }
3130  
3131 =head2 set_cache
3132  
3133 =over 4
3134  
3135 =item Arguments: L<\@result_objs|DBIx::Class::Manual::ResultClass>
3136  
3137 =item Return Value: L<\@result_objs|DBIx::Class::Manual::ResultClass>
3138  
3139 =back
3140  
3141 Sets the contents of the cache for the resultset. Expects an arrayref
3142 of objects of the same class as those produced by the resultset. Note that
3143 if the cache is set, the resultset will return the cached objects rather
3144 than re-querying the database even if the cache attr is not set.
3145  
3146 The contents of the cache can also be populated by using the
3147 L</prefetch> attribute to L</search>.
3148  
3149 =cut
3150  
3151 798 ▶sub set_cache {
Subroutine set_cache: called CC=3 SCAR=11.4
Pod documented
3152 798 my ( $self, $data ) = @_;
3153 798 ▶ $self->throw_exception("set_cache requires an arrayref")
Branch: 1 branch
BranchTrueFalseTotal
if defined $data and ref $data ne "ARRAY" 0 798 798
Condition66%2/3
defined $data and ref $data ne "ARRAY"
!ll&&!rl&&r
27960
MC/DC0%0/2
defined $data and ref $data ne "ARRAY"
defined $data ref $data ne "ARRAY"
Truth table66%2/3
A and B
A= defined $dataB= ref $data ne "ARRAY"
ABresult
0X0
100
111
3154 if defined($data) && (ref $data ne 'ARRAY');
3155 798 $self->{all_cache} = $data;
3156 }
3157  
3158 =head2 clear_cache
3159  
3160 =over 4
3161  
3162 =item Arguments: none
3163  
3164 =item Return Value: undef
3165  
3166 =back
3167  
3168 Clears the cache for the resultset.
3169  
3170 =cut
3171  
3172 2 ▶sub clear_cache {
Subroutine clear_cache: called CC=1 SCAR=0.0
Pod documented
3173 2 shift->set_cache(undef);
3174 }
3175  
3176 =head2 is_paged
3177  
3178 =over 4
3179  
3180 =item Arguments: none
3181  
3182 =item Return Value: true, if the resultset has been paginated
3183  
3184 =back
3185  
3186 =cut
3187  
3188 2 ▶sub is_paged {
Subroutine is_paged: called CC=1 SCAR=0.0
Pod documented
3189 2 my ($self) = @_;
3190 2 return !!$self->{attrs}{page};
3191 }
3192  
3193 =head2 is_ordered
3194  
3195 =over 4
3196  
3197 =item Arguments: none
3198  
3199 =item Return Value: true, if the resultset has been ordered with C<order_by>.
3200  
3201 =back
3202  
3203 =cut
3204  
3205 14 ▶sub is_ordered {
Subroutine is_ordered: called CC=1 SCAR=0.0
Pod documented
3206 14 my ($self) = @_;
3207 14 return scalar $self->result_source->storage->_extract_order_criteria($self->{attrs}{order_by});
3208 }
3209  
3210 =head2 related_resultset
3211  
3212 =over 4
3213  
3214 =item Arguments: $rel_name
3215  
3216 =item Return Value: L<$resultset|/search>
3217  
3218 =back
3219  
3220 Returns a related resultset for the supplied relationship name.
3221  
3222 $artist_rs = $schema->resultset('CD')->related_resultset('Artist');
3223  
3224 =cut
3225  
3226 230 ▶sub related_resultset {
Subroutine related_resultset: called CC=6 SCAR=17.9
Pod documented
3227 230 my ($self, $rel) = @_;
3228  
3229 return $self->{related_resultsets}{$rel}
3230 230 ▶ if defined $self->{related_resultsets}{$rel};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"related_resultsets"}{$rel} 14 216 230
3231  
3232 216 return $self->{related_resultsets}{$rel} = do {
3233 216 my $rsrc = $self->result_source;
3234 216 my $rel_info = $rsrc->relationship_info($rel);
3235  
3236 216 ▶ $self->throw_exception(
Branch: 1 branch
BranchTrueFalseTotal
unless $rel_info 0 216 216
3237 "search_related: result source '" . $rsrc->source_name .
3238 "' has no such relationship $rel")
3239 unless $rel_info;
3240  
3241 216 my $attrs = $self->_chain_relationship($rel);
3242  
3243 216 my $join_count = $attrs->{seen_join}{$rel};
3244  
3245 216 my $alias = $self->result_source->storage
3246 ->relname_to_table_alias($rel, $join_count);
3247  
3248 # since this is search_related, and we already slid the select window inwards
3249 # (the select/as attrs were deleted in the beginning), we need to flip all
3250 # left joins to inner, so we get the expected results
3251 # read the comment on top of the actual function to see what this does
3252 216 $attrs->{from} = $rsrc->schema->storage->_inner_join_to_node ($attrs->{from}, $alias);
3253  
3254  
3255 #XXX - temp fix for result_class bug. There likely is a more elegant fix -groditi
3256 216 delete @{$attrs}{qw(result_class alias)};
3257  
3258 216 my $rel_source = $rsrc->related_source($rel);
3259  
3260 216 my $new = do {
3261  
3262 # The reason we do this now instead of passing the alias to the
3263 # search_rs below is that if you wrap/overload resultset on the
3264 # source you need to know what alias it's -going- to have for things
3265 # to work sanely (e.g. RestrictWithObject wants to be able to add
3266 # extra query restrictions, and these may need to be $alias.)
3267  
3268 216 my $rel_attrs = $rel_source->resultset_attributes;
3269 216 local $rel_attrs->{alias} = $alias;
3270  
3271 $rel_source->resultset
3272 ->search_rs(
3273 undef, {
3274 %$attrs,
3275 where => $attrs->{where},
3276 216 });
3277 };
3278  
3279 216 ▶ if (my $cache = $self->get_cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cache = $self->get_cache) 13 203 216
3280 my @related_cache = map
3281 13 ▶ { $_->related_resultset($rel)->get_cache || () }
Condition66%2/3
$_->related_resultset($rel)->get_cache || ()
l!l&&r!l&&!r
21180
MC/DC0%0/2
$_->related_resultset($rel)->get_cache || ()
$_->related_resultset($rel)->get_cache ()
Truth table66%2/3
A || B
A= $_->related_resultset($rel)->get_cacheB= ()
ABresult
1X1
011
000
3282 @$cache
3283 ;
3284  
3285 13 ▶ $new->set_cache([ map @$_, @related_cache ]) if @related_cache == @$cache;
Branch: 1 branch
BranchTrueFalseTotal
if @related_cache == @$cache 7 6 13
3286 }
3287  
3288 216 $new;
3289 };
3290 }
3291  
3292 =head2 current_source_alias
3293  
3294 =over 4
3295  
3296 =item Arguments: none
3297  
3298 =item Return Value: $source_alias
3299  
3300 =back
3301  
3302 Returns the current table alias for the result source this resultset is built
3303 on, that will be used in the SQL query. Usually it is C<me>.
3304  
3305 Currently the source alias that refers to the result set returned by a
3306 L</search>/L</find> family method depends on how you got to the resultset: it's
3307 C<me> by default, but eg. L</search_related> aliases it to the related result
3308 source name (and keeps C<me> referring to the original result set). The long
3309 term goal is to make L<DBIx::Class> always alias the current resultset as C<me>
3310 (and make this method unnecessary).
3311  
3312 Thus it's currently necessary to use this method in predefined queries (see
3313 L<DBIx::Class::Manual::Cookbook/Predefined searches>) when referring to the
3314 source alias of the current result set:
3315  
3316 # in a result set class
3317 sub modified_by {
3318 my ($self, $user) = @_;
3319  
3320 my $me = $self->current_source_alias;
3321  
3322 return $self->search({
3323 "$me.modified" => $user->id,
3324 });
3325 }
3326  
3327 The alias of L<newly created resultsets|/search> can be altered by the
3328 L<alias attribute|/alias>.
3329  
3330 =cut
3331  
3332 719 ▶sub current_source_alias {
Subroutine current_source_alias: called CC=3 SCAR=12.7
Pod documented
3333 719 ▶ return (shift->{attrs} || {})->{alias} || 'me';
Condition50%2/4
+(+(shift())->{'attrs'} || {})->{'alias'} || 'me'
l!l
7190
+(shift())->{'attrs'} || {}
l!l
7190
MC/DC0%0/1
+(+(shift())->{'attrs'} || {})->{'alias'} || 'me'
+(shift())->{'attrs'}
Truth table50%1/2
A
A= +(shift())->{'attrs'}
Aresult
11
00
3334 }
3335  
3336 =head2 as_subselect_rs
3337  
3338 =over 4
3339  
3340 =item Arguments: none
3341  
3342 =item Return Value: L<$resultset|/search>
3343  
3344 =back
3345  
3346 Act as a barrier to SQL symbols. The resultset provided will be made into a
3347 "virtual view" by including it as a subquery within the from clause. From this
3348 point on, any joined tables are inaccessible to ->search on the resultset (as if
3349 it were simply where-filtered without joins). For example:
3350  
3351 my $rs = $schema->resultset('Bar')->search({'x.name' => 'abc'},{ join => 'x' });
3352  
3353 # 'x' now pollutes the query namespace
3354  
3355 # So the following works as expected
3356 my $ok_rs = $rs->search({'x.other' => 1});
3357  
3358 # But this doesn't: instead of finding a 'Bar' related to two x rows (abc and
3359 # def) we look for one row with contradictory terms and join in another table
3360 # (aliased 'x_2') which we never use
3361 my $broken_rs = $rs->search({'x.name' => 'def'});
3362  
3363 my $rs2 = $rs->as_subselect_rs;
3364  
3365 # doesn't work - 'x' is no longer accessible in $rs2, having been sealed away
3366 my $not_joined_rs = $rs2->search({'x.other' => 1});
3367  
3368 # works as expected: finds a 'table' row related to two x rows (abc and def)
3369 my $correctly_joined_rs = $rs2->search({'x.name' => 'def'});
3370  
3371 Another example of when one might use this would be to select a subset of
3372 columns in a group by clause:
3373  
3374 my $rs = $schema->resultset('Bar')->search(undef, {
3375 group_by => [qw{ id foo_id baz_id }],
3376 })->as_subselect_rs->search(undef, {
3377 columns => [qw{ id foo_id }]
3378 });
3379  
3380 In the above example normally columns would have to be equal to the group by,
3381 but because we isolated the group by into a subselect the above works.
3382  
3383 =cut
3384  
3385 109 ▶sub as_subselect_rs {
Subroutine as_subselect_rs: called CC=1 SCAR=0.0
Pod documented
3386 109 my $self = shift;
3387  
3388 109 my $attrs = $self->_resolved_attrs;
3389  
3390 109 my $fresh_rs = (ref $self)->new (
3391 $self->result_source,
3392 {},
3393 );
3394  
3395 # these pieces will be locked in the subquery
3396 109 delete $fresh_rs->{cond};
3397 109 delete @{$fresh_rs->{attrs}}{qw/where bind/};
3398  
3399 return $fresh_rs->search( {}, {
3400 from => [{
3401 $attrs->{alias} => $self->as_query,
3402 -alias => $attrs->{alias},
3403 -rsrc => $self->result_source,
3404 }],
3405 alias => $attrs->{alias},
3406 109 });
3407 }
3408  
3409 # This code is called by search_related, and makes sure there
3410 # is clear separation between the joins before, during, and
3411 # after the relationship. This information is needed later
3412 # in order to properly resolve prefetch aliases (any alias
3413 # with a relation_chain_depth less than the depth of the
3414 # current prefetch is not considered)
3415 #
3416 # The increments happen twice per join. An even number means a
3417 # relationship specified via a search_related, whereas an odd
3418 # number indicates a join/prefetch added via attributes
3419 #
3420 # Also this code will wrap the current resultset (the one we
3421 # chain to) in a subselect IFF it contains limiting attributes
3422 216 ▶sub _chain_relationship {
Subroutine _chain_relationship: called CC=11 SCAR=24.0
3423 216 my ($self, $rel) = @_;
3424 216 my $source = $self->result_source;
3425 216 ▶ my $attrs = { %{$self->{attrs}||{}} };
Condition50%1/2
$self->{'attrs'} || {}
l!l
2160
MC/DC0%0/1
$self->{'attrs'} || {}
$self->{'attrs'}
Truth table50%1/2
A
A= $self->{'attrs'}
Aresult
11
00
3426  
3427 # we need to take the prefetch the attrs into account before we
3428 # ->_resolve_join as otherwise they get lost - captainL
3429 216 my $join = $self->_merge_joinpref_attr( $attrs->{join}, $attrs->{prefetch} );
3430  
3431 216 delete @{$attrs}{qw/join prefetch collapse group_by distinct _grouped_by_distinct select as columns +select +as +columns/};
3432  
3433 216 ▶ my $seen = { %{ (delete $attrs->{seen_join}) || {} } };
Condition100%2/2
delete $attrs->{'seen_join'} || {}
l!l
24192
MC/DC100%1/1
delete $attrs->{'seen_join'} || {}
delete $attrs->{'seen_join'}
Truth table100%2/2
A
A= delete $attrs->{'seen_join'}
Aresult
11
00
3434  
3435 216 my $from;
3436 216 my @force_subq_attrs = qw/offset rows group_by having/;
3437  
3438 216 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($attrs->{'from'} and ref $attrs->{'from'} ne 'ARRAY' or $self->_has_resolved_attr(@force_subq_attrs)) { } 9 207 216
elsif ($attrs->{'from'}) { } 22 185 207
Condition66%4/6
$attrs->{"from"} and ref $attrs->{"from"} ne "ARRAY" or $self->_has_resolved_attr(@force_subq_attrs)
l!l&&r!l&&!r
09207
$attrs->{"from"} and ref $attrs->{"from"} ne "ARRAY"
!ll&&!rl&&r
192240
MC/DC33%1/3
$attrs->{"from"} and ref $attrs->{"from"} ne "ARRAY" or $self->_has_resolved_attr(@force_subq_attrs)
$attrs->{"from"} ref $attrs->{"from"} ne "ARRAY" $self->_has_resolved_attr(@force_subq_attrs)
Truth table80%4/5
A and B or C
A= $attrs->{"from"}B= ref $attrs->{"from"} ne "ARRAY"C= $self->_has_resolved_attr(@force_subq_attrs)
ABCresult
11X1
0X11
1011
0X00
1000
3439 ($attrs->{from} && ref $attrs->{from} ne 'ARRAY')
3440 ||
3441 $self->_has_resolved_attr (@force_subq_attrs)
3442 ) {
3443 # Nuke the prefetch (if any) before the new $rs attrs
3444 # are resolved (prefetch is useless - we are wrapping
3445 # a subquery anyway).
3446 9 my $rs_copy = $self->search;
3447 $rs_copy->{attrs}{join} = $self->_merge_joinpref_attr (
3448 $rs_copy->{attrs}{join},
3449 delete $rs_copy->{attrs}{prefetch},
3450 9 );
3451  
3452 $from = [{
3453 -rsrc => $source,
3454 -alias => $attrs->{alias},
3455 9 $attrs->{alias} => $rs_copy->as_query,
3456 }];
3457 9 delete @{$attrs}{@force_subq_attrs, qw/where bind/};
3458 9 $seen->{-relation_chain_depth} = 0;
3459 }
3460 elsif ($attrs->{from}) { #shallow copy suffices
3461 22 $from = [ @{$attrs->{from}} ];
3462 }
3463 else {
3464 $from = [{
3465 -rsrc => $source,
3466 -alias => $attrs->{alias},
3467 185 $attrs->{alias} => $source->from,
3468 }];
3469 }
3470  
3471 my $jpath = ($seen->{-relation_chain_depth})
3472 ? $from->[-1][0]{-join_path}
3473 216 ▶ : [];
Branch: 1 branch
BranchTrueFalseTotal
$seen->{'-relation_chain_depth'} ? : 22 194 216
3474  
3475 my @requested_joins = $source->_resolve_join(
3476 $join,
3477 $attrs->{alias},
3478 216 $seen,
3479 $jpath,
3480 );
3481  
3482 216 push @$from, @requested_joins;
3483  
3484 216 $seen->{-relation_chain_depth}++;
3485  
3486 # if $self already had a join/prefetch specified on it, the requested
3487 # $rel might very well be already included. What we do in this case
3488 # is effectively a no-op (except that we bump up the chain_depth on
3489 # the join in question so we could tell it *is* the search_related)
3490 216 my $already_joined;
3491  
3492 # we consider the last one thus reverse
3493 216 for my $j (reverse @requested_joins) {
3494 30 my ($last_j) = keys %{$j->[0]{-join_path}[-1]};
3495 30 ▶ if ($rel eq $last_j) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel eq $last_j) 12 18 30
3496 12 $j->[0]{-relation_chain_depth}++;
3497 12 $already_joined++;
3498 12 last;
3499 }
3500 }
3501  
3502 216 ▶ unless ($already_joined) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($already_joined) 204 12 216
3503 push @$from, $source->_resolve_join(
3504 $rel,
3505 $attrs->{alias},
3506 204 $seen,
3507 $jpath,
3508 );
3509 }
3510  
3511 216 $seen->{-relation_chain_depth}++;
3512  
3513 216 return {%$attrs, from => $from, seen_join => $seen};
3514 }
3515  
3516 20829 ▶sub _resolved_attrs {
Subroutine _resolved_attrs: called CC=71 SCAR=43.1
3517 20829 my $self = shift;
3518 20829 ▶ return $self->{_attrs} if $self->{_attrs};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_attrs"} 11848 8981 20829
3519  
3520 8981 ▶ my $attrs = { %{ $self->{attrs} || {} } };
Condition50%1/2
$self->{'attrs'} || {}
l!l
89810
MC/DC0%0/1
$self->{'attrs'} || {}
$self->{'attrs'}
Truth table50%1/2
A
A= $self->{'attrs'}
Aresult
11
00
3521 8981 my $source = $attrs->{result_source} = $self->result_source;
3522 8981 my $alias = $attrs->{alias};
3523  
3524 $self->throw_exception("Specifying distinct => 1 in conjunction with collapse => 1 is unsupported")
3525 8981 ▶ if $attrs->{collapse} and $attrs->{distinct};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"collapse"} and $attrs->{"distinct"} 0 8981 8981
Condition66%2/3
$attrs->{"collapse"} and $attrs->{"distinct"}
!ll&&!rl&&r
8945360
MC/DC0%0/2
$attrs->{"collapse"} and $attrs->{"distinct"}
$attrs->{"collapse"} $attrs->{"distinct"}
Truth table66%2/3
A and B
A= $attrs->{"collapse"}B= $attrs->{"distinct"}
ABresult
0X0
100
111
3526  
3527 # default selection list
3528 $attrs->{columns} = [ $source->columns ]
3529 8981 ▶ unless grep { exists $attrs->{$_} } qw/columns cols select as/;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {exists $attrs->{$_};} "columns", "cols", "select", "as" 6550 2431 8981
3530  
3531 # merge selectors together
3532 8981 for (qw/columns select as/) {
3533 $attrs->{$_} = $self->_merge_attr($attrs->{$_}, delete $attrs->{"+$_"})
3534 26943 ▶ if $attrs->{$_} or $attrs->{"+$_"};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{$_} or $$attrs{"+$_"} 10202 16741 26943
Condition100%3/3
$attrs->{$_} or $$attrs{"+$_"}
l!l&&r!l&&!r
101069616741
MC/DC100%2/2
$attrs->{$_} or $$attrs{"+$_"}
$attrs->{$_} $$attrs{"+$_"}
Truth table100%3/3
A or B
A= $attrs->{$_}B= $$attrs{"+$_"}
ABresult
1X1
011
000
3535 }
3536  
3537 # disassemble columns
3538 8981 my (@sel, @as);
3539 8981 ▶ if (my $cols = delete $attrs->{columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cols = delete $attrs->{"columns"}) 7865 1116 8981
3540 7865 ▶ for my $c (ref $cols eq 'ARRAY' ? @$cols : $cols) {
Branch: 1 branch
BranchTrueFalseTotal
ref $cols eq 'ARRAY' ? : 7865 0 7865
3541 35843 ▶ if (ref $c eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq 'HASH') { } 1067 34776 35843
3542 1067 for my $as (sort keys %$c) {
3543 1134 push @sel, $c->{$as};
3544 1134 push @as, $as;
3545 }
3546 }
3547 else {
3548 34776 push @sel, $c;
3549 34776 push @as, $c;
3550 }
3551 }
3552 }
3553  
3554 # when trying to weed off duplicates later do not go past this point -
3555 # everything added from here on is unbalanced "anyone's guess" stuff
3556 8981 my $dedup_stop_idx = $#as;
3557  
3558 1166 ▶ push @as, @{ ref $attrs->{as} eq 'ARRAY' ? $attrs->{as} : [ $attrs->{as} ] }
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs->{'as'} eq 'ARRAY' ? : 1166 0 1166
3559 8981 ▶ if $attrs->{as};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"as"} 1166 7815 8981
3560 1171 ▶ push @sel, @{ ref $attrs->{select} eq 'ARRAY' ? $attrs->{select} : [ $attrs->{select} ] }
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs->{'select'} eq 'ARRAY' ? : 1171 0 1171
3561 8981 ▶ if $attrs->{select};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"select"} 1171 7810 8981
3562  
3563 # assume all unqualified selectors to apply to the current alias (legacy stuff)
3564 8981 ▶ $_ = (ref $_ or $_ =~ /\./) ? $_ : "$alias.$_" for @sel;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ || $_ =~ /\./ ? : 2108 35107 37215
Condition100%3/3
ref $_ or $_ =~ /\./
l!l&&r!l&&!r
144965935107
MC/DC100%2/2
ref $_ or $_ =~ /\./
ref $_ $_ =~ /\./
Truth table100%3/3
A or B
A= ref $_B= $_ =~ /\./
ABresult
1X1
011
000
3565  
3566 # disqualify all $alias.col as-bits (inflate-map mandated)
3567 8981 ▶ $_ = ($_ =~ /^\Q$alias.\E(.+)$/) ? $1 : $_ for @as;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^\Q$alias.\E(.+)$/ ? : 234 36946 37180
3568  
3569 # de-duplicate the result (remove *identical* select/as pairs)
3570 # and also die on duplicate {as} pointing to different {select}s
3571 # not using a c-style for as the condition is prone to shrinkage
3572 8981 my $seen;
3573 8981 my $i = 0;
3574 8981 ▶ while ($i <= $dedup_stop_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $dedup_stop_idx) 35910 8981 44891
3575 35910 ▶ if ($seen->{"$sel[$i] \x00\x00 $as[$i]"}++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$seen{"$sel[$i] \000\000 $as[$i]"}++) { } 10 35900 35910
elsif ($$seen{$as[$i]}++) { } 0 35900 35900
3576 10 splice @sel, $i, 1;
3577 10 splice @as, $i, 1;
3578 10 $dedup_stop_idx--;
3579 }
3580 elsif ($seen->{$as[$i]}++) {
3581 0 $self->throw_exception(
3582 "inflate_result() alias '$as[$i]' specified twice with different SQL-side {select}-ors"
3583 );
3584 }
3585 else {
3586 35900 $i++;
3587 }
3588 }
3589  
3590 8981 $attrs->{select} = \@sel;
3591 8981 $attrs->{as} = \@as;
3592  
3593 $attrs->{from} ||= [{
3594 -rsrc => $source,
3595 -alias => $self->{attrs}{alias},
3596 8981 ▶ $self->{attrs}{alias} => $source->from,
Condition100%2/2
$attrs->{"from"} ||= [{"-rsrc", $source, "-alias", $self->{"attrs"}{"alias"}, $self->{"attrs"}{"alias"}, $source->from}]
l!l
15887393
MC/DC100%1/1
$attrs->{"from"} ||= [{"-rsrc", $source, "-alias", $self->{"attrs"}{"alias"}, $self->{"attrs"}{"alias"}, $source->from}]
$attrs->{"from"}
Truth table100%2/2
A
A= $attrs->{"from"}
Aresult
11
00
3597 }];
3598  
3599 8981 ▶ if ( $attrs->{join} || $attrs->{prefetch} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"join"} or $attrs->{"prefetch"}) 722 8259 8981
Condition100%3/3
$attrs->{"join"} or $attrs->{"prefetch"}
l!l&&r!l&&!r
5531698259
MC/DC100%2/2
$attrs->{"join"} or $attrs->{"prefetch"}
$attrs->{"join"} $attrs->{"prefetch"}
Truth table100%3/3
A or B
A= $attrs->{"join"}B= $attrs->{"prefetch"}
ABresult
1X1
011
000
3600  
3601 $self->throw_exception ('join/prefetch can not be used with a custom {from}')
3602 722 ▶ if ref $attrs->{from} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $attrs->{"from"} ne "ARRAY" 0 722 722
3603  
3604 722 ▶ my $join = (delete $attrs->{join}) || {};
Condition100%2/2
delete $attrs->{'join'} || {}
l!l
553169
MC/DC100%1/1
delete $attrs->{'join'} || {}
delete $attrs->{'join'}
Truth table100%2/2
A
A= delete $attrs->{'join'}
Aresult
11
00
3605  
3606 722 ▶ if ( defined $attrs->{prefetch} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attrs->{"prefetch"}) 485 237 722
3607 485 $join = $self->_merge_joinpref_attr( $join, $attrs->{prefetch} );
3608 }
3609  
3610 $attrs->{from} = # have to copy here to avoid corrupting the original
3611 [
3612 722 @{ $attrs->{from} },
3613 $source->_resolve_join(
3614 $join,
3615 $alias,
3616 722 ▶ { %{ $attrs->{seen_join} || {} } },
Branch: 1 branch
BranchTrueFalseTotal
$attrs->{'seen_join'} && keys %{$$attrs{'seen_join'};} ? : 46 676 722
Condition100%5/5
$attrs->{'seen_join'} || {}
l!l
48674
$attrs->{"seen_join"} and keys %{$$attrs{"seen_join"};}
!ll&&!rl&&r
674246
MC/DC100%1/1
$attrs->{'seen_join'} || {}
$attrs->{'seen_join'}
MC/DC100%2/2
$attrs->{"seen_join"} and keys %{$$attrs{"seen_join"};}
$attrs->{"seen_join"} keys %{$$attrs{"seen_join"};}
Truth table100%2/2
A
A= $attrs->{'seen_join'}
Aresult
11
00
Truth table100%3/3
A and B
A= $attrs->{"seen_join"}B= keys %{$$attrs{"seen_join"};}
ABresult
0X0
100
111
3617 48 ( $attrs->{seen_join} && keys %{$attrs->{seen_join}})
3618 ? $attrs->{from}[-1][0]{-join_path}
3619 722 : []
3620 ,
3621 )
3622 ];
3623 }
3624  
3625 8981 for my $attr (qw(order_by group_by)) {
3626  
3627 17962 ▶ if ( defined $attrs->{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attrs->{$attr}) 3580 14382 17962
3628 $attrs->{$attr} = (
3629 ref( $attrs->{$attr} ) eq 'ARRAY'
3630 631 ▶ ? [ @{ $attrs->{$attr} } ]
Condition66%2/3
$attrs->{$attr} || ()
l!l&&r!l&&!r
294720
MC/DC0%0/2
$attrs->{$attr} || ()
$attrs->{$attr} ()
Truth table66%2/3
A || B
A= $attrs->{$attr}B= ()
ABresult
1X1
011
000
3631 3580 ▶ : [ $attrs->{$attr} || () ]
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs->{$attr} eq 'ARRAY' ? : 631 2949 3580
3632 );
3633  
3634 3580 ▶ delete $attrs->{$attr} unless @{$attrs->{$attr}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$attrs->{$attr};} 23 3557 3580
3635 }
3636 }
3637  
3638 # generate selections based on the prefetch helper
3639 8981 my ($prefetch, @prefetch_select, @prefetch_as);
3640 $prefetch = $self->_merge_joinpref_attr( {}, delete $attrs->{prefetch} )
3641 8981 ▶ if defined $attrs->{prefetch};
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"prefetch"} 485 8496 8981
3642  
3643 8981 ▶ if ($prefetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefetch) 353 8628 8981
3644  
3645 $self->throw_exception("Unable to prefetch, resultset contains an unnamed selector $attrs->{_dark_selector}{string}")
3646 353 ▶ if $attrs->{_dark_selector};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"_dark_selector"} 1 352 353
3647  
3648 $self->throw_exception("Specifying prefetch in conjunction with an explicit collapse => 0 is unsupported")
3649 352 ▶ if defined $attrs->{collapse} and ! $attrs->{collapse};
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"collapse"} and not $attrs->{"collapse"} 0 352 352
Condition33%1/3
defined $attrs->{"collapse"} and not $attrs->{"collapse"}
!ll&&!rl&&r
35200
MC/DC0%0/2
defined $attrs->{"collapse"} and not $attrs->{"collapse"}
defined $attrs->{"collapse"} not $attrs->{"collapse"}
Truth table33%1/3
A and not(B)
A= defined $attrs->{"collapse"}B= not $attrs->{"collapse"}
ABresult
0X0
100
111
3650  
3651 352 $attrs->{collapse} = 1;
3652  
3653 # this is a separate structure (we don't look in {from} directly)
3654 # as the resolver needs to shift things off the lists to work
3655 # properly (identical-prefetches on different branches)
3656 352 my $join_map = {};
3657 352 ▶ if (ref $attrs->{from} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs->{"from"} eq "ARRAY") 352 0 352
3658  
3659 352 ▶ my $start_depth = $attrs->{seen_join}{-relation_chain_depth} || 0;
Condition100%2/2
$attrs->{'seen_join'}{'-relation_chain_depth'} || 0
l!l
36316
MC/DC100%1/1
$attrs->{'seen_join'}{'-relation_chain_depth'} || 0
$attrs->{'seen_join'}{'-relation_chain_depth'}
Truth table100%2/2
A
A= $attrs->{'seen_join'}{'-relation_chain_depth'}
Aresult
11
00
3660  
3661 352 for my $j ( @{$attrs->{from}}[1 .. $#{$attrs->{from}} ] ) {
3662 529 ▶ next unless $j->[0]{-alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $j->[0]{"-alias"} 0 529 529
3663 529 ▶ next unless $j->[0]{-join_path};
Branch: 1 branch
BranchTrueFalseTotal
unless $j->[0]{"-join_path"} 0 529 529
3664 529 ▶ next if ($j->[0]{-relation_chain_depth} || 0) < $start_depth;
Branch: 1 branch
BranchTrueFalseTotal
if ($j->[0]{"-relation_chain_depth"} || 0) < $start_depth 12 517 529
Condition50%1/2
$j->[0]{'-relation_chain_depth'} || 0
l!l
5290
MC/DC0%0/1
$j->[0]{'-relation_chain_depth'} || 0
$j->[0]{'-relation_chain_depth'}
Truth table50%1/2
A
A= $j->[0]{'-relation_chain_depth'}
Aresult
11
00
3665  
3666 517 my @jpath = map { keys %$_ } @{$j->[0]{-join_path}};
3667  
3668 517 my $p = $join_map;
3669 517 ▶ $p = $p->{$_} ||= {} for @jpath[ ($start_depth/2) .. $#jpath]; #only even depths are actual jpath boundaries
Condition100%2/2
$p->{$_} ||= {}
l!l
164475
MC/DC100%1/1
$p->{$_} ||= {}
$p->{$_}
Truth table100%2/2
A
A= $p->{$_}
Aresult
11
00
3670 517 push @{$p->{-join_aliases} }, $j->[0]{-alias};
3671 }
3672 }
3673  
3674 352 my @prefetch = $source->_resolve_prefetch( $prefetch, $alias, $join_map );
3675  
3676 # save these for after distinct resolution
3677 352 @prefetch_select = map { $_->[0] } @prefetch;
3678 352 @prefetch_as = map { $_->[1] } @prefetch;
3679 }
3680  
3681 # run through the resulting joinstructure (starting from our current slot)
3682 # and unset collapse if proven unnecessary
3683 #
3684 # also while we are at it find out if the current root source has
3685 # been premultiplied by previous related_source chaining
3686 #
3687 # this allows to predict whether a root object with all other relation
3688 # data set to NULL is in fact unique
3689 8980 ▶ if ($attrs->{collapse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{"collapse"}) 388 8592 8980
3690  
3691 388 ▶ if (ref $attrs->{from} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs->{'from'} eq 'ARRAY') { } 388 0 388
3692  
3693 388 ▶ if (@{$attrs->{from}} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$attrs->{'from'};} == 1) { } 63 325 388
3694 # no joins - no collapse
3695 63 $attrs->{collapse} = 0;
3696 }
3697 else {
3698 # find where our table-spec starts
3699 325 my @fromlist = @{$attrs->{from}};
3700 325 ▶ while (@fromlist) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fromlist) 373 0 373
3701 373 my $t = shift @fromlist;
3702  
3703 373 my $is_multi;
3704 # me vs join from-spec distinction - a ref means non-root
3705 373 ▶ if (ref $t eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $t eq "ARRAY") 48 325 373
3706 48 $t = $t->[0];
3707 48 ▶ $is_multi ||= ! $t->{-is_single};
Condition66%2/3
$is_multi ||= !$t->{"-is_single"}
l!l&&r!l&&!r
03612
MC/DC50%1/2
$is_multi ||= !$t->{"-is_single"}
$is_multi !$t->{"-is_single"}
Truth table66%2/3
A ||= not(B)
A= $is_multiB= !$t->{"-is_single"}
ABresult
1X1
011
000
3708 }
3709 373 ▶ last if ($t->{-alias} && $t->{-alias} eq $alias);
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"-alias"} and $t->{"-alias"} eq $alias 325 48 373
Condition66%2/3
$t->{"-alias"} and $t->{"-alias"} eq $alias
!ll&&!rl&&r
048325
MC/DC50%1/2
$t->{"-alias"} and $t->{"-alias"} eq $alias
$t->{"-alias"} $t->{"-alias"} eq $alias
Truth table66%2/3
A and B
A= $t->{"-alias"}B= $t->{"-alias"} eq $alias
ABresult
0X0
100
111
3710 48 ▶ $attrs->{_main_source_premultiplied} ||= $is_multi;
Condition100%3/3
$attrs->{"_main_source_premultiplied"} ||= $is_multi
l!l&&r!l&&!r
3738
MC/DC100%2/2
$attrs->{"_main_source_premultiplied"} ||= $is_multi
$attrs->{"_main_source_premultiplied"} $is_multi
Truth table100%3/3
A ||= B
A= $attrs->{"_main_source_premultiplied"}B= $is_multi
ABresult
1X1
011
000
3711 }
3712  
3713 # no non-singles remaining, nor any premultiplication - nothing to collapse
3714 325 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless ($attrs->{"_main_source_premultiplied"} or grep {not $_->[0]{"-is_single"};} @fromlist) 95 230 325
Condition100%3/3
$attrs->{"_main_source_premultiplied"} or grep {not $_->[0]{"-is_single"};} @fromlist
l!l&&r!l&&!r
722395
MC/DC100%2/2
$attrs->{"_main_source_premultiplied"} or grep {not $_->[0]{"-is_single"};} @fromlist
$attrs->{"_main_source_premultiplied"} grep {not $_->[0]{"-is_single"};} @fromlist
Truth table100%3/3
A or B
A= $attrs->{"_main_source_premultiplied"}B= grep {not $_->[0]{"-is_single"};} @fromlist
ABresult
1X1
011
000
3715 ! $attrs->{_main_source_premultiplied}
3716 and
3717 568 ! grep { ! $_->[0]{-is_single} } @fromlist
3718 ) {
3719 95 $attrs->{collapse} = 0;
3720 }
3721 }
3722 }
3723  
3724 else {
3725 # if we can not analyze the from - err on the side of safety
3726 0 $attrs->{_main_source_premultiplied} = 1;
3727 }
3728 }
3729  
3730 # generate the distinct induced group_by before injecting the prefetched select/as parts
3731 8980 ▶ if (delete $attrs->{distinct}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $attrs->{"distinct"}) 70 8910 8980
3732 70 ▶ if ($attrs->{group_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs->{'group_by'}) { } 1 69 70
3733 1 carp_unique ("Useless use of distinct on a grouped resultset ('distinct' is ignored when a 'group_by' is present)");
3734 }
3735 else {
3736 69 $attrs->{_grouped_by_distinct} = 1;
3737 # distinct affects only the main selection part, not what prefetch may add below
3738 69 ($attrs->{group_by}, my $new_order) = $source->storage->_group_over_selection($attrs);
3739  
3740 # FIXME possibly ignore a rewritten order_by (may turn out to be an issue)
3741 # The thinking is: if we are collapsing the subquerying prefetch engine will
3742 # rip stuff apart for us anyway, and we do not want to have a potentially
3743 # function-converted external order_by
3744 # ( there is an explicit if ( collapse && _grouped_by_distinct ) check in DBIHacks )
3745 69 ▶ $attrs->{order_by} = $new_order unless $attrs->{collapse};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs->{"collapse"} 61 8 69
3746 }
3747 }
3748  
3749 # inject prefetch-bound selection (if any)
3750 8980 push @{$attrs->{select}}, @prefetch_select;
3751 8980 push @{$attrs->{as}}, @prefetch_as;
3752  
3753 $attrs->{_simple_passthrough_construction} = !(
3754 $attrs->{collapse}
3755 or
3756 8980 ▶ grep { $_ =~ /\./ } @{$attrs->{as}}
Condition100%3/3
$attrs->{'collapse'} || grep({$_ =~ /\./;} @{$$attrs{'as'};})
l!l&&r!l&&!r
2301468604
MC/DC100%2/2
$attrs->{'collapse'} || grep({$_ =~ /\./;} @{$$attrs{'as'};})
$attrs->{'collapse'} grep({$_ =~ /\./;} @{$$attrs{'as'};})
Truth table100%3/3
A || B
A= $attrs->{'collapse'}B= grep({$_ =~ /\./;} @{$$attrs{'as'};})
ABresult
1X1
011
000
3757 );
3758  
3759 # if both page and offset are specified, produce a combined offset
3760 # even though it doesn't make much sense, this is what pre 081xx has
3761 # been doing
3762 8980 ▶ if (my $page = delete $attrs->{page}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $page = delete $attrs->{"page"}) 15 8965 8980
3763 $attrs->{offset} =
3764 ($attrs->{rows} * ($page - 1))
3765 +
3766 15 ▶ ($attrs->{offset} || 0)
Condition100%2/2
$attrs->{'offset'} || 0
l!l
114
MC/DC100%1/1
$attrs->{'offset'} || 0
$attrs->{'offset'}
Truth table100%2/2
A
A= $attrs->{'offset'}
Aresult
11
00
3767 ;
3768 }
3769  
3770 8980 return $self->{_attrs} = $attrs;
3771 }
3772  
3773 2038 ▶sub _rollout_attr {
Subroutine _rollout_attr: called CC=3 SCAR=11.0
3774 2038 my ($self, $attr) = @_;
3775  
3776 2038 ▶ if (ref $attr eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $attr eq 'HASH') { } 734 1304 2038
elsif (ref $attr eq 'ARRAY') { } 940 364 1304
3777 734 return $self->_rollout_hash($attr);
3778 } elsif (ref $attr eq 'ARRAY') {
3779 940 return $self->_rollout_array($attr);
3780 } else {
3781 364 return [$attr];
3782 }
3783 }
3784  
3785 1280 ▶sub _rollout_array {
Subroutine _rollout_array: called CC=4 SCAR=13.9
3786 1280 my ($self, $attr) = @_;
3787  
3788 1280 my @rolled_array;
3789 1280 foreach my $element (@{$attr}) {
3790 1362 ▶ if (ref $element eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $element eq 'HASH') { } 474 888 1362
elsif (ref $element eq 'ARRAY') { } 340 548 888
3791 474 push( @rolled_array, @{ $self->_rollout_hash( $element ) } );
3792 } elsif (ref $element eq 'ARRAY') {
3793 # XXX - should probably recurse here
3794 340 push( @rolled_array, @{$self->_rollout_array($element)} );
3795 } else {
3796 548 push( @rolled_array, $element );
3797 }
3798 }
3799 1280 return \@rolled_array;
3800 }
3801  
3802 1208 ▶sub _rollout_hash {
Subroutine _rollout_hash: called CC=2 SCAR=6.9
3803 1208 my ($self, $attr) = @_;
3804  
3805 1208 my @rolled_array;
3806 1208 foreach my $key (keys %{$attr}) {
3807 242 push( @rolled_array, { $key => $attr->{$key} } );
3808 }
3809 1208 return \@rolled_array;
3810 }
3811  
3812 324 ▶sub _calculate_score {
Subroutine _calculate_score: called CC=13 SCAR=25.6
3813 324 my ($self, $a, $b) = @_;
3814  
3815 324 ▶ if (defined $a xor defined $b) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $a xor defined $b) { } 44 280 324
elsif (not defined $a) { } 10 270 280
Condition100%4/4
defined $a xor defined $b
l&&rl&&!r!l&&r!l&&!r
270172710
MC/DC100%2/2
defined $a xor defined $b
defined $a defined $b
Truth table100%4/4
A xor B
A= defined $aB= defined $b
ABresult
110
101
011
000
3816 44 return 0;
3817 }
3818 elsif (not defined $a) {
3819 10 return 1;
3820 }
3821  
3822 270 ▶ if (ref $b eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $b eq 'HASH') { } 92 178 270
3823 92 my ($b_key) = keys %{$b};
3824 92 ▶ $b_key = '' if ! defined $b_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b_key 1 91 92
3825 92 ▶ if (ref $a eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'HASH') { } 35 57 92
3826 35 my ($a_key) = keys %{$a};
3827 35 ▶ $a_key = '' if ! defined $a_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a_key 1 34 35
3828 35 ▶ if ($a_key eq $b_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_key eq $b_key) { } 25 10 35
3829 25 return (1 + $self->_calculate_score( $a->{$a_key}, $b->{$b_key} ));
3830 } else {
3831 10 return 0;
3832 }
3833 } else {
3834 57 ▶ return ($a eq $b_key) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a eq $b_key ? : 20 37 57
3835 }
3836 } else {
3837 178 ▶ if (ref $a eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'HASH') { } 30 148 178
3838 30 my ($a_key) = keys %{$a};
3839 30 ▶ return ($b eq $a_key) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$b eq $a_key ? : 12 18 30
3840 } else {
3841 148 ▶ return ($b eq $a) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$b eq $a ? : 48 100 148
3842 }
3843 }
3844 }
3845  
3846 2831 ▶sub _merge_joinpref_attr {
Subroutine _merge_joinpref_attr: called CC=13 SCAR=25.6
3847 2831 my ($self, $orig, $import) = @_;
3848  
3849 2831 ▶ return $import unless defined($orig);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $orig 1789 1042 2831
3850 1042 ▶ return $orig unless defined($import);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $import 23 1019 1042
3851  
3852 1019 $orig = $self->_rollout_attr($orig);
3853 1019 $import = $self->_rollout_attr($import);
3854  
3855 1019 my $seen_keys;
3856 1019 foreach my $import_element ( @{$import} ) {
3857 # find best candidate from $orig to merge $b_element into
3858 865 my $best_candidate = { position => undef, score => 0 }; my $position = 0;
3859 865 foreach my $orig_element ( @{$orig} ) {
3860 299 my $score = $self->_calculate_score( $orig_element, $import_element );
3861 299 ▶ if ($score > $best_candidate->{score}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score > $best_candidate->{"score"}) 90 209 299
3862 90 $best_candidate->{position} = $position;
3863 90 $best_candidate->{score} = $score;
3864 }
3865 299 $position++;
3866 }
3867 865 ▶ my ($import_key) = ( ref $import_element eq 'HASH' ) ? keys %{$import_element} : ($import_element);
Branch: 1 branch
BranchTrueFalseTotal
ref $import_element eq 'HASH' ? : 176 689 865
3868 865 ▶ $import_key = '' if not defined $import_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $import_key 96 769 865
3869  
3870 865 ▶ if ($best_candidate->{score} == 0 || exists $seen_keys->{$import_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_candidate->{'score'} == 0 or exists $seen_keys->{$import_key}) { } 794 71 865
Condition100%3/3
$best_candidate->{"score"} == 0 or exists $seen_keys->{$import_key}
l!l&&r!l&&!r
7781671
MC/DC100%2/2
$best_candidate->{"score"} == 0 or exists $seen_keys->{$import_key}
$best_candidate->{"score"} == 0 exists $seen_keys->{$import_key}
Truth table100%3/3
A or B
A= $best_candidate->{"score"} == 0B= exists $seen_keys->{$import_key}
ABresult
1X1
011
000
3871 794 push( @{$orig}, $import_element );
3872 } else {
3873 71 my $orig_best = $orig->[$best_candidate->{position}];
3874 # merge orig_best and b_element together and replace original with merged
3875 71 ▶ if (ref $orig_best ne 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $orig_best ne 'HASH') { } 46 25 71
elsif (ref $import_element eq 'HASH') { } 15 10 25
3876 46 $orig->[$best_candidate->{position}] = $import_element;
3877 } elsif (ref $import_element eq 'HASH') {
3878 15 my ($key) = keys %{$orig_best};
3879 15 $orig->[$best_candidate->{position}] = { $key => $self->_merge_joinpref_attr($orig_best->{$key}, $import_element->{$key}) };
3880 }
3881 }
3882 865 $seen_keys->{$import_key} = 1; # don't merge the same key twice
3883 }
3884  
3885 1019 ▶ return @$orig ? $orig : ();
Branch: 1 branch
BranchTrueFalseTotal
@$orig ? : 841 178 1019
3886 }
3887  
3888 {
3889 379 my $hm;
3890  
3891 17222 ▶ sub _merge_attr {
Subroutine _merge_attr: called CC=2 SCAR=12.5
3892 17222 ▶ $hm ||= do {
Condition66%2/3
$hm ||= do { my $hm = "Hash::Merge"->new; $hm->specify_behavior({"SCALAR", {"SCALAR", sub { my($defl, $defr) = map({defined $_;} @_[0, 1]); if ($defl xor $defr) { return [$defl ? $_[0] : $_[1]]; } elsif (not $defl) { return []; } else { return [$_[0], $_[1]]; } ; } , "ARRAY", sub { return $_[1] unless defined $_[0]; '???'; return [$_[0], @{$_[1];}]; } , "HASH", sub { return [] unless defined $_[0] or keys %{$_[1];}; return [$_[1]] unless defined $_[0]; return [$_[0]] unless keys %{$_[1];}; return [$_[0], $_[1]]; } }, "ARRAY", {"SCALAR", sub { return $_[0] unless defined $_[1]; '???'; return [@{$_[0];}, $_[1]]; } , "ARRAY", sub { my(@ret) = @{$_[0];} or return $_[1]; return [@ret, @{$_[1];}]; my(%idx) = map({$_, 1;} @ret); push @ret, grep({not defined $idx{$_};} @{$_[1];}); \@ret; } , "HASH", sub { return [$_[1]] unless @{$_[0];}; return $_[0] unless keys %{$_[1];}; '???'; return [@{$_[0];}, $_[1]]; } }, "HASH", {"SCALAR", sub { return [] unless keys %{$_[0];} or defined $_[1]; return [$_[0]] unless defined $_[1]; return [$_[1]] unless keys %{$_[0];}; return [$_[0], $_[1]]; } , "ARRAY", sub { return [] unless keys %{$_[0];} or @{$_[1];}; return [$_[0]] unless @{$_[1];}; return $_[1] unless keys %{$_[0];}; '???'; return [$_[0], @{$_[1];}]; } , "HASH", sub { return [] unless keys %{$_[0];} or keys %{$_[1];}; return [$_[0]] unless keys %{$_[1];}; return [$_[1]] unless keys %{$_[0];}; return [$_[0]] if $_[0] eq $_[1]; return [$_[0], $_[1]]; } }}, "DBIC_RS_ATTR_MERGER"); $hm }
l!l&&r!l&&!r
170371850
MC/DC0%0/2
$hm ||= do { my $hm = "Hash::Merge"->new; $hm->specify_behavior({"SCALAR", {"SCALAR", sub { my($defl, $defr) = map({defined $_;} @_[0, 1]); if ($defl xor $defr) { return [$defl ? $_[0] : $_[1]]; } elsif (not $defl) { return []; } else { return [$_[0], $_[1]]; } ; } , "ARRAY", sub { return $_[1] unless defined $_[0]; '???'; return [$_[0], @{$_[1];}]; } , "HASH", sub { return [] unless defined $_[0] or keys %{$_[1];}; return [$_[1]] unless defined $_[0]; return [$_[0]] unless keys %{$_[1];}; return [$_[0], $_[1]]; } }, "ARRAY", {"SCALAR", sub { return $_[0] unless defined $_[1]; '???'; return [@{$_[0];}, $_[1]]; } , "ARRAY", sub { my(@ret) = @{$_[0];} or return $_[1]; return [@ret, @{$_[1];}]; my(%idx) = map({$_, 1;} @ret); push @ret, grep({not defined $idx{$_};} @{$_[1];}); \@ret; } , "HASH", sub { return [$_[1]] unless @{$_[0];}; return $_[0] unless keys %{$_[1];}; '???'; return [@{$_[0];}, $_[1]]; } }, "HASH", {"SCALAR", sub { return [] unless keys %{$_[0];} or defined $_[1]; return [$_[0]] unless defined $_[1]; return [$_[1]] unless keys %{$_[0];}; return [$_[0], $_[1]]; } , "ARRAY", sub { return [] unless keys %{$_[0];} or @{$_[1];}; return [$_[0]] unless @{$_[1];}; return $_[1] unless keys %{$_[0];}; '???'; return [$_[0], @{$_[1];}]; } , "HASH", sub { return [] unless keys %{$_[0];} or keys %{$_[1];}; return [$_[0]] unless keys %{$_[1];}; return [$_[1]] unless keys %{$_[0];}; return [$_[0]] if $_[0] eq $_[1]; return [$_[0], $_[1]]; } }}, "DBIC_RS_ATTR_MERGER"); $hm }
$hm do { my $hm = "Hash::Merge"->new; $hm->specify_behavior({"SCALAR", {"SCALAR", sub { my($defl, $defr) = map({defined $_;} @_[0, 1]); if ($defl xor $defr) { return [$defl ? $_[0] : $_[1]]; } elsif (not $defl) { return []; } else { return [$_[0], $_[1]]; } ; } , "ARRAY", sub { return $_[1] unless defined $_[0]; '???'; return [$_[0], @{$_[1];}]; } , "HASH", sub { return [] unless defined $_[0] or keys %{$_[1];}; return [$_[1]] unless defined $_[0]; return [$_[0]] unless keys %{$_[1];}; return [$_[0], $_[1]]; } }, "ARRAY", {"SCALAR", sub { return $_[0] unless defined $_[1]; '???'; return [@{$_[0];}, $_[1]]; } , "ARRAY", sub { my(@ret) = @{$_[0];} or return $_[1]; return [@ret, @{$_[1];}]; my(%idx) = map({$_, 1;} @ret); push @ret, grep({not defined $idx{$_};} @{$_[1];}); \@ret; } , "HASH", sub { return [$_[1]] unless @{$_[0];}; return $_[0] unless keys %{$_[1];}; '???'; return [@{$_[0];}, $_[1]]; } }, "HASH", {"SCALAR", sub { return [] unless keys %{$_[0];} or defined $_[1]; return [$_[0]] unless defined $_[1]; return [$_[1]] unless keys %{$_[0];}; return [$_[0], $_[1]]; } , "ARRAY", sub { return [] unless keys %{$_[0];} or @{$_[1];}; return [$_[0]] unless @{$_[1];}; return $_[1] unless keys %{$_[0];}; '???'; return [$_[0], @{$_[1];}]; } , "HASH", sub { return [] unless keys %{$_[0];} or keys %{$_[1];}; return [$_[0]] unless keys %{$_[1];}; return [$_[1]] unless keys %{$_[0];}; return [$_[0]] if $_[0] eq $_[1]; return [$_[0], $_[1]]; } }}, "DBIC_RS_ATTR_MERGER"); $hm }
Truth table66%2/3
A ||= B
A= $hmB= do { my $hm = "Hash::Merge"->new; $hm->specify_behavior({"SCALAR", {"SCALAR", sub { my($defl, $defr) = map({defined $_;} @_[0, 1]); if ($defl xor $defr) { return [$defl ? $_[0] : $_[1]]; } elsif (not $defl) { return []; } else { return [$_[0], $_[1]]; } ; } , "ARRAY", sub { return $_[1] unless defined $_[0]; '???'; return [$_[0], @{$_[1];}]; } , "HASH", sub { return [] unless defined $_[0] or keys %{$_[1];}; return [$_[1]] unless defined $_[0]; return [$_[0]] unless keys %{$_[1];}; return [$_[0], $_[1]]; } }, "ARRAY", {"SCALAR", sub { return $_[0] unless defined $_[1]; '???'; return [@{$_[0];}, $_[1]]; } , "ARRAY", sub { my(@ret) = @{$_[0];} or return $_[1]; return [@ret, @{$_[1];}]; my(%idx) = map({$_, 1;} @ret); push @ret, grep({not defined $idx{$_};} @{$_[1];}); \@ret; } , "HASH", sub { return [$_[1]] unless @{$_[0];}; return $_[0] unless keys %{$_[1];}; '???'; return [@{$_[0];}, $_[1]]; } }, "HASH", {"SCALAR", sub { return [] unless keys %{$_[0];} or defined $_[1]; return [$_[0]] unless defined $_[1]; return [$_[1]] unless keys %{$_[0];}; return [$_[0], $_[1]]; } , "ARRAY", sub { return [] unless keys %{$_[0];} or @{$_[1];}; return [$_[0]] unless @{$_[1];}; return $_[1] unless keys %{$_[0];}; '???'; return [$_[0], @{$_[1];}]; } , "HASH", sub { return [] unless keys %{$_[0];} or keys %{$_[1];}; return [$_[0]] unless keys %{$_[1];}; return [$_[1]] unless keys %{$_[0];}; return [$_[0]] if $_[0] eq $_[1]; return [$_[0], $_[1]]; } }}, "DBIC_RS_ATTR_MERGER"); $hm }
ABresult
1X1
011
000
3893 185 my $hm = Hash::Merge->new;
3894  
3895 $hm->specify_behavior({
3896 SCALAR => {
3897 72 ▶ SCALAR => sub {
Subroutine __ANON__: called CC=5 SCAR=22.5
3898 72 my ($defl, $defr) = map { defined $_ } (@_[0,1]);
3899  
3900 72 ▶ if ($defl xor $defr) {
Branch: 2 branches
BranchTrueFalseTotal
if ($defl xor $defr) { } 72 0 72
elsif (not $defl) { } 0 0 0
Condition25%1/4
$defl xor $defr
l&&rl&&!r!l&&r!l&&!r
00720
MC/DC0%0/2
$defl xor $defr
$defl $defr
Truth table25%1/4
A xor B
A= $deflB= $defr
ABresult
110
101
011
000
3901 72 ▶ return [ $defl ? $_[0] : $_[1] ];
Branch: 1 branch
BranchTrueFalseTotal
$defl ? : 0 72 72
3902 }
3903 elsif (! $defl) {
3904 0 return [];
3905 }
3906 elsif (__HM_DEDUP and $_[0] eq $_[1]) {
3907 return [ $_[0] ];
3908 }
3909 else {
3910 0 return [$_[0], $_[1]];
3911 }
3912 },
3913 5911 ▶ ARRAY => sub {
Subroutine __ANON__: called CC=2 SCAR=11.6
3914 5911 ▶ return $_[1] if !defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 5911 0 5911
3915 0 return $_[1] if __HM_DEDUP and grep { $_ eq $_[0] } @{$_[1]};
3916 0 return [$_[0], @{$_[1]}]
3917 },
3918 869 ▶ HASH => sub {
Subroutine __ANON__: called CC=5 SCAR=20.2
3919 869 ▶ return [] if !defined $_[0] and !keys %{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] or keys %{$_[1];} 1 868 869
Condition66%2/3
defined $_[0] or keys %{$_[1];}
l!l&&r!l&&!r
08681
MC/DC50%1/2
defined $_[0] or keys %{$_[1];}
defined $_[0] keys %{$_[1];}
Truth table66%2/3
A or B
A= defined $_[0]B= keys %{$_[1];}
ABresult
1X1
011
000
3920 868 ▶ return [ $_[1] ] if !defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 868 0 868
3921 0 ▶ return [ $_[0] ] if !keys %{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[1];} 0 0 0
3922 0 return [$_[0], $_[1]]
3923 },
3924 },
3925 ARRAY => {
3926 10245 ▶ SCALAR => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
3927 10245 ▶ return $_[0] if !defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 10244 1 10245
3928 1 return $_[0] if __HM_DEDUP and grep { $_ eq $_[1] } @{$_[0]};
3929 1 return [@{$_[0]}, $_[1]]
3930 },
3931 125 ▶ ARRAY => sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
3932 125 ▶ my @ret = @{$_[0]} or return $_[1];
Branch: 1 branch
BranchTrueFalseTotal
my(@ret) = @{$_[0];} or return $_[1] 120 5 125
3933 120 return [ @ret, @{$_[1]} ] unless __HM_DEDUP;
3934 0 my %idx = map { $_ => 1 } @ret;
3935 0 push @ret, grep { ! defined $idx{$_} } (@{$_[1]});
3936 0 \@ret;
3937 },
3938 0 ▶ HASH => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
3939 0 ▶ return [ $_[1] ] if ! @{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$_[0];} 0 0 0
3940 0 ▶ return $_[0] if !keys %{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[1];} 0 0 0
3941 0 return $_[0] if __HM_DEDUP and grep { $_ eq $_[1] } @{$_[0]};
3942 0 return [ @{$_[0]}, $_[1] ];
3943 },
3944 },
3945 HASH => {
3946 0 ▶ SCALAR => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
3947 0 ▶ return [] if !keys %{$_[0]} and !defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[0];} or defined $_[1] 0 0 0
Condition0%0/3
keys %{$_[0];} or defined $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
keys %{$_[0];} or defined $_[1]
keys %{$_[0];} defined $_[1]
Truth table0%0/3
A or B
A= keys %{$_[0];}B= defined $_[1]
ABresult
1X1
011
000
3948 0 ▶ return [ $_[0] ] if !defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 0 0
3949 0 ▶ return [ $_[1] ] if !keys %{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[0];} 0 0 0
3950 0 return [$_[0], $_[1]]
3951 },
3952 0 ▶ ARRAY => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
3953 0 ▶ return [] if !keys %{$_[0]} and !@{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[0];} or @{$_[1];} 0 0 0
Condition0%0/3
keys %{$_[0];} or @{$_[1];}
l!l&&r!l&&!r
000
MC/DC0%0/2
keys %{$_[0];} or @{$_[1];}
keys %{$_[0];} @{$_[1];}
Truth table0%0/3
A or B
A= keys %{$_[0];}B= @{$_[1];}
ABresult
1X1
011
000
3954 0 ▶ return [ $_[0] ] if !@{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$_[1];} 0 0 0
3955 0 ▶ return $_[1] if !keys %{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[0];} 0 0 0
3956 0 return $_[1] if __HM_DEDUP and grep { $_ eq $_[0] } @{$_[1]};
3957 0 return [ $_[0], @{$_[1]} ];
3958 },
3959 0 ▶ HASH => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
3960 0 ▶ return [] if !keys %{$_[0]} and !keys %{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[0];} or keys %{$_[1];} 0 0 0
Condition0%0/3
keys %{$_[0];} or keys %{$_[1];}
l!l&&r!l&&!r
000
MC/DC0%0/2
keys %{$_[0];} or keys %{$_[1];}
keys %{$_[0];} keys %{$_[1];}
Truth table0%0/3
A or B
A= keys %{$_[0];}B= keys %{$_[1];}
ABresult
1X1
011
000
3961 0 ▶ return [ $_[0] ] if !keys %{$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[1];} 0 0 0
3962 0 ▶ return [ $_[1] ] if !keys %{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[0];} 0 0 0
3963 0 ▶ return [ $_[0] ] if $_[0] eq $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq $_[1] 0 0 0
3964 0 return [ $_[0], $_[1] ];
3965 },
3966 }
3967 185 } => 'DBIC_RS_ATTR_MERGER');
3968 185 $hm;
3969 };
3970  
3971 17222 return $hm->merge ($_[1], $_[2]);
3972 }
3973 }
3974  
3975 379 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=3 SCAR=11.0
Pod undocumented
3976 146 my ($self, $cloning) = @_;
3977 146 my $to_serialize = { %$self };
3978  
3979 # A cursor in progress can't be serialized (and would make little sense anyway)
3980 # the parser can be regenerated (and can't be serialized)
3981 146 delete @{$to_serialize}{qw/cursor _row_parser _result_inflator/};
3982  
3983 # nor is it sensical to store a not-yet-fired-count pager
3984 146 ▶ if ($to_serialize->{pager} and ref $to_serialize->{pager}{total_entries} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_serialize->{"pager"} and ref $to_serialize->{"pager"}{"total_entries"} eq "CODE") 1 145 146
Condition100%3/3
$to_serialize->{"pager"} and ref $to_serialize->{"pager"}{"total_entries"} eq "CODE"
!ll&&!rl&&r
14411
MC/DC100%2/2
$to_serialize->{"pager"} and ref $to_serialize->{"pager"}{"total_entries"} eq "CODE"
$to_serialize->{"pager"} ref $to_serialize->{"pager"}{"total_entries"} eq "CODE"
Truth table100%3/3
A and B
A= $to_serialize->{"pager"}B= ref $to_serialize->{"pager"}{"total_entries"} eq "CODE"
ABresult
0X0
100
111
3985 1 delete $to_serialize->{pager};
3986 }
3987  
3988 146 Storable::nfreeze($to_serialize);
3989 }
3990  
3991 # need this hook for symmetry
3992 146 ▶sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
3993 146 my ($self, $cloning, $serialized) = @_;
3994  
3995 146 %$self = %{ Storable::thaw($serialized) };
3996  
3997 146 $self;
3998 }
3999  
4000  
4001 =head2 throw_exception
4002  
4003 See L<DBIx::Class::Schema/throw_exception> for details.
4004  
4005 =cut
4006  
4007 57 ▶sub throw_exception {
Subroutine throw_exception: called CC=3 SCAR=11.0
Pod documented
4008 57 my $self=shift;
4009  
4010 57 ▶ if (ref $self and my $rsrc = $self->result_source) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and my $rsrc = $self->result_source) { } 56 1 57
Condition66%2/3
ref $self and my $rsrc = $self->result_source
!ll&&!rl&&r
1056
MC/DC50%1/2
ref $self and my $rsrc = $self->result_source
ref $self my $rsrc = $self->result_source
Truth table66%2/3
A and B
A= ref $selfB= my $rsrc = $self->result_source
ABresult
0X0
100
111
4011 56 $rsrc->throw_exception(@_)
4012 }
4013 else {
4014 1 DBIx::Class::Exception->throw(@_);
4015 }
4016 }
4017  
4018 379 1;
4019  
4020 __END__
« DBIx/Class/Storage/DBIHacks.pm ↑ Summary DBIx/Class/Schema/Versioned.pm »