DBO2/RecordSet.pm

filter: Statementstmt 9.2% 10 / 108 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 12.5% 3 / 24 Podpod 4.7% 1 / 21 totaltotal 6.7% 14 / 208 CC 21 SCAR 23.4
CC
21
Cov
7%
filter
30.0
record
20.0
init
12.0
CRAP 10.4
« DBO2/Record.pm ↑ Summary DBO2/Schema.pm »
1 package DBIx::DBO2::RecordSet;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # use Data::Sorting 'sort_array';
7 1 ▶use DBIx::SQLEngine::Criteria qw( match_criteria new_group_from_values );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # $rs = DBIx::DBO2::RecordSet->new( @records );
10 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
11 0 my $callee = shift;
12 0 ▶ my $package = ref $callee || $callee;
Condition0%0/3
ref $callee || $callee
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $callee || $callee
ref $callee $callee
Truth table0%0/3
A || B
A= ref $calleeB= $callee
ABresult
1X1
011
000
13
14 0 my $self = [];
15 0 bless $self, $package;
16 0 $self->init( @_ );
17 0 return $self;
18 }
19  
20 # $rs->init( @records );
21 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
Pod undocumented
22 0 my $self = shift;
23
24 0 ▶ if ( scalar @_ == 1 and ref($_[0]) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq "ARRAY") 0 0 0
Condition0%0/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1 ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
25 0 @_ = @{ $_[0] };
26 }
27
28 0 @$self = @_;
29 }
30  
31 # @records = $rs->records();
32 0 ▶sub records {
Subroutine records: not called CC=1 SCAR=6.9
Pod undocumented
33 0 my $self = shift;
34 0 @$self;
35 }
36  
37 ###
38  
39 # $rs = DBIx::DBO2::RecordSet->new_class_ids( $class, @ids );
40 0 ▶sub new_ids {
Subroutine new_ids: not called CC=2 SCAR=17.9
Pod undocumented
41 0 my $callee = shift;
42 0 ▶ my $package = ref $callee || $callee;
Condition0%0/3
ref $callee || $callee
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $callee || $callee
ref $callee $callee
Truth table0%0/3
A || B
A= ref $calleeB= $callee
ABresult
1X1
011
000
43
44 0 my $self = [];
45 0 bless $self, $package;
46 0 $self->init_class_ids( @_ );
47 0 return $self;
48 }
49  
50 # $rs->init_ids( $class, @ids );
51 0 ▶sub init_ids {
Subroutine init_ids: not called CC=1 SCAR=6.9
Pod undocumented
52 0 my $self = shift;
53 0 my $class = shift;
54
55 0 @$self = map { $class->fetch_id( $_ ) } @_;
56 }
57  
58 # @records = $rs->class_ids();
59 0 ▶sub class_ids {
Subroutine class_ids: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my $self = shift;
61 0 my $class = ref( $self->[0] );
62 0 return $class, map { $_->{id} } @$self;
63 }
64  
65 ###
66  
67 0 ▶sub raw {
Subroutine raw: not called CC=2 SCAR=17.9
Pod undocumented
68 0 my $self = shift;
69 0 ▶ if ( scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 0 0 0
70 0 @$self = @_;
71 } else {
72 0 @$self;
73 }
74 }
75 #
76 # sub as_RecordArray {
77 # my $self = shift;
78 # }
79 #
80 # sub as_IDArray {
81 # my $self = shift;
82 # EBiz::Database::RecordSet::IDArray->new( $self->records );
83 # }
84 #
85 # sub as_IDString {
86 # my $self = shift;
87 # EBiz::Database::RecordSet::IDString->new( $self->records );
88 # }
89  
90 ###
91  
92 # $count = $rs->count();
93 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my $self = shift;
95 0 scalar @$self;
96 }
97  
98 # $record = $rs->record( $position );
99 0 ▶sub record {
Subroutine record: not called CC=4 SCAR=30.0
Pod undocumented
100 0 my $self = shift;
101 0 my $position = shift;
102 0 ▶ return unless ( length $position and $position !~ /\D/ and $position <= $#$self);
Branch: 1 branch
BranchTrueFalseTotal
unless length $position and not $position =~ /\D/ and $position <= $self->$#* 0 0 0
Condition0%0/6
length $position and not $position =~ /\D/ and $position <= $self->$#*
!ll&&!rl&&r
000
length $position and not $position =~ /\D/
!ll&&!rl&&r
000
MC/DC0%0/3
length $position and not $position =~ /\D/ and $position <= $self->$#*
length $position not $position =~ /\D/ $position <= $self->$#*
Truth table0%0/4
A and not(B) and C
A= length $positionB= not $position =~ /\D/C= $position <= $self->$#*
ABCresult
0XX0
10X0
1100
1111
103 0 $self->[ $position ];
104 }
105  
106 # $record = $rs->last_record();
107 0 ▶sub last_record {
Subroutine last_record: not called CC=2 SCAR=17.9
Pod undocumented
108 0 my $self = shift;
109 0 ▶ return unless $self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 0 0 0
110 0 $self->record( $self->count - 1 );
111 }
112  
113 # $clone = $rs->range_set( $start_pos, $stop_pos );
114 0 ▶sub range_set {
Subroutine range_set: not called CC=3 SCAR=24.8
Pod undocumented
115 0 my $self = shift;
116 0 my ( $start, $end ) = @_;
117 0 ▶ if ( $start < 0 ) { $start = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 0 0
118 0 ▶ if ( $end > $#$self ) { $end = $#$self }
Branch: 1 branch
BranchTrueFalseTotal
if ($end > $self->$#*) 0 0 0
119
120 0 $self->new( @{$self}[ $start .. $end ] );
121 }
122  
123 # @records = $rs->range_records( $start_pos, $stop_pos );
124 0 ▶sub range_records {
Subroutine range_records: not called CC=3 SCAR=24.8
Pod undocumented
125 0 my $self = shift;
126 0 my ( $start, $end ) = @_;
127 0 ▶ if ( $start < 0 ) { $start = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 0 0
128 0 ▶ if ( $end > $#$self ) { $end = $#$self }
Branch: 1 branch
BranchTrueFalseTotal
if ($end > $self->$#*) 0 0 0
129
130 0 @{$self}[ $start .. $end ];
131 }
132  
133 ###
134  
135 # $rs->sort( @fieldnames );
136 0 ▶sub sort {
Subroutine sort: not called CC=3 SCAR=24.8
Pod undocumented
137 0 my $self = shift;
138 0 ▶ local @_ = @{ $_[0] } if ( scalar @_ == 1 and ref $_[0] eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and ref $_[0] eq "ARRAY" 0 0 0
Condition0%0/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1 ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
139 0 sort_array(@$self, @_);
140 }
141  
142 # $clone = $rs->sorted_set( @fieldnames );
143 0 ▶sub sorted_set {
Subroutine sorted_set: not called CC=1 SCAR=6.9
Pod undocumented
144 0 my $self = shift;
145 0 my $clone = $self->new( @$self );
146 0 $clone->sort( @_ );
147 0 return $clone;
148 }
149  
150 # @records = $rs->sorted_records( @fieldnames );
151 0 ▶sub sorted_records {
Subroutine sorted_records: not called CC=1 SCAR=6.9
Pod undocumented
152 0 my $self = shift;
153 0 my $clone = $self->new( @$self );
154 0 $clone->sort( @_ );
155 0 $clone->records();
156 }
157  
158 ###
159  
160  
161 # $rs->filter( $criteria );
162 0 ▶sub filter {
Subroutine filter: not called CC=5 SCAR=34.0
Pod undocumented
163 0 my $self = shift;
164
165 0 ▶ my $criteria = shift
Branch: 1 branch
BranchTrueFalseTotal
my $criteria = shift() or return 0 0 0
166 or return;
167 0 ▶ if (ref $criteria eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $criteria eq 'ARRAY') { } 0 0 0
elsif (ref $criteria eq 'HASH') { } 0 0 0
elsif (ref $criteria eq 'CODE') { } 0 0 0
168 0 $criteria = new_group_from_values(@$criteria);
169 } elsif (ref $criteria eq 'HASH') {
170 0 $criteria = DBO::Criteria->new_from_hashref($criteria);
171 } elsif (ref $criteria eq 'CODE') {
172 0 @$self = grep { $criteria->( $_ ) } @$self;
173 0 return;
174 }
175
176 0 @$self = $criteria->matchers($self);
177 }
178  
179 # $clone = $rs->filtered_set( $criteria );
180 0 ▶sub filtered_set {
Subroutine filtered_set: not called CC=1 SCAR=6.9
Pod undocumented
181 0 my $self = shift;
182 0 my $clone = $self->new( @$self );
183 0 $clone->filter( @_ );
184 0 return $clone;
185 }
186  
187 # @records = $rs->filtered_records( $criteria );
188 0 ▶sub filtered_records {
Subroutine filtered_records: not called CC=1 SCAR=6.9
Pod undocumented
189 0 my $self = shift;
190 0 my $clone = $self->new( @$self );
191 0 $clone->filter( @_ );
192 0 $clone->records();
193 }
194  
195 ###
196  
197 # $rs->push_unique_records( @records );
198 0 ▶sub push_unique_records {
Subroutine push_unique_records: not called CC=1 SCAR=6.9
Pod undocumented
199 0 my $rs = shift;
200 0 my %record_ids = map { $_->id => 1 } $rs->records;
201 0 push @$rs, grep { ! ( $record_ids{ $_->id } ++ ) } @_
202 }
203  
204 ###
205  
206 0 ▶sub reverse {
Subroutine reverse: not called CC=1 SCAR=6.9
Pod undocumented
207 0 my $rs = shift;
208 0 @$rs = reverse @$rs;
209 }
210  
211 # $numeric = $rs->sum( $fieldname );
212 0 ▶sub sum {
Subroutine sum: not called CC=2 SCAR=17.9
Pod undocumented
213 0 my $rs = shift;
214 0 my $field = shift;
215 0 my $sum = 0;
216 0 foreach ( $rs->records ) {
217 0 $sum += $_->$field();
218 }
219 0 return $sum;
220 }
221  
222 1 1;
223  
224 =head1 NAME
225  
226 DBIx::DBO2::RecordSet - A sequential group of Record objects
227  
228  
229 =head1 SYNOPSIS
230  
231 $rs = DBIx::DBO2::RecordSet->new( @records );
232
233 print "Found " . $rs->count() . " records";
234  
235 $rs->filter( { 'status' => 'New' } );
236 $rs->sort( 'creation_date' );
237
238 foreach ( 0 .. $rs->count() ) {
239 print $rs->record( $_ )->name();
240 }
241
242 foreach ( $rs->range_records( 11, 20 ) ) {
243 print $_->name();
244 }
245  
246 =head1 DESCRIPTION
247  
248 The base implementation of RecordSet is an array of Record references.
249  
250 Constructor:
251  
252 =over 4
253  
254 =item new ( @records ) : $instance
255  
256 =back
257  
258  
259 =head2 Basics
260  
261 =over 4
262  
263 =item *
264  
265 $rs = DBIx::DBO2::RecordSet->new( @records );
266  
267 Create a new RecordSet.
268  
269 =item *
270  
271 $rs->init( @records );
272  
273 =item *
274  
275 @records = $rs->records();
276  
277 =back
278  
279 =head2 Class and IDs
280  
281 =over 4
282  
283 =item *
284  
285 $rs = DBIx::DBO2::RecordSet->new_class_ids( $class, @ids );
286  
287 =item *
288  
289 $rs->init_class_ids( $class, @ids );
290  
291 =item *
292  
293 ( $class, @ids ) = $rs->class_ids();
294  
295 =back
296  
297 =head2 Conversions
298  
299 Each of the below returns a RecordSet blessed into a particular subclass. Returns the original object if it is already of that subclass, or returns a cloned and converted copy.
300  
301 =over 4
302  
303 =item *
304  
305 @data = $rs->raw();
306  
307 Returns the contents of the RecordSet as stored internally within the object. Results are dependent on which subclass is in use.
308  
309 =item *
310  
311 $rs = $rs->as_RecordArray;
312  
313 INCOMPLETE
314  
315 =item *
316  
317 $clone = $rs->as_IDArray;
318  
319 INCOMPLETE
320  
321 =item *
322  
323 $clone = $rs->as_IDString;
324  
325 INCOMPLETE
326  
327 =back
328  
329 =head2 Positional Access
330  
331 =over 4
332  
333 =item *
334  
335 $count = $rs->count();
336  
337 =item *
338  
339 $record = $rs->record( $position );
340  
341 Return the record in the indicated position in the array.
342  
343 =item *
344  
345 $record = $rs->last_record();
346  
347 Return the last record in the array.
348  
349 =item *
350  
351 $clone = $rs->range_set( $start_pos, $stop_pos );
352  
353 Return a copy of the current set containing only those records at or between the start and stop positions.
354  
355 =item *
356  
357 @records = $rs->range_records( $start_pos, $stop_pos );
358  
359 Return the records at or between the start and stop positions.
360  
361 =back
362  
363 =head2 Sorting
364  
365 =over 4
366  
367 =item *
368  
369 $rs->sort( @fieldnames );
370  
371 Sort the contents of the set.
372  
373 =item *
374  
375 $clone = $rs->sorted_set( @fieldnames );
376  
377 Return a sorted copy of the current set.
378  
379 =item *
380  
381 @records = $rs->sorted_records( @fieldnames );
382  
383 Return the records from the current set, in sorted order.
384  
385 =back
386  
387 =head2 Criteria Matching
388  
389 =over 4
390  
391 =item *
392  
393 $rs->filter( $criteria );
394  
395 Remove non-matching records from the set.
396  
397 =item *
398  
399 $clone = $rs->filtered_set( $criteria );
400  
401 Return a set containing only the matching records from the current set.
402  
403 =item *
404  
405 @records = $rs->filtered_records( $criteria );
406  
407 Return the matching records from the current set.
408  
409 =back
410  
411  
412 =head1 CHANGES
413  
414 2001-06-29 Moved to DBIx::DBO2 namespace.
415  
416 2001-04-10 Added last_record.
417  
418 2000-12-13 Substantial revisions. Moved to EBiz::Database namespace.
419  
420 2000-12-01 Ed: Created.
421  
422 =cut
« DBO2/Record.pm ↑ Summary DBO2/Schema.pm »