File Coverage

blib/lib/SQL/Executor/Iterator.pm
Criterion Covered Total %
statement 22 22 100.0
branch 4 4 100.0
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 33 33 100.0


line stmt bran cond sub pod time code
1             package SQL::Executor::Iterator;
2 12     12   150 use strict;
  12         26  
  12         386  
3 12     12   56 use warnings;
  12         23  
  12         495  
4              
5             use Class::Accessor::Lite (
6 12         114 ro => ['sth', 'executor', 'table_name', 'select_id'],
7 12     12   59 );
  12         16  
8              
9             =head1 NAME
10              
11             SQL::Executor::Iterator - iterator for SQL::Executor
12              
13             =head1 SYNOPSIS
14              
15             use DBI;
16             use SQL::Executor;
17             my $dbh = DBI->connect($dsn, $id, $pass);
18             my $ex = SQL::Executor->new($dbh);
19             #
20             my $itr= $ex->select_named('SELECT id, value1 FROM SOME_TABLE WHERE value2 = :arg1', { arg1 => 'aaa' });
21              
22              
23             =head1 METHODS
24              
25             =cut
26              
27             =head2 new($sth, $table_name, $executor, $select_id)
28              
29             $sth: L's statement handler
30             $table_name: table name
31             $executor: SQL::Executor object
32             $select_id: select_id(UUID) this is used for to make Row object uniquely
33              
34             =cut
35              
36             sub new {
37 8     8 1 18 my ($class, $sth, $table_name, $executor, $select_id) = @_;
38              
39 8         45 my $self = {
40             sth => $sth,
41             table_name => $table_name,
42             executor => $executor,
43             select_id => $select_id,
44             };
45 8         50 bless $self, $class;
46             }
47              
48              
49             =head2 next
50              
51             return row hashref by default. if specified callback or table_callback option is specified in SQL::Executor::new(),
52             callback will be called.
53              
54             =cut
55              
56             sub next {
57 9     9 1 528 my ($self) = @_;
58 9         33 my $sth = $self->sth;
59 9         225 my $row = $sth->fetchrow_hashref;
60 9 100       42 if( !defined $row ) {
61 1         5 $sth->finish;
62 1         2 return;
63             }
64 8         31 my $callback = $self->executor->callback;
65 8 100       73 if( defined $callback ) {
66 4         20 return $callback->($self->executor, $row, $self->table_name, $self->select_id);
67             }
68 4         11 return $row;
69             }
70              
71              
72              
73             1;
74             __END__