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