Teng/Iterator.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 71 CC 7 SCAR 25.7
CC
7
Cov
0%
next
20.0
all
20.0
new
2.0
CRAP 13.1
1 package Teng::Iterator;
2 use strict;
3 use warnings;
4 use Carp ();
5 use Class::Accessor::Lite (
6 rw => [qw/suppress_object_creation/],
7 );
8  
9 sub new {
10 my ($class, %args) = @_;
11  
12 return bless \%args, $class;
13 }
14  
15 sub next {
16 my $self = shift;
17  
18 my $row;
19 if ($self->{sth}) {
20 $row = $self->{sth}->fetchrow_hashref;
21 $self->{select_columns} ||= $self->{sth}->{$self->{teng}->{fields_case}};
22 unless ( $row ) {
23 $self->{sth}->finish;
24 $self->{sth} = undef;
25 return;
26 }
27 } else {
28 return;
29 }
30  
31 if ($self->{suppress_object_creation}) {
32 return $row;
33 } else {
34 return $self->{row_class}->new(
35 {
36 sql => $self->{sql},
37 row_data => $row,
38 teng => $self->{teng},
39 table => $self->{table},
40 table_name => $self->{table_name},
41 select_columns => $self->{select_columns},
42 }
43 );
44 }
45 }
46  
47 sub all {
48 my $self = shift;
49  
50 my $result = [];
51  
52 if ($self->{sth}) {
53 $self->{select_columns} ||= $self->{sth}->{$self->{teng}->{fields_case}};
54 $result = $self->{sth}->fetchall_arrayref(+{});
55 $self->{sth}->finish;
56 $self->{sth} = undef;
57  
58 if (!$self->{suppress_object_creation}) {
59 $result = [map {
60 $self->{row_class}->new(
61 {
62 sql => $self->{sql},
63 row_data => $_,
64 teng => $self->{teng},
65 table => $self->{table},
66 table_name => $self->{table_name},
67 select_columns => $self->{select_columns},
68 }
69 )
70 } @$result];
71 }
72 }
73  
74 return wantarray ? @$result : $result;
75 }
76  
77 1;
78  
79 __END__
80 =head1 NAME
81  
82 Teng::Iterator - Iterator for Teng
83  
84 =head1 DESCRIPTION
85  
86 This is an iterator class for L<Teng>.
87  
88 =head1 SYNOPSIS
89  
90 my $itr = Your::Model->search('user',{});
91
92 my @rows = $itr->all; # get all rows
93  
94 # do iteration
95 while (my $row = $itr->next) {
96 ...
97 }
98  
99 =head1 METHODS
100  
101 =over
102  
103 =item $itr = Teng::Iterator->new()
104  
105 Create new Teng::Iterator's object. You may not call this method directly.
106  
107 =item my $row = $itr->next();
108  
109 Get next row data.
110  
111 =item my @ary = $itr->all;
112  
113 Get all row data in array.
114  
115 =item $itr->suppress_object_creation($bool)
116  
117 Set row object creation mode.
118  
119 =cut
120