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