DBIx/Class/Row.pm

filter: Statementstmt 95.0% 361 / 380 Branchbran 84.5% 181 / 214 Conditioncond 78.0% 114 / 146 MC/DCmcdc 62.6% 52 / 83 Subroutinesub 95.3% 41 / 43 Podpod 100% 25 / 25 totaltotal 86.8% 774 / 891 CC 178 SCAR 26.8
CC
178
Cov
89%
insert
34.2
new
28.6
delete
18.4
CRAP 14.5
« DBIx/Class/Relationship/BelongsTo.pm ↑ Summary DBIx/Class/ResultSource/RowParser/Util.pm »
1 package DBIx::Class::Row;
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  
6 379 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 379 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 379 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 379 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 379 ▶use SQL::Abstract::Util 'is_literal_value';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ###
14 ### Internal method
15 ### Do not use
16 ###
17 379 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
18 *MULTICREATE_DEBUG =
19 $ENV{DBIC_MULTICREATE_DEBUG}
20 ? sub () { 1 }
21 379 ▶ : sub () { 0 };
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'DBIC_MULTICREATE_DEBUG'} ? : 0 379 379
22 }
23  
24 379 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 379 __PACKAGE__->mk_group_accessors ( simple => [ in_storage => '_in_storage' ] );
27  
28 =head1 NAME
29  
30 DBIx::Class::Row - Basic row methods
31  
32 =head1 SYNOPSIS
33  
34 =head1 DESCRIPTION
35  
36 This class is responsible for defining and doing basic operations on rows
37 derived from L<DBIx::Class::ResultSource> objects.
38  
39 Result objects are returned from L<DBIx::Class::ResultSet>s using the
40 L<create|DBIx::Class::ResultSet/create>, L<find|DBIx::Class::ResultSet/find>,
41 L<next|DBIx::Class::ResultSet/next> and L<all|DBIx::Class::ResultSet/all> methods,
42 as well as invocations of 'single' (
43 L<belongs_to|DBIx::Class::Relationship/belongs_to>,
44 L<has_one|DBIx::Class::Relationship/has_one> or
45 L<might_have|DBIx::Class::Relationship/might_have>)
46 relationship accessors of L<Result|DBIx::Class::Manual::ResultClass> objects.
47  
48 =head1 NOTE
49  
50 All "Row objects" derived from a Schema-attached L<DBIx::Class::ResultSet>
51 object (such as a typical C<< L<search|DBIx::Class::ResultSet/search>->
52 L<next|DBIx::Class::ResultSet/next> >> call) are actually Result
53 instances, based on your application's
54 L<Result Class|DBIx::Class::Manual::Glossary/Result Class>.
55  
56 L<DBIx::Class::Row> implements most of the row-based communication with the
57 underlying storage, but a Result class B<should not inherit from it directly>.
58 Usually, Result classes inherit from L<DBIx::Class::Core>, which in turn
59 combines the methods from several classes, one of them being
60 L<DBIx::Class::Row>. Therefore, while many of the methods available to a
61 L<DBIx::Class::Core>-derived Result class are described in the following
62 documentation, it does not detail all of the methods available to Result
63 objects. Refer to L<DBIx::Class::Manual::ResultClass> for more info.
64  
65 =head1 METHODS
66  
67 =head2 new
68  
69 my $result = My::Class->new(\%attrs);
70  
71 my $result = $schema->resultset('MySource')->new(\%colsandvalues);
72  
73 =over
74  
75 =item Arguments: \%attrs or \%colsandvalues
76  
77 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
78  
79 =back
80  
81 While you can create a new result object by calling C<new> directly on
82 this class, you are better off calling it on a
83 L<DBIx::Class::ResultSet> object.
84  
85 When calling it directly, you will not get a complete, usable row
86 object until you pass or set the C<result_source> attribute, to a
87 L<DBIx::Class::ResultSource> instance that is attached to a
88 L<DBIx::Class::Schema> with a valid connection.
89  
90 C<$attrs> is a hashref of column name, value data. It can also contain
91 some other attributes such as the C<result_source>.
92  
93 Passing an object, or an arrayref of objects as a value will call
94 L<DBIx::Class::Relationship::Base/set_from_related> for you. When
95 passed a hashref or an arrayref of hashrefs as the value, these will
96 be turned into objects via new_related, and treated as if you had
97 passed objects.
98  
99 For a more involved explanation, see L<DBIx::Class::ResultSet/create>.
100  
101 Please note that if a value is not passed to new, no value will be sent
102 in the SQL INSERT call, and the column will therefore assume whatever
103 default value was specified in your database. While DBIC will retrieve the
104 value of autoincrement columns, it will never make an explicit database
105 trip to retrieve default values assigned by the RDBMS. You can explicitly
106 request that all values be fetched back from the database by calling
107 L</discard_changes>, or you can supply an explicit C<undef> to columns
108 with NULL as the default, and save yourself a SELECT.
109  
110 CAVEAT:
111  
112 The behavior described above will backfire if you use a foreign key column
113 with a database-defined default. If you call the relationship accessor on
114 an object that doesn't have a set value for the FK column, DBIC will throw
115 an exception, as it has no way of knowing the PK of the related object (if
116 there is one).
117  
118 =cut
119  
120 ## It needs to store the new objects somewhere, and call insert on that list later when insert is called on this object. We may need an accessor for these so the user can retrieve them, if just doing ->new().
121 ## This only works because DBIC doesn't yet care to check whether the new_related objects have been passed all their mandatory columns
122 ## When doing the later insert, we need to make sure the PKs are set.
123 ## using _relationship_data in new and funky ways..
124 ## check Relationship::CascadeActions and Relationship::Accessor for compat
125 ## tests!
126  
127 249 ▶sub __new_related_find_or_new_helper {
Subroutine __new_related_find_or_new_helper: called CC=5 SCAR=16.2
128 249 my ($self, $rel_name, $values) = @_;
129  
130 249 my $rsrc = $self->result_source;
131  
132 # create a mock-object so all new/set_column component overrides will run:
133 249 my $rel_rs = $rsrc->related_source($rel_name)->resultset;
134 249 my $new_rel_obj = $rel_rs->new_result($values);
135 249 my $proc_data = { $new_rel_obj->get_columns };
136  
137 249 ▶ if ($self->__their_pk_needs_us($rel_name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->__their_pk_needs_us($rel_name)) { } 148 101 249
elsif ($rsrc->_pk_depends_on($rel_name, $proc_data)) { } 101 0 101
138 148 MULTICREATE_DEBUG and print STDERR "MC $self constructing $rel_name via new_result\n";
139 148 return $new_rel_obj;
140 }
141 elsif ($rsrc->_pk_depends_on($rel_name, $proc_data )) {
142 101 ▶ if (! keys %$proc_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %$proc_data) { } 2 99 101
143 # there is nothing to search for - blind create
144 2 MULTICREATE_DEBUG and print STDERR "MC $self constructing default-insert $rel_name\n";
145 }
146 else {
147 99 MULTICREATE_DEBUG and print STDERR "MC $self constructing $rel_name via find_or_new\n";
148 # this is not *really* find or new, as we don't want to double-new the
149 # data (thus potentially double encoding or whatever)
150 99 my $exists = $rel_rs->find ($proc_data);
151 99 ▶ return $exists if $exists;
Branch: 1 branch
BranchTrueFalseTotal
if $exists 18 81 99
152 }
153 83 return $new_rel_obj;
154 }
155 else {
156 0 my $us = $rsrc->source_name;
157 0 $self->throw_exception (
158 "Unable to determine relationship '$rel_name' direction from '$us', "
159 . "possibly due to a missing reverse-relationship on '$rel_name' to '$us'."
160 );
161 }
162 }
163  
164 404 ▶sub __their_pk_needs_us { # this should maybe be in resultsource.
Subroutine __their_pk_needs_us: called CC=3 SCAR=11.0
165 404 my ($self, $rel_name) = @_;
166 404 my $rsrc = $self->result_source;
167 404 my $reverse = $rsrc->reverse_relationship_info($rel_name);
168 404 my $rel_source = $rsrc->related_source($rel_name);
169 404 my $us = { $self->get_columns };
170 404 foreach my $key (keys %$reverse) {
171 # if their primary key depends on us, then we have to
172 # just create a result and we'll fill it out afterwards
173 484 ▶ return 1 if $rel_source->_pk_depends_on($key, $us);
Branch: 1 branch
BranchTrueFalseTotal
if $rel_source->_pk_depends_on($key, $us) 303 181 484
174 }
175 101 return 0;
176 }
177  
178 1494 ▶sub new {
Subroutine new: called CC=28 SCAR=33.5
Pod documented
179 1494 my ($class, $attrs) = @_;
180 1494 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 1494 1494
181  
182 1494 my $new = bless { _column_data => {}, _in_storage => 0 }, $class;
183  
184 1494 ▶ if ($attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs) 1494 0 1494
185 1494 ▶ $new->throw_exception("attrs must be a hashref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs eq "HASH" 2 1492 1494
186 unless ref($attrs) eq 'HASH';
187  
188 1492 my $rsrc = delete $attrs->{-result_source};
189 1492 ▶ if ( my $h = delete $attrs->{-source_handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = delete $attrs->{"-source_handle"}) 1 1491 1492
190 1 ▶ $rsrc ||= $h->resolve;
Condition33%1/3
$rsrc ||= $h->resolve
l!l&&r!l&&!r
010
MC/DC0%0/2
$rsrc ||= $h->resolve
$rsrc $h->resolve
Truth table33%1/3
A ||= B
A= $rsrcB= $h->resolve
ABresult
1X1
011
000
191 }
192  
193 1492 ▶ $new->result_source($rsrc) if $rsrc;
Branch: 1 branch
BranchTrueFalseTotal
if $rsrc 1492 0 1492
194  
195 1492 ▶ if (my $col_from_rel = delete $attrs->{-cols_from_relations}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $col_from_rel = delete $attrs->{"-cols_from_relations"}) 6 1486 1492
196 6 @{$new->{_ignore_at_insert}={}}{@$col_from_rel} = ();
197 }
198  
199 1492 my ($related,$inflated);
200  
201 1492 foreach my $key (keys %$attrs) {
202 3531 ▶ if (ref $attrs->{$key} and ! is_literal_value($attrs->{$key}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs->{$key} and not &is_literal_value($attrs->{$key})) 428 3103 3531
Condition100%3/3
ref $attrs->{$key} and not &is_literal_value($attrs->{$key})
!ll&&!rl&&r
30949428
MC/DC100%2/2
ref $attrs->{$key} and not &is_literal_value($attrs->{$key})
ref $attrs->{$key} not &is_literal_value($attrs->{$key})
Truth table100%3/3
A and not(B)
A= ref $attrs->{$key}B= not &is_literal_value($attrs->{$key})
ABresult
0X0
100
111
203 ## Can we extract this lot to use with update(_or .. ) ?
204 428 ▶ $new->throw_exception("Can't do multi-create without result source")
Branch: 1 branch
BranchTrueFalseTotal
unless $rsrc 0 428 428
205 unless $rsrc;
206 428 my $info = $rsrc->relationship_info($key);
207 428 ▶ my $acc_type = $info->{attrs}{accessor} || '';
Condition100%2/2
$info->{'attrs'}{'accessor'} || ''
l!l
301127
MC/DC100%1/1
$info->{'attrs'}{'accessor'} || ''
$info->{'attrs'}{'accessor'}
Truth table100%2/2
A
A= $info->{'attrs'}{'accessor'}
Aresult
11
00
208 428 ▶ if ($acc_type eq 'single') {
Branch: 4 branches
BranchTrueFalseTotal
if ($acc_type eq 'single') { } 118 310 428
elsif ($acc_type eq 'multi' and ref $attrs->{$key} eq 'ARRAY') { } 81 229 310
elsif ($acc_type eq 'filter') { } 102 127 229
elsif ($rsrc->has_column($key) and $rsrc->column_info($key)->{'_inflate_info'}) { } 2 125 127
209 118 my $rel_obj = delete $attrs->{$key};
210 118 ▶ if(!blessed $rel_obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($rel_obj)) 43 75 118
211 43 $rel_obj = $new->__new_related_find_or_new_helper($key, $rel_obj);
212 }
213  
214 118 ▶ if ($rel_obj->in_storage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_obj->in_storage) { } 80 38 118
215 80 $new->{_rel_in_storage}{$key} = 1;
216 80 $new->set_from_related($key, $rel_obj);
217 } else {
218 38 MULTICREATE_DEBUG and print STDERR "MC $new uninserted $key $rel_obj\n";
219 }
220  
221 118 $related->{$key} = $rel_obj;
222 118 next;
223 }
224 ▶ elsif ($acc_type eq 'multi' && ref $attrs->{$key} eq 'ARRAY' ) {
Condition66%2/3
$acc_type eq "multi" and ref $attrs->{$key} eq "ARRAY"
!ll&&!rl&&r
229081
MC/DC50%1/2
$acc_type eq "multi" and ref $attrs->{$key} eq "ARRAY"
$acc_type eq "multi" ref $attrs->{$key} eq "ARRAY"
Truth table66%2/3
A and B
A= $acc_type eq "multi"B= ref $attrs->{$key} eq "ARRAY"
ABresult
0X0
100
111
225 81 my $others = delete $attrs->{$key};
226 81 my $total = @$others;
227 81 my @objects;
228 81 foreach my $idx (0 .. $#$others) {
229 147 my $rel_obj = $others->[$idx];
230 147 ▶ if(!blessed $rel_obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($rel_obj)) 136 11 147
231 136 $rel_obj = $new->__new_related_find_or_new_helper($key, $rel_obj);
232 }
233  
234 147 ▶ if ($rel_obj->in_storage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_obj->in_storage) { } 0 147 147
235 0 $rel_obj->throw_exception ('A multi relationship can not be pre-existing when doing multicreate. Something went wrong');
236 } else {
237 147 MULTICREATE_DEBUG and
238 print STDERR "MC $new uninserted $key $rel_obj (${\($idx+1)} of $total)\n";
239 }
240 147 push(@objects, $rel_obj);
241 }
242 81 $related->{$key} = \@objects;
243 81 next;
244 }
245 elsif ($acc_type eq 'filter') {
246 ## 'filter' should disappear and get merged in with 'single' above!
247 102 my $rel_obj = delete $attrs->{$key};
248 102 ▶ if(!blessed $rel_obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($rel_obj)) 70 32 102
249 70 $rel_obj = $new->__new_related_find_or_new_helper($key, $rel_obj);
250 }
251 102 ▶ if ($rel_obj->in_storage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_obj->in_storage) { } 41 61 102
252 41 $new->{_rel_in_storage}{$key} = 1;
253 }
254 else {
255 61 MULTICREATE_DEBUG and print STDERR "MC $new uninserted $key $rel_obj\n";
256 }
257 102 $inflated->{$key} = $rel_obj;
258 102 next;
259 }
260 ▶ elsif (
Condition66%2/3
$rsrc->has_column($key) and $rsrc->column_info($key)->{"_inflate_info"}
!ll&&!rl&&r
01252
MC/DC50%1/2
$rsrc->has_column($key) and $rsrc->column_info($key)->{"_inflate_info"}
$rsrc->has_column($key) $rsrc->column_info($key)->{"_inflate_info"}
Truth table66%2/3
A and B
A= $rsrc->has_column($key)B= $rsrc->column_info($key)->{"_inflate_info"}
ABresult
0X0
100
111
261 $rsrc->has_column($key)
262 and
263 $rsrc->column_info($key)->{_inflate_info}
264 ) {
265 2 $inflated->{$key} = $attrs->{$key};
266 2 next;
267 }
268 }
269 3228 $new->store_column($key => $attrs->{$key});
270 }
271  
272 1490 ▶ $new->{_relationship_data} = $related if $related;
Branch: 1 branch
BranchTrueFalseTotal
if $related 168 1322 1490
273 1490 ▶ $new->{_inflated_column} = $inflated if $inflated;
Branch: 1 branch
BranchTrueFalseTotal
if $inflated 97 1393 1490
274 }
275  
276 1490 return $new;
277 }
278  
279 =head2 $column_accessor
280  
281 # Each pair does the same thing
282  
283 # (un-inflated, regular column)
284 my $val = $result->get_column('first_name');
285 my $val = $result->first_name;
286  
287 $result->set_column('first_name' => $val);
288 $result->first_name($val);
289  
290 # (inflated column via DBIx::Class::InflateColumn::DateTime)
291 my $val = $result->get_inflated_column('last_modified');
292 my $val = $result->last_modified;
293  
294 $result->set_inflated_column('last_modified' => $val);
295 $result->last_modified($val);
296  
297 =over
298  
299 =item Arguments: $value?
300  
301 =item Return Value: $value
302  
303 =back
304  
305 A column accessor method is created for each column, which is used for
306 getting/setting the value for that column.
307  
308 The actual method name is based on the
309 L<accessor|DBIx::Class::ResultSource/accessor> name given during the
310 L<Result Class|DBIx::Class::Manual::ResultClass> L<column definition
311 |DBIx::Class::ResultSource/add_columns>. Like L</set_column>, this
312 will not store the data in the database until L</insert> or L</update>
313 is called on the row.
314  
315 =head2 insert
316  
317 $result->insert;
318  
319 =over
320  
321 =item Arguments: none
322  
323 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
324  
325 =back
326  
327 Inserts an object previously created by L</new> into the database if
328 it isn't already in there. Returns the object itself. To insert an
329 entirely new row into the database, use L<DBIx::Class::ResultSet/create>.
330  
331 To fetch an uninserted result object, call
332 L<new_result|DBIx::Class::ResultSet/new_result> on a resultset.
333  
334 This will also insert any uninserted, related objects held inside this
335 one, see L<DBIx::Class::ResultSet/create> for more details.
336  
337 =cut
338  
339 1494 ▶sub insert {
Subroutine insert: called CC=33 SCAR=35.3
Pod documented
340 1494 my ($self) = @_;
341 1494 ▶ return $self if $self->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
if $self->in_storage 0 1494 1494
342 1494 my $rsrc = $self->result_source;
343 1494 ▶ $self->throw_exception("No result_source set on this object; can't insert")
Branch: 1 branch
BranchTrueFalseTotal
unless $rsrc 0 1494 1494
344 unless $rsrc;
345  
346 1494 my $storage = $rsrc->storage;
347  
348 1494 my $rollback_guard;
349  
350 # Check if we stored uninserted relobjs here in new()
351 1494 ▶ my %related_stuff = (%{$self->{_relationship_data} || {}},
Condition100%2/2
$self->{'_relationship_data'} || {}
l!l
2761218
MC/DC100%1/1
$self->{'_relationship_data'} || {}
$self->{'_relationship_data'}
Truth table100%2/2
A
A= $self->{'_relationship_data'}
Aresult
11
00
352 1494 ▶ %{$self->{_inflated_column} || {}});
Condition100%2/2
$self->{'_inflated_column'} || {}
l!l
567927
MC/DC100%1/1
$self->{'_inflated_column'} || {}
$self->{'_inflated_column'}
Truth table100%2/2
A
A= $self->{'_inflated_column'}
Aresult
11
00
353  
354 # insert what needs to be inserted before us
355 1494 my %pre_insert;
356 1494 for my $rel_name (keys %related_stuff) {
357 300 my $rel_obj = $related_stuff{$rel_name};
358  
359 300 ▶ if (! $self->{_rel_in_storage}{$rel_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_rel_in_storage"}{$rel_name}) 181 119 300
360 181 ▶ next unless (blessed $rel_obj && $rel_obj->isa('DBIx::Class::Row'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($rel_obj) and $rel_obj->isa("DBIx::Class::Row") 80 101 181
Condition66%2/3
builtin::blessed($rel_obj) and $rel_obj->isa("DBIx::Class::Row")
!ll&&!rl&&r
800101
MC/DC50%1/2
builtin::blessed($rel_obj) and $rel_obj->isa("DBIx::Class::Row")
builtin::blessed($rel_obj) $rel_obj->isa("DBIx::Class::Row")
Truth table66%2/3
A and B
A= builtin::blessed($rel_obj)B= $rel_obj->isa("DBIx::Class::Row")
ABresult
0X0
100
111
361  
362 101 ▶ next unless $rsrc->_pk_depends_on(
Branch: 1 branch
BranchTrueFalseTotal
unless $rsrc->_pk_depends_on($rel_name, {$rel_obj->get_columns}) 13 88 101
363 $rel_name, { $rel_obj->get_columns }
364 );
365  
366 # The guard will save us if we blow out of this scope via die
367 88 ▶ $rollback_guard ||= $storage->txn_scope_guard;
Condition66%2/3
$rollback_guard ||= $storage->txn_scope_guard
l!l&&r!l&&!r
9790
MC/DC0%0/2
$rollback_guard ||= $storage->txn_scope_guard
$rollback_guard $storage->txn_scope_guard
Truth table66%2/3
A ||= B
A= $rollback_guardB= $storage->txn_scope_guard
ABresult
1X1
011
000
368  
369 88 MULTICREATE_DEBUG and print STDERR "MC $self pre-reconstructing $rel_name $rel_obj\n";
370  
371 88 ▶ my $them = { %{$rel_obj->{_relationship_data} || {} }, $rel_obj->get_columns };
Condition100%2/2
$rel_obj->{'_relationship_data'} || {}
l!l
1573
MC/DC100%1/1
$rel_obj->{'_relationship_data'} || {}
$rel_obj->{'_relationship_data'}
Truth table100%2/2
A
A= $rel_obj->{'_relationship_data'}
Aresult
11
00
372 88 my $existing;
373  
374 # if there are no keys - nothing to search for
375 88 ▶ if (keys %$them and $existing = $self->result_source
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$them and $existing = $self->result_source->related_source($rel_name)->resultset->find($them)) { } 13 75 88
Condition100%3/3
keys %$them and $existing = $self->result_source->related_source($rel_name)->resultset->find($them)
!ll&&!rl&&r
17413
MC/DC100%2/2
keys %$them and $existing = $self->result_source->related_source($rel_name)->resultset->find($them)
keys %$them $existing = $self->result_source->related_source($rel_name)->resultset->find($them)
Truth table100%3/3
A and B
A= keys %$themB= $existing = $self->result_source->related_source($rel_name)->resultset->find($them)
ABresult
0X0
100
111
376 ->related_source($rel_name)
377 ->resultset
378 ->find($them)
379 ) {
380 13 %{$rel_obj} = %{$existing};
381 }
382 else {
383 75 $rel_obj->insert;
384 }
385  
386 87 $self->{_rel_in_storage}{$rel_name} = 1;
387 }
388  
389 206 $self->set_from_related($rel_name, $rel_obj);
390 206 delete $related_stuff{$rel_name};
391 }
392  
393 # start a transaction here if not started yet and there is more stuff
394 # to insert after us
395 1493 ▶ if (keys %related_stuff) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %related_stuff) 81 1412 1493
396 81 ▶ $rollback_guard ||= $storage->txn_scope_guard
Condition66%2/3
$rollback_guard ||= $storage->txn_scope_guard
l!l&&r!l&&!r
14670
MC/DC0%0/2
$rollback_guard ||= $storage->txn_scope_guard
$rollback_guard $storage->txn_scope_guard
Truth table66%2/3
A ||= B
A= $rollback_guardB= $storage->txn_scope_guard
ABresult
1X1
011
000
397 }
398  
399 1493 MULTICREATE_DEBUG and do {
400 379 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
401 print STDERR "MC $self inserting (".join(', ', $self->get_columns).")\n";
402 };
403  
404 # perform the insert - the storage will return everything it is asked to
405 # (autoinc primary columns and any retrieve_on_insert columns)
406 1493 my %current_rowdata = $self->get_columns;
407 1493 my $returned_cols = $storage->insert(
408 $rsrc,
409 { %current_rowdata }, # what to insert, copy because the storage *will* change it
410 );
411  
412 1489 for (keys %$returned_cols) {
413 $self->store_column($_, $returned_cols->{$_})
414 # this ensures we fire store_column only once
415 # (some asshats like overriding it)
416 if (
417 (!exists $current_rowdata{$_})
418 or
419 (defined $current_rowdata{$_} xor defined $returned_cols->{$_})
420 or
421 5212 ▶ (defined $current_rowdata{$_} and $current_rowdata{$_} ne $returned_cols->{$_})
Branch: 1 branch
BranchTrueFalseTotal
if not exists $current_rowdata{$_} or (defined $current_rowdata{$_} xor defined $returned_cols->{$_}) or defined $current_rowdata{$_} and $current_rowdata{$_} ne $returned_cols->{$_} 1227 3985 5212
Condition76%10/13
not exists $current_rowdata{$_} or (defined $current_rowdata{$_} xor defined $returned_cols->{$_}) or defined $current_rowdata{$_} and $current_rowdata{$_} ne $returned_cols->{$_}
l!l&&r!l&&!r
122703985
not exists $current_rowdata{$_} or (defined $current_rowdata{$_} xor defined $returned_cols->{$_})
l!l&&r!l&&!r
122523985
defined $current_rowdata{$_} xor defined $returned_cols->{$_}
l&&rl&&!r!l&&r!l&&!r
39310254
defined $current_rowdata{$_} and $current_rowdata{$_} ne $returned_cols->{$_}
!ll&&!rl&&r
5439310
MC/DC60%3/5
not exists $current_rowdata{$_} or (defined $current_rowdata{$_} xor defined $returned_cols->{$_}) or defined $current_rowdata{$_} and $current_rowdata{$_} ne $returned_cols->{$_}
not exists $current_rowdata{$_} defined $current_rowdata{$_} defined $returned_cols->{$_} defined $current_rowdata{$_} $current_rowdata{$_} ne $returned_cols->{$_}
Truth table44%4/9
not(A) or B xor C or D and E
A= not exists $current_rowdata{$_}B= defined $current_rowdata{$_}C= defined $returned_cols->{$_}D= defined $current_rowdata{$_}E= $current_rowdata{$_} ne $returned_cols->{$_}
ABCDEresult
1XXXX1
010XX1
001XX1
011111
000111
0110X0
011100
0000X0
000100
422 );
423 }
424  
425 1489 delete $self->{_column_data_in_storage};
426 1489 $self->in_storage(1);
427  
428 1489 $self->{_dirty_columns} = {};
429 1489 $self->{related_resultsets} = {};
430  
431 1489 foreach my $rel_name (keys %related_stuff) {
432 93 ▶ next unless $rsrc->has_relationship ($rel_name);
Branch: 1 branch
BranchTrueFalseTotal
unless $rsrc->has_relationship($rel_name) 4 89 93
433  
434 my @cands = ref $related_stuff{$rel_name} eq 'ARRAY'
435 76 ? @{$related_stuff{$rel_name}}
436 89 ▶ : $related_stuff{$rel_name}
Branch: 1 branch
BranchTrueFalseTotal
ref $related_stuff{$rel_name} eq 'ARRAY' ? : 76 13 89
437 ;
438  
439 89 ▶ if (@cands && blessed $cands[0] && $cands[0]->isa('DBIx::Class::Row')
Branch: 1 branch
BranchTrueFalseTotal
if (@cands and builtin::blessed($cands[0]) and $cands[0]->isa("DBIx::Class::Row")) 88 1 89
Condition66%4/6
@cands and builtin::blessed($cands[0]) and $cands[0]->isa("DBIx::Class::Row")
!ll&&!rl&&r
1088
@cands and builtin::blessed($cands[0])
!ll&&!rl&&r
1088
MC/DC33%1/3
@cands and builtin::blessed($cands[0]) and $cands[0]->isa("DBIx::Class::Row")
@cands builtin::blessed($cands[0]) $cands[0]->isa("DBIx::Class::Row")
Truth table50%2/4
A and B and C
A= @candsB= builtin::blessed($cands[0])C= $cands[0]->isa("DBIx::Class::Row")
ABCresult
0XX0
10X0
1100
1111
440 ) {
441 88 my $reverse = $rsrc->reverse_relationship_info($rel_name);
442 88 foreach my $obj (@cands) {
443 155 $obj->set_from_related($_, $self) for keys %$reverse;
444 155 ▶ if ($self->__their_pk_needs_us($rel_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->__their_pk_needs_us($rel_name)) { } 155 0 155
445 155 ▶ if (exists $self->{_ignore_at_insert}{$rel_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_ignore_at_insert'}{$rel_name}) { } 6 149 155
446 6 MULTICREATE_DEBUG and print STDERR "MC $self skipping post-insert on $rel_name\n";
447 }
448 else {
449 149 MULTICREATE_DEBUG and print STDERR "MC $self inserting $rel_name $obj\n";
450 149 $obj->insert;
451 }
452 } else {
453 0 MULTICREATE_DEBUG and print STDERR "MC $self post-inserting $obj\n";
454 0 $obj->insert();
455 }
456 }
457 }
458 }
459  
460 1489 delete $self->{_ignore_at_insert};
461  
462 1489 ▶ $rollback_guard->commit if $rollback_guard;
Branch: 1 branch
BranchTrueFalseTotal
if $rollback_guard 145 1344 1489
463  
464 1489 return $self;
465 }
466  
467 =head2 in_storage
468  
469 $result->in_storage; # Get value
470 $result->in_storage(1); # Set value
471  
472 =over
473  
474 =item Arguments: none or 1|0
475  
476 =item Return Value: 1|0
477  
478 =back
479  
480 Indicates whether the object exists as a row in the database or
481 not. This is set to true when L<DBIx::Class::ResultSet/find>,
482 L<DBIx::Class::ResultSet/create> or L<DBIx::Class::Row/insert>
483 are invoked.
484  
485 Creating a result object using L<DBIx::Class::ResultSet/new_result>, or
486 calling L</delete> on one, sets it to false.
487  
488  
489 =head2 update
490  
491 $result->update(\%columns?)
492  
493 =over
494  
495 =item Arguments: none or a hashref
496  
497 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
498  
499 =back
500  
501 Throws an exception if the result object is not yet in the database,
502 according to L</in_storage>. Returns the object itself.
503  
504 This method issues an SQL UPDATE query to commit any changes to the
505 object to the database if required (see L</get_dirty_columns>).
506 It throws an exception if a proper WHERE clause uniquely identifying
507 the database row can not be constructed (see
508 L<significance of primary keys|DBIx::Class::Manual::Intro/The Significance and Importance of Primary Keys>
509 for more details).
510  
511 Also takes an optional hashref of C<< column_name => value >> pairs
512 to update on the object first. Be aware that the hashref will be
513 passed to C<set_inflated_columns>, which might edit it in place, so
514 don't rely on it being the same after a call to C<update>. If you
515 need to preserve the hashref, it is sufficient to pass a shallow copy
516 to C<update>, e.g. ( { %{ $href } } )
517  
518 If the values passed or any of the column values set on the object
519 contain scalar references, e.g.:
520  
521 $result->last_modified(\'NOW()')->update();
522 # OR
523 $result->update({ last_modified => \'NOW()' });
524  
525 The update will pass the values verbatim into SQL. (See
526 L<SQL::Abstract::Classic> docs). The values in your Result object will NOT
527 change as a result of the update call, if you want the object to be updated
528 with the actual values from the database, call L</discard_changes> after the
529 update.
530  
531 $result->update()->discard_changes();
532  
533 To determine before calling this method, which column values have
534 changed and will be updated, call L</get_dirty_columns>.
535  
536 To check if any columns will be updated, call L</is_changed>.
537  
538 To force a column to be updated, call L</make_column_dirty> before
539 this method.
540  
541 =cut
542  
543 817 ▶sub update {
Subroutine update: called CC=6 SCAR=18.6
Pod documented
544 817 my ($self, $upd) = @_;
545  
546 817 ▶ $self->set_inflated_columns($upd) if $upd;
Branch: 1 branch
BranchTrueFalseTotal
if $upd 771 46 817
547  
548 816 ▶ my %to_update = $self->get_dirty_columns
Branch: 1 branch
BranchTrueFalseTotal
my(%to_update) = $self->get_dirty_columns or return $self 777 39 816
549 or return $self;
550  
551 777 ▶ $self->throw_exception( "Not in database" ) unless $self->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_storage 0 777 777
552  
553 777 my $rows = $self->result_source->storage->update(
554 $self->result_source, \%to_update, $self->_storage_ident_condition
555 );
556 775 ▶ if ($rows == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rows == 0) { } 0 775 775
elsif ($rows > 1) { } 0 775 775
557 0 $self->throw_exception( "Can't update ${self}: row not found" );
558 } elsif ($rows > 1) {
559 0 $self->throw_exception("Can't update ${self}: updated more than one row");
560 }
561 775 $self->{_dirty_columns} = {};
562 775 $self->{related_resultsets} = {};
563 775 delete $self->{_column_data_in_storage};
564 775 return $self;
565 }
566  
567 =head2 delete
568  
569 $result->delete
570  
571 =over
572  
573 =item Arguments: none
574  
575 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
576  
577 =back
578  
579 Throws an exception if the object is not in the database according to
580 L</in_storage>. Also throws an exception if a proper WHERE clause
581 uniquely identifying the database row can not be constructed (see
582 L<significance of primary keys|DBIx::Class::Manual::Intro/The Significance and Importance of Primary Keys>
583 for more details).
584  
585 The object is still perfectly usable, but L</in_storage> will
586 now return 0 and the object must be reinserted using L</insert>
587 before it can be used to L</update> the row again.
588  
589 If you delete an object in a class with a C<has_many> relationship, an
590 attempt is made to delete all the related objects as well. To turn
591 this behaviour off, pass C<< cascade_delete => 0 >> in the C<$attr>
592 hashref of the relationship, see L<DBIx::Class::Relationship>. Any
593 database-level cascade or restrict will take precedence over a
594 DBIx-Class-based cascading delete, since DBIx-Class B<deletes the
595 main row first> and only then attempts to delete any remaining related
596 rows.
597  
598 If you delete an object within a txn_do() (see L<DBIx::Class::Storage/txn_do>)
599 and the transaction subsequently fails, the result object will remain marked as
600 not being in storage. If you know for a fact that the object is still in
601 storage (i.e. by inspecting the cause of the transaction's failure), you can
602 use C<< $obj->in_storage(1) >> to restore consistency between the object and
603 the database. This would allow a subsequent C<< $obj->delete >> to work
604 as expected.
605  
606 See also L<DBIx::Class::ResultSet/delete>.
607  
608 =cut
609  
610 117 ▶sub delete {
Subroutine delete: called CC=7 SCAR=29.1
Pod documented
611 117 my $self = shift;
612 117 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 117 0 117
613 117 ▶ $self->throw_exception( "Not in database" ) unless $self->in_storage;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_storage 1 116 117
614  
615 116 $self->result_source->storage->delete(
616 $self->result_source, $self->_storage_ident_condition
617 );
618  
619 115 delete $self->{_column_data_in_storage};
620 115 $self->in_storage(0);
621 }
622 else {
623 0 ▶ my $rsrc = try { $self->result_source_instance }
Subroutine __ANON__: not called CC=1 SCAR=6.9
624 0 ▶ or $self->throw_exception("Can't do class delete without a ResultSource instance");
Branch: 1 branch
BranchTrueFalseTotal
my $rsrc = &try(sub { $self->result_source_instance; } ) or $self->throw_exception("Can't do class delete without a ResultSource instance") 0 0 0
625  
626 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
627 0 ▶ my $query = ref $_[0] eq 'HASH' ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
628 0 $rsrc->resultset->search(@_)->delete;
629 }
630 115 return $self;
631 }
632  
633 =head2 get_column
634  
635 my $val = $result->get_column($col);
636  
637 =over
638  
639 =item Arguments: $columnname
640  
641 =item Return Value: The value of the column
642  
643 =back
644  
645 Throws an exception if the column name given doesn't exist according
646 to L<has_column|DBIx::Class::ResultSource/has_column>.
647  
648 Returns a raw column value from the result object, if it has already
649 been fetched from the database or set by an accessor.
650  
651 If an L<inflated value|DBIx::Class::InflateColumn> has been set, it
652 will be deflated and returned.
653  
654 Note that if you used the C<columns> or the C<select/as>
655 L<search attributes|DBIx::Class::ResultSet/ATTRIBUTES> on the resultset from
656 which C<$result> was derived, and B<did not include> C<$columnname> in the list,
657 this method will return C<undef> even if the database contains some value.
658  
659 To retrieve all loaded column values as a hash, use L</get_columns>.
660  
661 =cut
662  
663 19899 ▶sub get_column {
Subroutine get_column: called CC=5 SCAR=16.1
Pod documented
664 19899 my ($self, $column) = @_;
665 19899 ▶ $self->throw_exception( "Can't fetch data as class method" ) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 19899 19899
666  
667 return $self->{_column_data}{$column}
668 19899 ▶ if exists $self->{_column_data}{$column};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_column_data"}{$column} 18533 1366 19899
669  
670 1366 ▶ if (exists $self->{_inflated_column}{$column}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_inflated_column"}{$column}) 63 1303 1366
671 # deflate+return cycle
672 return $self->store_column($column, $self->_deflated_column(
673 63 $column, $self->{_inflated_column}{$column}
674 ));
675 }
676  
677 1303 ▶ $self->throw_exception( "No such column '${column}' on " . ref $self )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->result_source->has_column($column) 1 1302 1303
678 unless $self->result_source->has_column($column);
679  
680 1302 return undef;
681 }
682  
683 =head2 has_column_loaded
684  
685 if ( $result->has_column_loaded($col) ) {
686 print "$col has been loaded from db";
687 }
688  
689 =over
690  
691 =item Arguments: $columnname
692  
693 =item Return Value: 0|1
694  
695 =back
696  
697 Returns a true value if the column value has been loaded from the
698 database (or set locally).
699  
700 =cut
701  
702 5928 ▶sub has_column_loaded {
Subroutine has_column_loaded: called CC=4 SCAR=13.9
Pod documented
703 5928 my ($self, $column) = @_;
704 5928 ▶ $self->throw_exception( "Can't call has_column data as class method" ) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 5928 5928
705  
706 return (
707 exists $self->{_inflated_column}{$column}
708 or
709 5928 ▶ exists $self->{_column_data}{$column}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_inflated_column'}{$column} || exists $self->{'_column_data'}{$column} ? : 5263 665 5928
Condition100%3/3
exists $self->{"_inflated_column"}{$column} or exists $self->{"_column_data"}{$column}
l!l&&r!l&&!r
1155148665
MC/DC100%2/2
exists $self->{"_inflated_column"}{$column} or exists $self->{"_column_data"}{$column}
exists $self->{"_inflated_column"}{$column} exists $self->{"_column_data"}{$column}
Truth table100%3/3
A or B
A= exists $self->{"_inflated_column"}{$column}B= exists $self->{"_column_data"}{$column}
ABresult
1X1
011
000
710 ) ? 1 : 0;
711 }
712  
713 =head2 get_columns
714  
715 my %data = $result->get_columns;
716  
717 =over
718  
719 =item Arguments: none
720  
721 =item Return Value: A hash of columnname, value pairs.
722  
723 =back
724  
725 Returns all loaded column data as a hash, containing raw values. To
726 get just one value for a particular column, use L</get_column>.
727  
728 See L</get_inflated_columns> to get the inflated values.
729  
730 =cut
731  
732 3094 ▶sub get_columns {
Subroutine get_columns: called CC=7 SCAR=19.5
Pod documented
733 3094 my $self = shift;
734 3094 ▶ if (exists $self->{_inflated_column}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_inflated_column"}) 1075 2019 3094
735 # deflate cycle for each inflation, including filter rels
736 1075 foreach my $col (keys %{$self->{_inflated_column}}) {
737 104 ▶ unless (exists $self->{_column_data}{$col}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_column_data"}{$col}) 49 55 104
738  
739 # if cached related_resultset is present assume this was a prefetch
740 carp_unique(
741 "Returning primary keys of prefetched 'filter' rels as part of get_columns() is deprecated and will "
742 . 'eventually be removed entirely (set DBIC_COLUMNS_INCLUDE_FILTER_RELS to disable this warning)'
743 ) if (
744 ! $ENV{DBIC_COLUMNS_INCLUDE_FILTER_RELS}
745 and
746 defined $self->{related_resultsets}{$col}
747 and
748 49 ▶ defined $self->{related_resultsets}{$col}->get_cache
Branch: 1 branch
BranchTrueFalseTotal
if not $ENV{"DBIC_COLUMNS_INCLUDE_FILTER_RELS"} and defined $self->{"related_resultsets"}{$col} and defined $self->{"related_resultsets"}{$col}->get_cache 3 46 49
Condition83%5/6
not $ENV{"DBIC_COLUMNS_INCLUDE_FILTER_RELS"} and defined $self->{"related_resultsets"}{$col} and defined $self->{"related_resultsets"}{$col}->get_cache
!ll&&!rl&&r
4603
not $ENV{"DBIC_COLUMNS_INCLUDE_FILTER_RELS"} and defined $self->{"related_resultsets"}{$col}
!ll&&!rl&&r
6403
MC/DC66%2/3
not $ENV{"DBIC_COLUMNS_INCLUDE_FILTER_RELS"} and defined $self->{"related_resultsets"}{$col} and defined $self->{"related_resultsets"}{$col}->get_cache
not $ENV{"DBIC_COLUMNS_INCLUDE_FILTER_RELS"} defined $self->{"related_resultsets"}{$col} defined $self->{"related_resultsets"}{$col}->get_cache
Truth table75%3/4
not(A) and B and C
A= not $ENV{"DBIC_COLUMNS_INCLUDE_FILTER_RELS"}B= defined $self->{"related_resultsets"}{$col}C= defined $self->{"related_resultsets"}{$col}->get_cache
ABCresult
0XX0
10X0
1100
1111
749 );
750  
751 49 $self->store_column($col, $self->_deflated_column($col, $self->{_inflated_column}{$col}));
752 }
753 }
754 }
755 3094 return %{$self->{_column_data}};
756 }
757  
758 =head2 get_dirty_columns
759  
760 my %data = $result->get_dirty_columns;
761  
762 =over
763  
764 =item Arguments: none
765  
766 =item Return Value: A hash of column, value pairs
767  
768 =back
769  
770 Only returns the column, value pairs for those columns that have been
771 changed on this object since the last L</update> or L</insert> call.
772  
773 See L</get_columns> to fetch all column/value pairs.
774  
775 =cut
776  
777 828 ▶sub get_dirty_columns {
Subroutine get_dirty_columns: called CC=1 SCAR=0.0
Pod documented
778 828 my $self = shift;
779 808 return map { $_ => $self->{_column_data}{$_} }
780 828 keys %{$self->{_dirty_columns}};
781 }
782  
783 =head2 make_column_dirty
784  
785 $result->make_column_dirty($col)
786  
787 =over
788  
789 =item Arguments: $columnname
790  
791 =item Return Value: not defined
792  
793 =back
794  
795 Throws an exception if the column does not exist.
796  
797 Marks a column as having been changed regardless of whether it has
798 really changed.
799  
800 =cut
801  
802 13 ▶sub make_column_dirty {
Subroutine make_column_dirty: called CC=5 SCAR=16.1
Pod documented
803 13 my ($self, $column) = @_;
804  
805 $self->throw_exception( "No such column '${column}' on " . ref $self )
806 13 ▶ unless exists $self->{_column_data}{$column} || $self->result_source->has_column($column);
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_column_data"}{$column} or $self->result_source->has_column($column) 1 12 13
Condition100%3/3
exists $self->{"_column_data"}{$column} or $self->result_source->has_column($column)
l!l&&r!l&&!r
931
MC/DC100%2/2
exists $self->{"_column_data"}{$column} or $self->result_source->has_column($column)
exists $self->{"_column_data"}{$column} $self->result_source->has_column($column)
Truth table100%3/3
A or B
A= exists $self->{"_column_data"}{$column}B= $self->result_source->has_column($column)
ABresult
1X1
011
000
807  
808 # the entire clean/dirty code relies on exists, not on true/false
809 12 ▶ return 1 if exists $self->{_dirty_columns}{$column};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_dirty_columns"}{$column} 3 9 12
810  
811 9 $self->{_dirty_columns}{$column} = 1;
812  
813 # if we are just now making the column dirty, and if there is an inflated
814 # value, force it over the deflated one
815 9 ▶ if (exists $self->{_inflated_column}{$column}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_inflated_column"}{$column}) 1 8 9
816 $self->store_column($column,
817 $self->_deflated_column(
818 1 $column, $self->{_inflated_column}{$column}
819 )
820 );
821 }
822 }
823  
824 =head2 get_inflated_columns
825  
826 my %inflated_data = $obj->get_inflated_columns;
827  
828 =over
829  
830 =item Arguments: none
831  
832 =item Return Value: A hash of column, object|value pairs
833  
834 =back
835  
836 Returns a hash of all column keys and associated values. Values for any
837 columns set to use inflation will be inflated and returns as objects.
838  
839 See L</get_columns> to get the uninflated values.
840  
841 See L<DBIx::Class::InflateColumn> for how to setup inflation.
842  
843 =cut
844  
845 6 ▶sub get_inflated_columns {
Subroutine get_inflated_columns: called CC=12 SCAR=25.0
Pod documented
846 6 my $self = shift;
847  
848 6 my $loaded_colinfo = $self->result_source->columns_info;
849 $self->has_column_loaded($_) or delete $loaded_colinfo->{$_}
850 6 ▶ for keys %$loaded_colinfo;
Branch: 1 branch
BranchTrueFalseTotal
$self->has_column_loaded($_) or delete $loaded_colinfo->{$_} 9 27 36
851  
852 6 my %cols_to_return = ( %{$self->{_column_data}}, %$loaded_colinfo );
853  
854 6 ▶ unless ($ENV{DBIC_COLUMNS_INCLUDE_FILTER_RELS}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"DBIC_COLUMNS_INCLUDE_FILTER_RELS"}) 6 0 6
855 6 for (keys %$loaded_colinfo) {
856 # if cached related_resultset is present assume this was a prefetch
857 9 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($loaded_colinfo->{$_}{"_inflate_info"} and defined $self->{"related_resultsets"}{$_} and defined $self->{"related_resultsets"}{$_}->get_cache) 3 6 9
Condition83%5/6
$loaded_colinfo->{$_}{"_inflate_info"} and defined $self->{"related_resultsets"}{$_} and defined $self->{"related_resultsets"}{$_}->get_cache
!ll&&!rl&&r
603
$loaded_colinfo->{$_}{"_inflate_info"} and defined $self->{"related_resultsets"}{$_}
!ll&&!rl&&r
423
MC/DC66%2/3
$loaded_colinfo->{$_}{"_inflate_info"} and defined $self->{"related_resultsets"}{$_} and defined $self->{"related_resultsets"}{$_}->get_cache
$loaded_colinfo->{$_}{"_inflate_info"} defined $self->{"related_resultsets"}{$_} defined $self->{"related_resultsets"}{$_}->get_cache
Truth table75%3/4
A and B and C
A= $loaded_colinfo->{$_}{"_inflate_info"}B= defined $self->{"related_resultsets"}{$_}C= defined $self->{"related_resultsets"}{$_}->get_cache
ABCresult
0XX0
10X0
1100
1111
858 $loaded_colinfo->{$_}{_inflate_info}
859 and
860 defined $self->{related_resultsets}{$_}
861 and
862 defined $self->{related_resultsets}{$_}->get_cache
863 ) {
864 3 carp_unique(
865 "Returning prefetched 'filter' rels as part of get_inflated_columns() is deprecated and will "
866 . 'eventually be removed entirely (set DBIC_COLUMNS_INCLUDE_FILTER_RELS to disable this warning)'
867 );
868 3 last;
869 }
870 }
871 }
872  
873 6 map { $_ => (
874 (
875 ! exists $loaded_colinfo->{$_}
876 or
877 (
878 exists $loaded_colinfo->{$_}{accessor}
879 and
880 ! defined $loaded_colinfo->{$_}{accessor}
881 )
882 ) ? $self->get_column($_)
883 11 ▶ : $self->${ \(
Branch: 1 branch
BranchTrueFalseTotal
!exists $loaded_colinfo->{$_} || exists $loaded_colinfo->{$_}{'accessor'} && !defined($loaded_colinfo->{$_}{'accessor'}) ? : 3 8 11
Condition83%5/6
not exists $loaded_colinfo->{$_} or exists $loaded_colinfo->{$_}{"accessor"} and not defined $loaded_colinfo->{$_}{"accessor"}
l!l&&r!l&&!r
218
exists $loaded_colinfo->{$_}{"accessor"} and not defined $loaded_colinfo->{$_}{"accessor"}
!ll&&!rl&&r
801
MC/DC66%2/3
not exists $loaded_colinfo->{$_} or exists $loaded_colinfo->{$_}{"accessor"} and not defined $loaded_colinfo->{$_}{"accessor"}
not exists $loaded_colinfo->{$_} exists $loaded_colinfo->{$_}{"accessor"} not defined $loaded_colinfo->{$_}{"accessor"}
Truth table75%3/4
not(A) or B and not(C)
A= not exists $loaded_colinfo->{$_}B= exists $loaded_colinfo->{$_}{"accessor"}C= not defined $loaded_colinfo->{$_}{"accessor"}
ABCresult
1XX1
0111
00X0
0100
884 defined $loaded_colinfo->{$_}{accessor}
885 ? $loaded_colinfo->{$_}{accessor}
886 8 ▶ : $_
Branch: 1 branch
BranchTrueFalseTotal
defined $loaded_colinfo->{$_}{'accessor'} ? : 0 8 8
887 )}
888 )} keys %cols_to_return;
889 }
890  
891 716 ▶sub _is_column_numeric {
Subroutine _is_column_numeric: called CC=5 SCAR=16.1
892 716 my ($self, $column) = @_;
893  
894 716 ▶ return undef unless $self->result_source->has_column($column);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->result_source->has_column($column) 1 715 716
895  
896 715 my $colinfo = $self->result_source->column_info ($column);
897  
898 # cache for speed (the object may *not* have a resultsource instance)
899 715 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $colinfo->{"is_numeric"} and my $storage = &try(sub { $self->result_source->schema->storage; } )) 45 670 715
Condition100%3/3
not defined $colinfo->{"is_numeric"} and my $storage = &try(sub { $self->result_source->schema->storage; } )
!ll&&!rl&&r
669145
MC/DC100%2/2
not defined $colinfo->{"is_numeric"} and my $storage = &try(sub { $self->result_source->schema->storage; } )
not defined $colinfo->{"is_numeric"} my $storage = &try(sub { $self->result_source->schema->storage; } )
Truth table100%3/3
not(A) and B
A= not defined $colinfo->{"is_numeric"}B= my $storage = &try(sub { $self->result_source->schema->storage; } )
ABresult
0X0
100
111
900 ! defined $colinfo->{is_numeric}
901 and
902 46 ▶ my $storage = try { $self->result_source->schema->storage }
Subroutine __ANON__: called CC=1 SCAR=0.0
903 ) {
904 $colinfo->{is_numeric} =
905 $storage->is_datatype_numeric ($colinfo->{data_type})
906 45 ▶ ? 1
Branch: 1 branch
BranchTrueFalseTotal
$storage->is_datatype_numeric($colinfo->{'data_type'}) ? : 25 20 45
907 : 0
908 ;
909 }
910  
911 715 return $colinfo->{is_numeric};
912 }
913  
914 =head2 set_column
915  
916 $result->set_column($col => $val);
917  
918 =over
919  
920 =item Arguments: $columnname, $value
921  
922 =item Return Value: $value
923  
924 =back
925  
926 Sets a raw column value. If the new value is different from the old one,
927 the column is marked as dirty for when you next call L</update>.
928  
929 If passed an object or reference as a value, this method will happily
930 attempt to store it, and a later L</insert> or L</update> will try and
931 stringify/numify as appropriate. To set an object to be deflated
932 instead, see L</set_inflated_columns>, or better yet, use L</$column_accessor>.
933  
934 =cut
935  
936 1828 ▶sub set_column {
Subroutine set_column: called CC=14 SCAR=26.4
Pod documented
937 1828 my ($self, $column, $new_value) = @_;
938  
939 1828 my $had_value = $self->has_column_loaded($column);
940 1828 my $old_value = $self->get_column($column);
941  
942 1828 $new_value = $self->store_column($column, $new_value);
943  
944 my $dirty =
945 1828 ▶ $self->{_dirty_columns}{$column}
Branch: 1 branch
BranchTrueFalseTotal
$self->in_storage ? : 900 699 1599
Condition100%3/3
$self->{'_dirty_columns'}{$column} || ($self->in_storage ? !$self->_eq_column_values($column, $old_value, $new_value) : 1)
l!l&&r!l&&!r
229154158
MC/DC100%2/2
$self->{'_dirty_columns'}{$column} || ($self->in_storage ? !$self->_eq_column_values($column, $old_value, $new_value) : 1)
$self->{'_dirty_columns'}{$column} ($self->in_storage ? !$self->_eq_column_values($column, $old_value, $new_value) : 1)
Truth table100%3/3
A || B
A= $self->{'_dirty_columns'}{$column}B= ($self->in_storage ? !$self->_eq_column_values($column, $old_value, $new_value) : 1)
ABresult
1X1
011
000
946 ||
947 ( $self->in_storage # no point tracking dirtyness on uninserted data
948 ? ! $self->_eq_column_values ($column, $old_value, $new_value)
949 : 1
950 )
951 ;
952  
953 1828 ▶ if ($dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dirty) 1770 58 1828
954 # FIXME sadly the update code just checks for keys, not for their value
955 1770 $self->{_dirty_columns}{$column} = 1;
956  
957 # Clear out the relation/inflation cache related to this column
958 #
959 # FIXME - this is a quick *largely incorrect* hack, pending a more
960 # serious rework during the merge of single and filter rels
961 1770 my $rel_names = $self->result_source->{_relationships};
962 1770 for my $rel_name (keys %$rel_names) {
963  
964 9946 ▶ my $acc = $rel_names->{$rel_name}{attrs}{accessor} || '';
Condition50%1/2
$rel_names->{$rel_name}{'attrs'}{'accessor'} || ''
l!l
99460
MC/DC0%0/1
$rel_names->{$rel_name}{'attrs'}{'accessor'} || ''
$rel_names->{$rel_name}{'attrs'}{'accessor'}
Truth table50%1/2
A
A= $rel_names->{$rel_name}{'attrs'}{'accessor'}
Aresult
11
00
965  
966 9946 ▶ if ( $acc eq 'single' and $rel_names->{$rel_name}{attrs}{fk_columns}{$column} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($acc eq 'single' and $rel_names->{$rel_name}{'attrs'}{'fk_columns'}{$column}) { } 896 9050 9946
elsif ($acc eq 'filter' and $rel_name eq $column) { } 439 16737 17176
Condition100%3/3
$acc eq "single" and $rel_names->{$rel_name}{"attrs"}{"fk_columns"}{$column}
!ll&&!rl&&r
48464204896
MC/DC100%2/2
$acc eq "single" and $rel_names->{$rel_name}{"attrs"}{"fk_columns"}{$column}
$acc eq "single" $rel_names->{$rel_name}{"attrs"}{"fk_columns"}{$column}
Truth table100%3/3
A and B
A= $acc eq "single"B= $rel_names->{$rel_name}{"attrs"}{"fk_columns"}{$column}
ABresult
0X0
100
111
967 896 delete $self->{related_resultsets}{$rel_name};
968 896 delete $self->{_relationship_data}{$rel_name};
969 #delete $self->{_inflated_column}{$rel_name};
970 }
971 ▶ elsif ( $acc eq 'filter' and $rel_name eq $column) {
Condition100%3/3
$acc eq "filter" and $rel_name eq $column
!ll&&!rl&&r
8126485439
MC/DC100%2/2
$acc eq "filter" and $rel_name eq $column
$acc eq "filter" $rel_name eq $column
Truth table100%3/3
A and B
A= $acc eq "filter"B= $rel_name eq $column
ABresult
0X0
100
111
972 439 delete $self->{related_resultsets}{$rel_name};
973 #delete $self->{_relationship_data}{$rel_name};
974 439 delete $self->{_inflated_column}{$rel_name};
975 }
976 }
977  
978 1770 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($had_value and $self->in_storage and not exists $self->{"_column_data_in_storage"}{$column} and $self->_track_storage_value($column)) 603 1167 1770
Condition100%9/9
$had_value and $self->in_storage and not exists $self->{"_column_data_in_storage"}{$column} and $self->_track_storage_value($column)
!ll&&!rl&&r
933234603
$had_value and $self->in_storage and not exists $self->{"_column_data_in_storage"}{$column}
!ll&&!rl&&r
90825837
$had_value and $self->in_storage
!ll&&!rl&&r
544364862
MC/DC100%4/4
$had_value and $self->in_storage and not exists $self->{"_column_data_in_storage"}{$column} and $self->_track_storage_value($column)
$had_value $self->in_storage not exists $self->{"_column_data_in_storage"}{$column} $self->_track_storage_value($column)
Truth table100%5/5
A and B and not(C) and D
A= $had_valueB= $self->in_storageC= not exists $self->{"_column_data_in_storage"}{$column}D= $self->_track_storage_value($column)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
979 # value change from something (even if NULL)
980 $had_value
981 and
982 # no storage - no storage-value
983 $self->in_storage
984 and
985 # no value already stored (multiple changes before commit to storage)
986 ! exists $self->{_column_data_in_storage}{$column}
987 and
988 $self->_track_storage_value($column)
989 ) {
990 603 $self->{_column_data_in_storage}{$column} = $old_value;
991 }
992 }
993  
994 1828 return $new_value;
995 }
996  
997 900 ▶sub _eq_column_values {
Subroutine _eq_column_values: called CC=8 SCAR=21.1
998 900 my ($self, $col, $old, $new) = @_;
999  
1000 900 ▶ if (defined $old xor defined $new) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $old xor defined $new) { } 27 873 900
elsif (not defined $old) { } 0 873 873
elsif (&is_literal_value($old) or &is_literal_value($new)) { } 101 772 873
elsif ($old eq $new) { } 56 716 772
elsif ($self->_is_column_numeric($col)) { } 669 47 716
Condition75%3/4
defined $old xor defined $new
l&&rl&&!r!l&&r!l&&!r
8732430
MC/DC100%2/2
defined $old xor defined $new
defined $old defined $new
Truth table75%3/4
A xor B
A= defined $oldB= defined $new
ABresult
110
101
011
000
1001 27 return 0;
1002 }
1003 elsif (not defined $old) { # both undef
1004 0 return 1;
1005 }
1006 ▶ elsif (
Condition66%2/3
&is_literal_value($old) or &is_literal_value($new)
l!l&&r!l&&!r
0101772
MC/DC50%1/2
&is_literal_value($old) or &is_literal_value($new)
&is_literal_value($old) &is_literal_value($new)
Truth table66%2/3
A or B
A= &is_literal_value($old)B= &is_literal_value($new)
ABresult
1X1
011
000
1007 is_literal_value $old
1008 or
1009 is_literal_value $new
1010 ) {
1011 101 return 0;
1012 }
1013 elsif ($old eq $new) {
1014 56 return 1;
1015 }
1016 elsif ($self->_is_column_numeric($col)) { # do a numeric comparison if datatype allows it
1017 669 return $old == $new;
1018 }
1019 else {
1020 47 return 0;
1021 }
1022 }
1023  
1024 # returns a boolean indicating if the passed column should have its original
1025 # value tracked between column changes and commitment to storage
1026 837 ▶sub _track_storage_value {
Subroutine _track_storage_value: called CC=1 SCAR=0.0
1027 837 my ($self, $col) = @_;
1028 return scalar grep
1029 837 { $col eq $_ }
1030 $self->result_source->primary_columns
1031 ;
1032 }
1033  
1034 =head2 set_columns
1035  
1036 $result->set_columns({ $col => $val, ... });
1037  
1038 =over
1039  
1040 =item Arguments: \%columndata
1041  
1042 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
1043  
1044 =back
1045  
1046 Sets multiple column, raw value pairs at once.
1047  
1048 Works as L</set_column>.
1049  
1050 =cut
1051  
1052 1428 ▶sub set_columns {
Subroutine set_columns: called CC=2 SCAR=6.9
Pod documented
1053 1428 my ($self, $values) = @_;
1054 1428 $self->set_column( $_, $values->{$_} ) for keys %$values;
1055 1428 return $self;
1056 }
1057  
1058 =head2 set_inflated_columns
1059  
1060 $result->set_inflated_columns({ $col => $val, $rel_name => $obj, ... });
1061  
1062 =over
1063  
1064 =item Arguments: \%columndata
1065  
1066 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
1067  
1068 =back
1069  
1070 Sets more than one column value at once. Any inflated values are
1071 deflated and the raw values stored.
1072  
1073 Any related values passed as Result objects, using the relation name as a
1074 key, are reduced to the appropriate foreign key values and stored. If
1075 instead of related result objects, a hashref of column, value data is
1076 passed, will create the related object first then store.
1077  
1078 Will even accept arrayrefs of data as a value to a
1079 L<DBIx::Class::Relationship/has_many> key, and create the related
1080 objects if necessary.
1081  
1082 Be aware that the input hashref might be edited in place, so don't rely
1083 on it being the same after a call to C<set_inflated_columns>. If you
1084 need to preserve the hashref, it is sufficient to pass a shallow copy
1085 to C<set_inflated_columns>, e.g. ( { %{ $href } } )
1086  
1087 See also L<DBIx::Class::Relationship::Base/set_from_related>.
1088  
1089 =cut
1090  
1091 848 ▶sub set_inflated_columns {
Subroutine set_inflated_columns: called CC=9 SCAR=22.1
Pod documented
1092 848 my ( $self, $upd ) = @_;
1093 848 my $rsrc;
1094 848 foreach my $key (keys %$upd) {
1095 889 ▶ if (ref $upd->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $upd->{$key}) 110 779 889
1096 110 ▶ $rsrc ||= $self->result_source;
Condition33%1/3
$rsrc ||= $self->result_source
l!l&&r!l&&!r
01100
MC/DC0%0/2
$rsrc ||= $self->result_source
$rsrc $self->result_source
Truth table33%1/3
A ||= B
A= $rsrcB= $self->result_source
ABresult
1X1
011
000
1097 110 my $info = $rsrc->relationship_info($key);
1098 110 ▶ my $acc_type = $info->{attrs}{accessor} || '';
Condition100%2/2
$info->{'attrs'}{'accessor'} || ''
l!l
8102
MC/DC100%1/1
$info->{'attrs'}{'accessor'} || ''
$info->{'attrs'}{'accessor'}
Truth table100%2/2
A
A= $info->{'attrs'}{'accessor'}
Aresult
11
00
1099  
1100 110 ▶ if ($acc_type eq 'single') {
Branch: 3 branches
BranchTrueFalseTotal
if ($acc_type eq 'single') { } 2 108 110
elsif ($acc_type eq 'multi') { } 1 107 108
elsif ($rsrc->has_column($key) and exists $rsrc->column_info($key)->{'_inflate_info'}) { } 8 99 107
1101 2 my $rel_obj = delete $upd->{$key};
1102 2 $self->set_from_related($key => $rel_obj);
1103 2 $self->{_relationship_data}{$key} = $rel_obj;
1104 }
1105 elsif ($acc_type eq 'multi') {
1106 1 $self->throw_exception(
1107 "Recursive update is not supported over relationships of type '$acc_type' ($key)"
1108 );
1109 }
1110 ▶ elsif (
Condition66%2/3
$rsrc->has_column($key) and exists $rsrc->column_info($key)->{"_inflate_info"}
!ll&&!rl&&r
0998
MC/DC50%1/2
$rsrc->has_column($key) and exists $rsrc->column_info($key)->{"_inflate_info"}
$rsrc->has_column($key) exists $rsrc->column_info($key)->{"_inflate_info"}
Truth table66%2/3
A and B
A= $rsrc->has_column($key)B= exists $rsrc->column_info($key)->{"_inflate_info"}
ABresult
0X0
100
111
1111 $rsrc->has_column($key)
1112 and
1113 exists $rsrc->column_info($key)->{_inflate_info}
1114 ) {
1115 8 $self->set_inflated_column($key, delete $upd->{$key});
1116 }
1117 }
1118 }
1119 847 $self->set_columns($upd);
1120 }
1121  
1122 =head2 copy
1123  
1124 my $copy = $orig->copy({ change => $to, ... });
1125  
1126 =over
1127  
1128 =item Arguments: \%replacementdata
1129  
1130 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass> copy
1131  
1132 =back
1133  
1134 Inserts a new row into the database, as a copy of the original
1135 object. If a hashref of replacement data is supplied, these will take
1136 precedence over data in the original. Also any columns which have
1137 the L<column info attribute|DBIx::Class::ResultSource/add_columns>
1138 C<< is_auto_increment => 1 >> are explicitly removed before the copy,
1139 so that the database can insert its own autoincremented values into
1140 the new object.
1141  
1142 Relationships will be followed by the copy procedure B<only> if the
1143 relationship specifies a true value for its
1144 L<cascade_copy|DBIx::Class::Relationship::Base> attribute. C<cascade_copy>
1145 is set by default on C<has_many> relationships and unset on all others.
1146  
1147 =cut
1148  
1149 50 ▶sub copy {
Subroutine copy: called CC=10 SCAR=23.0
Pod documented
1150 50 my ($self, $changes) = @_;
1151 50 ▶ $changes ||= {};
Condition50%1/2
$changes ||= {}
l!l
500
MC/DC0%0/1
$changes ||= {}
$changes
Truth table50%1/2
A
A= $changes
Aresult
11
00
1152 50 my $col_data = { $self->get_columns };
1153  
1154 50 my $rsrc = $self->result_source;
1155  
1156 50 my $colinfo = $rsrc->columns_info;
1157 50 foreach my $col (keys %$col_data) {
1158 delete $col_data->{$col}
1159 201 ▶ if ( ! $colinfo->{$col} or $colinfo->{$col}{is_auto_increment} );
Branch: 1 branch
BranchTrueFalseTotal
if not $colinfo->{$col} or $colinfo->{$col}{"is_auto_increment"} 32 169 201
Condition100%3/3
not $colinfo->{$col} or $colinfo->{$col}{"is_auto_increment"}
l!l&&r!l&&!r
131169
MC/DC100%2/2
not $colinfo->{$col} or $colinfo->{$col}{"is_auto_increment"}
not $colinfo->{$col} $colinfo->{$col}{"is_auto_increment"}
Truth table100%3/3
not(A) or B
A= not $colinfo->{$col}B= $colinfo->{$col}{"is_auto_increment"}
ABresult
1X1
011
000
1160 }
1161  
1162 50 my $new = { _column_data => $col_data };
1163 50 bless $new, ref $self;
1164  
1165 50 $new->result_source($rsrc);
1166 50 $new->set_inflated_columns($changes);
1167 50 $new->insert;
1168  
1169 # Its possible we'll have 2 relations to the same Source. We need to make
1170 # sure we don't try to insert the same row twice else we'll violate unique
1171 # constraints
1172 50 my $rel_names_copied = {};
1173  
1174 50 foreach my $rel_name ($rsrc->relationships) {
1175 289 my $rel_info = $rsrc->relationship_info($rel_name);
1176  
1177 289 ▶ next unless $rel_info->{attrs}{cascade_copy};
Branch: 1 branch
BranchTrueFalseTotal
unless $rel_info->{"attrs"}{"cascade_copy"} 247 42 289
1178  
1179 my $resolved = $rsrc->_resolve_condition(
1180 42 $rel_info->{cond}, $rel_name, $new, $rel_name
1181 );
1182  
1183 42 ▶ my $copied = $rel_names_copied->{ $rel_info->{source} } ||= {};
Condition100%2/2
$$rel_names_copied{$rel_info->{"source"}} ||= {}
l!l
438
MC/DC100%1/1
$$rel_names_copied{$rel_info->{"source"}} ||= {}
$$rel_names_copied{$rel_info->{"source"}}
Truth table100%2/2
A
A= $$rel_names_copied{$rel_info->{"source"}}
Aresult
11
00
1184 42 foreach my $related ($self->search_related($rel_name)->all) {
1185 $related->copy($resolved)
1186 52 ▶ unless $copied->{$related->ID}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $$copied{$related->ID}++ 46 6 52
1187 }
1188  
1189 }
1190 50 return $new;
1191 }
1192  
1193 =head2 store_column
1194  
1195 $result->store_column($col => $val);
1196  
1197 =over
1198  
1199 =item Arguments: $columnname, $value
1200  
1201 =item Return Value: The value sent to storage
1202  
1203 =back
1204  
1205 Set a raw value for a column without marking it as changed. This
1206 method is used internally by L</set_column> which you should probably
1207 be using.
1208  
1209 This is the lowest level at which data is set on a result object,
1210 extend this method to catch all data setting methods.
1211  
1212 =cut
1213  
1214 6448 ▶sub store_column {
Subroutine store_column: called CC=4 SCAR=13.9
Pod documented
1215 6448 my ($self, $column, $value) = @_;
1216 $self->throw_exception( "No such column '${column}' on " . ref $self )
1217 6448 ▶ unless exists $self->{_column_data}{$column} || $self->result_source->has_column($column);
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_column_data"}{$column} or $self->result_source->has_column($column) 2 6446 6448
Condition100%3/3
exists $self->{"_column_data"}{$column} or $self->result_source->has_column($column)
l!l&&r!l&&!r
133651102
MC/DC100%2/2
exists $self->{"_column_data"}{$column} or $self->result_source->has_column($column)
exists $self->{"_column_data"}{$column} $self->result_source->has_column($column)
Truth table100%3/3
A or B
A= exists $self->{"_column_data"}{$column}B= $self->result_source->has_column($column)
ABresult
1X1
011
000
1218 6446 ▶ $self->throw_exception( "set_column called for ${column} without value" )
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 3 0 6446 6446
1219 if @_ < 3;
1220 6446 return $self->{_column_data}{$column} = $value;
1221 }
1222  
1223 =head2 inflate_result
1224  
1225 Class->inflate_result($result_source, \%me, \%prefetch?)
1226  
1227 =over
1228  
1229 =item Arguments: L<$result_source|DBIx::Class::ResultSource>, \%columndata, \%prefetcheddata
1230  
1231 =item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
1232  
1233 =back
1234  
1235 All L<DBIx::Class::ResultSet> methods that retrieve data from the
1236 database and turn it into result objects call this method.
1237  
1238 Extend this method in your Result classes to hook into this process,
1239 for example to rebless the result into a different class.
1240  
1241 Reblessing can also be done more easily by setting C<result_class> in
1242 your Result class. See L<DBIx::Class::ResultSource/result_class>.
1243  
1244 Different types of results can also be created from a particular
1245 L<DBIx::Class::ResultSet>, see L<DBIx::Class::ResultSet/result_class>.
1246  
1247 =cut
1248  
1249 9907 ▶sub inflate_result {
Subroutine inflate_result: called CC=14 SCAR=26.6
Pod documented
1250 9907 my ($class, $rsrc, $me, $prefetch) = @_;
1251  
1252 9907 ▶ my $new = bless
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
099070
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
1253 { _column_data => $me, _result_source => $rsrc },
1254 ref $class || $class
1255 ;
1256  
1257 9907 ▶ if ($prefetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefetch) 551 9356 9907
1258 551 for my $rel_name ( keys %$prefetch ) {
1259  
1260 622 ▶ my $relinfo = $rsrc->relationship_info($rel_name) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $relinfo = $rsrc->relationship_info($rel_name) or do { my $err = sprintf("Inflation into non-existent relationship '%s' of '%s' requested", $rel_name, $rsrc->source_name); if (my($colname) = (sort {length $a <=> length $b;} keys %{$prefetch->{$rel_name}[0] or {};})) { $err .= sprintf(q[, check the inflation specification (columns/as) ending in '...%s.%s'], $rel_name, $colname); } ; $rsrc->throw_exception($err) } 620 2 622
1261 2 my $err = sprintf
1262 "Inflation into non-existent relationship '%s' of '%s' requested",
1263 $rel_name,
1264 $rsrc->source_name,
1265 ;
1266 2 ▶ if (my ($colname) = sort { length($a) <=> length ($b) } keys %{$prefetch->{$rel_name}[0] || {}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($colname) = (sort {length $a <=> length $b;} keys %{$prefetch->{$rel_name}[0] or {};})) 2 0 2
Condition50%1/2
$prefetch->{$rel_name}[0] || {}
l!l
20
MC/DC0%0/1
$prefetch->{$rel_name}[0] || {}
$prefetch->{$rel_name}[0]
Truth table50%1/2
A
A= $prefetch->{$rel_name}[0]
Aresult
11
00
1267 2 $err .= sprintf ", check the inflation specification (columns/as) ending in '...%s.%s'",
1268 $rel_name,
1269 $colname,
1270 }
1271  
1272 2 $rsrc->throw_exception($err);
1273 };
1274  
1275 $class->throw_exception("No accessor type declared for prefetched relationship '$rel_name'")
1276 620 ▶ unless $relinfo->{attrs}{accessor};
Branch: 1 branch
BranchTrueFalseTotal
unless $relinfo->{"attrs"}{"accessor"} 0 620 620
1277  
1278 620 my $rel_rs = $new->related_resultset($rel_name);
1279  
1280 620 my @rel_objects;
1281 620 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (@{$prefetch->{$rel_name} or [];} and ref $prefetch->{$rel_name} ne $DBIx::Class::ResultSource::RowParser::Util::null_branch_class) 469 151 620
Condition66%2/3
@{$prefetch->{$rel_name} or [];} and ref $prefetch->{$rel_name} ne $DBIx::Class::ResultSource::RowParser::Util::null_branch_class
!ll&&!rl&&r
1510469
MC/DC50%1/2
@{$prefetch->{$rel_name} or [];} and ref $prefetch->{$rel_name} ne $DBIx::Class::ResultSource::RowParser::Util::null_branch_class
@{$prefetch->{$rel_name} or [];} ref $prefetch->{$rel_name} ne $DBIx::Class::ResultSource::RowParser::Util::null_branch_class
Truth table66%2/3
A and B
A= @{$prefetch->{$rel_name} or [];}B= ref $prefetch->{$rel_name} ne $DBIx::Class::ResultSource::RowParser::Util::null_branch_class
ABresult
0X0
100
111
1282 620 ▶ @{ $prefetch->{$rel_name} || [] }
Condition100%2/2
$prefetch->{$rel_name} || []
l!l
57644
MC/DC100%1/1
$prefetch->{$rel_name} || []
$prefetch->{$rel_name}
Truth table100%2/2
A
A= $prefetch->{$rel_name}
Aresult
11
00
1283 and
1284 ref($prefetch->{$rel_name}) ne $DBIx::Class::ResultSource::RowParser::Util::null_branch_class
1285 ) {
1286  
1287 469 ▶ if (ref $prefetch->{$rel_name}[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $prefetch->{$rel_name}[0] eq 'ARRAY') { } 263 206 469
1288 263 my $rel_rsrc = $rel_rs->result_source;
1289 263 my $rel_class = $rel_rs->result_class;
1290 263 my $rel_inflator = $rel_class->can('inflate_result');
1291 @rel_objects = map
1292 626 { $rel_class->$rel_inflator ( $rel_rsrc, @$_ ) }
1293 263 @{$prefetch->{$rel_name}}
1294 ;
1295 }
1296 else {
1297 @rel_objects = $rel_rs->result_class->inflate_result(
1298 206 $rel_rs->result_source, @{$prefetch->{$rel_name}}
1299 );
1300 }
1301 }
1302  
1303 620 ▶ if ($relinfo->{attrs}{accessor} eq 'single') {
Branch: 2 branches
BranchTrueFalseTotal
if ($relinfo->{'attrs'}{'accessor'} eq 'single') { } 176 444 620
elsif ($relinfo->{'attrs'}{'accessor'} eq 'filter') { } 145 299 444
1304 176 $new->{_relationship_data}{$rel_name} = $rel_objects[0];
1305 }
1306 elsif ($relinfo->{attrs}{accessor} eq 'filter') {
1307 145 $new->{_inflated_column}{$rel_name} = $rel_objects[0];
1308 }
1309  
1310 620 $rel_rs->set_cache(\@rel_objects);
1311 }
1312 }
1313  
1314 9905 $new->in_storage (1);
1315 9905 return $new;
1316 }
1317  
1318 =head2 update_or_insert
1319  
1320 $result->update_or_insert
1321  
1322 =over
1323  
1324 =item Arguments: none
1325  
1326 =item Return Value: Result of update or insert operation
1327  
1328 =back
1329  
1330 L</update>s the object if it's already in the database, according to
1331 L</in_storage>, else L</insert>s it.
1332  
1333 =head2 insert_or_update
1334  
1335 $obj->insert_or_update
1336  
1337 Alias for L</update_or_insert>
1338  
1339 =cut
1340  
1341 0 ▶sub insert_or_update { shift->update_or_insert(@_) }
Subroutine insert_or_update: not called CC=1 SCAR=6.9
Pod documented
1342  
1343 2 ▶sub update_or_insert {
Subroutine update_or_insert: called CC=2 SCAR=6.9
Pod documented
1344 2 my $self = shift;
1345 2 ▶ return ($self->in_storage ? $self->update : $self->insert);
Branch: 1 branch
BranchTrueFalseTotal
$self->in_storage ? : 1 1 2
1346 }
1347  
1348 =head2 is_changed
1349  
1350 my @changed_col_names = $result->is_changed();
1351 if ($result->is_changed()) { ... }
1352  
1353 =over
1354  
1355 =item Arguments: none
1356  
1357 =item Return Value: 0|1 or @columnnames
1358  
1359 =back
1360  
1361 In list context returns a list of columns with uncommited changes, or
1362 in scalar context returns a true value if there are uncommitted
1363 changes.
1364  
1365 =cut
1366  
1367 14 ▶sub is_changed {
Subroutine is_changed: called CC=2 SCAR=6.9
Pod documented
1368 14 ▶ return keys %{shift->{_dirty_columns} || {}};
Condition100%2/2
+(shift())->{'_dirty_columns'} || {}
l!l
122
MC/DC100%1/1
+(shift())->{'_dirty_columns'} || {}
+(shift())->{'_dirty_columns'}
Truth table100%2/2
A
A= +(shift())->{'_dirty_columns'}
Aresult
11
00
1369 }
1370  
1371 =head2 is_column_changed
1372  
1373 if ($result->is_column_changed('col')) { ... }
1374  
1375 =over
1376  
1377 =item Arguments: $columname
1378  
1379 =item Return Value: 0|1
1380  
1381 =back
1382  
1383 Returns a true value if the column has uncommitted changes.
1384  
1385 =cut
1386  
1387 628 ▶sub is_column_changed {
Subroutine is_column_changed: called CC=1 SCAR=0.0
Pod documented
1388 628 my( $self, $col ) = @_;
1389 628 return exists $self->{_dirty_columns}->{$col};
1390 }
1391  
1392 =head2 result_source
1393  
1394 my $resultsource = $result->result_source;
1395  
1396 =over
1397  
1398 =item Arguments: L<$result_source?|DBIx::Class::ResultSource>
1399  
1400 =item Return Value: L<$result_source|DBIx::Class::ResultSource>
1401  
1402 =back
1403  
1404 Accessor to the L<DBIx::Class::ResultSource> this object was created from.
1405  
1406 =cut
1407  
1408 31360 ▶sub result_source {
Subroutine result_source: called CC=5 SCAR=21.9
Pod documented
1409 31360 ▶ $_[0]->throw_exception( 'result_source can be called on instances only' )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 31360 31360
1410 unless ref $_[0];
1411  
1412 @_ > 1
1413 ? $_[0]->{_result_source} = $_[1]
1414  
1415 # note this is a || not a ||=, the difference is important
1416 31360 ▶ : $_[0]->{_result_source} || do {
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1544 29816 31360
Condition33%1/3
$_[0]{"_result_source"} or do { my $class = ref $_[0]; $_[0]->can("result_source_instance") ? $_[0]->result_source_instance : $_[0]->throw_exception("No result source instance registered for $class, did you forget to call $class->table(...) ?") }
l!l&&r!l&&!r
2981600
MC/DC0%0/2
$_[0]{"_result_source"} or do { my $class = ref $_[0]; $_[0]->can("result_source_instance") ? $_[0]->result_source_instance : $_[0]->throw_exception("No result source instance registered for $class, did you forget to call $class->table(...) ?") }
$_[0]{"_result_source"} do { my $class = ref $_[0]; $_[0]->can("result_source_instance") ? $_[0]->result_source_instance : $_[0]->throw_exception("No result source instance registered for $class, did you forget to call $class->table(...) ?") }
Truth table33%1/3
A or B
A= $_[0]{"_result_source"}B= do { my $class = ref $_[0]; $_[0]->can("result_source_instance") ? $_[0]->result_source_instance : $_[0]->throw_exception("No result source instance registered for $class, did you forget to call $class->table(...) ?") }
ABresult
1X1
011
000
1417 0 my $class = ref $_[0];
1418 0 ▶ $_[0]->can('result_source_instance')
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->can('result_source_instance') ? : 0 0 0
1419 ? $_[0]->result_source_instance
1420 : $_[0]->throw_exception(
1421 "No result source instance registered for $class, did you forget to call $class->table(...) ?"
1422 )
1423 }
1424 ;
1425 }
1426  
1427 =head2 register_column
1428  
1429 $column_info = { .... };
1430 $class->register_column($column_name, $column_info);
1431  
1432 =over
1433  
1434 =item Arguments: $columnname, \%columninfo
1435  
1436 =item Return Value: not defined
1437  
1438 =back
1439  
1440 Registers a column on the class. If the column_info has an 'accessor'
1441 key, creates an accessor named after the value if defined; if there is
1442 no such key, creates an accessor with the same name as the column
1443  
1444 The column_info attributes are described in
1445 L<DBIx::Class::ResultSource/add_columns>
1446  
1447 =cut
1448  
1449 45408 ▶sub register_column {
Subroutine register_column: called CC=3 SCAR=11.0
Pod documented
1450 45408 my ($class, $col, $info) = @_;
1451 45408 my $acc = $col;
1452 45408 ▶ if (exists $info->{accessor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $info->{"accessor"}) 999 44409 45408
1453 999 ▶ return unless defined $info->{accessor};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"accessor"} 336 663 999
1454 663 $acc = [ $info->{accessor}, $col ];
1455 }
1456 45072 $class->mk_group_accessors('column' => $acc);
1457 }
1458  
1459 =head2 get_from_storage
1460  
1461 my $copy = $result->get_from_storage($attrs)
1462  
1463 =over
1464  
1465 =item Arguments: \%attrs
1466  
1467 =item Return Value: A Result object
1468  
1469 =back
1470  
1471 Fetches a fresh copy of the Result object from the database and returns it.
1472 Throws an exception if a proper WHERE clause identifying the database row
1473 can not be constructed (i.e. if the original object does not contain its
1474 entire
1475 L<primary key|DBIx::Class::Manual::Intro/The Significance and Importance of Primary Keys>
1476 ). If passed the \%attrs argument, will first apply these attributes to
1477 the resultset used to find the row.
1478  
1479 This copy can then be used to compare to an existing result object, to
1480 determine if any changes have been made in the database since it was
1481 created.
1482  
1483 To just update your Result object with any latest changes from the
1484 database, use L</discard_changes> instead.
1485  
1486 The \%attrs argument should be compatible with
1487 L<DBIx::Class::ResultSet/ATTRIBUTES>.
1488  
1489 =cut
1490  
1491 312 ▶sub get_from_storage {
Subroutine get_from_storage: called CC=2 SCAR=6.9
Pod documented
1492 312 my $self = shift @_;
1493 312 my $attrs = shift @_;
1494 312 my $resultset = $self->result_source->resultset;
1495  
1496 312 ▶ if(defined $attrs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attrs) 309 3 312
1497 309 $resultset = $resultset->search(undef, $attrs);
1498 }
1499  
1500 312 return $resultset->find($self->_storage_ident_condition);
1501 }
1502  
1503 =head2 discard_changes
1504  
1505 $result->discard_changes
1506  
1507 =over
1508  
1509 =item Arguments: none or $attrs
1510  
1511 =item Return Value: self (updates object in-place)
1512  
1513 =back
1514  
1515 Re-selects the row from the database, losing any changes that had
1516 been made. Throws an exception if a proper C<WHERE> clause identifying
1517 the database row can not be constructed (i.e. if the original object
1518 does not contain its entire
1519 L<primary key|DBIx::Class::Manual::Intro/The Significance and Importance of Primary Keys>).
1520  
1521 This method can also be used to refresh from storage, retrieving any
1522 changes made since the row was last read from storage.
1523  
1524 $attrs, if supplied, is expected to be a hashref of attributes suitable for passing as the
1525 second argument to C<< $resultset->search($cond, $attrs) >>;
1526  
1527 Note: If you are using L<DBIx::Class::Storage::DBI::Replicated> as your
1528 storage, a default of
1529 L<< C<< { force_pool => 'master' } >>
1530 |DBIx::Class::Storage::DBI::Replicated/SYNOPSIS >> is automatically set for
1531 you. Prior to C<< DBIx::Class 0.08109 >> (before 2010) one would have been
1532 required to explicitly wrap the entire operation in a transaction to guarantee
1533 that up-to-date results are read from the master database.
1534  
1535 =cut
1536  
1537 309 ▶sub discard_changes {
Subroutine discard_changes: called CC=4 SCAR=15.0
Pod documented
1538 309 my ($self, $attrs) = @_;
1539 309 ▶ return unless $self->in_storage; # Don't reload if we aren't real!
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in_storage 0 309 309
1540  
1541 # add a replication default to read from the master only
1542 309 ▶ $attrs = { force_pool => 'master', %{$attrs||{}} };
Condition50%1/2
$attrs || {}
l!l
0309
MC/DC0%0/1
$attrs || {}
$attrs
Truth table50%1/2
A
A= $attrs
Aresult
11
00
1543  
1544 309 ▶ if( my $current_storage = $self->get_from_storage($attrs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current_storage = $self->get_from_storage($attrs)) { } 308 0 308
1545  
1546 # Set $self to the current.
1547 308 %$self = %$current_storage;
1548  
1549 # Avoid a possible infinite loop with
1550 # sub DESTROY { $_[0]->discard_changes }
1551 308 bless $current_storage, 'Do::Not::Exist';
1552  
1553 308 return $self;
1554 }
1555 else {
1556 0 $self->in_storage(0);
1557 0 return $self;
1558 }
1559 }
1560  
1561 =head2 throw_exception
1562  
1563 See L<DBIx::Class::Schema/throw_exception>.
1564  
1565 =cut
1566  
1567 61 ▶sub throw_exception {
Subroutine throw_exception: called CC=3 SCAR=11.0
Pod documented
1568 61 my $self=shift;
1569  
1570 61 ▶ if (ref $self && ref (my $rsrc = try { $self->result_source_instance } ) ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and ref(my $rsrc = &try(sub { $self->result_source_instance; } ))) { } 11 50 61
Condition100%3/3
ref $self and ref(my $rsrc = &try(sub { $self->result_source_instance; } ))
!ll&&!rl&&r
49111
MC/DC100%2/2
ref $self and ref(my $rsrc = &try(sub { $self->result_source_instance; } ))
ref $self ref(my $rsrc = &try(sub { $self->result_source_instance; } ))
Truth table100%3/3
A and B
A= ref $selfB= ref(my $rsrc = &try(sub { $self->result_source_instance; } ))
ABresult
0X0
100
111
1571 11 $rsrc->throw_exception(@_)
1572 }
1573 else {
1574 50 DBIx::Class::Exception->throw(@_);
1575 }
1576 }
1577  
1578 =head2 id
1579  
1580 my @pk = $result->id;
1581  
1582 =over
1583  
1584 =item Arguments: none
1585  
1586 =item Returns: A list of primary key values
1587  
1588 =back
1589  
1590 Returns the primary key(s) for a row. Can't be called as a class method.
1591 Actually implemented in L<DBIx::Class::PK>
1592  
1593 =head1 FURTHER QUESTIONS?
1594  
1595 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
1596  
1597 =head1 COPYRIGHT AND LICENSE
1598  
1599 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
1600 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
1601 redistribute it and/or modify it under the same terms as the
1602 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
1603  
1604 =cut
1605  
1606 379 1;
« DBIx/Class/Relationship/BelongsTo.pm ↑ Summary DBIx/Class/ResultSource/RowParser/Util.pm »