SQLEngine/Record/Base.pm

filter: Statementstmt 63.7% 37 / 58 Branchbran 25.0% 6 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 72.2% 13 / 18 Podpod 100% 12 / 12 totaltotal 58.9% 69 / 117 CC 14 SCAR 14.9
CC
14
Cov
52%
get_values
12.0
record_as_db_data
12.0
new_copy
6.0
CRAP 4.5
« SQLEngine/Schema/ColumnSet.pm ↑ Summary SQLEngine/Schema/Table.pm »
1 =head1 NAME
2  
3 DBIx::SQLEngine::Record::Base - Base Class for Records
4  
5 =head1 SYNOPSIS
6  
7 B<Setup:> Several ways to create a class.
8  
9 my $sqldb = DBIx::SQLEngine->new( ... );
10  
11 $class_name = $sqldb->record_class( $table_name );
12
13 $sqldb->record_class( $table_name, $class_name );
14
15 package My::Record;
16 use DBIx::SQLEngine::Record::Class '-isasubclass', @Traits;
17  
18 B<Basics:> Common operations on a record.
19
20 $record = $class_name->new_with_values(somefield => 'My Value');
21
22 print $record->get_values( 'somefield' );
23  
24 $record->change_values( somefield => 'New Value' );
25  
26 B<Fetch:> Retrieve records by ID or other query.
27  
28 $record = $class_name->select_record( $primary_key );
29
30 @records = $class_name->fetch_select(%clauses)->records;
31  
32 B<Modify:> Write changes to the data source.
33  
34 $record->insert_record();
35
36 $record->update_record();
37
38 $record->delete_record();
39  
40  
41 =head1 DESCRIPTION
42  
43 DBIx::SQLEngine::Record::Base is a superclass for database records in tables accessible via DBIx::SQLEngine.
44  
45 By subclassing this package, you can easily create a class whose instances represent each of the rows in a SQL database table.
46  
47 =cut
48  
49 ########################################################################
50  
51 package DBIx::SQLEngine::Record::Base;
52  
53 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 ########################################################################
57  
58 ########################################################################
59  
60 =head1 SIMPLE RECORD OBJECT
61  
62 The following concrete methods provide a constructor, accessor, mutator and destructor for a record object.
63  
64 =head2 Constructor
65  
66 You may create your own records for new instances, or fetch records from the database as described in L</"FETCHING DATA">
67  
68 =over 4
69  
70 =item new_empty_record()
71  
72 $class_name->new_empty_record() : $empty_record
73  
74 Creates and blesses an empty hash object into the given record class.
75  
76 =back
77  
78 =cut
79  
80 # $record = $record_class->new_empty_record();
81 # $record = $record->new_empty_record();
82 18 ▶sub new_empty_record {
Subroutine new_empty_record: called CC=2 SCAR=7.6
Pod documented
83 18 my $self = shift;
84 18 ▶ my $class = ( ref($self) || $self );
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0180
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
85 18 bless {}, $class;
86 }
87  
88 ########################################################################
89  
90 =head2 Getting and Changing Values
91  
92 Records are stored as simple hashes, and their contents can typically
93 be accessed that way, but methods are also available to get and
94 set field values.
95  
96 =over 4
97  
98 =item get_values()
99  
100 $record->get_values( key1 ) : $value
101 $record->get_values( key1, key2, ... ) : $values_joined_with_comma
102 $record->get_values( key1, key2, ... ) : @values
103  
104 Returns the values associated with the keys in the provided record.
105  
106 =item change_values()
107  
108 $record->change_values( key1 => value1, ... )
109  
110 Sets the associated key-value pairs in the provided record.
111  
112 =back
113  
114 =cut
115  
116 0 ▶sub get_values {
Subroutine get_values: not called CC=3 SCAR=24.8
Pod documented
117 0 my $self = shift;
118 0 ▶ ref($self) or croak("Can't call this object method on a record class");
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("Can't call this object method on a record class") 0 0 0
119 0 my @values = @{$self}{ @_ };
120 0 ▶ wantarray ? @values : join(', ', @values)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
121 }
122  
123 17 ▶sub change_values {
Subroutine change_values: called CC=2 SCAR=7.1
Pod documented
124 17 my $self = shift;
125 17 ▶ ref($self) or croak("Can't call this object method on a record class");
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("Can't call this object method on a record class") 17 0 17
126 17 %$self = ( %$self, @_ )
127 }
128  
129 ########################################################################
130  
131 =head2 Vivifying and Serializing Records
132  
133 These methods are called internally by the various public methods and do not need to be called directly.
134  
135 =over 4
136  
137 =item record_from_db_data()
138  
139 $class_name->record_from_db_data( $hash_ref )
140 $class_name->record_from_db_data( $hash_ref ) : $record
141 $class_name->record_from_db_data( %hash_contents ) : $record
142  
143 Converts a hash retrieved from the table to a Record object.
144  
145 =item record_set_from_db_data()
146  
147 $class_name->record_set_from_db_data( $hash_array_ref )
148 $class_name->record_set_from_db_data( $hash_array_ref ) : $record_set
149 $class_name->record_set_from_db_data( @hash_refs ) : $record_set
150  
151 Converts an array of hashrefs retrieved from the table to a RecordSet::Set object containing Record objects.
152  
153 =item record_as_db_data()
154  
155 $record->record_as_db_data() : $hash_ref
156 $record->record_as_db_data() : %hash_values
157  
158 Returns an unblessed copy of the values in the record.
159  
160 =back
161  
162 =cut
163  
164 # $record_class->record_from_db_data( $hash_ref );
165 # $record = $record_class->record_from_db_data( $hash_ref );
166 # $record = $record_class->record_from_db_data( %hash_contents );
167 6 ▶sub record_from_db_data {
Subroutine record_from_db_data: called CC=3 SCAR=11.7
Pod documented
168 6 my $class = shift;
169 6 ▶ my $hash = ( @_ == 1 ) ? shift : { @_ }
Branch: 2 branches
BranchTrueFalseTotal
my $hash = @_ == 1 ? shift() : {@_} or return 6 0 6
@_ == 1 ? : 0 6 6
170 or return;
171 6 bless $hash, $class;
172 }
173  
174 # $record_class->record_set_from_db_data( $hash_array_ref );
175 # $record_set = $record_class->record_set_from_db_data( $hash_array_ref );
176 # $record_set = $record_class->record_set_from_db_data( @hash_refs );
177 10 ▶sub record_set_from_db_data {
Subroutine record_set_from_db_data: called CC=2 SCAR=7.6
Pod documented
178 10 my $class = shift;
179 10 ▶ my $array = ( @_ == 1 ) ? shift : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 10 0 10
180 0 bless [ map { bless $_, $class } @$array ], 'DBIx::SQLEngine::RecordSet::Set';
181 }
182  
183 # $hash_ref = $record_obj->record_as_db_data();
184 # %hash_list = $record_obj->record_as_db_data();
185 0 ▶sub record_as_db_data {
Subroutine record_as_db_data: not called CC=3 SCAR=24.8
Pod documented
186 0 my $self = shift;
187 0 ▶ ref($self) or croak("Can't call this object method on a record class");
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("Can't call this object method on a record class") 0 0 0
188 0 ▶ wantarray ? %$self : { %$self }
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
189 }
190  
191 ########################################################################
192  
193 =head2 Destructor
194  
195 =over 4
196  
197 =item DESTROY()
198  
199 $record->DESTROY()
200  
201 For internal use only. Does nothing. Subclasses can override this with any functions they wish called when an individual record is being garbage collected.
202  
203 =back
204  
205 =cut
206  
207 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
208 # Do nothing
209 }
210  
211 ########################################################################
212  
213 ########################################################################
214  
215 =head1 CONVENIENCE METHODS
216  
217 The following wrapper methods chain together combinations of other methods.
218  
219 =head2 Constructor Methods
220  
221 =item new_with_values()
222  
223 $class_name->new_with_values ( %key_argument_pairs ) : $record
224  
225 Calls new_empty_record, and then change_values.
226  
227 =item new_copy()
228  
229 $record->new_copy() : $new_record
230 $record->new_copy( %key_argument_pairs ) : $new_record
231  
232 Makes a copy of a record and then clears its primary key so that it will be recognized as a distinct, new row in the database rather than overwriting the original when you save it. Also includes any provided arguments in its call to new_with_values.
233  
234 =item get_record()
235  
236 $class_name->get_record ( ) : $new_empty_record
237 $class_name->get_record ( $p_key ) : $fetched_record_or_undef
238  
239 Calls new if no primary key is provided, or if the primary key is zero; otherwise calls select_record.
240  
241 =back
242  
243 =cut
244  
245 # $record = $record_class->new_with_values( 'fieldname' => 'new_value', ... )
246 18 ▶sub new_with_values {
Subroutine new_with_values: called CC=1 SCAR=0.0
Pod documented
247 18 my $self = (shift)->new_empty_record();
248 18 $self->change_values( @_ );
249 18 $self;
250 }
251  
252 # $record = $record->new_copy();
253 0 ▶sub new_copy {
Subroutine new_copy: not called CC=2 SCAR=17.9
Pod documented
254 0 my $self = shift;
255 0 ▶ ref($self) or croak("Can't call this object method on a record class");
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("Can't call this object method on a record class") 0 0 0
256 0 $self->new_with_values(
257 $self->record_as_db_data, $self->column_primary_name() => '', @_
258 )
259 }
260  
261 # $new_record = $package->get_record()
262 # $selected_record = $package->get_record( $id )
263 0 ▶sub get_record {
Subroutine get_record: not called CC=2 SCAR=17.9
Pod documented
264 0 my $package = shift;
265 0 my $id = shift;
266 0 ▶ if ( ! $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $id) { } 0 0 0
267 0 $package->new_empty_record();
268 } else {
269 0 $package->select_record( $id );
270 }
271 }
272  
273 ########################################################################
274  
275 =head2 Save Methods
276  
277 These methods hide the distinction between insert and update.
278  
279 =over 4
280  
281 =item save_record()
282  
283 $record->save_record () : $record_or_undef
284  
285 Determines whether the record has an primary key assigned to it and then calls either insert_record or update_record. Returns the record unless it fails to save it.
286  
287 =item new_and_save()
288  
289 $class_name->new_and_save ( %key_argument_pairs ) : $record
290  
291 Calls new_empty_record, and then change_and_save.
292  
293 =item change_and_save()
294  
295 $record->change_and_save ( %key_argument_pairs ) : $record
296  
297 Calls change_values, and then save_record.
298  
299 =back
300  
301 =cut
302  
303 # $record->save_record()
304 3 ▶sub save_record {
Subroutine save_record: called CC=3 SCAR=11.8
Pod documented
305 3 my $self = shift;
306 3 ▶ ref($self) or croak("Can't call this object method on a record class");
Branch: 1 branch
BranchTrueFalseTotal
ref $self or croak("Can't call this object method on a record class") 3 0 3
307
308 3 ▶ if ( ! $self->primary_key_value() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->primary_key_value) { } 3 0 3
309 3 $self->insert_record( @_ );
310 } else {
311 0 $self->update_record( @_ );
312 }
313 3 $self;
314 }
315  
316 # $record_class->new_and_save( 'fieldname' => 'new_value', ... )
317 3 ▶sub new_and_save {
Subroutine new_and_save: called CC=1 SCAR=0.0
Pod documented
318 3 (shift)->new_with_values( @_ )->save_record();
319 }
320  
321 # $record->change_and_save( 'fieldname' => 'new_value', ... )
322 0 ▶sub change_and_save {
Subroutine change_and_save: not called CC=1 SCAR=6.9
Pod documented
323 0 my $self = shift;
324 0 $self->change_values( @_ );
325 0 $self->save_record;
326 }
327  
328 ########################################################################
329  
330 ########################################################################
331  
332 =head1 FETCHING DATA
333  
334 The following abstract methods are to be implemented by subclasses; in particular, see L<DBIx::SQLEngine::Record::Table>.
335  
336 =head2 Select to Retrieve Records
337  
338 =over 4
339  
340 =item fetch_one_record()
341  
342 $sqldb->fetch_one_record( %select_clauses ) : $record_hash
343  
344 Retrives one record from the table using the provided SQL select clauses.
345  
346 =item fetch_select()
347  
348 $class_name->fetch_select ( %select_clauses ) : $record_set
349  
350 Retrives records from the table using the provided SQL select clauses.
351  
352 =item visit_select()
353  
354 $class_name->visit_select ( $sub_ref, %select_clauses ) : @results
355 $class_name->visit_select ( %select_clauses, $sub_ref ) : @results
356  
357 Calls the provided subroutine on each matching record as it is retrieved. Returns the accumulated results of each subroutine call (in list context).
358  
359 =back
360  
361 =cut
362  
363 use Class::MakeMethods::Standard::Universal
364 5 ▶ 'abstract' => "fetch_one_record fetch_select visit_select";
Subroutine BEGIN: called CC=1 SCAR=0.0
365  
366 ########################################################################
367  
368 =head2 Selecting by Primary Key
369  
370 =over 4
371  
372 =item select_record()
373  
374 $class_name->select_record ( $primary_key_value ) : $record_obj
375 $class_name->select_record ( \@compound_primary_key ) : $record_obj
376 $class_name->select_record ( \%hash_with_primary_key_value ) : $record_obj
377  
378 Fetches a single record by primary key.
379  
380 The row hash is blessed into the record class before being returned.
381  
382 =item select_records()
383  
384 $class_name->select_records ( @primary_key_values_or_hashrefs ) : $record_set
385  
386 Fetches a set of one or more records by primary key.
387  
388 Each row hash is blessed into the record class before being wrapped in a RecordSet::Set object.
389  
390 =back
391  
392 =cut
393  
394 use Class::MakeMethods::Standard::Universal
395 5 ▶ 'abstract' => "select_record select_records";
Subroutine BEGIN: called CC=1 SCAR=0.0
396  
397 ########################################################################
398  
399 ########################################################################
400  
401 =head1 ALTERING DATA
402  
403 The following abstract methods are to be implemented by subclasses; in particular, see L<DBIx::SQLEngine::Record::Table>.
404  
405 =head2 Insert to Add Records
406  
407 After constructing a record with one of the new_*() methods, you may save any changes by calling insert_record.
408  
409 =over 4
410  
411 =item insert_record()
412  
413 $record_obj->insert_record() : $flag
414  
415 Adds the values from this record to the table. Returns the number of rows affected, which should be 1 unless there's an error.
416  
417 =back
418  
419 =head2 Update to Change Records
420  
421 After retrieving a record with one of the fetch methods, you may save any changes by calling update_record.
422  
423 =over 4
424  
425 =item update_record()
426  
427 $record_obj->update_record() : $record_count
428  
429 Attempts to update the record using its primary key as a unique identifier. Returns the number of rows affected, which should be 1 unless there's an error.
430  
431 =back
432  
433 =head2 Delete to Remove Records
434  
435 =over 4
436  
437 =item delete_record()
438  
439 $record_obj->delete_record() : $record_count
440  
441 Delete this existing record based on its primary key. Returns the number of rows affected, which should be 1 unless there's an error.
442  
443 =back
444  
445 =cut
446  
447 use Class::MakeMethods::Standard::Universal
448 5 ▶ 'abstract' => "insert_record update_record delete_record";
Subroutine BEGIN: called CC=1 SCAR=0.0
449  
450 ########################################################################
451  
452 ########################################################################
453  
454 =head1 SEE ALSO
455  
456 For more about the Record classes, see L<DBIx::SQLEngine::Record::Class>.
457  
458 See L<DBIx::SQLEngine> for the overall interface and developer documentation.
459  
460 See L<DBIx::SQLEngine::Docs::ReadMe> for general information about
461 this distribution, including installation and license information.
462  
463 =cut
464  
465 ########################################################################
466  
467 5 1;
« SQLEngine/Schema/ColumnSet.pm ↑ Summary SQLEngine/Schema/Table.pm »