reworded result -> row
[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
33=back
34
35Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
36
37=cut
38
28927b50 39sub new {
cb5f2eea 40 my ($class, $storage, $args, $attrs) = @_;
28927b50 41 #use Data::Dumper; warn Dumper(@_);
cb5f2eea 42 $class = ref $class if ref $class;
28927b50 43 my $new = {
cb5f2eea 44 storage => $storage,
28927b50 45 args => $args,
46 pos => 0,
1346e22d 47 attrs => $attrs,
48 pid => $$,
49 };
50
51 $new->{tid} = threads->tid if $INC{'threads.pm'};
52
cb5f2eea 53 return bless ($new, $class);
28927b50 54}
55
5cf243f6 56=head2 next
57
58=back
59
8e23eaf2 60Advances the cursor to the next row and returns it.
5cf243f6 61
62=cut
63
28927b50 64sub next {
65 my ($self) = @_;
1346e22d 66
67 $self->_check_forks_threads;
cb5f2eea 68 if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) {
71e65b39 69 $self->{sth}->finish if $self->{sth}->{Active};
cb5f2eea 70 delete $self->{sth};
71 $self->{done} = 1;
72 }
73 return if $self->{done};
74 unless ($self->{sth}) {
75 $self->{sth} = ($self->{storage}->_select(@{$self->{args}}))[1];
5c91499f 76 if ($self->{attrs}{software_limit}) {
77 if (my $offset = $self->{attrs}{offset}) {
cb5f2eea 78 $self->{sth}->fetch for 1 .. $offset;
5c91499f 79 }
80 }
28927b50 81 }
cb5f2eea 82 my @row = $self->{sth}->fetchrow_array;
83 if (@row) {
84 $self->{pos}++;
85 } else {
86 delete $self->{sth};
87 $self->{done} = 1;
88 }
28927b50 89 return @row;
90}
91
5cf243f6 92=head2 all
93
94=back
95
8e23eaf2 96Returns all rows in the L<DBIx::Class::ResultSet>.
5cf243f6 97
98=cut
99
1a14aa3f 100sub all {
101 my ($self) = @_;
1346e22d 102
103 $self->_check_forks_threads;
1a14aa3f 104 return $self->SUPER::all if $self->{attrs}{rows};
cb5f2eea 105 $self->{sth}->finish if $self->{sth}->{Active};
106 delete $self->{sth};
107 my ($rv, $sth) = $self->{storage}->_select(@{$self->{args}});
1a14aa3f 108 return @{$sth->fetchall_arrayref};
109}
110
5cf243f6 111=head2 reset
112
113=back
114
115Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
116
117=cut
118
28927b50 119sub reset {
120 my ($self) = @_;
1346e22d 121
122 $self->_check_forks_threads;
28927b50 123 $self->{sth}->finish if $self->{sth}->{Active};
1346e22d 124 $self->_soft_reset;
125}
126
127sub _soft_reset {
128 my ($self) = @_;
129
cb5f2eea 130 delete $self->{sth};
28927b50 131 $self->{pos} = 0;
cb5f2eea 132 delete $self->{done};
28927b50 133 return $self;
134}
135
1346e22d 136sub _check_forks_threads {
137 my ($self) = @_;
138
139 if($INC{'threads.pm'} && $self->{tid} != threads->tid) {
140 $self->_soft_reset;
141 $self->{tid} = threads->tid;
142 }
143
144 if($self->{pid} != $$) {
145 $self->_soft_reset;
146 $self->{pid} = $$;
147 }
148}
149
28927b50 150sub DESTROY {
151 my ($self) = @_;
1346e22d 152
153 $self->_check_forks_threads;
28927b50 154 $self->{sth}->finish if $self->{sth}->{Active};
155}
156
1571;