Commit | Line | Data |
1923c0b4 |
1 | package DBIx::Class::Cursor; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use overload |
6 | '0+' => 'count', |
7 | fallback => 1; |
8 | |
9 | sub new { |
54644855 |
10 | my ($it_class, $db_class, $sth, $args, $cols, $attrs) = @_; |
1923c0b4 |
11 | $sth->execute(@{$args || []}) unless $sth->{Active}; |
12 | my $new = { |
13 | class => $db_class, |
14 | sth => $sth, |
15 | cols => $cols, |
54644855 |
16 | args => $args, |
17 | attrs => $attrs }; |
1923c0b4 |
18 | return bless ($new, $it_class); |
19 | } |
20 | |
21 | sub next { |
22 | my ($self) = @_; |
23 | my @row = $self->{sth}->fetchrow_array; |
24 | return unless @row; |
25 | #unless (@row) { $self->{sth}->finish; return; } |
26 | return $self->{class}->_row_to_object($self->{cols}, \@row); |
27 | } |
28 | |
29 | sub count { |
54644855 |
30 | my ($self) = @_; |
31 | if (my $cond = $self->{attrs}->{where}) { |
32 | my $class = $self->{class}; |
33 | my $sth = $class->_get_sth( 'select', [ 'COUNT(*)' ], |
34 | $class->_table_name, $cond); |
2840de8f |
35 | my ($count) = $class->_get_dbh->selectrow_array( |
36 | $sth, undef, @{$self->{args} || []}); |
54644855 |
37 | return $count; |
38 | } else { |
39 | return scalar $_[0]->all; # So inefficient |
40 | } |
1923c0b4 |
41 | } |
42 | |
43 | sub all { |
44 | my ($self) = @_; |
45 | $self->reset; |
46 | my @all; |
47 | while (my $obj = $self->next) { |
48 | push(@all, $obj); |
49 | } |
50 | $self->reset; |
51 | return @all; |
52 | } |
53 | |
54 | sub reset { |
55 | $_[0]->{sth}->finish if $_[0]->{sth}->{Active}; |
56 | $_[0]->{sth}->execute(@{$_[0]->{args} || []}); |
57 | return $_[0]; |
58 | } |
59 | |
60 | sub first { |
61 | return $_[0]->reset->next; |
62 | } |
63 | |
64 | 1; |