DBI/Record.pm

filter: Statementstmt 84.7% 523 / 617 Branchbran 71.8% 194 / 270 Conditioncond 67.6% 96 / 142 MC/DCmcdc 39.2% 31 / 79 Subroutinesub 83.9% 68 / 81 Podpod 100% 28 / 28 totaltotal 77.2% 940 / 1217 CC 201 SCAR 23.0
CC
201
Cov
79%
load_by_cols
25.0
__create
22.9
_init_methods_for_column
20.5
CRAP 9.9
« DBI/Collection/Union.pm ↑ Summary DBI/Handle/ODBC.pm »
1 package Jifty::DBI::Record;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 30 ▶use Class::ReturnValue ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Lingua::EN::Inflect ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Jifty::DBI::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use UNIVERSAL::require ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use Class::Trigger; # exports by default
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use Scalar::Defer 'force';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 30 use base qw/
15 Class::Data::Inheritable
16 Jifty::DBI::HasFilters
17 30 ▶ /;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 30 our $VERSION = '0.01';
20  
21 30 Jifty::DBI::Record->mk_classdata('COLUMNS');
22 30 Jifty::DBI::Record->mk_classdata('TABLE_NAME');
23 30 Jifty::DBI::Record->mk_classdata('_READABLE_COLS_CACHE');
24 30 Jifty::DBI::Record->mk_classdata('_WRITABLE_COLS_CACHE');
25 30 Jifty::DBI::Record->mk_classdata('_COLUMNS_CACHE');
26 30 Jifty::DBI::Record->mk_classdata('RECORD_MIXINS' => []);
27  
28 =head1 NAME
29  
30 Jifty::DBI::Record - Superclass for records loaded by Jifty::DBI::Collection
31  
32 =head1 SYNOPSIS
33  
34 package MyRecord;
35 use base qw/Jifty::DBI::Record/;
36  
37 =head1 DESCRIPTION
38  
39 Jifty::DBI::Record encapsulates records and tables as part of the L<Jifty::DBI>
40 object-relational mapper.
41  
42 =head1 METHODS
43  
44 =head2 new ARGS
45  
46 Instantiate a new, empty record object.
47  
48 ARGS is a hash used to pass parameters to the C<_init()> function.
49  
50 Unless it is overloaded, the _init() function expects one key of
51 'handle' with a value containing a reference to a Jifty::DBI::Handle
52 object.
53  
54 =cut
55  
56 sub new {
57 367 ▶ my $proto = shift;
Subroutine new: called CC=4 SCAR=14.7
Pod documented
58  
59 367 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
53620
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
60 367 my $self = {};
61 367 bless( $self, $class );
62  
63 367 ▶ $self->_init_columns() unless $self->COLUMNS;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->COLUMNS 0 367 367
64 367 $self->input_filters('Jifty::DBI::Filter::Truncate');
65  
66 367 ▶ if ( scalar(@_) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 367 367
67 0 Carp::cluck(
68 "new(\$handle) is deprecated, use new( handle => \$handle )");
69 0 $self->_init( handle => shift );
70 } else {
71 367 $self->_init(@_);
72 }
73  
74 367 return $self;
75 }
76  
77 # Not yet documented here. Should almost certainly be overloaded.
78 sub _init {
79 367 ▶ my $self = shift;
Subroutine _init: called CC=2 SCAR=6.9
80 367 my %args = (@_);
81 367 ▶ if ( $args{'handle'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"handle"}) 353 14 367
82 353 $self->_handle( $args{'handle'} );
83 }
84  
85 }
86  
87 sub import {
88 47 ▶ my $class = shift;
Subroutine import: called CC=6 SCAR=18.0
89 47 my ($flag) = @_;
90 47 ▶ if ( $class->isa(__PACKAGE__) and defined $flag and $flag eq '-base' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa(__PACKAGE__) and defined $flag and $flag eq "-base") 42 5 47
Condition66%4/6
$class->isa(__PACKAGE__) and defined $flag and $flag eq "-base"
!ll&&!rl&&r
5042
$class->isa(__PACKAGE__) and defined $flag
!ll&&!rl&&r
0542
MC/DC33%1/3
$class->isa(__PACKAGE__) and defined $flag and $flag eq "-base"
$class->isa(__PACKAGE__) defined $flag $flag eq "-base"
Truth table50%2/4
A and B and C
A= $class->isa(__PACKAGE__)B= defined $flagC= $flag eq "-base"
ABCresult
0XX0
10X0
1100
1111
91 42 my $descendant = (caller)[0];
92 42 ▶ unless ( $descendant->isa($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($descendant->isa($class)) 8 34 42
93 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
94 8 push @{ $descendant . '::ISA' }, $class
95 }
96 42 shift;
97  
98 # run the schema callback
99 42 my $callback = shift;
100 42 ▶ $callback->() if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 42 0 42
101 }
102 47 $class->SUPER::import(@_);
103  
104 # Turn off redefinition warnings in the caller's scope
105 47 @_ = ( warnings => 'redefine' );
106 47 goto &warnings::unimport;
107 }
108  
109 =head2 id
110  
111 Returns this row's primary key.
112  
113 =cut
114  
115 sub id {
116 130 ▶ my $pkey = $_[0]->_primary_key();
Subroutine id: called CC=1 SCAR=0.0
Pod documented
117 130 my $ret = $_[0]->{'values'}->{$pkey};
118 130 return $ret;
119 }
120  
121 =head2 primary_keys
122  
123 Return a hash of the values of our primary keys for this function.
124  
125 =cut
126  
127 sub primary_keys {
128 189 ▶ my $self = shift;
Subroutine primary_keys: called CC=1 SCAR=0.0
Pod documented
129 my %hash
130 189 = map { $_ => $self->{'values'}->{$_} } @{ $self->_primary_keys };
131 189 return (%hash);
132 }
133  
134 =head2 _accessible COLUMN ATTRIBUTE
135  
136 Private method.
137  
138 DEPRECATED
139  
140 Returns undef unless C<COLUMN> has a true value for C<ATTRIBUTE>.
141  
142 Otherwise returns C<COLUMN>'s value for that attribute.
143  
144  
145 =cut
146  
147 sub _accessible {
148 4 ▶ my $self = shift;
Subroutine _accessible: called CC=4 SCAR=13.9
149 4 my $column_name = shift;
150 4 ▶ my $attribute = lc( shift || '' );
Condition100%2/2
shift() || ''
l!l
31
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
151 4 my $col = $self->column($column_name);
152 4 ▶ return undef unless ( $col and $col->can($attribute) );
Branch: 1 branch
BranchTrueFalseTotal
unless $col and $col->can($attribute) 2 2 4
Condition100%3/3
$col and $col->can($attribute)
!ll&&!rl&&r
112
MC/DC100%2/2
$col and $col->can($attribute)
$col $col->can($attribute)
Truth table100%3/3
A and B
A= $colB= $col->can($attribute)
ABresult
0X0
100
111
153 2 return $col->$attribute();
154  
155 }
156  
157 =head2 _primary_keys
158  
159 Return our primary keys. (Subclasses should override this, but our
160 default is that we have one primary key, named 'id'.)
161  
162 =cut
163  
164 sub _primary_keys {
165 368 ▶ my $self = shift;
Subroutine _primary_keys: called CC=1 SCAR=0.0
166 368 return ['id'];
167 }
168  
169 sub _primary_key {
170 133 ▶ my $self = shift;
Subroutine _primary_key: called CC=4 SCAR=15.2
171 133 my $pkeys = $self->_primary_keys();
172 133 ▶ die "No primary key" unless ( ref($pkeys) eq 'ARRAY' and $pkeys->[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pkeys eq "ARRAY" and $pkeys->[0] 0 133 133
Condition33%1/3
ref $pkeys eq "ARRAY" and $pkeys->[0]
!ll&&!rl&&r
00133
MC/DC0%0/2
ref $pkeys eq "ARRAY" and $pkeys->[0]
ref $pkeys eq "ARRAY" $pkeys->[0]
Truth table33%1/3
A and B
A= ref $pkeys eq "ARRAY"B= $pkeys->[0]
ABresult
0X0
100
111
173 133 ▶ die "Too many primary keys" unless ( scalar(@$pkeys) == 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$pkeys == 1 0 133 133
174 133 return $pkeys->[0];
175 }
176  
177 =head2 _init_columns
178  
179 Sets up the primary key columns.
180  
181 =cut
182  
183 sub _init_columns {
184 42 ▶ my $self = shift;
Subroutine _init_columns: called CC=3 SCAR=11.0
185  
186 42 ▶ return if defined $self->COLUMNS;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->COLUMNS 1 41 42
187  
188 41 $self->COLUMNS( {} );
189  
190 41 foreach my $column_name ( @{ $self->_primary_keys } ) {
191 41 my $column = $self->add_column($column_name);
192 41 $column->writable(0);
193 41 $column->readable(1);
194 41 $column->type('serial');
195 41 $column->mandatory(1);
196  
197 41 $self->_init_methods_for_column($column);
198 }
199  
200 }
201  
202 =head2 _init_methods_for_columns
203  
204 This is an internal method responsible for calling
205 L</_init_methods_for_column> for each column that has been configured.
206  
207 =cut
208  
209 sub _init_methods_for_columns {
210 42 ▶ my $self = shift;
Subroutine _init_methods_for_columns: called CC=3 SCAR=11.1
211  
212 42 ▶ for my $column ( sort keys %{ $self->COLUMNS || {} } ) {
Branch: 1 branch
BranchTrueFalseTotal
$self->COLUMNS or {} 0 42 42
213 147 $self->_init_methods_for_column( $self->COLUMNS->{$column} );
214 }
215 }
216  
217 =head2 schema_version
218  
219 If present, this method must return a string in '1.2.3' format to be
220 used to determine which columns are currently active in the
221 schema. That is, this value is used to determine which columns are
222 defined, based upon comparison to values set in C<till> and C<since>.
223  
224 If no implementation is present, the "latest" schema version is
225 assumed, meaning that any column defining a C<till> is not active and
226 all others are.
227  
228 =head2 _init_methods_for_column COLUMN
229  
230 This method is used internally to update the symbol table for the
231 record class to include an accessor and mutator for each column based
232 upon the column's name.
233  
234 In addition, if your record class defines the method
235 L</schema_version>, it will automatically generate methods according
236 to whether the column currently exists for the current application
237 schema version returned by that method. The C<schema_version> method
238 must return a value in the same form used by C<since> and C<till>.
239  
240 If the column doesn't currently exist, it will create the methods, but
241 they will die with an error message stating that the column does not
242 exist for the current version of the application. If it does exist, a
243 normal accessor and mutator will be created.
244  
245 See also L<Jifty::DBI::Column/active>, L<Jifty::DBI::Schema/since>,
246 L<Jifty::DBI::Schema/till> for more information.
247  
248 =cut
249  
250 sub _init_methods_for_column {
251 195 ▶ my $self = $_[0];
Subroutine _init_methods_for_column: called CC=18 SCAR=30.2
252 195 my $column = $_[1];
253 195 ▶ my $column_name
Branch: 1 branch
BranchTrueFalseTotal
$column->aliased_as ? : 12 183 195
254 = ( $column->aliased_as ? $column->aliased_as : $column->name );
255 195 ▶ my $package = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
01950
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
256  
257 # Make sure column has a record_class set as not all columns are added
258 # through add_column
259 195 ▶ $column->record_class($package) if not $column->record_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $column->record_class 102 93 195
260  
261 # Check for the correct column type when the Storable filter is in use
262 195 ▶ if ( grep { $_ eq 'Jifty::DBI::Filter::Storable' }
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "Jifty::DBI::Filter::Storable";} $column->input_filters, $column->output_filters and not grep({$_ eq "Jifty::DBI::Filter::base64";} $column->input_filters, $column->output_filters) and not $column->type =~ /^(blob|bytea)$/i) 0 195 195
Condition66%4/6
grep {$_ eq "Jifty::DBI::Filter::Storable";} $column->input_filters, $column->output_filters and not grep({$_ eq "Jifty::DBI::Filter::base64";} $column->input_filters, $column->output_filters) and not $column->type =~ /^(blob|bytea)$/i
!ll&&!rl&&r
19410
grep {$_ eq "Jifty::DBI::Filter::Storable";} $column->input_filters, $column->output_filters and not grep({$_ eq "Jifty::DBI::Filter::base64";} $column->input_filters, $column->output_filters)
!ll&&!rl&&r
19401
MC/DC0%0/3
grep {$_ eq "Jifty::DBI::Filter::Storable";} $column->input_filters, $column->output_filters and not grep({$_ eq "Jifty::DBI::Filter::base64";} $column->input_filters, $column->output_filters) and not $column->type =~ /^(blob|bytea)$/i
grep {$_ eq "Jifty::DBI::Filter::Storable";} $column->input_filters, $column->output_filters not grep({$_ eq "Jifty::DBI::Filter::base64";} $column->input_filters, $column->output_filters) not $column->type =~ /^(blob|bytea)$/i
Truth table50%2/4
A and not(B) and not(C)
A= grep {$_ eq "Jifty::DBI::Filter::Storable";} $column->input_filters, $column->output_filtersB= not grep({$_ eq "Jifty::DBI::Filter::base64";} $column->input_filters, $column->output_filters)C= not $column->type =~ /^(blob|bytea)$/i
ABCresult
0XX0
10X0
1100
1111
263 ( $column->input_filters, $column->output_filters )
264 2 and not grep { $_ eq 'Jifty::DBI::Filter::base64' }
265 ( $column->input_filters, $column->output_filters )
266 and $column->type !~ /^(blob|bytea)$/i )
267 {
268 0 die "Column '$column_name' in @{[$column->record_class]} "
269 . "uses the Storable filter but is not of type 'blob'.\n";
270 }
271  
272 30 ▶ no strict 'refs'; # We're going to be defining subs
Subroutine BEGIN: called CC=1 SCAR=0.0
273  
274 195 ▶ if ( not $self->can($column_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($column_name)) 101 94 195
275 # Accessor
276 101 my $subref;
277  
278 101 ▶ if ($column->computed) {
Branch: 2 branches
BranchTrueFalseTotal
if ($column->computed) { } 2 99 101
elsif ($column->active) { } 99 0 99
279 $subref = sub {
280 0 ▶ Carp::croak("column '$column_name' in $package is computed but has no corresponding method");
Subroutine __ANON__: not called CC=1 SCAR=6.9
281 2 };
282 }
283 elsif ( $column->active ) {
284  
285 99 ▶ if ( $column->readable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->readable) { } 99 0 99
286 99 ▶ if (UNIVERSAL::isa(
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($column->refers_to, 'Jifty::DBI::Record')) { } 6 93 99
elsif (UNIVERSAL::isa($column->refers_to, 'Jifty::DBI::Collection')) { } 2 91 93
287 $column->refers_to, "Jifty::DBI::Record"
288 )
289 )
290 {
291 $subref = sub {
292 25 ▶ if ( @_ > 1 ) {
Subroutine __ANON__: called CC=2 SCAR=8.1
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 25 25
293 0 Carp::carp
294 "Value passed to column $column_name accessor. You probably want to use the mutator.";
295 }
296 # This should be using _value, so we acl_check
297 # appropriately, except the acl checks often
298 # involve object references. So even if you
299 # don't have rights to $object->foo_id,
300 # $object->foo->id will always have to
301 # work. :/
302 25 $_[0]->_to_record( $column_name,
303 $_[0]->__value($column_name) );
304 6 };
305 } elsif (
306 UNIVERSAL::isa(
307 $column->refers_to, "Jifty::DBI::Collection"
308 )
309 )
310 {
311 2 ▶ $subref = sub { $_[0]->_collection_value($column_name) };
Subroutine __ANON__: called CC=1 SCAR=0.0
312 } else {
313 $subref = sub {
314 231 ▶ if ( @_ > 1 ) {
Subroutine __ANON__: called CC=2 SCAR=8.1
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 231 231
315 0 Carp::carp
316 "Value passed to column $column_name accessor. You probably want to use the mutator.";
317 }
318 231 return ( $_[0]->_value($column_name) );
319 91 };
320 }
321 } else {
322 0 ▶ $subref = sub { return '' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
323 0 }
324 } else {
325  
326 # XXX sterling: should this be done with Class::ReturnValue instead
327 $subref = sub {
328 0 ▶ Carp::croak(
Subroutine __ANON__: not called CC=1 SCAR=6.9
329 "column $column_name is not available for $package for schema version "
330 . $self->schema_version );
331 0 };
332 }
333 101 *{ $package . "::" . $column_name } = $subref;
334  
335 }
336  
337 195 ▶ if ( not $self->can( "set_" . $column_name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->can('set_' . $column_name)) 142 53 195
338  
339 # Mutator
340 142 my $subref;
341 142 ▶ if ( $column->active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->active) { } 142 0 142
342 142 ▶ if ( $column->writable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->writable) { } 101 41 142
343 101 ▶ if (UNIVERSAL::isa(
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($column->refers_to, 'Jifty::DBI::Record')) { } 6 95 101
elsif (UNIVERSAL::isa($column->refers_to, 'Jifty::DBI::Collection')) { } 2 93 95
344 $column->refers_to, "Jifty::DBI::Record"
345 )
346 )
347 {
348 $subref = sub {
349 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
350 4 my $val = shift;
351  
352 4 ▶ if (UNIVERSAL::isa( $val, 'Jifty::DBI::Record' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, "Jifty::DBI::Record")) 2 2 4
353 2 my $col = $self->column($column_name);
354 2 ▶ my $by = defined $col->by ? $col->by : 'id';
Branch: 1 branch
BranchTrueFalseTotal
defined $col->by ? : 2 0 2
355 2 $val = $val->$by;
356 }
357  
358 return (
359 4 $self->_set(
360 column => $column_name,
361 value => $val
362 )
363 );
364 6 };
365 } elsif (
366 UNIVERSAL::isa(
367 $column->refers_to, "Jifty::DBI::Collection"
368 )
369 )
370 { # XXX elw: collections land here, now what?
371 2 my $ret = Class::ReturnValue->new();
372 2 my $message
373 = "Collection column '$column_name' not writable";
374 2 $ret->as_array( 0, $message );
375 2 $ret->as_error(
376 errno => 3,
377 do_backtrace => 0,
378 message => $message
379 );
380 2 ▶ $subref = sub { return ( $ret->return_value ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
381 } else {
382 $subref = sub {
383 return (
384 56 ▶ $_[0]->_set(
Subroutine __ANON__: called CC=1 SCAR=0.0
385 column => $column_name,
386 value => $_[1]
387 )
388 );
389 93 };
390 }
391 } else {
392 41 my $ret = Class::ReturnValue->new();
393 41 my $message = 'Immutable column';
394 41 $ret->as_array( 0, $message );
395 41 $ret->as_error(
396 errno => 3,
397 do_backtrace => 0,
398 message => $message
399 );
400 41 ▶ $subref = sub { return ( $ret->return_value ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
401 }
402 } else {
403  
404 # XXX sterling: should this be done with Class::ReturnValue instead
405 $subref = sub {
406 0 ▶ Carp::croak(
Subroutine __ANON__: not called CC=1 SCAR=6.9
407 "column $column_name is not available for $package for schema version "
408 . $self->schema_version );
409 0 };
410 }
411 142 *{ $package . "::" . "set_" . $column_name } = $subref;
412 }
413 }
414  
415 =head2 null_reference
416  
417 By default, Jifty::DBI::Record will return C<undef> for non-existent
418 foreign references which don't exist. That is, if each Employee
419 C<refers_to> a Department, but isn't required to,
420 C<<$model->department>> will return C<undef> for employees not in a
421 department.
422  
423 Overriding this method to return 0 will cause it to return a record
424 with no id. That is, C<<$model->department>> will return a Department
425 object, but C<<$model->department->id>> will be C<undef>.
426  
427 =cut
428  
429 sub null_reference {
430 3 ▶ return 1;
Subroutine null_reference: called CC=1 SCAR=0.0
Pod documented
431 }
432  
433 =head2 _to_record COLUMN VALUE
434  
435 This B<PRIVATE> method takes a column name and a value for that column.
436  
437 It returns C<undef> unless C<COLUMN> is a valid column for this record
438 that refers to another record class.
439  
440 If it is valid, this method returns a new record object with an id
441 of C<VALUE>.
442  
443 =cut
444  
445 sub _to_record {
446 25 ▶ my $self = shift;
Subroutine _to_record: called CC=8 SCAR=20.9
447 25 my $column_name = shift;
448 25 my $value = shift;
449  
450 25 my $column = $self->column($column_name);
451 25 my $classname = $column->refers_to();
452 25 ▶ my $remote_column = $column->by() || 'id';
Condition50%1/2
$column->by || 'id'
l!l
250
MC/DC0%0/1
$column->by || 'id'
$column->by
Truth table50%1/2
A
A= $column->by
Aresult
11
00
453  
454 25 ▶ return undef if not defined $value and $self->null_reference;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and $self->null_reference 3 22 25
Condition100%3/3
not defined $value and $self->null_reference
!ll&&!rl&&r
2023
MC/DC100%2/2
not defined $value and $self->null_reference
not defined $value $self->null_reference
Truth table100%3/3
not(A) and B
A= not defined $valueB= $self->null_reference
ABresult
0X0
100
111
455 22 ▶ return undef unless $classname;
Branch: 1 branch
BranchTrueFalseTotal
unless $classname 0 22 22
456 22 ▶ return unless UNIVERSAL::isa( $classname, 'Jifty::DBI::Record' );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($classname, "Jifty::DBI::Record") 0 22 22
457  
458 22 ▶ if ( my $prefetched = $self->prefetched($column_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prefetched = $self->prefetched($column_name)) 1 21 22
459 1 return $prefetched;
460 }
461  
462 21 my $object = $classname->new( $self->_new_record_args );
463 21 ▶ $object->load_by_cols( $remote_column => $value ) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 19 2 21
464 21 return $object;
465 }
466  
467 sub _new_record_args {
468 26 ▶ my $self = shift;
Subroutine _new_record_args: called CC=1 SCAR=0.0
469 26 return ( handle => $self->_handle );
470 }
471  
472 sub _collection_value {
473 7 ▶ my $self = shift;
Subroutine _collection_value: called CC=6 SCAR=18.6
474 7 my $column_name = shift;
475  
476 7 my $column = $self->column($column_name);
477 7 my $classname = $column->refers_to();
478  
479 7 ▶ return undef unless $classname;
Branch: 1 branch
BranchTrueFalseTotal
unless $classname 0 7 7
480 7 ▶ return unless UNIVERSAL::isa( $classname, 'Jifty::DBI::Collection' );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($classname, "Jifty::DBI::Collection") 0 7 7
481  
482 7 ▶ if ( my $prefetched = $self->prefetched($column_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prefetched = $self->prefetched($column_name)) 4 3 7
483 4 return $prefetched;
484 }
485  
486 3 my $coll = $classname->new( $self->_new_collection_args );
487 3 ▶ $coll->limit( column => $column->by, value => $self->id )
Branch: 1 branch
BranchTrueFalseTotal
if $column->by and $self->id 3 0 3
Condition33%1/3
$column->by and $self->id
!ll&&!rl&&r
003
MC/DC0%0/2
$column->by and $self->id
$column->by $self->id
Truth table33%1/3
A and B
A= $column->byB= $self->id
ABresult
0X0
100
111
488 if $column->by and $self->id;
489 3 return $coll;
490 }
491  
492 sub _new_collection_args {
493 3 ▶ my $self = shift;
Subroutine _new_collection_args: called CC=1 SCAR=0.0
494 3 return ( handle => $self->_handle );
495 }
496  
497 =head2 prefetched NAME
498  
499 Returns the prefetched value for column of property C<NAME>, if it
500 exists.
501  
502 =cut
503  
504 sub prefetched {
505 39 ▶ my $self = shift;
Subroutine prefetched: called CC=6 SCAR=21.9
Pod documented
506 39 my $column_name = shift;
507 39 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 10 29 39
508 10 my $column = $self->column($column_name);
509 10 ▶ if ( $column and not $column->refers_to ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($column and not $column->refers_to) { } 0 10 10
elsif ($column and not UNIVERSAL::isa($_[0], $column->refers_to)) { } 0 10 10
Condition33%1/3
$column and not $column->refers_to
!ll&&!rl&&r
0100
MC/DC0%0/2
$column and not $column->refers_to
$column not $column->refers_to
Truth table33%1/3
A and not(B)
A= $columnB= not $column->refers_to
ABresult
0X0
100
111
510 0 warn "$column_name isn't supposed to be an object reference!";
511 0 return;
512 ▶ } elsif ( $column
Condition33%1/3
$column and not UNIVERSAL::isa($_[0], $column->refers_to)
!ll&&!rl&&r
0100
MC/DC0%0/2
$column and not UNIVERSAL::isa($_[0], $column->refers_to)
$column not UNIVERSAL::isa($_[0], $column->refers_to)
Truth table33%1/3
A and not(B)
A= $columnB= not UNIVERSAL::isa($_[0], $column->refers_to)
ABresult
0X0
100
111
513 and not UNIVERSAL::isa( $_[0], $column->refers_to ) )
514 {
515 0 warn "$column_name is supposed to be a @{[$column->refers_to]}!";
516 } else {
517 10 $self->{'_prefetched'}->{$column_name} = shift;
518 }
519 } else {
520 29 return $self->{'_prefetched'}->{$column_name};
521 }
522 }
523  
524 =head2 add_column
525  
526 =cut
527  
528 sub add_column {
529 47 ▶ my $self = shift;
Subroutine add_column: called CC=3 SCAR=11.2
Pod documented
530 47 my $name = shift;
531  
532 #$name = lc $name;
533  
534 $self->COLUMNS->{$name} = Jifty::DBI::Column->new()
535 47 ▶ unless exists $self->COLUMNS->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->COLUMNS->{$name} 47 0 47
536 47 $self->_READABLE_COLS_CACHE(undef);
537 47 $self->_WRITABLE_COLS_CACHE(undef);
538 47 $self->_COLUMNS_CACHE(undef);
539 47 $self->COLUMNS->{$name}->name($name);
540  
541 47 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0470
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
542 47 $self->COLUMNS->{$name}->record_class($class);
543  
544 47 return $self->COLUMNS->{$name};
545 }
546  
547 =head2 column
548  
549 my $column = $self->column($column_name);
550  
551 Returns the L<Jifty::DBI::Column> object of the specified column name.
552  
553 =cut
554  
555 sub column {
556 689 ▶ my $self = shift;
Subroutine column: called CC=4 SCAR=14.0
Pod documented
557 689 ▶ my $name = ( shift || '' );
Condition50%1/2
shift() || ''
l!l
6890
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
558 689 my $col = $self->_columns_hashref;
559 689 ▶ return undef unless $col && exists $col->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $col and exists $col->{$name} 9 680 689
Condition66%2/3
$col and exists $col->{$name}
!ll&&!rl&&r
09680
MC/DC50%1/2
$col and exists $col->{$name}
$col exists $col->{$name}
Truth table66%2/3
A and B
A= $colB= exists $col->{$name}
ABresult
0X0
100
111
560 680 return $col->{$name};
561  
562 }
563  
564 =head2 columns
565  
566 my @columns = $record->columns;
567  
568 Returns a sorted list of a $record's @columns.
569  
570 =cut
571  
572 sub columns {
573 497 ▶ my $self = shift;
Subroutine columns: called CC=2 SCAR=6.9
Pod documented
574 return @{
575 497 $self->_COLUMNS_CACHE() || $self->_COLUMNS_CACHE(
576 497 ▶ [ grep { $_->active } $self->all_columns ]
Branch: 1 branch
BranchTrueFalseTotal
$self->_COLUMNS_CACHE or $self->_COLUMNS_CACHE([grep({$_->active;} $self->all_columns)]) 44 453 497
577 )
578 };
579 }
580  
581 =head2 all_columns
582  
583 my @all_columns = $record->all_columns;
584  
585 Returns all the columns for the table, even those that are inactive.
586  
587 =cut
588  
589 sub all_columns {
590 49 ▶ my $self = shift;
Subroutine all_columns: called CC=7 SCAR=19.5
Pod documented
591  
592 # Not cached because it's not expected to be used often
593 return sort {
594 265 ▶ ((($b->type || '') eq 'serial') <=> (($a->type || '') eq 'serial'))
Branch: 1 branch
BranchTrueFalseTotal
(($b->type || "") eq "serial") <=> (($a->type || "") eq "serial") or ($a->sort_order || 0) <=> ($b->sort_order || 0) or $a->name cmp $b->name 17 248 265
Condition100%11/11
(($b->type || "") eq "serial") <=> (($a->type || "") eq "serial") or ($a->sort_order || 0) <=> ($b->sort_order || 0)
l!l&&r!l&&!r
8716117
$b->type || ''
l!l
2632
$a->type || ''
l!l
2632
$a->sort_order || 0
l!l
9286
$b->sort_order || 0
l!l
13345
MC/DC100%2/2
(($b->type || "") eq "serial") <=> (($a->type || "") eq "serial") or ($a->sort_order || 0) <=> ($b->sort_order || 0)
$b->type $a->sort_order
MC/DC100%1/1
$a->type || ''
$a->type
MC/DC100%1/1
$b->sort_order || 0
$b->sort_order
Truth table100%3/3
A or B
A= $b->typeB= $a->sort_order
ABresult
1X1
011
000
Truth table100%2/2
A
A= $a->type
Aresult
11
00
Truth table100%2/2
A
A= $b->sort_order
Aresult
11
00
595 or (($a->sort_order || 0) <=> ($b->sort_order || 0))
596 or ( $a->name cmp $b->name )
597 49 } values %{ $self->_columns_hashref }
598 }
599  
600 sub _columns_hashref {
601 739 ▶ my $self = shift;
Subroutine _columns_hashref: called CC=2 SCAR=7.2
602  
603 739 ▶ return ( $self->COLUMNS || {} );
Condition50%1/2
$self->COLUMNS || {}
l!l
7390
MC/DC0%0/1
$self->COLUMNS || {}
$self->COLUMNS
Truth table50%1/2
A
A= $self->COLUMNS
Aresult
11
00
604 }
605  
606 =head2 readable_attributes
607  
608 Returns the list of this table's readable columns. They are first sorted so
609 that primary keys come first, and then they are sorted in alphabetical order.
610  
611 =cut
612  
613 sub readable_attributes {
614 2 ▶ my $self = shift;
Subroutine readable_attributes: called CC=3 SCAR=11.0
Pod documented
615  
616 2 my %is_primary = map { $_ => 1 } @{ $self->_primary_keys };
617  
618 2 return @{ $self->_READABLE_COLS_CACHE() || $self->_READABLE_COLS_CACHE([
619 5 map { $_->name }
620 6 sort { do {
621 30 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
622 6 ▶ ($is_primary{$b->name} <=> $is_primary{$a->name})
Branch: 1 branch
BranchTrueFalseTotal
$is_primary{$b->name} <=> $is_primary{$a->name} or $a->name cmp $b->name 3 3 6
623 ||
624 ($a->name cmp $b->name)
625 } }
626 2 ▶ grep { $_->readable }
Branch: 1 branch
BranchTrueFalseTotal
$self->_READABLE_COLS_CACHE or $self->_READABLE_COLS_CACHE([map({$_->name;} (sort {do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} $is_primary{$b->name} <=> $is_primary{$a->name} or $a->name cmp $b->name };} grep({$_->readable;} $self->columns)))]) 1 1 2
627 $self->columns
628 ])};
629 }
630  
631 =head2 serialize_metadata
632  
633 Returns a hash which describes how this class is stored in the
634 database. Right now, the keys are C<class>, C<table>, and
635 C<columns>. C<class> and C<table> return simple scalars, but
636 C<columns> returns a hash of C<name =&gt; value> pairs for all the
637 columns in this model. See C<Jifty::DBI::Column/serialize_metadata>
638 for the format of that hash.
639  
640  
641 =cut
642  
643 sub serialize_metadata {
644 0 ▶ my $self = shift;
Subroutine serialize_metadata: not called CC=2 SCAR=17.9
Pod documented
645 return {
646 0 ▶ class => ( ref($self) || $self ),
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
647 table => $self->table,
648 columns => { $self->_serialize_columns },
649 };
650 }
651  
652 sub _serialize_columns {
653 0 ▶ my $self = shift;
Subroutine _serialize_columns: not called CC=2 SCAR=17.9
654 0 my %serialized_columns;
655 0 foreach my $column ( $self->columns ) {
656 0 $serialized_columns{ $column->name } = $column->serialize_metadata();
657 }
658  
659 0 return %serialized_columns;
660 }
661  
662 =head2 writable_attributes
663  
664 Returns a list of this table's writable columns
665  
666  
667 =cut
668  
669 sub writable_attributes {
670 1 ▶ my $self = shift;
Subroutine writable_attributes: called CC=2 SCAR=7.0
Pod documented
671 return @{
672 1 $self->_WRITABLE_COLS_CACHE() || $self->_WRITABLE_COLS_CACHE(
673 1 ▶ [ sort map { $_->name } grep { $_->writable } $self->columns ]
Branch: 1 branch
BranchTrueFalseTotal
$self->_WRITABLE_COLS_CACHE or $self->_WRITABLE_COLS_CACHE([sort(map({$_->name;} grep({$_->writable;} $self->columns)))]) 1 0 1
674 )
675 };
676 }
677  
678 =head2 record values
679  
680 As you've probably already noticed, C<Jifty::DBI::Record> automatically
681 creates methods for your standard get/set accessors. It also provides you
682 with some hooks to massage the values being loaded or stored.
683  
684 When you fetch a record value by calling
685 C<$my_record-E<gt>some_field>, C<Jifty::DBI::Record> provides the
686 following hook
687  
688 =over
689  
690  
691  
692 =item after_I<column_name>
693  
694 This hook is called with a reference to the value returned by
695 Jifty::DBI. Its return value is discarded.
696  
697 =back
698  
699 When you set a value, C<Jifty::DBI> provides the following hooks
700  
701 =over
702  
703 =item before_set_I<column_name> PARAMHASH
704  
705 C<Jifty::DBI::Record> passes this function a reference to a paramhash
706 composed of:
707  
708 =over
709  
710 =item column
711  
712 The name of the column we're updating.
713  
714 =item value
715  
716 The new value for I<column>.
717  
718 =item is_sql_function
719  
720 A boolean that, if true, indicates that I<value> is an SQL function,
721 not just a value.
722  
723 =back
724  
725 If before_set_I<column_name> returns false, the new value isn't set.
726  
727 =item before_set PARAMHASH
728  
729 This is identical to the C<before_set_I<column_name>>, but is called
730 for every column set.
731  
732 =item after_set_I<column_name> PARAMHASH
733  
734 This hook will be called after a value is successfully set in the
735 database. It will be called with a reference to a paramhash that
736 contains C<column>, C<value>, and C<old_value> keys. If C<value> was a
737 SQL function, it will now contain the actual value that was set. If
738 C<column> has filters on it, C<value> will be the result of going
739 through an encode and decode cycle.
740  
741 This hook's return value is ignored.
742  
743 =item after_set PARAMHASH
744  
745 This is identical to the C<after_set_I<column_name>>, but is called
746 for every column set.
747  
748 =item validate_I<column_name> VALUE
749  
750 This hook is called just before updating the database. It expects the
751 actual new value you're trying to set I<column_name> to. It returns
752 two values. The first is a boolean with truth indicating success. The
753 second is an optional message. Note that validate_I<column_name> may
754 be called outside the context of a I<set> operation to validate a
755 potential value. (The Jifty application framework uses this as part of
756 its AJAX validation system.)
757  
758 =back
759  
760  
761 =cut
762  
763 =head2 _value
764  
765 _value takes a single column name and returns that column's value for
766 this row. Subclasses can override _value to insert custom access
767 control.
768  
769 =cut
770  
771 sub _value {
772 339 ▶ my $self = shift;
Subroutine _value: called CC=1 SCAR=0.0
773 339 my $column = shift;
774  
775 339 my $value = $self->__value( $column => @_ );
776 339 $self->_run_callback(
777 name => "after_" . $column,
778 args => \$value
779 );
780 339 return $value;
781 }
782  
783 =head2 __raw_value
784  
785 Takes a column name and returns that column's raw value.
786 Subclasses should never override __raw_value.
787  
788 =cut
789  
790 sub __raw_value {
791 10 ▶ my $self = shift;
Subroutine __raw_value: called CC=4 SCAR=13.9
792  
793 10 my $column_name = shift;
794  
795 # In the default case of "yeah, we have a value", return it as
796 # fast as we can.
797 return $self->{'raw_values'}{$column_name}
798 10 ▶ if $self->{'fetched'}{$column_name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fetched"}{$column_name} 6 4 10
799  
800 4 ▶ if ( !$self->{'fetched'}{$column_name} and my $id = $self->id() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"fetched"}{$column_name} and my $id = $self->id) 3 1 4
Condition66%2/3
not $self->{"fetched"}{$column_name} and my $id = $self->id
!ll&&!rl&&r
013
MC/DC50%1/2
not $self->{"fetched"}{$column_name} and my $id = $self->id
not $self->{"fetched"}{$column_name} my $id = $self->id
Truth table66%2/3
not(A) and B
A= not $self->{"fetched"}{$column_name}B= my $id = $self->id
ABresult
0X0
100
111
801 3 my $pkey = $self->_primary_key();
802 3 my $query_string =
803 "SELECT "
804 . $column_name
805 . " FROM "
806 . $self->table
807 . " WHERE $pkey = ?";
808 3 my $sth = $self->_handle->simple_query( $query_string, $id );
809 3 my ($value) = eval { $sth->fetchrow_array() };
810 3 $self->{'raw_values'}{$column_name} = $value;
811 3 $self->{'fetched'}{$column_name} = 1;
812 }
813  
814 4 return $self->{'raw_values'}{$column_name};
815 }
816  
817 =head2 resolve_column
818  
819 given a column name, resolve it, even if it's actually an alias
820 return the column object.
821  
822 =cut
823  
824 sub resolve_column {
825 0 ▶ my $self = shift;
Subroutine resolve_column: not called CC=2 SCAR=17.9
Pod documented
826 0 my $column_name = shift;
827 0 ▶ return unless $column_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $column_name 0 0 0
828 0 return $self->COLUMNS->{$column_name};
829 }
830  
831 =head2 __value
832  
833 Takes a column name and returns that column's value. Subclasses should
834 never override __value.
835  
836 =cut
837  
838 sub __value {
839 376 ▶ my $self = shift;
Subroutine __value: called CC=8 SCAR=21.0
840  
841 376 my $column = $self->COLUMNS->{ +shift }; # Shortcut around ->resolve_column
842 376 ▶ return unless $column;
Branch: 1 branch
BranchTrueFalseTotal
unless $column 2 374 376
843  
844 374 my $column_name = $column->{name}; # Speed optimization
845  
846 374 ▶ if ($column->computed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->computed) 0 374 374
847 0 return $self->$column_name;
848 }
849  
850 # In the default case of "yeah, we have a value", return it as
851 # fast as we can.
852 return $self->{'values'}{$column_name}
853 if ( $self->{'fetched'}{$column_name}
854 374 ▶ && $self->{'decoded'}{$column_name} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fetched"}{$column_name} and $self->{"decoded"}{$column_name} 159 215 374
Condition100%3/3
$self->{"fetched"}{$column_name} and $self->{"decoded"}{$column_name}
!ll&&!rl&&r
4211159
MC/DC100%2/2
$self->{"fetched"}{$column_name} and $self->{"decoded"}{$column_name}
$self->{"fetched"}{$column_name} $self->{"decoded"}{$column_name}
Truth table100%3/3
A and B
A= $self->{"fetched"}{$column_name}B= $self->{"decoded"}{$column_name}
ABresult
0X0
100
111
855  
856 215 ▶ unless ($self->{'fetched'}{$column_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fetched"}{$column_name}) 4 211 215
857 # Fetch it, and mark it as not decoded
858 4 $self->{'values'}{$column_name} = $self->__raw_value( $column_name );
859 4 $self->{'decoded'}{$column_name} = 0;
860 }
861  
862 215 ▶ unless ( $self->{'decoded'}{$column_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"decoded"}{$column_name}) 215 0 215
863 $self->_apply_output_filters(
864 column => $column,
865 value_ref => \$self->{'values'}{$column_name},
866 215 ▶ ) if exists $self->{'values'}{$column_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"values"}{$column_name} 215 0 215
867 215 $self->{'decoded'}{$column_name} = 1;
868 }
869  
870 215 return $self->{'values'}{$column_name};
871 }
872  
873 =head2 as_hash
874  
875 Returns a version of this record's readable columns rendered as a hash
876 of key => value pairs
877  
878 =cut
879  
880 sub as_hash {
881 1 ▶ my $self = shift;
Subroutine as_hash: called CC=2 SCAR=6.9
Pod documented
882 1 my %values;
883 1 $values{$_} = $self->$_() for $self->readable_attributes;
884 1 return %values;
885 }
886  
887 =head2 _set
888  
889 _set takes a single column name and a single unquoted value. It
890 updates both the in-memory value of this column and the in-database
891 copy. Subclasses can override _set to insert custom access control.
892  
893 =cut
894  
895 sub _set {
896 61 ▶ my $self = shift;
Subroutine _set: called CC=4 SCAR=13.9
897 61 my %args = (
898 'column' => undef,
899 'value' => undef,
900 'is_sql_function' => undef,
901 @_
902 );
903  
904 # Call the general before_set triggers
905 61 my $ok = $self->_run_callback(
906 name => "before_set",
907 args => \%args,
908 );
909 61 ▶ return $ok if ( not defined $ok );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ok 0 61 61
910  
911 # Call the specific before_set_column triggers
912 $ok = $self->_run_callback(
913 name => "before_set_" . $args{column},
914 61 args => \%args,
915 );
916 61 ▶ return $ok if ( not defined $ok );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ok 0 61 61
917  
918 # Fetch the old value for the benefit of the triggers
919 61 my $old_value = $self->_value( $args{column} );
920  
921 61 $ok = $self->__set(%args);
922 61 ▶ return $ok if not $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 3 58 61
923  
924 # Fetch the value back to make sure we have the actual value
925 58 my $value = $self->_value( $args{column} );
926  
927 # Call the general after_set triggers
928 $self->_run_callback(
929 name => "after_set",
930 58 args => { column => $args{column}, value => $value, old_value => $old_value },
931 );
932  
933 # Call the specific after_set_column triggers
934 $self->_run_callback(
935 name => "after_set_" . $args{column},
936 58 args => { column => $args{column}, value => $value, old_value => $old_value },
937 );
938  
939 58 return $ok;
940 }
941  
942 sub __set {
943 61 ▶ my $self = shift;
Subroutine __set: called CC=17 SCAR=28.9
944  
945 61 my %args = (
946 'column' => undef,
947 'value' => undef,
948 'is_sql_function' => undef,
949 @_
950 );
951  
952 61 my $ret = Class::ReturnValue->new();
953  
954 61 my $column = $self->column( $args{'column'} );
955 61 ▶ unless ($column) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($column) 1 60 61
956 1 $ret->as_array( 0, 'No column specified' );
957 1 $ret->as_error(
958 errno => 5,
959 do_backtrace => 0,
960 message => "No column specified"
961 );
962 1 return ( $ret->return_value );
963 }
964  
965 60 my $unmunged_value;
966 60 ▶ unless ($args{is_sql_function}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"is_sql_function"}) 60 0 60
967 $self->_apply_input_filters(
968 column => $column,
969 60 value_ref => \$args{'value'}
970 );
971  
972 # if value is not fetched or it's already decoded
973 # then we don't check eqality
974 # we also don't call __value because it decodes value, but
975 # we need encoded value
976 60 ▶ if ( $self->{'fetched'}{ $column->name }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fetched"}{$column->name} or not $self->{"decoded"}{$column->name}) 60 0 60
Condition33%1/3
$self->{"fetched"}{$column->name} or not $self->{"decoded"}{$column->name}
l!l&&r!l&&!r
6000
MC/DC0%0/2
$self->{"fetched"}{$column->name} or not $self->{"decoded"}{$column->name}
$self->{"fetched"}{$column->name} not $self->{"decoded"}{$column->name}
Truth table33%1/3
A or not(B)
A= $self->{"fetched"}{$column->name}B= not $self->{"decoded"}{$column->name}
ABresult
1X1
011
000
977 || !$self->{'decoded'}{ $column->name } )
978 {
979 60 ▶ if (( !defined $args{'value'}
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{"value"} || defined $self->{"values"}{$column->name} or defined $args{"value"} and defined $self->{"values"}{$column->name} and $args{"value"} . '' eq '' . $self->{"values"}{$column->name}) 2 58 60
Condition100%12/12
not defined $args{"value"} || defined $self->{"values"}{$column->name} or defined $args{"value"} and defined $self->{"values"}{$column->name} and $args{"value"} . '' eq '' . $self->{"values"}{$column->name}
l!l&&r!l&&!r
1257
defined $args{'value'} || defined $self->{'values'}{$column->name}
l!l&&r!l&&!r
33261
defined $args{"value"} and defined $self->{"values"}{$column->name} and $args{"value"} . '' eq '' . $self->{"values"}{$column->name}
!ll&&!rl&&r
46121
defined $args{"value"} and defined $self->{"values"}{$column->name}
!ll&&!rl&&r
262013
MC/DC40%2/5
not defined $args{"value"} || defined $self->{"values"}{$column->name} or defined $args{"value"} and defined $self->{"values"}{$column->name} and $args{"value"} . '' eq '' . $self->{"values"}{$column->name}
defined $args{'value'} defined $self->{'values'}{$column->name} defined $args{"value"} defined $self->{"values"}{$column->name} $args{"value"} . '' eq '' . $self->{"values"}{$column->name}
Truth table33%3/9
not(A || B) or C and D and E
A= defined $args{'value'}B= defined $self->{'values'}{$column->name}C= defined $args{"value"}D= defined $self->{"values"}{$column->name}E= $args{"value"} . '' eq '' . $self->{"values"}{$column->name}
ABCDEresult
00XXX1
1X1111
011111
1X0XX0
1X10X0
1X1100
010XX0
0110X0
011100
980 && !defined $self->{'values'}{ $column->name }
981 )
982 || ( defined $args{'value'}
983 && defined $self->{'values'}{ $column->name }
984  
985 # XXX: This is a bloody hack to stringify DateTime
986 # and other objects for compares
987 && $args{value}
988 . "" eq ""
989 . $self->{'values'}{ $column->name }
990 )
991 )
992 {
993 2 $ret->as_array( 1, "That is already the current value" );
994 2 return ( $ret->return_value );
995 }
996 }
997  
998 58 ▶ if ( my $sub = $column->validator ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $column->validator) 4 54 58
999 4 my ( $ok, $msg ) = $sub->( $self, $args{'value'} );
1000 4 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 3 4
1001 1 $ret->as_array( 0, 'Illegal value for ' . $column->name );
1002 1 $ret->as_error(
1003 errno => 3,
1004 do_backtrace => 0,
1005 message => "Illegal value for " . $column->name
1006 );
1007 1 return ( $ret->return_value );
1008 }
1009 }
1010  
1011 # Implement 'is distinct' checking
1012 57 ▶ if ( $column->distinct ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->distinct) 1 56 57
1013 1 my $ret = $self->is_distinct( $column->name, $args{'value'} );
1014 1 ▶ return ($ret) if not($ret);
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 1 0 1
1015 }
1016  
1017 # The blob handling will destroy $args{'value'}. But we assign
1018 # that back to the object at the end. this works around that
1019 56 $unmunged_value = $args{'value'};
1020  
1021 56 ▶ if ( $column->type =~ /^(text|longtext|clob|blob|lob|bytea)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->type =~ /^(text|longtext|clob|blob|lob|bytea)$/i) 5 51 56
1022 5 my $bhash
1023 = $self->_handle->blob_params( $column->name, $column->type );
1024 5 $bhash->{'value'} = $args{'value'};
1025 5 $args{'value'} = $bhash;
1026 }
1027 }
1028  
1029 56 my $val = $self->_handle->update_record_value(
1030 %args,
1031 table => $self->table(),
1032 primary_keys => { $self->primary_keys() }
1033 );
1034  
1035 56 ▶ unless ($val) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val) 0 56 56
1036 my $message
1037 0 = $column->name . " could not be set to " . $args{'value'} . ".";
1038 0 $ret->as_array( 0, $message );
1039 0 $ret->as_error(
1040 errno => 4,
1041 do_backtrace => 0,
1042 message => $message
1043 );
1044 0 return ( $ret->return_value );
1045 }
1046  
1047 # If we've performed some sort of "functional update"
1048 # then we need to reload the object from the DB to know what's
1049 # really going on. (ex SET Cost = Cost+5)
1050 56 ▶ if ( $args{'is_sql_function'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'is_sql_function'}) { } 0 56 56
1051  
1052 # XXX TODO primary_keys
1053 0 $self->load_by_cols( id => $self->id );
1054 } else {
1055 56 $self->{'raw_values'}{ $column->name } = $unmunged_value;
1056 56 $self->{'values'}{ $column->name } = $unmunged_value;
1057 56 $self->{'decoded'}{ $column->name } = 0;
1058 }
1059 56 $ret->as_array( 1, "The new value has been set." );
1060 56 return ( $ret->return_value );
1061 }
1062  
1063 =head2 load
1064  
1065 C<load> can be called as a class or object method.
1066  
1067 Takes a single argument, $id. Calls load_by_cols to retrieve the row
1068 whose primary key is $id.
1069  
1070 =cut
1071  
1072 sub load {
1073 68 ▶ my $self = shift;
Subroutine load: called CC=4 SCAR=14.1
Pod documented
1074 68 ▶ return unless @_ and defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and defined $_[0] 1 67 68
Condition66%2/3
@_ and defined $_[0]
!ll&&!rl&&r
1067
MC/DC50%1/2
@_ and defined $_[0]
@_ defined $_[0]
Truth table66%2/3
A and B
A= @_B= defined $_[0]
ABresult
0X0
100
111
1075 67 ▶ Carp::carp("load called with more than one argument. Did you mean load_by_cols?") if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 67 67
1076  
1077 67 return $self->load_by_cols( id => shift );
1078 }
1079  
1080 =head2 load_by_cols
1081  
1082 C<load_by_cols> can be called as a class or object method.
1083  
1084 Takes a hash of columns and values. Loads the first record that
1085 matches all keys.
1086  
1087 The hash's keys are the columns to look at.
1088  
1089 The hash's values are either: scalar values to look for OR hash
1090 references which contain 'operator', 'value', 'case_sensitive'
1091 or 'function'
1092  
1093 To load something case sensitively on a case insensitive database,
1094 you can do:
1095  
1096 $record->load_by_cols( column => { operator => '=',
1097 value => 'Foo',
1098 case_sensitive => 1 } );
1099  
1100 =cut
1101  
1102 sub load_by_cols {
1103 122 ▶ my $class = shift;
Subroutine load_by_cols: called CC=24 SCAR=32.2
Pod documented
1104 122 my %hash = (@_);
1105 122 my ($self);
1106 122 ▶ if ( ref($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 120 2 122
1107 120 ( $self, $class ) = ( $class, undef );
1108 } else {
1109 2 ▶ $self = $class->new( handle => ( delete $hash{'_handle'} || undef ) );
Condition100%2/2
delete $hash{'_handle'} || undef
l!l
11
MC/DC100%1/1
delete $hash{'_handle'} || undef
delete $hash{'_handle'}
Truth table100%2/2
A
A= delete $hash{'_handle'}
Aresult
11
00
1110 }
1111  
1112 122 my ( @bind, @phrases );
1113 122 foreach my $key ( keys %hash ) {
1114 124 ▶ if ( defined $hash{$key} && $hash{$key} ne '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $hash{$key} and $hash{$key} ne '') { } 120 4 124
elsif (not defined $hash{$key}) { } 2 2 4
Condition100%3/3
defined $hash{$key} and $hash{$key} ne ""
!ll&&!rl&&r
22120
MC/DC100%2/2
defined $hash{$key} and $hash{$key} ne ""
defined $hash{$key} $hash{$key} ne ""
Truth table100%3/3
A and B
A= defined $hash{$key}B= $hash{$key} ne ""
ABresult
0X0
100
111
1115 120 my $op;
1116 my $value;
1117 120 my $function = "?";
1118 120 my $column_obj = $self->column($key);
1119 120 ▶ Carp::confess(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $column_obj 0 120 120
1120 "Unknown column '$key' in class '" . ref($self) . "'" )
1121 if !defined $column_obj;
1122 120 my $case_sensitive = $column_obj->case_sensitive;
1123 120 ▶ if ( ref $hash{$key} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash{$key} eq 'HASH') { } 3 117 120
1124 3 $op = $hash{$key}->{operator};
1125 3 $value = $hash{$key}->{value};
1126 3 ▶ $function = $hash{$key}->{function} || "?";
Condition50%1/2
$hash{$key}{'function'} || '?'
l!l
03
MC/DC0%0/1
$hash{$key}{'function'} || '?'
$hash{$key}{'function'}
Truth table50%1/2
A
A= $hash{$key}{'function'}
Aresult
11
00
1127 $case_sensitive = $hash{$key}->{case_sensitive}
1128 3 ▶ if exists $hash{$key}->{case_sensitive};
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash{$key}{"case_sensitive"} 1 2 3
1129 } else {
1130 117 $op = '=';
1131 117 $value = $hash{$key};
1132 }
1133  
1134 120 ▶ if ( blessed $value && $value->isa('Jifty::DBI::Record') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Jifty::DBI::Record")) 1 119 120
Condition66%2/3
builtin::blessed($value) and $value->isa("Jifty::DBI::Record")
!ll&&!rl&&r
11901
MC/DC50%1/2
builtin::blessed($value) and $value->isa("Jifty::DBI::Record")
builtin::blessed($value) $value->isa("Jifty::DBI::Record")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("Jifty::DBI::Record")
ABresult
0X0
100
111
1135 1 ▶ my $by = defined $column_obj->by ? $column_obj->by : 'id';
Branch: 1 branch
BranchTrueFalseTotal
defined $column_obj->by ? : 1 0 1
1136 1 $value = $value->$by;
1137 }
1138  
1139 $self->_apply_input_filters(
1140 120 ▶ column => $column_obj,
Branch: 1 branch
BranchTrueFalseTotal
if $column_obj->encode_on_select 12 108 120
1141 value_ref => \$value,
1142 ) if $column_obj->encode_on_select;
1143  
1144 # if the handle is in a case_sensitive world and we need to make
1145 # a case-insensitive query
1146 120 ▶ if ( $self->_handle->case_sensitive && $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_handle->case_sensitive and $value) 112 8 120
Condition66%2/3
$self->_handle->case_sensitive and $value
!ll&&!rl&&r
08112
MC/DC50%1/2
$self->_handle->case_sensitive and $value
$self->_handle->case_sensitive $value
Truth table66%2/3
A and B
A= $self->_handle->case_sensitiveB= $value
ABresult
0X0
100
111
1147 112 ▶ if ( $column_obj->is_string && !$case_sensitive ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_obj->is_string and not $case_sensitive) 21 91 112
Condition100%3/3
$column_obj->is_string and not $case_sensitive
!ll&&!rl&&r
89221
MC/DC100%2/2
$column_obj->is_string and not $case_sensitive
$column_obj->is_string not $case_sensitive
Truth table100%3/3
A and not(B)
A= $column_obj->is_stringB= not $case_sensitive
ABresult
0X0
100
111
1148 21 ( $key, $op, $function )
1149 = $self->_handle->_make_clause_case_insensitive( $key,
1150 $op, $function );
1151 }
1152 }
1153  
1154 120 ▶ if ($column_obj and $column_obj->no_placeholder and $function eq "?") {
Branch: 1 branch
BranchTrueFalseTotal
if ($column_obj and $column_obj->no_placeholder and $function eq '?') { } 0 120 120
Condition33%2/6
$column_obj and $column_obj->no_placeholder and $function eq "?"
!ll&&!rl&&r
12000
$column_obj and $column_obj->no_placeholder
!ll&&!rl&&r
01200
MC/DC0%0/3
$column_obj and $column_obj->no_placeholder and $function eq "?"
$column_obj $column_obj->no_placeholder $function eq "?"
Truth table25%1/4
A and B and C
A= $column_objB= $column_obj->no_placeholderC= $function eq "?"
ABCresult
0XX0
10X0
1100
1111
1155 0 push @phrases, "$key $op ".$self->_handle->quote_value($value);
1156 } else {
1157 120 push @phrases, "$key $op $function";
1158 120 push @bind, $value;
1159 }
1160  
1161 } elsif ( !defined $hash{$key} ) {
1162 2 push @phrases, "$key IS NULL";
1163 } else {
1164 2 push @phrases, "($key IS NULL OR $key = ?)";
1165 2 my $column = $self->column($key);
1166  
1167 2 ▶ if ( $column->is_numeric ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->is_numeric) { } 1 1 2
1168 1 push @bind, 0;
1169 } else {
1170 1 push @bind, '';
1171 }
1172  
1173 }
1174 }
1175  
1176 122 my $query_string
1177 = "SELECT * FROM "
1178 . $self->table
1179 . " WHERE "
1180 . join( ' AND ', @phrases );
1181 122 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 2 120 122
1182 2 $self->_load_from_sql( $query_string, @bind );
1183 2 return $self;
1184 } else {
1185 120 return $self->_load_from_sql( $query_string, @bind );
1186 }
1187  
1188 }
1189  
1190 =head2 load_by_primary_keys
1191  
1192 Loads records with a given set of primary keys.
1193  
1194 =cut
1195  
1196 sub load_by_primary_keys {
1197 3 ▶ my $self = shift;
Subroutine load_by_primary_keys: called CC=4 SCAR=13.9
Pod documented
1198 3 ▶ my $data = ( ref $_[0] eq 'HASH' ) ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 2 3
1199  
1200 3 my %cols = ();
1201 3 foreach ( @{ $self->_primary_keys } ) {
1202 3 ▶ return ( 0, "Missing PK column: '$_'" ) unless defined $data->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{$_} 1 2 3
1203 2 $cols{$_} = $data->{$_};
1204 }
1205 2 return ( $self->load_by_cols(%cols) );
1206 }
1207  
1208 =head2 load_from_hash
1209  
1210 Takes a hashref, such as created by Jifty::DBI and populates this
1211 record's loaded values hash.
1212  
1213 =cut
1214  
1215 sub load_from_hash {
1216 158 ▶ my $self = shift;
Subroutine load_from_hash: called CC=6 SCAR=24.5
Pod documented
1217 158 my $hashref = shift;
1218 158 my %args = @_;
1219 158 ▶ if ($args{fast}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"fast"}) 158 0 158
1220 # Optimization for loading from database
1221 158 $self->{values} = $hashref;
1222 158 $self->{fetched}{$_} = 1 for keys %{$hashref};
1223 # copy $hashref so changing 'values' doesn't change 'raw_values'
1224 158 $self->{raw_values}{$_} = $hashref->{$_} for keys %{$hashref};
1225 158 $self->{decoded} = {};
1226 158 return $self->{values}{id};
1227 }
1228  
1229 0 ▶ unless ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
1230 0 $self = $self->new( handle => delete $hashref->{'_handle'} );
1231 }
1232  
1233 0 $self->{'values'} = {};
1234 0 $self->{'raw_values'} = {};
1235 0 $self->{'fetched'} = {};
1236  
1237 0 foreach my $col ( grep exists $hashref->{ lc $_ }, map $_->name, $self->columns ) {
1238 0 $self->{'fetched'}{$col} = 1;
1239 0 $self->{'values'}{$col} = $hashref->{ lc $col };
1240 0 $self->{'raw_values'}{$col} = $hashref->{ lc $col };
1241 }
1242  
1243 0 $self->{'decoded'} = {};
1244 0 return $self->id();
1245 }
1246  
1247 =head2 _load_from_sql QUERYSTRING @BIND_VALUES
1248  
1249 Load a record as the result of an SQL statement
1250  
1251 =cut
1252  
1253 sub _load_from_sql {
1254 126 ▶ my $self = shift;
Subroutine _load_from_sql: called CC=8 SCAR=20.8
1255 126 my $query_string = shift;
1256 126 my @bind_values = (@_);
1257  
1258 126 my $sth = $self->_handle->simple_query( $query_string, @bind_values );
1259  
1260 #TODO this only gets the first row. we should check if there are more.
1261  
1262 126 ▶ return ( 0, "Couldn't execute query" ) unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 1 125 126
1263  
1264 125 my $hashref = $sth->fetchrow_hashref;
1265 125 delete $self->{'values'};
1266 125 delete $self->{'raw_values'};
1267 125 $self->{'fetched'} = {};
1268 125 $self->{'decoded'} = {};
1269  
1270 #foreach my $f ( keys %$hashref ) { $self->{'fetched'}{ $f } = 1; }
1271 125 foreach my $col ( map { $_->name } $self->columns ) {
1272 521 ▶ next unless exists $hashref->{ lc($col) };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$hashref{lc $col} 48 473 521
1273 473 $self->{'fetched'}{$col} = 1;
1274 473 $self->{'values'}->{$col} = $hashref->{ lc($col) };
1275 473 $self->{'raw_values'}->{$col} = $hashref->{ lc($col) };
1276 }
1277 125 ▶ if ( !$self->{'values'} && $sth->err ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"values"} and $sth->err) 0 125 125
Condition66%2/3
not $self->{"values"} and $sth->err
!ll&&!rl&&r
11960
MC/DC0%0/2
not $self->{"values"} and $sth->err
not $self->{"values"} $sth->err
Truth table66%2/3
not(A) and B
A= not $self->{"values"}B= $sth->err
ABresult
0X0
100
111
1278 0 return ( 0, "Couldn't fetch row: " . $sth->err );
1279 }
1280  
1281 125 ▶ unless ( $self->{'values'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"values"}) 6 119 125
1282 6 return ( 0, "Couldn't find row" );
1283 }
1284  
1285 ## I guess to be consistant with the old code, make sure the primary
1286 ## keys exist.
1287  
1288 119 ▶ if ( grep { not defined } $self->primary_keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined $_;} $self->primary_keys) 1 118 119
1289 1 return ( 0, "Missing a primary key?" );
1290 }
1291  
1292 118 return ( 1, "Found object" );
1293  
1294 }
1295  
1296 =head2 create PARAMHASH
1297  
1298 C<create> can be called as either a class or object method
1299  
1300 This method creates a new record with the values specified in the PARAMHASH.
1301  
1302 This method calls two hooks in your subclass:
1303  
1304 =over
1305  
1306 =item before_create
1307  
1308 When adding the C<before_create> trigger, you can determine whether
1309 the trigger may cause an abort or not by passing the C<abortable>
1310 parameter to the C<add_trigger> method. If this is not set, then the
1311 return value is ignored regardless.
1312  
1313 sub before_create {
1314 my $self = shift;
1315 my $args = shift;
1316  
1317 # Do any checks and changes on $args here.
1318 $args->{first_name} = ucfirst $args->{first_name};
1319  
1320 return; # false return vallue will abort the create
1321 return 1; # true return value will allow create to continue
1322 }
1323  
1324 This method is called before trying to create our row in the
1325 database. It's handed a reference to your paramhash. (That means it
1326 can modify your parameters on the fly). C<before_create> returns a
1327 true or false value. If it returns C<undef> and the trigger has been
1328 added as C<abortable>, the create is aborted.
1329  
1330 =item after_create
1331  
1332 When adding the C<after_create> trigger, you can determine whether the
1333 trigger may cause an abort or not by passing the C<abortable>
1334 parameter to the C<add_trigger> method. If this is not set, then the
1335 return value is ignored regardless.
1336  
1337 sub after_create {
1338 my $self = shift;
1339 my $insert_return_value_ref = shift;
1340  
1341 return unless $$insert_return_value_ref; # bail if insert failed
1342 $self->load($$insert_return_value_ref); # load ourselves from db
1343  
1344 # Do whatever needs to be done here
1345  
1346 return; # aborts the create, possibly preventing a load
1347 return 1; # continue normally
1348 }
1349  
1350 This method is called after attempting to insert the record into the
1351 database. It gets handed a reference to the return value of the
1352 insert. That will either be a true value or a L<Class::ReturnValue>.
1353  
1354 Aborting the trigger merely causes C<create> to return a false
1355 (undefined) value even thought he create may have succeeded. This
1356 prevents the loading of the record that would normally be returned.
1357  
1358 =back
1359  
1360  
1361 =cut
1362  
1363 sub create {
1364 106 ▶ my $class = shift;
Subroutine create: called CC=6 SCAR=18.0
Pod documented
1365 106 my %attribs = @_;
1366  
1367 106 my ($self);
1368 106 ▶ if ( ref($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) { } 105 1 106
1369 105 ( $self, $class ) = ( $class, undef );
1370 } else {
1371 $self = $class->new(
1372 1 ▶ handle => ( delete $attribs{'_handle'} || undef ) );
Condition50%1/2
delete $attribs{'_handle'} || undef
l!l
10
MC/DC0%0/1
delete $attribs{'_handle'} || undef
delete $attribs{'_handle'}
Truth table50%1/2
A
A= delete $attribs{'_handle'}
Aresult
11
00
1373 }
1374  
1375 106 my $ok
1376 = $self->_run_callback( name => "before_create", args => \%attribs );
1377 106 ▶ return $ok if ( not defined $ok );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ok 0 106 106
1378  
1379 106 my $ret = $self->__create(%attribs);
1380  
1381 106 $ok = $self->_run_callback(
1382 name => "after_create",
1383 args => \$ret
1384 );
1385 106 ▶ return $ok if ( not defined $ok );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ok 0 106 106
1386  
1387 106 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 1 105 106
1388 1 $self->load_by_cols( id => $ret );
1389 1 return ($self);
1390 } else {
1391 105 return ($ret);
1392 }
1393 }
1394  
1395 sub __create {
1396 106 ▶ my ( $self, %attribs ) = @_;
Subroutine __create: called CC=21 SCAR=31.3
1397  
1398 106 foreach my $column_name ( keys %attribs ) {
1399 173 my $column = $self->column($column_name);
1400 173 ▶ unless ($column) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($column) 0 173 173
1401  
1402 # "Virtual" columns beginning with __ are passed through
1403 # to handle without munging.
1404 0 ▶ next if $column_name =~ /^__/;
Branch: 1 branch
BranchTrueFalseTotal
if $column_name =~ /^__/ 0 0 0
1405  
1406 0 Carp::confess "$column_name isn't a column we know about";
1407 }
1408 173 ▶ if ( $column->readable
Branch: 1 branch
BranchTrueFalseTotal
if ($column->readable and $column->refers_to and UNIVERSAL::isa($column->refers_to, "Jifty::DBI::Record") and UNIVERSAL::isa($attribs{$column_name}, "Jifty::DBI::Record")) 4 169 173
Condition77%7/9
$column->readable and $column->refers_to and UNIVERSAL::isa($column->refers_to, "Jifty::DBI::Record") and UNIVERSAL::isa($attribs{$column_name}, "Jifty::DBI::Record")
!ll&&!rl&&r
159104
$column->readable and $column->refers_to and UNIVERSAL::isa($column->refers_to, "Jifty::DBI::Record")
!ll&&!rl&&r
159014
$column->readable and $column->refers_to
!ll&&!rl&&r
015914
MC/DC50%2/4
$column->readable and $column->refers_to and UNIVERSAL::isa($column->refers_to, "Jifty::DBI::Record") and UNIVERSAL::isa($attribs{$column_name}, "Jifty::DBI::Record")
$column->readable $column->refers_to UNIVERSAL::isa($column->refers_to, "Jifty::DBI::Record") UNIVERSAL::isa($attribs{$column_name}, "Jifty::DBI::Record")
Truth table60%3/5
A and B and C and D
A= $column->readableB= $column->refers_toC= UNIVERSAL::isa($column->refers_to, "Jifty::DBI::Record")D= UNIVERSAL::isa($attribs{$column_name}, "Jifty::DBI::Record")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1409 and $column->refers_to
1410 and UNIVERSAL::isa( $column->refers_to, "Jifty::DBI::Record" )
1411 and UNIVERSAL::isa( $attribs{$column_name}, 'Jifty::DBI::Record' ) )
1412 {
1413 # lookup the column referenced or default to id
1414 4 ▶ my $by = defined $column->by ? $column->by : 'id';
Branch: 1 branch
BranchTrueFalseTotal
defined $column->by ? : 4 0 4
1415 4 $attribs{$column_name} = $attribs{$column_name}->$by;
1416 }
1417  
1418 $self->_apply_input_filters(
1419 column => $column,
1420 173 value_ref => \$attribs{$column_name},
1421 );
1422  
1423 # Implement 'is distinct' checking
1424 173 ▶ if ( $column->distinct ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->distinct) 4 169 173
1425 my $ret
1426 4 = $self->is_distinct( $column_name, $attribs{$column_name} );
1427 4 ▶ if ( not $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret) 1 3 4
1428 Carp::cluck(
1429 "$self failed a 'is_distinct' check for $column_name on "
1430 1 . $attribs{$column_name} );
1431 1 return ($ret);
1432 }
1433 }
1434  
1435 172 ▶ if ( $column->type =~ /^(text|longtext|clob|blob|lob|bytea)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->type =~ /^(text|longtext|clob|blob|lob|bytea)$/i) 8 164 172
1436 8 my $bhash
1437 = $self->_handle->blob_params( $column_name, $column->type );
1438 8 $bhash->{'value'} = $attribs{$column_name};
1439 8 $attribs{$column_name} = $bhash;
1440 }
1441 }
1442  
1443 105 for my $column ( $self->columns ) {
1444 399 ▶ if ( not defined $attribs{ $column->name }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $attribs{$column->name} and defined $column->default and not ref $column->default) 75 324 399
Condition83%5/6
not defined $attribs{$column->name} and defined $column->default and not ref $column->default
!ll&&!rl&&r
324075
not defined $attribs{$column->name} and defined $column->default
!ll&&!rl&&r
17215275
MC/DC66%2/3
not defined $attribs{$column->name} and defined $column->default and not ref $column->default
not defined $attribs{$column->name} defined $column->default not ref $column->default
Truth table75%3/4
not(A) and B and not(C)
A= not defined $attribs{$column->name}B= defined $column->defaultC= not ref $column->default
ABCresult
0XX0
10X0
1100
1111
1445 and defined $column->default
1446 and not ref $column->default )
1447 {
1448 75 my $default = force $column->default;
1449 75 ▶ $default = $default->id
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($default, "Jifty::DBI::Record") 0 75 75
1450 if UNIVERSAL::isa( $default, 'Jifty::DBI::Record' );
1451  
1452 75 $attribs{ $column->name } = $default;
1453  
1454 $self->_apply_input_filters(
1455 column => $column,
1456 75 value_ref => \$attribs{ $column->name },
1457 );
1458 }
1459  
1460 399 ▶ if ( not defined $attribs{ $column->name }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $attribs{$column->name} and $column->mandatory and $column->type ne "serial") 1 398 399
Condition100%6/6
not defined $attribs{$column->name} and $column->mandatory and $column->type ne "serial"
!ll&&!rl&&r
2931051
not defined $attribs{$column->name} and $column->mandatory
!ll&&!rl&&r
24746106
MC/DC100%3/3
not defined $attribs{$column->name} and $column->mandatory and $column->type ne "serial"
not defined $attribs{$column->name} $column->mandatory $column->type ne "serial"
Truth table100%4/4
not(A) and B and C
A= not defined $attribs{$column->name}B= $column->mandatoryC= $column->type ne "serial"
ABCresult
0XX0
10X0
1100
1111
1461 and $column->mandatory
1462 and $column->type ne "serial" )
1463 {
1464 # Enforce "mandatory"
1465 1 Carp::carp "Did not supply value for mandatory column "
1466 . $column->name;
1467 1 ▶ unless ( $column->active ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($column->active) 0 1 1
1468 0 Carp::carp "The mandatory column "
1469 . $column->name
1470 . " is no longer active. This is likely to cause problems!";
1471 }
1472  
1473 1 return (0);
1474 }
1475 }
1476  
1477 104 return $self->_handle->insert( $self->table, %attribs );
1478 }
1479  
1480 =head2 delete
1481  
1482 Delete this record from the database. On failure return a
1483 Class::ReturnValue with the error. On success, return 1;
1484  
1485 This method has two hooks:
1486  
1487 =over
1488  
1489 =item before_delete
1490  
1491 This method is called before the record deletion, if it exists. On
1492 failure it returns a L<Class::ReturnValue> with the error. On success
1493 it returns 1.
1494  
1495 If this method returns an error, it causes the delete to abort and
1496 return the return value from this hook.
1497  
1498 =item after_delete
1499  
1500 This method is called after deletion, with a reference to the return
1501 value from the delete operation.
1502  
1503 =back
1504  
1505 =cut
1506  
1507 sub delete {
1508 3 ▶ my $self = shift;
Subroutine delete: called CC=3 SCAR=11.2
Pod documented
1509 3 my $before_ret = $self->_run_callback( name => 'before_delete' );
1510 3 ▶ return $before_ret unless ( defined $before_ret );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $before_ret 0 3 3
1511 3 my $ret = $self->__delete;
1512  
1513 3 my $after_ret
1514 = $self->_run_callback( name => 'after_delete', args => \$ret );
1515 3 ▶ return $after_ret unless ( defined $after_ret );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $after_ret 0 3 3
1516 3 return ($ret);
1517  
1518 }
1519  
1520 sub __delete {
1521 3 ▶ my $self = shift;
Subroutine __delete: called CC=2 SCAR=7.2
1522  
1523 #TODO Check to make sure the key's not already listed.
1524 #TODO Update internal data structure
1525  
1526 ## Constructs the where clause.
1527 3 my %pkeys = $self->primary_keys();
1528 3 my $return = $self->_handle->delete( $self->table, $self->primary_keys );
1529  
1530 3 ▶ if ( UNIVERSAL::isa( 'Class::ReturnValue', $return ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa('Class::ReturnValue', $return)) { } 0 3 3
1531 0 return ($return);
1532 } else {
1533 3 return (1);
1534 }
1535 }
1536  
1537 =head2 table
1538  
1539 This method returns this class's default table name. It uses
1540 Lingua::EN::Inflect to pluralize the class's name as we believe that
1541 class names for records should be in the singular and table names
1542 should be plural.
1543  
1544 If your class name is C<My::App::Rhino>, your table name will default
1545 to C<rhinos>. If your class name is C<My::App::RhinoOctopus>, your
1546 default table name will be C<rhino_octopuses>. Not perfect, but
1547 arguably correct.
1548  
1549 =cut
1550  
1551 sub table {
1552 683 ▶ my $self = shift;
Subroutine table: called CC=2 SCAR=6.9
Pod documented
1553 683 ▶ $self->TABLE_NAME( $self->_guess_table_name )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->TABLE_NAME 38 645 683
1554 unless ( $self->TABLE_NAME() );
1555 683 return $self->TABLE_NAME();
1556 }
1557  
1558 =head2 collection_class
1559  
1560 Returns the collection class which this record belongs to; override
1561 this to subclass. If you haven't specified a collection class, this
1562 returns a best guess at the name of the collection class for this
1563 collection.
1564  
1565 It uses a simple heuristic to determine the collection class name --
1566 It appends "Collection" to its own name. If you want to name your
1567 records and collections differently, go right ahead, but don't say we
1568 didn't warn you.
1569  
1570 =cut
1571  
1572 sub collection_class {
1573 0 ▶ my $self = shift;
Subroutine collection_class: not called CC=2 SCAR=17.9
Pod documented
1574 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1575 0 $class . 'Collection';
1576 }
1577  
1578 =head2 _guess_table_name
1579  
1580 Guesses a table name based on the class's last part.
1581  
1582  
1583 =cut
1584  
1585 sub _guess_table_name {
1586 38 ▶ my $self = shift;
Subroutine _guess_table_name: called CC=3 SCAR=11.0
1587 38 ▶ my $class = ref($self) ? ref($self) : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 36 2 38
1588 38 ▶ die "Couldn't turn " . $class . " into a table name"
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /(?:\:\:)?(\w+)$/ 0 38 38
1589 unless ( $class =~ /(?:\:\:)?(\w+)$/ );
1590 38 my $table = $1;
1591 38 $table =~ s/(?<=[a-z])([A-Z]+)/"_" . lc($1)/eg;
1592 38 $table =~ tr/A-Z/a-z/;
1593 38 $table = Lingua::EN::Inflect::PL_N($table);
1594 38 return ($table);
1595  
1596 }
1597  
1598 =head2 _handle
1599  
1600 Returns or sets the current Jifty::DBI::Handle object
1601  
1602 =cut
1603  
1604 sub _handle {
1605 2242 ▶ my $self = shift;
Subroutine _handle: called CC=2 SCAR=6.9
1606 2242 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 365 1877 2242
1607 365 $self->{'DBIxHandle'} = shift;
1608 }
1609 2242 return ( $self->{'DBIxHandle'} );
1610 }
1611  
1612 =head2 PRIVATE refers_to
1613  
1614 used for the declarative syntax
1615  
1616 =cut
1617  
1618 sub _filters {
1619 553 ▶ my $self = shift;
Subroutine _filters: called CC=2 SCAR=6.9
1620 553 my %args = ( direction => 'input', column => undef, @_ );
1621  
1622 553 ▶ if ( $args{'direction'} eq 'input' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'direction'} eq 'input') { } 338 215 553
1623 return grep $_, map $_->input_filters,
1624 338 ( $self, $args{'column'}, $self->_handle );
1625 } else {
1626 return grep $_, map $_->output_filters,
1627 215 ( $self->_handle, $args{'column'}, $self );
1628 }
1629 }
1630  
1631 sub _apply_input_filters {
1632 338 ▶ return (shift)->_apply_filters( direction => 'input', @_ );
Subroutine _apply_input_filters: called CC=1 SCAR=0.0
1633 }
1634  
1635 sub _apply_output_filters {
1636 215 ▶ return (shift)->_apply_filters( direction => 'output', @_ );
Subroutine _apply_output_filters: called CC=1 SCAR=0.0
1637 }
1638  
1639 30 { my %cache = ();
1640 sub _apply_filters {
1641 553 ▶ my $self = shift;
Subroutine _apply_filters: called CC=6 SCAR=19.2
1642 553 my %args = (
1643 direction => 'input',
1644 column => undef,
1645 value_ref => undef,
1646 @_
1647 );
1648  
1649 553 my @filters = $self->_filters(%args);
1650 553 ▶ my $action = $args{'direction'} eq 'output' ? 'decode' : 'encode';
Branch: 1 branch
BranchTrueFalseTotal
$args{'direction'} eq 'output' ? : 215 338 553
1651 553 foreach my $filter_class (@filters) {
1652 818 ▶ unless ( exists $cache{ $filter_class } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $cache{$filter_class}) { } 773 45 818
(do { not $cache{$filter_class} }) 0 773 773
1653 45 local $UNIVERSAL::require::ERROR;
1654 45 $filter_class->require;
1655 45 ▶ if ($UNIVERSAL::require::ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UNIVERSAL::require::ERROR) 0 45 45
1656 0 warn $UNIVERSAL::require::ERROR;
1657 0 $cache{ $filter_class } = 0;
1658 0 next;
1659 }
1660 45 $cache{ $filter_class } = 1;
1661 }
1662 0 elsif ( !$cache{ $filter_class } ) {
1663 0 next;
1664 }
1665  
1666 my $filter = $filter_class->new(
1667 record => $self,
1668 column => $args{'column'},
1669 818 value_ref => $args{'value_ref'},
1670 handle => $self->_handle,
1671 );
1672  
1673 # XXX TODO error proof this
1674 818 $filter->$action();
1675 }
1676 } }
1677  
1678 =head2 is_distinct COLUMN_NAME, VALUE
1679  
1680 Checks to see if there is already a record in the database where
1681 COLUMN_NAME equals VALUE. If no such record exists then the
1682 COLUMN_NAME and VALUE pair is considered distinct and it returns 1.
1683 If a value is already present the test is considered to have failed
1684 and it returns a L<Class::ReturnValue> with the error.
1685  
1686 =cut
1687  
1688 30 sub is_distinct {
1689 5 ▶ my $self = shift;
Subroutine is_distinct: called CC=2 SCAR=6.9
Pod documented
1690 5 my $column = shift;
1691 5 my $value = shift;
1692  
1693 5 my $record = $self->new( $self->_new_record_args );
1694 5 $record->load_by_cols( $column => $value );
1695  
1696 5 my $ret = Class::ReturnValue->new();
1697  
1698 5 ▶ if ( $record->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->id) { } 2 3 5
1699 2 $ret->as_array( 0, "Value already exists for unique column $column" );
1700 2 $ret->as_error(
1701 errno => 3,
1702 do_backtrace => 0,
1703 message => "Value already exists for unique column $column",
1704 );
1705 2 return ( $ret->return_value );
1706 } else {
1707 3 return (1);
1708 }
1709 }
1710  
1711 =head2 run_canonicalization_for_column column => 'COLUMN', value => 'VALUE'
1712  
1713 Runs all canonicalizers for the specified column.
1714  
1715 =cut
1716  
1717 sub run_canonicalization_for_column {
1718 0 ▶ my $self = shift;
Subroutine run_canonicalization_for_column: not called CC=3 SCAR=24.8
Pod documented
1719 0 my %args = (
1720 column => undef,
1721 value => undef,
1722 extra => [],
1723 @_
1724 );
1725  
1726 my ( $ret, $value_ref ) = $self->_run_callback(
1727 name => "canonicalize_" . $args{'column'},
1728 args => $args{'value'},
1729 0 extra => $args{'extra'},
1730 short_circuit => 0,
1731 );
1732 0 ▶ return unless defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 0 0
1733 return (
1734 exists $value_ref->[-1]->[0]
1735 ? $value_ref->[-1]->[0]
1736 0 ▶ : $args{'value'}
Branch: 1 branch
BranchTrueFalseTotal
exists $value_ref->[-1][0] ? : 0 0 0
1737 );
1738 }
1739  
1740 =head2 has_canonicalizer_for_column COLUMN
1741  
1742 Returns true if COLUMN has a canonicalizer, otherwise returns undef.
1743  
1744 =cut
1745  
1746 sub has_canonicalizer_for_column {
1747 0 ▶ my $self = shift;
Subroutine has_canonicalizer_for_column: not called CC=3 SCAR=24.8
Pod documented
1748 0 my $key = shift;
1749 0 my $method = "canonicalize_$key";
1750 0 ▶ if ( $self->can($method) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can($method)) { } 0 0 0
elsif (my(@sighs) = Class::Trigger::__fetch_all_triggers($self, $method)) { } 0 0 0
1751 0 return 1;
1752 # We have to force context here because we're reaching inside Class::Trigger
1753 } elsif ( my @sighs = Class::Trigger::__fetch_all_triggers($self, $method) ) {
1754 0 return 1;
1755 } else {
1756 0 return undef;
1757 }
1758 }
1759  
1760 =head2 run_validation_for_column column => 'COLUMN', value => 'VALUE' [extra => \@ARGS]
1761  
1762 Runs all validators for the specified column.
1763  
1764 =cut
1765  
1766 sub run_validation_for_column {
1767 0 ▶ my $self = shift;
Subroutine run_validation_for_column: not called CC=2 SCAR=17.9
Pod documented
1768 0 my %args = (
1769 column => undef,
1770 value => undef,
1771 extra => [],
1772 @_
1773 );
1774 0 my $key = $args{'column'};
1775 0 my $attr = $args{'value'};
1776  
1777 my ( $ret, $results )
1778 = $self->_run_callback(
1779 name => "validate_" . $key,
1780 args => $attr,
1781 0 extra => $args{'extra'},
1782 );
1783  
1784 0 ▶ if ( defined $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret) { } 0 0 0
1785 0 return ( 1, 'Validation ok' );
1786 } else {
1787 0 return ( @{ $results->[-1] } );
1788 }
1789  
1790 }
1791  
1792 =head2 has_validator_for_column COLUMN
1793  
1794 Returns true if COLUMN has a validator, otherwise returns undef.
1795  
1796 =cut
1797  
1798 sub has_validator_for_column {
1799 0 ▶ my $self = shift;
Subroutine has_validator_for_column: not called CC=3 SCAR=24.8
Pod documented
1800 0 my $key = shift;
1801 0 my $method = "validate_$key";
1802 0 ▶ if ( $self->can( $method ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->can($method)) { } 0 0 0
elsif (my(@sighs) = Class::Trigger::__fetch_all_triggers($self, $method)) { } 0 0 0
1803 0 return 1;
1804 # We have to force context here because we're reaching inside Class::Trigger
1805 } elsif ( my @sighs = Class::Trigger::__fetch_all_triggers($self, $method) ) {
1806 0 return 1;
1807 } else {
1808 0 return undef;
1809 }
1810 }
1811  
1812 sub _run_callback {
1813 795 ▶ my $self = shift;
Subroutine _run_callback: called CC=6 SCAR=24.6
1814 795 my %args = (
1815 name => undef,
1816 args => undef,
1817 short_circuit => 1,
1818 extra => [],
1819 @_
1820 );
1821  
1822 795 my $ret;
1823 795 my $method = $args{'name'};
1824 795 my @results;
1825 795 ▶ if ( my $func = $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $func = $self->can($method)) 0 795 795
1826 0 @results = $func->( $self, $args{args}, @{$args{'extra'}} );
1827 return ( wantarray ? ( undef, [ [@results] ] ) : undef )
1828 0 ▶ if $args{short_circuit} and not $results[0];
Branch: 2 branches
BranchTrueFalseTotal
if $args{"short_circuit"} and not $results[0] 0 0 0
wantarray ? : 0 0 0
Condition0%0/3
$args{"short_circuit"} and not $results[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"short_circuit"} and not $results[0]
$args{"short_circuit"} not $results[0]
Truth table0%0/3
A and not(B)
A= $args{"short_circuit"}B= not $results[0]
ABresult
0X0
100
111
1829 }
1830 795 $ret = $self->call_trigger( $args{'name'} => $args{args}, @{$args{'extra'}} );
1831 return (
1832 wantarray
1833 795 ▶ ? ( $ret, [ [@results], @{ $self->last_trigger_results } ] )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 795 795
1834 : $ret
1835 );
1836 }
1837  
1838 =head2 unload_value COLUMN
1839  
1840 Purges the cached value of COLUMN from the object, forcing it to be
1841 fetched from the database next time it is queried.
1842  
1843 =cut
1844  
1845 sub unload_value {
1846 0 ▶ my $self = shift;
Subroutine unload_value: not called CC=2 SCAR=17.9
Pod documented
1847 0 my $column = shift;
1848 0 delete $self->{$_}{$column} for qw/values raw_values fetched decoded _prefetched/;
1849 }
1850  
1851 30 1;
1852  
1853 __END__
1854  
1855  
1856  
1857 =head1 AUTHOR
1858  
1859 Jesse Vincent <jesse@bestpractical.com>, Alex Vandiver
1860 <alexmv@bestpractical.com>, David Glasser <glasser@bestpractical.com>,
1861 Ruslan Zakirov <ruslan.zakirov@gmail.com>
1862  
1863 Based on DBIx::SearchBuilder::Record, whose credits read:
1864  
1865 Jesse Vincent, <jesse@fsck.com>
1866 Enhancements by Ivan Kohler, <ivan-rt@420.am>
1867 Docs by Matt Knopp <mhat@netlag.com>
1868  
1869 =head1 SEE ALSO
1870  
1871 L<Jifty::DBI>, L<Jifty::DBI::Handle>, L<Jifty::DBI::Collection>.
1872  
1873 =cut
« DBI/Collection/Union.pm ↑ Summary DBI/Handle/ODBC.pm »