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