e6ff5dd5e6be843b4c0d26c75025e32fefc734d9
[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 =head1 NAME
9
10 DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a
11 resultset.
12
13 =head1 SYNOPSIS
14
15   my $cursor = $schema->resultset('CD')->cursor();
16   my $first_cd = $cursor->next;
17
18 =head1 DESCRIPTION
19
20 A Cursor represents a query cursor on a L<DBIx::Class::ResultSet> object. It
21 allows for traversing the result set with L</next>, retrieving all results with
22 L</all> and resetting the cursor with L</reset>.
23
24 Usually, you would use the cursor methods built into L<DBIx::Class::ResultSet>
25 to traverse it. See L<DBIx::Class::ResultSet/next>,
26 L<DBIx::Class::ResultSet/reset> and L<DBIx::Class::ResultSet/all> for more
27 information.
28
29 =head1 METHODS
30
31 =head2 new
32
33 Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
34
35 =cut
36
37 sub new {
38   my ($class, $storage, $args, $attrs) = @_;
39   #use Data::Dumper; warn Dumper(@_);
40   $class = ref $class if ref $class;
41   my $new = {
42     storage => $storage,
43     args => $args,
44     pos => 0,
45     attrs => $attrs,
46     _dbh_gen => $storage->{_dbh_gen},
47   };
48
49   return bless ($new, $class);
50 }
51
52 =head2 next
53
54 =over 4
55
56 =item Arguments: none
57
58 =item Return Value: \@row_columns
59
60 =back
61
62 Advances the cursor to the next row and returns an array of column
63 values (the result of L<DBI/fetchrow_array> method).
64
65 =cut
66
67 sub _dbh_next {
68   my ($storage, $dbh, $self) = @_;
69
70   $self->_check_dbh_gen;
71   if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) {
72     $self->{sth}->finish if $self->{sth}->{Active};
73     delete $self->{sth};
74     $self->{done} = 1;
75   }
76   return if $self->{done};
77   unless ($self->{sth}) {
78     $self->{sth} = ($storage->_select(@{$self->{args}}))[1];
79     if ($self->{attrs}{software_limit}) {
80       if (my $offset = $self->{attrs}{offset}) {
81         $self->{sth}->fetch for 1 .. $offset;
82       }
83     }
84   }
85   my @row = $self->{sth}->fetchrow_array;
86   if (@row) {
87     $self->{pos}++;
88   } else {
89     delete $self->{sth};
90     $self->{done} = 1;
91   }
92   return @row;
93 }
94
95 sub next {
96   my ($self) = @_;
97   $self->{storage}->dbh_do($self->can('_dbh_next'), $self);
98 }
99
100 =head2 all
101
102 =over 4
103
104 =item Arguments: none
105
106 =item Return Value: \@row_columns+
107
108 =back
109
110 Returns a list of arrayrefs of column values for all rows in the
111 L<DBIx::Class::ResultSet>.
112
113 =cut
114
115 sub _dbh_all {
116   my ($storage, $dbh, $self) = @_;
117
118   $self->_check_dbh_gen;
119   $self->{sth}->finish if $self->{sth}->{Active};
120   delete $self->{sth};
121   my ($rv, $sth) = $storage->_select(@{$self->{args}});
122   return @{$sth->fetchall_arrayref};
123 }
124
125 sub all {
126   my ($self) = @_;
127   return $self->SUPER::all if $self->{attrs}{rows};
128   $self->{storage}->dbh_do($self->can('_dbh_all'), $self);
129 }
130
131 =head2 reset
132
133 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
134
135 =cut
136
137 sub reset {
138   my ($self) = @_;
139
140   # No need to care about failures here
141   eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} };
142   $self->_soft_reset;
143 }
144
145 sub _soft_reset {
146   my ($self) = @_;
147
148   delete $self->{sth};
149   delete $self->{done};
150   $self->{pos} = 0;
151   return $self;
152 }
153
154 sub _check_dbh_gen {
155   my ($self) = @_;
156
157   if($self->{_dbh_gen} != $self->{storage}->{_dbh_gen}) {
158     $self->{_dbh_gen} = $self->{storage}->{_dbh_gen};
159     $self->_soft_reset;
160   }
161 }
162
163 sub DESTROY {
164   my ($self) = @_;
165
166   # None of the reasons this would die matter if we're in DESTROY anyways
167   local $@;
168   eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} };
169 }
170
171 1;