optimisation in cursor
[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   if ($self->{attrs}{software_limit}
128         && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
129     return $self->SUPER::all;
130   }
131   $self->{storage}->dbh_do($self->can('_dbh_all'), $self);
132 }
133
134 =head2 reset
135
136 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
137
138 =cut
139
140 sub reset {
141   my ($self) = @_;
142
143   # No need to care about failures here
144   eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} };
145   $self->_soft_reset;
146 }
147
148 sub _soft_reset {
149   my ($self) = @_;
150
151   delete $self->{sth};
152   delete $self->{done};
153   $self->{pos} = 0;
154   return $self;
155 }
156
157 sub _check_dbh_gen {
158   my ($self) = @_;
159
160   if($self->{_dbh_gen} != $self->{storage}->{_dbh_gen}) {
161     $self->{_dbh_gen} = $self->{storage}->{_dbh_gen};
162     $self->_soft_reset;
163   }
164 }
165
166 sub DESTROY {
167   my ($self) = @_;
168
169   # None of the reasons this would die matter if we're in DESTROY anyways
170   local $@;
171   eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} };
172 }
173
174 1;