41b3da549da62d166ad36a0291c2933df6f5eb97
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Cursor.pm
1 package DBIx::Class::Storage::DBI::Cursor;
2
3 use base qw/DBIx::Class::Cursor/;
4
5 use strict;
6 use warnings;
7
8 sub new {
9   my ($class, $storage, $args, $attrs) = @_;
10   #use Data::Dumper; warn Dumper(@_);
11   $class = ref $class if ref $class;
12   my $new = {
13     storage => $storage,
14     args => $args,
15     pos => 0,
16     attrs => $attrs };
17   return bless ($new, $class);
18 }
19
20 sub next {
21   my ($self) = @_;
22   if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) {
23     delete $self->{sth};
24     $self->{done} = 1;
25   }
26   return if $self->{done};
27   unless ($self->{sth}) {
28     $self->{sth} = ($self->{storage}->_select(@{$self->{args}}))[1];
29     if ($self->{attrs}{software_limit}) {
30       if (my $offset = $self->{attrs}{offset}) {
31         $self->{sth}->fetch for 1 .. $offset;
32       }
33     }
34   }
35   my @row = $self->{sth}->fetchrow_array;
36   if (@row) {
37     $self->{pos}++;
38   } else {
39     delete $self->{sth};
40     $self->{done} = 1;
41   }
42   return @row;
43 }
44
45 sub all {
46   my ($self) = @_;
47   return $self->SUPER::all if $self->{attrs}{rows};
48   $self->{sth}->finish if $self->{sth}->{Active};
49   delete $self->{sth};
50   my ($rv, $sth) = $self->{storage}->_select(@{$self->{args}});
51   return @{$sth->fetchall_arrayref};
52 }
53
54 sub reset {
55   my ($self) = @_;
56   $self->{sth}->finish if $self->{sth}->{Active};
57   delete $self->{sth};
58   $self->{pos} = 0;
59   delete $self->{done};
60   return $self;
61 }
62
63 sub DESTROY {
64   my ($self) = @_;
65   $self->{sth}->finish if $self->{sth}->{Active};
66 }
67
68 1;