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