Wrapper/Factory.pm

filter: Statementstmt 96.8% 123 / 127 Branchbran 82.6% 38 / 46 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 95.4% 21 / 22 Podpod 100% 19 / 19 totaltotal 91.7% 211 / 230 CC 28 SCAR 18.0
CC
28
Cov
92%
fast_loop_through
13.3
loop_through
9.0
first
2.0
CRAP 6.0
↑ Summary Wrapper.pm »
1 package DBIx::Class::Wrapper::Factory;
2 4 $DBIx::Class::Wrapper::Factory::VERSION = '0.008';
3 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 extends qw/DBIx::Class::Wrapper::FactoryBase/;
5  
6 =head1 NAME
7  
8 DBIx::Class::Wrapper::Factory - A factory class that decorates a L<DBIx::Class::ResultSet>.
9  
10 =head1 SYNOPSIS
11  
12 A model implementing the role DBIx::Class::Wrapper will automatically instantiate
13 subclasses of this for any underlying DBIx::Class ResultSet.
14  
15 To implement your own factory containing your business code for the underlying
16 DBIC resulsets, you need to subclass this.
17  
18 See L<DBIx::Class::Wrapper> for a simple synopsis overview.
19  
20 =head1 PROPERTIES
21  
22 =head2 dbic_rs
23  
24 The original L<DBIx::Class::ResultSet>. Mandatory.
25  
26 =head2 bm
27  
28 The business model consuming the role L<DBIx::Class::Wrapper>. Mandatory.
29  
30 See L<DBIx::Class::Wrapper> for more details.
31  
32 =cut
33  
34 4 has 'dbic_rs' => ( is => 'ro' , isa => 'DBIx::Class::ResultSet', required => 1 , lazy_build => 1);
35 4 has 'bm' => ( is => 'ro' , does => 'DBIx::Class::Wrapper' , required => 1 , weak_ref => 1 );
36 4 has 'name' => ( is => 'ro' , isa => 'Str' , required => 1 );
37  
38 sub _build_dbic_rs{
39 20 ▶ my ($self) = @_;
Subroutine _build_dbic_rs: called CC=1 SCAR=0.0
40 20 return $self->build_dbic_rs();
41 }
42  
43 =head2 build_dbic_rs
44  
45 Builds the dbic ResultSet to be wrapped by this factory.
46  
47 Defaults to the DBIx::Class Resultset with the same name
48 as this factory.
49  
50 You can override this in your business specific factories to build
51 specific resultsets:
52  
53 package My::Model::Factory::SomeName;
54  
55 use Moose; extends qw/DBIx::Class::Wrapper::Factory/ ;
56  
57 sub build_dbic_rs{
58 my ($self) = @_;
59 return $self->bm->dbic_schema->resultset('SomeOtherName');
60  
61 # Or with some restriction:
62  
63 return $self->bm->dbic_schema->resultset('SomeOtherName')
64 ->search({ bla => ... });
65 }
66  
67  
68 =cut
69  
70 sub build_dbic_rs{
71 10 ▶ my ($self) = @_;
Subroutine build_dbic_rs: called CC=2 SCAR=6.9
Pod documented
72 10 my $resultset = eval{ return $self->bm->dbic_schema->resultset($self->name); };
73 10 ▶ if( my $err = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 8 10
74 2 confess("Cannot build resultset for $self NAME=".$self->name().' :'.$err);
75 }
76 8 return $resultset;
77 }
78  
79  
80 =head2 new_result
81  
82 Instantiate a new NOT INSERTED IN DB row and wrap it using
83 the wrap method.
84  
85 See L<DBIx::Class::ResultSet/new_result>
86  
87 =cut
88  
89 sub new_result{
90 1 ▶ my ($self, $args) = @_;
Subroutine new_result: called CC=1 SCAR=0.0
Pod documented
91 1 return $self->wrap($self->dbic_rs->new_result($args));
92 }
93  
94 =head2 create
95  
96 Creates a new object in the DBIC Schema and return it wrapped
97 using the wrapper method.
98  
99 See L<DBIx::Class::ResultSet/create>
100  
101 =cut
102  
103 sub create{
104 2204 ▶ my ($self , $args) = @_;
Subroutine create: called CC=1 SCAR=0.0
Pod documented
105 2204 return $self->wrap($self->dbic_rs->create($args));
106 }
107  
108 =head2 find
109  
110 Finds an object in the DBIC schema and returns it wrapped
111 using the wrapper method.
112  
113 See L<DBIx::Class::ResultSet/find>
114  
115 =cut
116  
117 sub find{
118 8 ▶ my ($self , @rest) = @_;
Subroutine find: called CC=2 SCAR=6.9
Pod documented
119 8 my $original = $self->dbic_rs->find(@rest);
120 8 ▶ return $original ? $self->wrap($original) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$original ? : 4 4 8
121 }
122  
123 =head2 first
124  
125 Equivalent to DBIC Resultset 'first' method.
126  
127 See <DBIx::Class::ResultSet/first>
128  
129 =cut
130  
131 sub first{
132 2 ▶ my ($self) = @_;
Subroutine first: called CC=2 SCAR=7.1
Pod documented
133 2 my $original = $self->dbic_rs->first();
134 2 ▶ return $original ? $self->wrap($original) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$original ? : 2 0 2
135 }
136  
137 =head2 single
138  
139 Equivalent to DBIx::Class::ResultSet::single. It's a bit more efficient than C<first()>.
140  
141 =cut
142  
143 sub single {
144 1 ▶ my ($self) = @_;
Subroutine single: called CC=2 SCAR=7.1
Pod documented
145 1 my $original = $self->dbic_rs->single();
146 1 ▶ return $original ? $self->wrap($original) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$original ? : 1 0 1
147 }
148  
149 =head2 find_or_create
150  
151 Wraps around the original DBIC find_or_create method.
152  
153 See L<DBIx::Class::ResultSet/find_or_create>
154  
155 =cut
156  
157 sub find_or_create{
158 4 ▶ my ($self , $args) = @_;
Subroutine find_or_create: called CC=2 SCAR=7.1
Pod documented
159 4 my $original = $self->dbic_rs->find_or_create($args);
160 4 ▶ return $original ? $self->wrap($original) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$original ? : 4 0 4
161 }
162  
163 =head2 find_or_new
164  
165 Wraps around the original DBIC find_or_new method.
166  
167 See L<DBIx::Class::ResultSet/find_or_new>
168  
169 =cut
170  
171 sub find_or_new {
172 2 ▶ my ($self, @args) = @_;
Subroutine find_or_new: called CC=1 SCAR=0.0
Pod documented
173 2 return $self->wrap( $self->dbic_rs->find_or_new( @args ) );
174 }
175  
176  
177 =head2 pager
178  
179 Shortcut to underlying dbic_rs pager.
180  
181 See L<DBIx::Class::ResultSet/pager>.
182  
183 =cut
184  
185 sub pager{
186 8 ▶ my ($self) = @_;
Subroutine pager: called CC=1 SCAR=0.0
Pod documented
187 8 return $self->dbic_rs->pager();
188 }
189  
190 =head2 delete
191  
192 Shortcut to L<DBIx::Class::ResultSet/delete>
193  
194 =cut
195  
196 sub delete{
197 2 ▶ my ($self , @rest) = @_;
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
198 2 return $self->dbic_rs->delete(@rest);
199 }
200  
201 =head2 get_column
202  
203 Shortcut to the get_column of the decorated dbic_rs
204  
205 See L<DBIx::Class::ResultSet/get_column>
206  
207 =cut
208  
209 sub get_column{
210 2 ▶ my ($self, @rest) = @_;
Subroutine get_column: called CC=1 SCAR=0.0
Pod documented
211 2 return $self->dbic_rs->get_column(@rest);
212 }
213  
214 =head2 search_rs
215  
216 Alias for search
217  
218 =cut
219  
220 sub search_rs{
221 2 ▶ goto &search;
Subroutine search_rs: called CC=1 SCAR=0.0
Pod documented
222 }
223  
224 =head2 search
225  
226 Search objects in the DBIC Schema and returns a new instance
227 of this factory.
228  
229 Note that unlike DBIx::Class::ResultSet, this search method
230 will not return an Array of all results in an array context.
231  
232 =cut
233  
234 sub search{
235 91 ▶ my ($self , @rest) = @_;
Subroutine search: called CC=1 SCAR=0.0
Pod documented
236 91 my $class = ref($self);
237 91 return $class->new({ dbic_rs => $self->dbic_rs->search_rs(@rest),
238 bm => $self->bm(),
239 name => $self->name()
240 });
241 }
242  
243  
244 =head2 wrap
245  
246 Wraps an L<DBIx::Class::Row> in a business object. By default, it returns the
247 Row itself.
248  
249 Override that in your subclasses of factories if you need to wrap some business code
250 around the L<DBIx::Class::Row>:
251  
252 sub wrap{
253 my ($self, $o) = @_;
254  
255 return My::Model::O::SomeObject->new({ o => $o , ... });
256 }
257  
258 =cut
259  
260 sub wrap{
261 10407 ▶ my ($self , $o) = @_;
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
262 10407 return $o;
263 }
264  
265  
266 =head2 all
267  
268 Similar to DBIC Resultset all.
269  
270 Usage:
271  
272 my @objs = $this->all();
273  
274 =cut
275  
276 sub all{
277 2 ▶ my ($self) = @_;
Subroutine all: called CC=2 SCAR=6.9
Pod documented
278 2 my $search = $self->search();
279 2 my @res = ();
280 2 ▶ while( my $next = $search->next() ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $search->next) 4 2 6
281 4 push @res , $next;
282 }
283 2 return @res;
284 }
285  
286 =head2 loop_through
287  
288 Loop through all the elements of this factory
289 whilst paging and execute the given code
290 with the current retrieved object.
291  
292 WARNINGS:
293  
294 Make sure your resultset is ordered as
295 it wouldn't make much sense to page through an unordered resultset.
296  
297 In case other things are concurrently adding to this resultset, it is possible
298 that the code you give will be called with the same objects twice.
299  
300 If it's not the problem and if the rate at which objects are added is
301 not too fast compared to the processing you are doing in the code, it
302 should be just fine.
303  
304 In other cases, you probably want to wrap this in a transaction to have
305 a frozen view of the resultset.
306  
307 Usage:
308  
309 $this->loop_through(sub{ my $o = shift ; do something with o });
310 $this->loop_through(sub{...} , { limit => 1000 }); # Do only 1000 calls to sub.
311 $this->loop_through(sub{...} , { rows => 20 }); # Go by pages of 20 rows
312  
313 =cut
314  
315 sub loop_through{
316 6 ▶ my ($self, $code , $opts ) = @_;
Subroutine loop_through: called CC=9 SCAR=22.0
Pod documented
317  
318 6 ▶ unless( defined $opts ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts) 2 4 6
319 2 $opts = {};
320 }
321  
322 6 my $limit = $opts->{limit};
323 6 ▶ my $rows = defined $opts->{rows} ? $opts->{rows} : 10;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'rows'} ? : 2 4 6
324  
325 6 ▶ my $attrs = { %{$self->dbic_rs->{attrs} || {} } };
Branch: 1 branch
BranchTrueFalseTotal
$self->dbic_rs->{"attrs"} or {} 0 6 6
326 6 ▶ unless( $attrs->{order_by} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($attrs->{"order_by"}) 0 6 6
327 0 warn(q|
328  
329 Missing order_by attribute. Order will be undefined in |.__PACKAGE__.q| loop_through.
330  
331 |);
332  
333 }
334  
335 # init
336 6 my $page = 1;
337 6 my $search = $self->search(undef , { page => $page , rows => $rows });
338 6 my $last_page = $search->pager->last_page();
339  
340 6 my $ncalls = 0;
341 # loop though all pages.
342 PAGELOOP:
343 6 ▶ while( $page <= $last_page ){
Branch: 1 branch
BranchTrueFalseTotal
if ($page <= $last_page) 62 4 66
344 # Loop through this page
345 62 ▶ while( my $o = $search->next() ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $search->next) 2402 60 2462
346 2402 $code->($o);
347 2402 $ncalls++;
348 2402 ▶ if( $limit && ( $ncalls >= $limit ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($limit and $ncalls >= $limit) 2 2400 2402
Condition100%3/3
$limit and $ncalls >= $limit
!ll&&!rl&&r
22021982
MC/DC100%2/2
$limit and $ncalls >= $limit
$limit $ncalls >= $limit
Truth table100%3/3
A and B
A= $limitB= $ncalls >= $limit
ABresult
0X0
100
111
349 2 last PAGELOOP;
350 }
351 }
352 # Done with this page.
353 # Go to the next one.
354 60 $page++;
355 60 $search = $self->search(undef, { page => $page , rows => $rows });
356 }
357 }
358  
359  
360 =head2 fast_loop_through
361  
362 Loops through all the objects of this factory
363 in a Seeking fashion. If the primary key of the underlying
364 resultset is orderable and indexed, this should run
365 in linear time of the number of rows on the resultset.
366  
367 Usage:
368  
369 $this->fast_loop_through(sub{my ($o) = @_; ... } );
370  
371 $this->fast_loop_through(sub{ .. } , { rows => 100 , limit => 1000 });
372  
373 Options:
374  
375 rows: Fetch this amount of rows at each query. Default to 100
376  
377 limit: Return after looping through this amount of rows.
378  
379 B<Important>
380  
381 =over
382  
383 You do not need to order the set, as this will order it by ascending primary key.
384  
385 This means aggregation functions (such as group_by) will not work.
386  
387 Incidentally, it means that if other processes are writing to this resultset,
388 this method will play catch up on the resultset, so if the writing rate is higher
389 than the reading rate, this might take a while to return.
390  
391 If you want to avoid this, set the option 'order' to 'desc'.
392  
393 =back
394  
395 Returns the number of rows looped through.
396  
397 Prerequisites:
398  
399 Must have:
400  
401 - The underlying L<DBIx::Class::ResultSource> has a primary key
402  
403 - Each component of the primary key supports the operators '>' and '<'
404  
405 - It is possible to order all the rows by this primary key alone.
406  
407 Should have:
408  
409 - This primary key is indexed and offers fast comparison access.
410  
411 Inspired by http://use-the-index-luke.com/sql/partial-results/fetch-next-page
412  
413 =cut
414  
415 sub fast_loop_through{
416 7 ▶ my ($self , $code, $opts) = @_;
Subroutine fast_loop_through: called CC=13 SCAR=25.9
Pod documented
417  
418 7 ▶ unless( defined $code ){ $code = sub{}; }
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code) 0 7 7
419 7 ▶ unless( defined $opts ){ $opts = {}; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts) 3 4 7
420  
421 7 ▶ my $order = $opts->{order} || 'asc';
Condition100%2/2
$opts->{'order'} || 'asc'
l!l
43
MC/DC100%1/1
$opts->{'order'} || 'asc'
$opts->{'order'}
Truth table100%2/2
A
A= $opts->{'order'}
Aresult
11
00
422 7 ▶ my $rows = $opts->{rows} || 100;
Condition50%1/2
$opts->{'rows'} || 100
l!l
07
MC/DC0%0/1
$opts->{'rows'} || 100
$opts->{'rows'}
Truth table50%1/2
A
A= $opts->{'rows'}
Aresult
11
00
423 7 my $limit = $opts->{limit};
424  
425 # Gather the required info about the resultset
426 7 my $rs = $self->dbic_rs();
427  
428 # What is this source alias?
429 7 my $me = $rs->current_source_alias();
430  
431 # The source
432 7 my $source = $rs->result_source();
433 7 my @primary_columns = $source->primary_columns();
434 7 ▶ unless( @primary_columns ){
Branch: 1 branch
BranchTrueFalseTotal
unless (@primary_columns) 0 7 7
435 0 confess("Result Source ".$source->source_name()." does not have a primary key");
436 }
437  
438 7 my $order_by = [ map{ +{ '-'.$order => $me.'.'.$_ } } @primary_columns ];
439  
440 7 my $n_rows = 0;
441  
442 7 my $last_row;
443 7 ▶ do{
Branch: 1 branch
BranchTrueFalseTotal
$last_row and do { do { my $resultset = $self->dbic_rs->search_rs(undef, {"order_by", $order_by, "rows", $rows}); if ($last_row) { my $top_or = {"-or", []}; my $cur_or = $top_or; my $cmp_op = $order eq "asc" ? ">" : "<"; my $key_i = 0; for (; $key_i < @primary_columns - 1; ++$key_i) { my $column = $primary_columns[$key_i]; my $nested_or = {"-or", []}; push @{$$cur_or{"-or"};}, {$me . '.' . $column, {$cmp_op, $last_row->get_column($column)}}; push @{$$cur_or{"-or"};}, {"-and", [{$me . '.' . $column, {"=", $last_row->get_column($column)}}, $nested_or]}; $cur_or = $nested_or; } ; my $last_column = $primary_columns[$key_i]; push @{$$cur_or{"-or"};}, {$me . '.' . $last_column, {$cmp_op, $last_row->get_column($last_column)}}; $resultset = $resultset->search_rs($top_or); } ; $last_row = undef; while (my $o = $resultset->next) { do { $last_row = $o; my $wrapped = $self->wrap($o); &$code($wrapped); ++$n_rows; if ($limit and $n_rows == $limit) { return $n_rows; } }; } } } 59 7 66
444 66 my $resultset = $self->dbic_rs->search_rs(undef , { order_by => $order_by , rows => $rows });
445 66 ▶ if( $last_row ){
Branch: 1 branch
BranchTrueFalseTotal
if ($last_row) 59 7 66
446 # We have a last row.
447 # The idea here is to use the primary key to get the rows above
448 # this last row.
449  
450 # The primary key of the first queried row should be greater than
451 # the last row's one.
452 # Logically it should be: ( queried PK components ) > ( last row PK components )
453 # If the primary key is A , B , C , then the where clause should contain (for order desc):
454 # A > a || ( A = a && B > b || ( B = b && C > c ) )
455  
456 59 my $top_or = { -or => [] };
457 59 my $cur_or = $top_or;
458  
459 59 ▶ my $cmp_op = $order eq 'asc' ? '>' : '<';
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'asc' ? : 21 38 59
460  
461 59 my $key_i = 0;
462 59 ▶ for(; $key_i < @primary_columns - 1 ; $key_i++ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($key_i < @primary_columns - 1) 58 59 117
463  
464 58 my $column = $primary_columns[$key_i];
465  
466 58 my $nested_or = { -or => [] };
467  
468 58 push @{ $cur_or->{-or} } , { $me.'.'.$column => { $cmp_op => $last_row->get_column($column) }};
469 58 push @{ $cur_or->{-or} } , { -and => [ { $me.'.'.$column => { '=' => $last_row->get_column($column) } },
470 $nested_or
471 ]
472 };
473 58 $cur_or = $nested_or;
474 }
475  
476 59 my $last_column = $primary_columns[$key_i];
477 59 push @{ $cur_or->{-or} } , { $me.'.'.$last_column => { $cmp_op => $last_row->get_column($last_column) } };
478  
479 59 $resultset = $resultset->search_rs($top_or);
480 } # End of above last row seeking
481  
482 66 $last_row = undef;
483  
484 66 ▶ while( my $o = $resultset->next() ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $resultset->next) 5793 66 5859
485 5793 $last_row = $o;
486 5793 my $wrapped = $self->wrap($o);
487 5793 &$code($wrapped);
488 5793 $n_rows++;
489  
490 5793 ▶ if( $limit && ( $n_rows == $limit ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($limit and $n_rows == $limit) 0 5793 5793
Condition33%1/3
$limit and $n_rows == $limit
!ll&&!rl&&r
579300
MC/DC0%0/2
$limit and $n_rows == $limit
$limit $n_rows == $limit
Truth table33%1/3
A and B
A= $limitB= $n_rows == $limit
ABresult
0X0
100
111
491 0 return $n_rows;
492 }
493 }
494 }while( $last_row );
495  
496 7 return $n_rows;
497 }
498  
499 =head2 next
500  
501 Returns next Business Object from this current DBIx::Resultset.
502  
503 See L<DBIx::Class::ResultSet/next>
504  
505 =cut
506  
507 sub next{
508 2478 ▶ my ($self) = @_;
Subroutine next: called CC=2 SCAR=6.9
Pod documented
509 2478 my $next_o = $self->dbic_rs->next();
510 2478 ▶ return undef unless $next_o;
Branch: 1 branch
BranchTrueFalseTotal
unless $next_o 66 2412 2478
511 2412 return $self->wrap($next_o);
512 }
513  
514 =head2 count
515  
516 Returns the number of objects in this ResultSet.
517  
518 See L<DBIx::Class::ResultSet/count>
519  
520 =cut
521  
522 sub count{
523 18 ▶ my ($self) = @_;
Subroutine count: called CC=1 SCAR=0.0
Pod documented
524 18 return $self->dbic_rs->count();
525 }
526  
527  
528 4 __PACKAGE__->meta->make_immutable();
529 4 1;
↑ Summary Wrapper.pm »