SQLEngine/RecordSet/Set.pm

filter: Statementstmt 11.8% 12 / 101 Branchbran 8.3% 3 / 36 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 13.0% 3 / 23 Podpod 75.0% 15 / 20 totaltotal 17.0% 35 / 205 CC 26 SCAR 26.2
CC
26
Cov
11%
visit_sub
42.0
filter
30.0
init
12.0
CRAP 13.8
« SQLEngine/Cache/BasicCache.pm ↑ Summary SQLEngine/Driver/Trait/PerlDBLib.pm »
1 =head1 NAME
2  
3 DBIx::SQLEngine::RecordSet::Set - Array of Record Objects
4  
5 =head1 SYNOPSIS
6  
7 use DBIx::SQLEngine::RecordSet::Set;
8  
9 $record_set = DBIx::SQLEngine::RecordSet::Set->new( @records );
10  
11 $record_set = $record_class->fetch_select( criteria => { status => 2 } );
12
13 print "Found " . $record_set->count() . " records";
14  
15 $record_set->filter( { 'status' => 'New' } );
16 $record_set->sort( 'creation_date' );
17
18 foreach ( 0 .. $record_set->count() ) {
19 print $record_set->record( $_ )->name();
20 }
21
22 foreach ( $record_set->range_records( 11, 20 ) ) {
23 print $_->name();
24 }
25  
26  
27 =head1 DESCRIPTION
28  
29 This package is not yet complete.
30  
31 The base implementation of RecordSet is an array of Record references.
32  
33 =cut
34  
35 ########################################################################
36  
37 package DBIx::SQLEngine::RecordSet::Set;
38  
39 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 ########################################################################
42  
43 ########################################################################
44  
45 =head2 Constructor
46  
47 =over 4
48  
49 =item new()
50  
51 $class->new ( @records ) : $recordset
52  
53 Array constructor.
54  
55 =item clone()
56  
57 $recordset->clone() : $recordset
58  
59 Create a shallow copy of the record set.
60  
61 =back
62  
63 =cut
64  
65 # $rs = DBIx::SQLEngine::RecordSet::Set->new( @records );
66 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
67 0 my $callee = shift;
68 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
69 0 my $set = bless [], $package;
70 0 $set->init( @_ );
71 0 return $set;
72 }
73  
74 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
75 0 my $self = shift;
76 0 $self->new( @$self );
77 }
78  
79 ########################################################################
80  
81 =head2 Contents
82  
83 =over 4
84  
85 =item init()
86  
87 $recordset->init ( @records )
88  
89 Array content setter.
90  
91 =item records()
92  
93 $rs->records() : @records
94  
95 Array content accessor.
96  
97 =back
98  
99 =cut
100  
101 # $rs->init( @records );
102 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
103 0 my $self = shift;
104
105 0 ▶ @$self = ( scalar @_ == 1 and ref($_[0]) eq 'ARRAY' ) ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 && 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
106 }
107  
108 # @records = $rs->records();
109 0 ▶sub records {
Subroutine records: not called CC=1 SCAR=6.9
Pod documented
110 0 my $records = shift;
111 0 @$records
112 }
113  
114 ########################################################################
115  
116 ########################################################################
117  
118 =head2 Positional Access
119  
120 =over 4
121  
122 =item count()
123  
124 $count = $rs->count();
125  
126 Returns the number of records in this set.
127  
128 =item record()
129  
130 $record = $rs->record( $position );
131  
132 Return the record in the indicated position in the array. Returns nothing if position is undefined.
133  
134 Indexes start with zero. Negative indexes are counted back from the end, with -1 being the last, -2 being the one before that, and so forth.
135  
136 =item last_record
137  
138 $record = $rs->last_record();
139  
140 Return the last record in the array.
141  
142 =back
143  
144 =cut
145  
146 # $count = $rs->count();
147 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
148 0 my $self = shift;
149 0 scalar @$self;
150 }
151  
152 # $record = $rs->record( $position );
153 2 ▶sub record {
Subroutine record: called CC=6 SCAR=22.1
Pod documented
154 2 my $self = shift;
155 2 my $position = shift;
156 2 ▶ return unless ( defined $position and length $position );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $position and length $position 0 2 2
Condition33%1/3
defined $position and length $position
!ll&&!rl&&r
002
MC/DC0%0/2
defined $position and length $position
defined $position length $position
Truth table33%1/3
A and B
A= defined $positionB= length $position
ABresult
0X0
100
111
157 2 ▶ $position += $self->count if ( $position < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $position < 0 0 2 2
158 2 ▶ return unless ( $position !~ /\D/ and $position <= $#$self);
Branch: 1 branch
BranchTrueFalseTotal
unless not $position =~ /\D/ and $position <= $self->$#* 2 0 2
Condition33%1/3
not $position =~ /\D/ and $position <= $self->$#*
!ll&&!rl&&r
020
MC/DC0%0/2
not $position =~ /\D/ and $position <= $self->$#*
not $position =~ /\D/ $position <= $self->$#*
Truth table33%1/3
not(A) and B
A= not $position =~ /\D/B= $position <= $self->$#*
ABresult
0X0
100
111
159 0 $self->[ $position ];
160 }
161  
162 # @records = $rs->get_records( @positions );
163 0 ▶sub get_records {
Subroutine get_records: not called CC=1 SCAR=6.9
Pod undocumented
164 0 my $self = shift;
165 0 map { $self->record( $_ ) } @_
166 }
167  
168 ########################################################################
169  
170 # $record = $rs->last_record();
171 0 ▶sub last_record {
Subroutine last_record: not called CC=2 SCAR=17.9
Pod documented
172 0 my $self = shift;
173 0 ▶ return unless $self->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count 0 0 0
174 0 $self->record( $self->count - 1 );
175 }
176  
177 ########################################################################
178  
179 ########################################################################
180  
181 =head2 Positional Subsets
182  
183 =over 4
184  
185 =item range_set()
186  
187 $clone = $rs->range_set( $start_pos, $stop_pos );
188  
189 Return a copy of the current set containing only those records at or between the start and stop positions.
190  
191 =item range_records()
192  
193 @records = $rs->range_records( $start_pos, $stop_pos );
194  
195 Return the records at or between the start and stop positions.
196  
197 =back
198  
199 =cut
200  
201 # $clone = $rs->range_set( $start_pos, $stop_pos );
202 0 ▶sub range_set {
Subroutine range_set: not called CC=3 SCAR=24.8
Pod documented
203 0 my $self = shift;
204 0 my ( $start, $end ) = @_;
205 0 ▶ if ( $start < 0 ) { $start = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 0 0
206 0 ▶ if ( $end > $#$self ) { $end = $#$self }
Branch: 1 branch
BranchTrueFalseTotal
if ($end > $self->$#*) 0 0 0
207
208 0 $self->new( $self->get_records( $start .. $end ) );
209 }
210  
211 # @records = $rs->range_records( $start_pos, $stop_pos );
212 0 ▶sub range_records {
Subroutine range_records: not called CC=3 SCAR=24.8
Pod documented
213 0 my $self = shift;
214 0 my ( $start, $end ) = @_;
215 0 ▶ if ( $start < 0 ) { $start = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 0 0
216 0 ▶ if ( $end > $#$self ) { $end = $#$self }
Branch: 1 branch
BranchTrueFalseTotal
if ($end > $self->$#*) 0 0 0
217
218 0 $self->get_records( $start .. $end )
219 }
220  
221 ########################################################################
222  
223 ########################################################################
224  
225 =head2 Sorting
226  
227 Use of these methods requires the Data::Sorting module from CPAN.
228  
229 See L<Data::Sorting> for more information.
230  
231 =over 4
232  
233 =item sort()
234  
235 $rs->sort( @fieldnames );
236  
237 Sort the contents of the set.
238  
239 =item sorted_set()
240  
241 $clone = $rs->sorted_set( @fieldnames );
242  
243 Return a sorted copy of the current set.
244  
245 =item sorted_records()
246  
247 @records = $rs->sorted_records( @fieldnames );
248  
249 Return the records from the current set, in sorted order.
250  
251 =back
252  
253 =cut
254  
255 # $rs->sort( @fieldnames );
256 0 ▶sub sort {
Subroutine sort: not called CC=3 SCAR=24.8
Pod documented
257 0 my $self = shift;
258 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
259 0 require Data::Sorting;
260 0 Data::Sorting::sort_array(@$self, @_);
261 }
262  
263 # $clone = $rs->sorted_set( @fieldnames );
264 0 ▶sub sorted_set {
Subroutine sorted_set: not called CC=1 SCAR=6.9
Pod documented
265 0 my $self = shift;
266 0 my $clone = $self->clone();
267 0 $clone->sort( @_ );
268 0 return $clone;
269 }
270  
271 # @records = $rs->sorted_records( @fieldnames );
272 0 ▶sub sorted_records {
Subroutine sorted_records: not called CC=1 SCAR=6.9
Pod documented
273 0 my $self = shift;
274 0 my $clone = $self->clone();
275 0 $clone->sort( @_ );
276 0 $clone->records();
277 }
278  
279 ########################################################################
280  
281 0 ▶sub reverse {
Subroutine reverse: not called CC=1 SCAR=6.9
Pod undocumented
282 0 my $rs = shift;
283 0 @$rs = reverse @$rs;
284 }
285  
286 ########################################################################
287  
288 ########################################################################
289  
290 =head2 Criteria Matching
291  
292 B<Caution:> This set of methods is currently not working.
293  
294 =over 4
295  
296 =item filter()
297  
298 $rs->filter( $criteria );
299  
300 Remove non-matching records from the set.
301  
302 =item filtered_set()
303  
304 $clone = $rs->filtered_set( $criteria );
305  
306 Return a set containing only the matching records from the current set.
307  
308 =item filtered_records()
309  
310 @records = $rs->filtered_records( $criteria );
311  
312 Return the matching records from the current set.
313  
314 =back
315  
316 =cut
317  
318 5 ▶use DBIx::SQLEngine::Criteria qw( new_group_from_values );
Subroutine BEGIN: called CC=1 SCAR=0.0
319  
320 # $rs->filter( $criteria );
321 0 ▶sub filter {
Subroutine filter: not called CC=5 SCAR=34.0
Pod documented
322 0 my $self = shift;
323
324 0 ▶ my $criteria = shift
Branch: 1 branch
BranchTrueFalseTotal
my $criteria = shift() or return 0 0 0
325 or return;
326 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
327 0 $criteria = new_group_from_values(@$criteria);
328 } elsif (ref $criteria eq 'HASH') {
329 0 $criteria = DBIx::SQLEngine::Criteria->new_from_hashref($criteria);
330 } elsif (ref $criteria eq 'CODE') {
331 0 @$self = grep { $criteria->( $_ ) } @$self;
332 0 return;
333 }
334
335 0 @$self = $criteria->matchers($self);
336 }
337  
338 # $clone = $rs->filtered_set( $criteria );
339 0 ▶sub filtered_set {
Subroutine filtered_set: not called CC=1 SCAR=6.9
Pod documented
340 0 my $self = shift;
341 0 my $clone = $self->clone();
342 0 $clone->filter( @_ );
343 0 return $clone;
344 }
345  
346 # @records = $rs->filtered_records( $criteria );
347 0 ▶sub filtered_records {
Subroutine filtered_records: not called CC=1 SCAR=6.9
Pod documented
348 0 my $self = shift;
349 0 my $clone = $self->clone();
350 0 $clone->filter( @_ );
351 0 $clone->records();
352 }
353  
354 ########################################################################
355  
356 ########################################################################
357  
358 # @results = $rs->visit_sub( $subref, @$leading_args, @$trailing_args );
359 0 ▶sub visit_sub {
Subroutine visit_sub: not called CC=6 SCAR=37.4
Pod undocumented
360 0 my $rs = shift;
361 0 my $subref = shift;
362 0 ▶ my @pre_args = map { ref($_) ? @$_ : defined($_) ? $_ : () } shift;
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 0 0 0
defined $_ ? : 0 0 0
363 0 ▶ my @post_args = map { ref($_) ? @$_ : defined($_) ? $_ : () } shift;
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 0 0 0
defined $_ ? : 0 0 0
364 0 my @result;
365 0 foreach my $record ( $rs->records ) {
366 0 push @result, $subref->( @pre_args, $record, @post_args )
367 }
368 0 return @result;
369 }
370  
371 ########################################################################
372  
373 # $numeric = $rs->sum( $fieldname );
374 0 ▶sub sum {
Subroutine sum: not called CC=2 SCAR=17.9
Pod undocumented
375 0 my $rs = shift;
376 0 my $field = shift;
377 0 my $sum = 0;
378 0 ▶ foreach ( $rs->visit_sub( sub { ( shift )->$field() } ) ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
379 0 $sum += $_;
380 }
381 0 return $sum;
382 }
383  
384 ########################################################################
385  
386 ########################################################################
387  
388 # $rs->add_records( @records );
389 0 ▶sub add_records {
Subroutine add_records: not called CC=1 SCAR=6.9
Pod undocumented
390 0 my $rs = shift;
391 0 my %record_ids = map { $_->id => 1 } $rs->records;
392 0 push @$rs, grep { ! ( $record_ids{ $_->id } ++ ) } @_
393 }
394  
395 ########################################################################
396  
397 ########################################################################
398  
399 =head1 SEE ALSO
400  
401 See L<DBIx::SQLEngine> for the overall interface and developer documentation.
402  
403 See L<DBIx::SQLEngine::Docs::ReadMe> for general information about
404 this distribution, including installation and license information.
405  
406 =cut
407  
408 ########################################################################
409  
410 5 1;
« SQLEngine/Cache/BasicCache.pm ↑ Summary SQLEngine/Driver/Trait/PerlDBLib.pm »