File Coverage

blib/lib/DBIx/DataStore/ResultSet.pm
Criterion Covered Total %
statement 41 66 62.1
branch 9 24 37.5
condition 3 14 21.4
subroutine 9 13 69.2
pod 0 7 0.0
total 62 124 50.0


line stmt bran cond sub pod time code
1             package DBIx::DataStore::ResultSet;
2             $DBIx::DataStore::ResultSet::VERSION = '0.097';
3             *DEBUG = *DBIx::DataStore::DEBUG;
4             *dslog = *DBIx::DataStore::dslog;
5              
6 2     2   8 use strict;
  2         4  
  2         60  
7 2     2   8 use warnings;
  2         2  
  2         85  
8              
9 2     2   8 use base 'DBIx::DataStore::ResultRow';
  2         2  
  2         304  
10              
11             use overload (
12 38     38   932 'bool' => sub { !${$_[0]}->{'error'} },
  38         226  
13 2     2   10 );
  2         9  
  2         18  
14              
15             sub all {
16 0     0 0 0 my ($self) = @_;
17              
18 0         0 my $data = $$self->{'_sth'}->fetchall_arrayref();
19              
20 0 0       0 if ($$self->{'_sth'}->err) {
21 0         0 die dslog("Encountered error when retrieving complete result set: " . $$self->{'_sth'}->errstr);
22             }
23              
24 0 0 0     0 if ($data && @$data) {
25 0         0 my $fields = [ @{ $$self->{'_sth'}->{'NAME'} } ];
  0         0  
26 0         0 my $index = { %{ $$self->{'_sth'}->{'NAME_hash'} } };
  0         0  
27              
28 0         0 foreach (@$data) {
29 0         0 $_ = DBIx::DataStore::ResultRow->new($fields, $index, $_);
30             }
31             }
32              
33 0         0 return $data;
34             }
35              
36             sub error {
37 0     0 0 0 return ${$_[0]}->{'error'};
  0         0  
38             }
39              
40             sub next {
41 19     19 0 2762 my ($self) = @_;
42              
43 19 100       396 if (my $row = $$self->{'_sth'}->fetchrow_arrayref)
44             {
45 14         34 $$self->{'impl'}->[DBIx::DataStore::ResultRow::VALUES()] = $row;
46 14         65 return $self;
47             }
48 5         31 return 0;
49             }
50              
51             sub next_hashref {
52 0     0 0 0 my ($self) = @_;
53              
54 0 0       0 if ($self->next) {
55 0         0 return $self->hashref;
56             }
57 0         0 return 0;
58             }
59              
60             sub page {
61 0     0 0 0 my ($self) = @_;
62             }
63              
64             sub pager {
65 1     1 0 7 my ($self) = @_;
66              
67 1 50   1   177 eval("use Data::Page") unless defined $Data::Page::VERSION;
  1         1266  
  1         8819  
  1         9  
68              
69 1         53 my $p = Data::Page->new();
70              
71 1         91 $p->total_entries($self->count());
72 1         25 $p->entries_per_page($$self->{'_page_per'});
73 1         23 $p->current_page($$self->{'_page_num'});
74              
75 1         19 return $p;
76             }
77              
78             sub count {
79 3     3 0 343 my ($self) = @_;
80              
81             # This might seem a bit round-a-bout, but it will make it a simple matter to add
82             # more statement types to the check if it turns out there are non-select statements
83             # that should be handled in the same way as well.
84             # if (!exists { map { $_ => '' } qw( select ) }->{$$self->{'_st_type'}}) {
85             # return $$self->{'_rows'};
86             # }
87             # Faster method when selects are the only things that get handled below
88 3 100       79 return $$self->{'_rows'} unless $$self->{'_st_type'} eq 'select';
89              
90             # See if we've already been called before and stored the total number of rows
91 2 50 33     11 if (defined $$self->{'_total_rows'} && $$self->{'_total_rows'} =~ /^\d+$/o) {
92 0         0 return $$self->{'_total_rows'};
93             }
94              
95 2 50       13 if ($$self->{'_sql'} =~ /^\s*select\s+count\(\s*[*]\s*\)\s+/ois) {
96 0 0 0     0 if ($self && $self->next) {
97 0         0 $$self->{'_total_rows'} = $self->[0];
98 0         0 return $self->[0];
99             }
100             } else {
101             # negative look-ahead tries to prevent warning when the limit is part of a subquery
102 2 50       19 if ($$self->{'_sql'} =~ /\s+limit\s+\d+(?!.*[)])/oi) {
103 0 0       0 dslog("Getting result set row count for a query that appears to have used a LIMIT clause. Eh... why not?")
104             if DEBUG() >= 3;
105             }
106              
107 2         16 my $query = "select count(*) from ( " . $$self->{'_sql'} . " ) derived";
108             my $sth = $$self->{'_dbh'}->prepare($query)
109 2   50     27 || die dslog("Error encountered preparing row count query: " . $$self->{'_dbh'}->errstr);
110 2 50       336 $sth->execute(@{$$self->{'_binds'}})
  2         68  
111             || die dslog("Error encountered executing row count query: " . $sth->errstr);
112              
113 2   33     31 my $row = $sth->fetchrow_arrayref || confess("Error retrieving row from database result: " . $sth->errstr);
114 2         9 $$self->{'_total_rows'} = $row->[0];
115 2         45 return $row->[0];
116             }
117              
118 0         0 return;
119             }
120              
121             1;