- Storage/DBI.pm now uses Abstract internally
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Cursor.pm
1 package DBIx::Class::Cursor;
2
3 use strict;
4 use warnings;
5
6 sub new {
7   my ($it_class, $sth, $args, $attrs) = @_;
8   #use Data::Dumper; warn Dumper(@_);
9   $it_class = ref $it_class if ref $it_class;
10   my $new = {
11     sth => $sth,
12     args => $args,
13     pos => 0,
14     attrs => $attrs };
15   return bless ($new, $it_class);
16 }
17
18 sub next {
19   my ($self) = @_;
20   return if $self->{attrs}{rows}
21     && $self->{pos} >= $self->{attrs}{rows}; # + $self->{attrs}{offset});
22   unless ($self->{live_sth}) {
23     $self->{sth}->execute(@{$self->{args} || []});
24     if (my $offset = $self->{attrs}{offset}) {
25       $self->{sth}->fetch for 1 .. $offset;
26     }
27     $self->{live_sth} = 1;
28   }
29   my @row = $self->{sth}->fetchrow_array;
30   $self->{pos}++ if @row;
31   return @row;
32 }
33
34 sub reset {
35   my ($self) = @_;
36   $self->{sth}->finish if $self->{sth}->{Active};
37   $self->{pos} = 0;
38   $self->{live_sth} = 0;
39   return $self;
40 }
41
42 sub DESTROY {
43   my ($self) = @_;
44   $self->{sth}->finish if $self->{sth}->{Active};
45 }
46
47 1;