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