DBO2/Record.pm

filter: Statementstmt 33.1% 53 / 160 Branchbran 2.5% 1 / 40 Conditioncond 2.5% 1 / 39 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 48.5% 17 / 35 Podpod 80.0% 12 / 15 totaltotal 26.7% 84 / 314 CC 34 SCAR 25.0
CC
34
Cov
23%
fetch_one
30.0
fetch_id
20.0
refetch_record
20.0
CRAP 12.1
« DBO2/Fields.pm ↑ Summary DBO2/RecordSet.pm »
1 =head1 NAME
2  
3 DBIx::DBO2::Record - A row in a table in a datasource
4  
5 =head1 SYNOPSIS
6  
7 package MyRecord;
8 use DBIx::DBO2::Record '-isasubclass';
9 use DBIx::DBO2::Fields (
10 'sequential' => 'id',
11 'string -length 64' => 'name',
12 'timestamp --modified' => 'lastupdate',
13 );
14
15 my $sqldb = DBIx::SQLEngine->new( ... );
16 MyRecord->table( $sqldb->table( 'foo' );
17 unless ( MyRecord->table->table_exists ) {
18 MyRecord->table->create_table( MyRecord->field_columns );
19 }
20  
21 my $record = MyRecord->new( name => 'Dave' );
22 $record->save_record;
23
24 my $results = MyRecord->fetch_records( criteria => { name => 'Dave' } );
25 foreach my $rec ( $results->records ) {
26 print $rec->name() . ' ' . $rec->lastupdate_readable();
27 }
28  
29 =head1 DESCRIPTION
30  
31 The DBIx::DBO2::Record class represents database records in tables accessible via DBIx::SQLEngine.
32  
33 By subclassing this package, you can easily create a class whose instances represent each of the rows in a SQL database table.
34  
35 =cut
36  
37 package DBIx::DBO2::Record;
38  
39 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 ########################################################################
43  
44 =head1 REFERENCE
45  
46 =cut
47  
48 1 ▶use Class::MakeMethods;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 1 ▶use DBIx::SQLEngine;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use DBIx::SQLEngine::Schema::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use DBIx::SQLEngine::Criteria;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 1 ▶use DBIx::DBO2::RecordSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 ########################################################################
57  
58 =head2 Subclass Factory
59  
60 =over 4
61  
62 =item import
63  
64 package My::Record;
65 use DBIx::DBO2::Record '-isasubclass';
66  
67 Allows for a simple declaration of inheritance.
68  
69 =back
70  
71 =cut
72  
73 1 ▶sub import {
Subroutine import: called CC=3 SCAR=13.6
74 1 my $class = shift;
75
76 1 ▶ if ( scalar @_ == 1 and $_[0] eq '-isasubclass' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and $_[0] eq "-isasubclass") 0 1 1
Condition33%1/3
scalar @_ == 1 and $_[0] eq "-isasubclass"
!ll&&!rl&&r
100
MC/DC0%0/2
scalar @_ == 1 and $_[0] eq "-isasubclass"
scalar @_ == 1 $_[0] eq "-isasubclass"
Truth table33%1/3
A and B
A= scalar @_ == 1B= $_[0] eq "-isasubclass"
ABresult
0X0
100
111
77 0 shift;
78 0 my $target_class = ( caller )[0];
79 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 0 push @{"$target_class\::ISA"}, $class;
81 }
82
83 1 $class->SUPER::import( @_ );
84 }
85  
86 ########################################################################
87  
88 # =item type - Template::ClassName:subclass_name
89 #
90 # Access subclasses by name.
91  
92 # Class::MakeMethods->make(
93 # 'Template::ClassName:subclass_name' => 'type',
94 # );
95  
96 ########################################################################
97  
98 =head2 Table and SQLEngine
99  
100 Each Record class stores a reference to the table its instances are stored in.
101  
102 =over 4
103  
104 =item table
105  
106 RecordClass->table ( $table )
107 RecordClass->table () : $table
108  
109 Establishes the table a specific class of record will be stored in.
110  
111 =item count_rows
112  
113 RecordClass->count_rows () : $integer
114  
115 Delegated to table.
116  
117 =item datasource
118  
119 RecordClass->datasource () : $datasource
120  
121 Delegated to table. Returns the table's SQLEngine.
122  
123 =item do_sql
124  
125 RecordClass->do_sql ( $sql_statement )
126  
127 Delegated to datasource.
128  
129 =back
130  
131 =cut
132  
133 1 Class::MakeMethods->make(
134 'Template::ClassInherit:object' => [ table => {class=>'DBIx::SQLEngine::Schema::Table'} ],
135 'Standard::Universal:delegate' => [
136 [ qw( count_rows datasource do_sql column_primary_name ) ] => { target=>'table' },
137 ],
138 );
139  
140 0 ▶sub demand_table {
Subroutine demand_table: not called CC=3 SCAR=24.8
Pod undocumented
141 0 my $self = shift;
142 0 ▶ $self->table() or croak("No table set for " . ( ref( $self ) || $self ));
Branch: 1 branch
BranchTrueFalseTotal
$self->table or croak('No table set for ' . (ref $self || $self)) 0 0 0
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
143 }
144  
145 ########################################################################
146  
147 =head2 Hooks
148  
149 Many of the methods below are labeled "Inheritable Hook." These methods allow you to register callbacks which are then invoked at specific points in each record's lifecycle. You can add these callbacks to all record classes, to a particular class, or even to a particular object instance.
150  
151 To register a callback, call the install_hooks method, and pass it pairs of a hook method name, and a subroutine reference, as follows: I<callee>->install_hooks( I<methodname> => I<coderef>, ... ).
152  
153 Here are a few examples to show the possibilities this provides you with:
154  
155 =over 4
156  
157 =item *
158  
159 To have each record write to a log when it's loaded from the database:
160  
161 sub log_fetch { my $record = shift; warn "Loaded record $record->{id}" } );
162 MyClass->install_hooks( post_fetch => \&log_fetch );
163  
164 =item *
165  
166 To make a class "read-only" by preventing all inserts, updates, and deletes:
167  
168 my $refusal = sub { return 0 };
169 MyClass->install_hooks(
170 ok_insert => $refusal,
171 ok_update => $refusal,
172 ok_delete => $refusal,
173 );
174  
175 =item *
176  
177 To have a particular record automatically save any changes you've made to it when it goes out of scope:
178  
179 my $record = MyClass->fetch_one( ... );
180 my $saver = sub { my $record = shift; $record->save_record };
181 $record->install_hooks( pre_destroy => $saver );
182  
183 =back
184  
185 =cut
186  
187 0 ▶sub install_hooks {
Subroutine install_hooks: not called CC=2 SCAR=17.9
Pod undocumented
188 0 my $callee = shift;
189 0 ▶ while ( my( $method_name, $code_ref ) = splice( @_, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
190 0 $callee->$method_name(
191 Class::MakeMethods::Composite::Inheritable->Hook( $code_ref )
192 );
193 }
194 }
195  
196 ########################################################################
197  
198 =head2 Constructor
199  
200 Record objects are constructed when they are fetched from their table as described in the next section, or you may create your own for new instances.
201  
202 =over 4
203  
204 =item new
205  
206 my $obj = MyRecord->new( method1 => value1, ... );
207  
208 my $shallow_copy = $record->new;
209  
210 Create a new instance.
211 (Class::MakeMethods::Standard::Hash:new).
212  
213 =item clone
214  
215 my $similar_record = $record->clone;
216  
217 Makes a copy of a record and then clears its id so that it will be recognized as a distinct, new row in the database rather than overwriting the original when you save it.
218  
219 =item post_new
220  
221 Inheritable Hook. Subclasses should override this with any functions they wish performed immediately after each record is created and initialized.
222  
223 =back
224  
225 =cut
226  
227 1 ▶use Class::MakeMethods::Composite::Hash (
Subroutine BEGIN: called CC=1 SCAR=0.2
228 0 ▶ 'new' => [ 'new' => { post_rules => [ sub { map $_->post_new, Class::MakeMethods::Composite->CurrentResults } ] } ],
Subroutine __ANON__: not called CC=1 SCAR=1.2
229 1 );
230  
231 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'post_new' );
Subroutine BEGIN: called CC=1 SCAR=0.0
232  
233 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
234 0 my $callee = shift;
235 0 $callee->new( $callee->column_primary_name() => '', @_ );
236 }
237  
238 ########################################################################
239  
240 =head2 Selecting Records
241  
242 =over 4
243  
244 =item fetch_records
245  
246 $recordset = My::Students->fetch_records( criteria => {status=>'active'} );
247  
248 Fetch all matching records and return them in a RecordSet.
249  
250 =item fetch_one
251  
252 $dave = My::Students->fetch_one( criteria => { name => 'Dave' } );
253  
254 Fetch a single matching record.
255  
256 =item fetch_id
257  
258 $prisoner = My::Students->fetch_id( 6 );
259  
260 Fetch a single record based on its primary key.
261  
262 =item visit_records
263  
264 @results = My::Students->visit_records( \&mysub, criteria=> ... );
265  
266 Calls the provided subroutine on each matching record as it is retrieved. Returns the accumulated results of each subroutine call (in list context).
267  
268 =item refetch_record
269  
270 $record->refetch_record();
271  
272 Re-retrieve the values for this record from the database based on its primary key.
273  
274 =item post_fetch
275  
276 Inheritable Hook. Subclasses should override this with any functions they wish performed immediately after each record is retrieved from the database.
277  
278 =back
279  
280 =cut
281  
282 1 ▶use Class::MakeMethods::Composite::Inheritable( hook=>'post_fetch' );
Subroutine BEGIN: called CC=1 SCAR=0.0
283  
284 0 ▶sub fetch_records {
Subroutine fetch_records: not called CC=3 SCAR=24.8
Pod documented
285 0 my $record_or_class = shift;
286 0 ▶ my $class = ref( $record_or_class ) || $record_or_class;
Condition0%0/3
ref $record_or_class || $record_or_class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $record_or_class || $record_or_class
ref $record_or_class $record_or_class
Truth table0%0/3
A || B
A= ref $record_or_classB= $record_or_class
ABresult
1X1
011
000
287 0 ▶ my $table = $record_or_class->table() or croak("No table set for $class");
Branch: 1 branch
BranchTrueFalseTotal
my $table = $record_or_class->table or croak("No table set for $class") 0 0 0
288 0 my $records = $table->fetch_select( @_ );
289 0 bless [ map { bless $_, $class; $_->post_fetch; $_ } @$records ], 'DBIx::DBO2::RecordSet';
290 }
291  
292 0 ▶sub visit_records {
Subroutine visit_records: not called CC=3 SCAR=24.8
Pod documented
293 0 my $record_or_class = shift;
294 0 ▶ my $class = ref( $record_or_class ) || $record_or_class;
Condition0%0/3
ref $record_or_class || $record_or_class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $record_or_class || $record_or_class
ref $record_or_class $record_or_class
Truth table0%0/3
A || B
A= ref $record_or_classB= $record_or_class
ABresult
1X1
011
000
295 0 ▶ my $table = $record_or_class->table() or croak("No table set for $class");
Branch: 1 branch
BranchTrueFalseTotal
my $table = $record_or_class->table or croak("No table set for $class") 0 0 0
296 0 my $sub = shift;
297 0 ▶ my $func = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 0 my $record = shift;
299 0 bless $record, $class;
300 0 $record->post_fetch;
301 0 &$sub( $record )
302 0 };
303 0 $table->visit_select( $func, @_ );
304 }
305  
306 0 ▶sub fetch_one {
Subroutine fetch_one: not called CC=5 SCAR=34.0
Pod documented
307 0 my $record_or_class = shift;
308 0 ▶ my $class = ref( $record_or_class ) || $record_or_class;
Condition0%0/3
ref $record_or_class || $record_or_class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $record_or_class || $record_or_class
ref $record_or_class $record_or_class
Truth table0%0/3
A || B
A= ref $record_or_classB= $record_or_class
ABresult
1X1
011
000
309 0 ▶ my $table = $record_or_class->table() or croak("No table set for $class");
Branch: 1 branch
BranchTrueFalseTotal
my $table = $record_or_class->table or croak("No table set for $class") 0 0 0
310
311 0 my $records = $table->fetch_select( @_ );
312 0 ▶ ( scalar @$records < 2 ) or
Branch: 1 branch
BranchTrueFalseTotal
scalar @$records < 2 or carp('Multiple matches for fetch_one: ' . join(", ", map(("'${_}'"), @_))) 0 0 0
313 carp "Multiple matches for fetch_one: " . join(', ', map "'$_'", @_ );
314
315 0 ▶ my $record = $records->[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $record = $records->[0] or return 0 0 0
316 0 bless $record, $class;
317 0 $record->post_fetch;
318 0 $record;
319 }
320  
321 0 ▶sub fetch_id {
Subroutine fetch_id: not called CC=4 SCAR=30.0
Pod documented
322 0 my $record_or_class = shift;
323 0 ▶ my $class = ref( $record_or_class ) || $record_or_class;
Condition0%0/3
ref $record_or_class || $record_or_class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $record_or_class || $record_or_class
ref $record_or_class $record_or_class
Truth table0%0/3
A || B
A= ref $record_or_classB= $record_or_class
ABresult
1X1
011
000
324 0 ▶ my $table = $record_or_class->table() or croak("No table set for $class");
Branch: 1 branch
BranchTrueFalseTotal
my $table = $record_or_class->table or croak("No table set for $class") 0 0 0
325 0 ▶ my $record = $table->fetch_id( @_ ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $record = $table->fetch_id(@_) or return 0 0 0
326 0 bless $record, $class;
327 0 $record->post_fetch;
328 0 $record;
329 }
330  
331 0 ▶sub refetch_record {
Subroutine refetch_record: not called CC=4 SCAR=30.0
Pod documented
332 0 my $self = shift();
333 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
334 0 ▶ my $table = $self->table() or croak("No table set for $class");
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->table or croak("No table set for $class") 0 0 0
335 0 my $id = $self->{ $table->column_primary_name() };
336 0 ▶ my $db_row = $table->fetch_id( $id )
Branch: 1 branch
BranchTrueFalseTotal
my $db_row = $table->fetch_id($id) or confess() 0 0 0
337 or confess;
338 0 %$self = %$db_row;
339 0 $self->post_fetch;
340 0 $self;
341 }
342  
343 ########################################################################
344  
345 =head2 Row Inserts
346  
347 After constructing a record with new(), you may save any changes by calling insert_record.
348  
349 =over 4
350  
351 =item insert_record
352  
353 Attempt to insert the record into the database.
354  
355 $record->insert_record () : $record_or_undef
356  
357 Calls ok_insert to ensure that it's OK to insert this row, and aborts if any of hook subroutines return 0.
358  
359 Calls any pre_insert hooks, then calls its table's insert_row method, then calls any post_insert hooks.
360  
361 Returns undef if the update was aborted, or the record if the insert was successful.
362  
363 =item ok_insert
364  
365 Inheritable Hook. Subclasses should override this with any functions they wish performed to validate rows before they are inserted.
366  
367 =item pre_insert
368  
369 Inheritable Hook. Subclasses should override this with any functions they wish performed immediately before a row is inserted.
370  
371 =item post_insert
372  
373 Inheritable Hook. Subclasses should override this with any functions they wish performed after a row is inserted.
374  
375 =back
376  
377 =cut
378  
379 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'ok_insert pre_insert post_insert');
Subroutine BEGIN: called CC=1 SCAR=0.0
380  
381 # $record->insert_record()
382 0 ▶sub insert_record {
Subroutine insert_record: not called CC=4 SCAR=30.0
Pod documented
383 0 my $self = shift;
384 0 ▶ my $class = ref( $self ) or croak("Not a class method");
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or croak("Not a class method") 0 0 0
385 0 my $table = $class->demand_table();
386 0 my @flags = $self->ok_insert();
387 0 ▶ if ( grep { length $_ and ! $_ } @flags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {length $_ and not $_;} @flags) 0 0 0
Condition0%0/3
length $_ && !$_
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ && !$_
length $_ !$_
Truth table0%0/3
A && not(B)
A= length $_B= !$_
ABresult
0X0
100
111
388 # warn "Cancelling insert of $self, flags are " . join(', ', map "'$_'", @flags);
389 0 return undef;
390 }
391 0 $self->pre_insert();
392 0 $table->insert_row( $self );
393 0 $self->post_insert();
394 0 $self;
395 }
396  
397 ########################################################################
398  
399 =head2 Row Updates
400  
401 After retrieving a record with one of the fetch methods, you may save any changes by calling update_record.
402  
403 =over 4
404  
405 =item update_record
406  
407 Attempts to update the record using its primary key as a unique identifier.
408  
409 $record->update_record () : $record_or_undef
410  
411 Calls ok_update to ensure that it's OK to update this row, and aborts if any of hook subroutines return 0.
412  
413 Calls any pre_update hooks, then calls its table's update_row method, then calls any post_update hooks.
414  
415 Returns undef if the update was aborted, or the record if the update was successful.
416  
417 =item ok_update
418  
419 Inheritable Hook. Subclasses should override this with any functions they wish to use to validate rows before they are updated. Return 0 to abort the update.
420  
421 =item pre_update
422  
423 Inheritable Hook. Subclasses should override this with any functions they wish performed immediately before a row is updated.
424  
425 =item post_update
426  
427 Inheritable Hook. Subclasses should override this with any functions they wish performed immediately after a row is updated.
428  
429 =back
430  
431 =cut
432  
433 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'ok_update pre_update post_update');
Subroutine BEGIN: called CC=1 SCAR=0.0
434  
435 # $record->update_record()
436 0 ▶sub update_record {
Subroutine update_record: not called CC=4 SCAR=30.0
Pod documented
437 0 my $self = shift;
438 0 ▶ my $class = ref( $self ) or croak("Not a class method");
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or croak("Not a class method") 0 0 0
439 0 my $table = $class->demand_table();
440 0 my @flags = $self->ok_update;
441 0 ▶ if ( grep { length $_ and ! $_ } @flags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {length $_ and not $_;} @flags) 0 0 0
Condition0%0/3
length $_ && !$_
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ && !$_
length $_ !$_
Truth table0%0/3
A && not(B)
A= length $_B= !$_
ABresult
0X0
100
111
442 # warn "Cancelling update of $self, flags are " . join(', ', map "'$_'", @flags );
443 0 return undef;
444 }
445 # warn "About to update $self, flags are " . join(', ', map "'$_'", @flags );
446 0 $self->pre_update();
447 0 $table->update_row( $self );
448 0 $self->post_update();
449 0 $self;
450 }
451  
452 ########################################################################
453  
454 =head2 Deletion
455  
456 =over 4
457  
458 =item delete_record
459  
460 $record->delete_record () : $boolean_completed
461  
462 Checks to see if any of the pre_delete results is "0". If not, asks the table to delete the row.
463  
464 Returns 1 if the deletion was successful, or 0 if it was aborted.
465  
466 =item ok_delete
467  
468 $record->ok_delete () : @booleans
469  
470 Inheritable Hook. Subclasses should override this with any functions they wish to use to validate rows before they are updated. Return 0 to abort the deletion.
471  
472 =item pre_delete
473  
474 $record->pre_delete ()
475  
476 Inheritable Hook. Subclasses should override this with any functions they wish performed before a row is deleted.
477  
478 =item post_delete
479  
480 $record->post_delete ()
481  
482 Inheritable Hook. Subclasses should override this with any functions they wish performed after a row is deleted.
483  
484 =back
485  
486 =cut
487  
488 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'ok_delete pre_delete post_delete');
Subroutine BEGIN: called CC=1 SCAR=0.0
489  
490 # $success = $record->delete_record();
491 0 ▶sub delete_record {
Subroutine delete_record: not called CC=3 SCAR=24.8
Pod documented
492 0 my $self = shift;
493 0 my @flags = $self->ok_delete;
494 0 ▶ if ( grep { length $_ and ! $_ } @flags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {length $_ and not $_;} @flags) 0 0 0
Condition0%0/3
length $_ && !$_
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ && !$_
length $_ !$_
Truth table0%0/3
A && not(B)
A= length $_B= !$_
ABresult
0X0
100
111
495 # warn "Cancelling delete of $self, flags are " . join(', ', map "'$_'", @flags );
496 0 return 0;
497 }
498 # warn "About to delete $self, flags are " . join(', ', map "'$_'", @flags );
499 0 $self->pre_delete();
500 0 $self->table->delete_row($self);
501 0 $self->post_delete();
502 0 return 1;
503 }
504  
505 ########################################################################
506  
507 =head2 Load and Save Wrappers
508  
509 Wrappers for new/fetch and insert/update.
510  
511 =over 4
512  
513 =item get_record
514  
515 RecordClass->get_record ( $id_or_undef ) : $new_or_fetched_record_or_undef
516  
517 Calls new if no ID is provided, or if the ID is the special string "-new"; otherwise calls fetch_id.
518  
519 =item save_record
520  
521 $record->save_record () : $record_or_undef
522  
523 Determines whether the record has an id assigned to it and then calls either insert_record or update_record. Returns the record unless it fails to save the record.
524  
525 =back
526  
527 =cut
528  
529 # $record = $package->get_record()
530 # $record = $package->get_record( $id )
531 0 ▶sub get_record {
Subroutine get_record: not called CC=4 SCAR=30.0
Pod documented
532 0 my $package = shift;
533 0 my $id = shift;
534 0 ▶ if ( ! $id or $id eq "new" or $id eq "-new" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $id or $id eq 'new' or $id eq '-new') { } 0 0 0
Condition0%0/6
not $id or $id eq "new" or $id eq "-new"
l!l&&r!l&&!r
000
not $id or $id eq "new"
l!l&&r!l&&!r
000
MC/DC0%0/3
not $id or $id eq "new" or $id eq "-new"
not $id $id eq "new" $id eq "-new"
Truth table0%0/4
not(A) or B or C
A= not $idB= $id eq "new"C= $id eq "-new"
ABCresult
1XX1
01X1
0011
0000
535 0 $package->new();
536 } else {
537 0 $package->fetch_id( $id );
538 }
539 }
540  
541 # $record->save_record()
542 0 ▶sub save_record {
Subroutine save_record: not called CC=4 SCAR=30.0
Pod documented
543 0 my $self = shift;
544 0 ▶ if ( $self->{id} and $self->{id} eq 'new' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"id"} and $self->{"id"} eq "new") 0 0 0
Condition0%0/3
$self->{"id"} and $self->{"id"} eq "new"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"id"} and $self->{"id"} eq "new"
$self->{"id"} $self->{"id"} eq "new"
Truth table0%0/3
A and B
A= $self->{"id"}B= $self->{"id"} eq "new"
ABresult
0X0
100
111
545 0 undef $self->{id};
546 }
547 0 ▶ if ( $self->{ $self->column_primary_name() } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{$self->column_primary_name}) { } 0 0 0
548 0 $self->update_record( @_ );
549 } else {
550 0 $self->insert_record( @_ );
551 }
552 0 $self;
553 }
554  
555 ########################################################################
556  
557 =head2 Modification Wrappers
558  
559 Simple interface for applying changes.
560  
561 =over 4
562  
563 =item call_methods
564  
565 $record->call_methods( method1 => value1, ... );
566  
567 Call provided method names with supplied values.
568 (Class::MakeMethods::Standard::Universal:call_methods).
569  
570 =item change_and_save
571  
572 RecordClass->new_and_save ( %method_argument_pairs ) : $record
573  
574 Calls call_methods, and then save_record.
575  
576 =item change_and_save
577  
578 $record->change_and_save ( %method_argument_pairs ) : $record
579  
580 Calls call_methods, and then save_record.
581  
582 =back
583  
584 =cut
585  
586 1 ▶use Class::MakeMethods::Standard::Universal ( 'call_methods'=>'call_methods' );
Subroutine BEGIN: called CC=1 SCAR=0.0
587  
588 # $record->new_and_save( 'fieldname' => 'new_value', ... )
589 0 ▶sub new_and_save {
Subroutine new_and_save: not called CC=1 SCAR=6.9
Pod undocumented
590 0 my $callee = shift;
591 0 my $record = $callee->new( @_ );
592 0 $record->save_record;
593 0 $record;
594 }
595  
596 # $record->change_and_save( 'fieldname' => 'new_value', ... )
597 0 ▶sub change_and_save {
Subroutine change_and_save: not called CC=1 SCAR=6.9
Pod documented
598 0 my $record = shift;
599 0 $record->call_methods( @_ );
600 0 $record->save_record;
601 0 $record;
602 }
603  
604 ########################################################################
605  
606 =head2 Destructor
607  
608 Automatically invoked when record is being garbage collected.
609  
610 =over 4
611  
612 =item pre_destroy
613  
614 $record->pre_destroy ()
615  
616 Inheritable Hook. Subclasses should override this with any functions they wish called when an individual record is being garbage collected.
617  
618 =back
619  
620 =cut
621  
622 1 ▶use Class::MakeMethods::Composite::Inheritable(hook=>'pre_destroy');
Subroutine BEGIN: called CC=1 SCAR=0.0
623  
624 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
625 0 my $self = shift;
626 0 $self->pre_destroy();
627 }
628  
629 ########################################################################
630  
631 =head1 SEE ALSO
632  
633 See L<DBIx::DBO2> for an overview of this framework.
634  
635 =cut
636  
637 ########################################################################
638  
639 1 1;
« DBO2/Fields.pm ↑ Summary DBO2/RecordSet.pm »