File Coverage

blib/lib/DBIx/QuickORM/STH.pm
Criterion Covered Total %
statement 47 54 87.0
branch 20 28 71.4
condition 1 8 12.5
subroutine 12 16 75.0
pod 0 8 0.0
total 80 114 70.1


line stmt bran cond sub pod time code
1             package DBIx::QuickORM::STH;
2 24     24   184 use strict;
  24         86  
  24         1079  
3 24     24   166 use warnings;
  24         49  
  24         2109  
4              
5             our $VERSION = '0.000019';
6              
7 24     24   201 use Carp qw/croak/;
  24         106  
  24         1694  
8              
9 24     24   674 use Role::Tiny::With qw/with/;
  24         12935  
  24         2583  
10              
11             with 'DBIx::QuickORM::Role::STH';
12              
13 24         322 use DBIx::QuickORM::Util::HashBase qw{
14             <connection
15             <dbh
16             <sth
17             <sql
18             <source
19              
20             only_one
21             no_rows
22              
23             +dialect
24             +ready
25             <result
26             <done
27              
28             on_ready
29             +fetch_cb
30 24     24   191 };
  24         68  
31              
32       153 0   sub clear { }
33 0   0 0 0 0 sub ready { $_[0]->{+READY} //= 1 }
34 0     0 0 0 sub got_result { 1 }
35              
36 0   0 0 0 0 sub dialect { $_[0]->{+DIALECT} //= $_[0]->{+CONNECTION}->dialect }
37              
38 0     0 0 0 sub deferred_result { 0 }
39              
40             sub init {
41 153     153 0 347 my $self = shift;
42              
43 153 50       851 croak "'connection' is a required attribute" unless $self->{+CONNECTION};
44 153 50       568 croak "'source' is a required attribute" unless $self->{+SOURCE};
45 153 50       697 croak "'sth' is a required attribute" unless $self->{+STH};
46 153 50       588 croak "'dbh' is a required attribute" unless $self->{+DBH};
47 153 50 33     816 croak "'result' is a required attribute" unless exists($self->{+RESULT}) || $self->deferred_result;
48             }
49              
50             sub next {
51 164     164 0 357 my $self = shift;
52 164         645 my $row_hr = $self->_next;
53              
54 164 100       730 if ($self->{+ONLY_ONE}) {
55 116 50       455 croak "Expected only 1 row, but got more than one" if $self->_next;
56 116         340 $self->set_done;
57             }
58              
59 164         1360 return $row_hr;
60             }
61              
62             sub _fetch {
63 426     426   845 my $self = shift;
64 426 100       2585 return $self->{+FETCH_CB} if exists $self->{+FETCH_CB};
65              
66 153 50       642 if (my $on_ready = $self->{+ON_READY}) {
67 153         1070 return $self->{+FETCH_CB} = $on_ready->($self->{+DBH}, $self->{+STH}, $self->result, $self->{+SQL});
68             }
69              
70 0         0 $self->result;
71 0         0 $self->{+FETCH_CB} = undef;
72 0         0 return;
73             }
74              
75             sub _next {
76 280     280   639 my $self = shift;
77              
78 280 100       1121 return if $self->{+DONE};
79              
80 273 50       905 if (my $fetch = $self->_fetch) {
81 273         881 my $row_hr = $fetch->();
82 273 100       1245 return $row_hr if $row_hr;
83             }
84              
85 124         590 $self->set_done;
86              
87 124         495 return undef;
88             }
89              
90             sub set_done {
91 269     269 0 564 my $self = shift;
92              
93 269 100       849 return if $self->{+DONE};
94              
95             # Do this to make sure on_ready runs if it has not already.
96 153         590 $self->_fetch;
97 153         755 $self->clear;
98              
99 153         432 $self->{+DONE} = 1;
100             }
101              
102             sub DESTROY {
103 153     153   29086 my $self = shift;
104 153 100       34609 return if $self->{+DONE};
105 29         163 $self->set_done();
106 29         59364 return;
107             }
108              
109             1;