register_resultset, Cursor fixes
[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     $self->{sth}->finish if $self->{sth}->{Active};
24     delete $self->{sth};
25     $self->{done} = 1;
26   }
27   return if $self->{done};
28   unless ($self->{sth}) {
29     $self->{sth} = ($self->{storage}->_select(@{$self->{args}}))[1];
30     if ($self->{attrs}{software_limit}) {
31       if (my $offset = $self->{attrs}{offset}) {
32         $self->{sth}->fetch for 1 .. $offset;
33       }
34     }
35   }
36   my @row = $self->{sth}->fetchrow_array;
37   if (@row) {
38     $self->{pos}++;
39   } else {
40     delete $self->{sth};
41     $self->{done} = 1;
42   }
43   return @row;
44 }
45
46 sub all {
47   my ($self) = @_;
48   return $self->SUPER::all if $self->{attrs}{rows};
49   $self->{sth}->finish if $self->{sth}->{Active};
50   delete $self->{sth};
51   my ($rv, $sth) = $self->{storage}->_select(@{$self->{args}});
52   return @{$sth->fetchall_arrayref};
53 }
54
55 sub reset {
56   my ($self) = @_;
57   $self->{sth}->finish if $self->{sth}->{Active};
58   delete $self->{sth};
59   $self->{pos} = 0;
60   delete $self->{done};
61   return $self;
62 }
63
64 sub DESTROY {
65   my ($self) = @_;
66   $self->{sth}->finish if $self->{sth}->{Active};
67 }
68
69 1;