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