File Coverage

blib/lib/Alzabo/Runtime/RowCursor.pm
Criterion Covered Total %
statement 21 50 42.0
branch 0 6 0.0
condition 0 9 0.0
subroutine 7 10 70.0
pod 3 3 100.0
total 31 78 39.7


line stmt bran cond sub pod time code
1             package Alzabo::Runtime::RowCursor;
2              
3 11     11   64 use strict;
  11         29  
  11         453  
4 11     11   80 use vars qw($VERSION);
  11         23  
  11         497  
5              
6 11     11   69 use Alzabo::Exceptions;
  11         24  
  11         100  
7 11     11   57 use Alzabo::Runtime;
  11         21  
  11         283  
8              
9 11     11   59 use Params::Validate qw( :all );
  11         25  
  11         3573  
10             Params::Validate::validation_options( on_fail => sub { Alzabo::Exception::Params->throw( error => join '', @_ ) } );
11              
12 11     11   74 use base qw( Alzabo::Runtime::Cursor );
  11         30  
  11         1449  
13              
14             $VERSION = 2.0;
15              
16 11         5595 use constant NEW_SPEC => { statement => { isa => 'Alzabo::DriverStatement' },
17             table => { isa => 'Alzabo::Runtime::Table' },
18 11     11   76 };
  11         38  
19              
20             sub new
21             {
22 0     0 1   my $proto = shift;
23 0   0       my $class = ref $proto || $proto;
24              
25 0           my %p = validate( @_, NEW_SPEC );
26              
27 0           my $self = bless { %p,
28             count => 0,
29             }, $class;
30              
31 0           return $self;
32             }
33              
34             sub next
35             {
36 0     0 1   my $self = shift;
37              
38 0           my $row;
39              
40             # This loop is intended to allow the end caller to ignore rows
41             # that can't be created because they're not in the table.
42             #
43             # For example, imagine that query in the statement is looking at
44             # table 'foo' to get PK values for table 'bar'. If table 'foo'
45             # has a record indicating that there is a row in 'bar' where PK ==
46             # 1 but no such row actually exists then we want to skip this.
47             #
48             # If they really want to know we do save the exception.
49 0           until ( defined $row )
50             {
51 0           my @row = $self->{statement}->next;
52              
53 0 0 0       last unless @row && grep { defined } @row;
  0            
54              
55 0           my %hash;
56 0           my @pk = $self->{table}->primary_key;
57 0           @hash{ map { $_->name } @pk } = @row[0..$#pk];
  0            
58              
59 0           my %prefetch;
60 0 0 0       if ( (my @pre = $self->{table}->prefetch) && @row > @pk )
61             {
62 0           @prefetch{@pre} = @row[$#pk + 1 .. $#row];
63             }
64              
65 0           $row = $self->{table}->row_by_pk( @_,
66             pk => \%hash,
67             prefetch => \%prefetch,
68 0           %{ $self->{row_params} },
69             );
70             }
71              
72 0 0         return unless $row;
73 0           $self->{count}++;
74              
75 0           return $row;
76             }
77              
78             sub all_rows
79             {
80 0     0 1   my $self = shift;
81              
82 0           my @rows;
83 0           while ( my $row = $self->next )
84             {
85 0           push @rows, $row;
86             }
87              
88              
89 0           $self->{count} = scalar @rows;
90              
91 0           return @rows;
92             }
93              
94              
95             1;
96              
97             __END__