documented DBIx::Class::Cursor and DBIx::Class::Storage::DBI::Cursor
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Cursor.pm
1 package DBIx::Class::Storage::DBI::Cursor;
2
3 use base qw/DBIx::Class::Cursor/;
4
5 use strict;
6 use warnings;
7
8 =head1 NAME
9
10 DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a
11 resultset.
12
13 =head1 SYNOPSIS
14
15   my $cursor = $schema->resultset('CD')->cursor();
16   my $first_cd = $cursor->next;
17
18 =head1 DESCRIPTION
19
20 A Cursor represents a query cursor on a L<DBIx::Class::ResultSet> object. It
21 allows for traversing the result set with L</next>, retrieving all results with
22 L</all> and resetting the cursor with L</reset>.
23
24 Usually, you would use the cursor methods built into L<DBIx::Class::ResultSet>
25 to traverse it. See L<DBIx::Class::ResultSet/next>,
26 L<DBIx::Class::ResultSet/reset> and L<DBIx::Class::ResultSet/all> for more
27 information.
28
29 =head1 METHODS
30
31 =head2 new
32
33 =back
34
35 Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
36
37 =cut
38
39 sub new {
40   my ($class, $storage, $args, $attrs) = @_;
41   #use Data::Dumper; warn Dumper(@_);
42   $class = ref $class if ref $class;
43   my $new = {
44     storage => $storage,
45     args => $args,
46     pos => 0,
47     attrs => $attrs,
48     pid => $$,
49   };
50
51   $new->{tid} = threads->tid if $INC{'threads.pm'};
52   
53   return bless ($new, $class);
54 }
55
56 =head2 next
57
58 =back
59
60 Advances the cursor to the next result and returns it.
61
62 =cut
63
64 sub next {
65   my ($self) = @_;
66
67   $self->_check_forks_threads;
68   if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) {
69     $self->{sth}->finish if $self->{sth}->{Active};
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];
76     if ($self->{attrs}{software_limit}) {
77       if (my $offset = $self->{attrs}{offset}) {
78         $self->{sth}->fetch for 1 .. $offset;
79       }
80     }
81   }
82   my @row = $self->{sth}->fetchrow_array;
83   if (@row) {
84     $self->{pos}++;
85   } else {
86     delete $self->{sth};
87     $self->{done} = 1;
88   }
89   return @row;
90 }
91
92 =head2 all
93
94 =back
95
96 Returns all results in the L<DBIx::Class::ResultSet>.
97
98 =cut
99
100 sub all {
101   my ($self) = @_;
102
103   $self->_check_forks_threads;
104   return $self->SUPER::all if $self->{attrs}{rows};
105   $self->{sth}->finish if $self->{sth}->{Active};
106   delete $self->{sth};
107   my ($rv, $sth) = $self->{storage}->_select(@{$self->{args}});
108   return @{$sth->fetchall_arrayref};
109 }
110
111 =head2 reset
112
113 =back
114
115 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
116
117 =cut
118
119 sub reset {
120   my ($self) = @_;
121
122   $self->_check_forks_threads;
123   $self->{sth}->finish if $self->{sth}->{Active};
124   $self->_soft_reset;
125 }
126
127 sub _soft_reset {
128   my ($self) = @_;
129
130   delete $self->{sth};
131   $self->{pos} = 0;
132   delete $self->{done};
133   return $self;
134 }
135
136 sub _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
150 sub DESTROY {
151   my ($self) = @_;
152
153   $self->_check_forks_threads;
154   $self->{sth}->finish if $self->{sth}->{Active};
155 }
156
157 1;