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