Cursor.pm because I'm an idiot and forgot to svk add it last time
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Cursor.pm
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 {
10   my ($it_class, $db_class, $sth, $args, $cols) = @_;
11   $sth->execute(@{$args || []}) unless $sth->{Active};
12   my $new = {
13     class => $db_class,
14     sth => $sth,
15     cols => $cols,
16     args => $args };
17   return bless ($new, $it_class);
18 }
19
20 sub next {
21   my ($self) = @_;
22   my @row = $self->{sth}->fetchrow_array;
23   return unless @row;
24   #unless (@row) { $self->{sth}->finish; return; }
25   return $self->{class}->_row_to_object($self->{cols}, \@row);
26 }
27
28 sub count {
29   return scalar $_[0]->all; # So inefficient
30 }
31
32 sub all {
33   my ($self) = @_;
34   $self->reset;
35   my @all;
36   while (my $obj = $self->next) {
37     push(@all, $obj);
38   }
39   $self->reset;
40   return @all;
41 }
42
43 sub reset {
44   $_[0]->{sth}->finish if $_[0]->{sth}->{Active};
45   $_[0]->{sth}->execute(@{$_[0]->{args} || []});
46   return $_[0];
47 }
48
49 sub first {
50   return $_[0]->reset->next;
51 }
52
53 1;