And score! (all works)
[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   $class = ref $class if ref $class;
40
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 (
72     $self->{attrs}{software_limit}
73       && $self->{attrs}{rows}
74         && $self->{pos} >= $self->{attrs}{rows}
75   ) {
76     $self->{sth}->finish if $self->{sth}->{Active};
77     delete $self->{sth};
78     $self->{done} = 1;
79   }
80   return if $self->{done};
81   unless ($self->{sth}) {
82     $self->{sth} = ($storage->_select(@{$self->{args}}))[1];
83     if ($self->{attrs}{software_limit}) {
84       if (my $offset = $self->{attrs}{offset}) {
85         $self->{sth}->fetch for 1 .. $offset;
86       }
87     }
88   }
89   my @row = $self->{sth}->fetchrow_array;
90   if (@row) {
91     $self->{pos}++;
92   } else {
93     delete $self->{sth};
94     $self->{done} = 1;
95   }
96   return @row;
97 }
98
99 sub next {
100   my ($self) = @_;
101   $self->{storage}->dbh_do($self->can('_dbh_next'), $self);
102 }
103
104 =head2 all
105
106 =over 4
107
108 =item Arguments: none
109
110 =item Return Value: \@row_columns+
111
112 =back
113
114 Returns a list of arrayrefs of column values for all rows in the
115 L<DBIx::Class::ResultSet>.
116
117 =cut
118
119 sub _dbh_all {
120   my ($storage, $dbh, $self) = @_;
121
122   $self->_check_dbh_gen;
123   $self->{sth}->finish if $self->{sth}->{Active};
124   delete $self->{sth};
125   my ($rv, $sth) = $storage->_select(@{$self->{args}});
126   return @{$sth->fetchall_arrayref};
127 }
128
129 sub all {
130   my ($self) = @_;
131   if ($self->{attrs}{software_limit}
132         && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
133     return $self->next::method;
134   }
135
136   $self->{storage}->dbh_do($self->can('_dbh_all'), $self);
137 }
138
139 =head2 reset
140
141 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
142
143 =cut
144
145 sub reset {
146   my ($self) = @_;
147
148   # No need to care about failures here
149   eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} };
150   $self->_soft_reset;
151 }
152
153 sub _soft_reset {
154   my ($self) = @_;
155
156   delete $self->{sth};
157   delete $self->{done};
158   $self->{pos} = 0;
159   return $self;
160 }
161
162 sub _check_dbh_gen {
163   my ($self) = @_;
164
165   if($self->{_dbh_gen} != $self->{storage}->{_dbh_gen}) {
166     $self->{_dbh_gen} = $self->{storage}->{_dbh_gen};
167     $self->_soft_reset;
168   }
169 }
170
171 sub DESTROY {
172   my ($self) = @_;
173
174   # None of the reasons this would die matter if we're in DESTROY anyways
175   local $@;
176   eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} };
177 }
178
179 1;