Include the DBI error message if a deploy statement fails.
[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 array of column
65 values (the result of L<DBI/fetchrow_array> method).
66
67 =cut
68
69 sub next {
70   my ($self) = @_;
71
72   $self->_check_forks_threads;
73   if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) {
74     $self->{sth}->finish if $self->{sth}->{Active};
75     delete $self->{sth};
76     $self->{done} = 1;
77   }
78   return if $self->{done};
79   unless ($self->{sth}) {
80     $self->{sth} = ($self->{storage}->_select(@{$self->{args}}))[1];
81     if ($self->{attrs}{software_limit}) {
82       if (my $offset = $self->{attrs}{offset}) {
83         $self->{sth}->fetch for 1 .. $offset;
84       }
85     }
86   }
87   my @row = $self->{sth}->fetchrow_array;
88   if (@row) {
89     $self->{pos}++;
90   } else {
91     delete $self->{sth};
92     $self->{done} = 1;
93   }
94   return @row;
95 }
96
97 =head2 all
98
99 =over 4
100
101 =item Arguments: none
102
103 =item Return Value: \@row_columns+
104
105 =back
106
107 Returns a list of arrayrefs of column values for all rows in the
108 L<DBIx::Class::ResultSet>.
109
110 =cut
111
112 sub all {
113   my ($self) = @_;
114
115   $self->_check_forks_threads;
116   return $self->SUPER::all if $self->{attrs}{rows};
117   $self->{sth}->finish if $self->{sth}->{Active};
118   delete $self->{sth};
119   my ($rv, $sth) = $self->{storage}->_select(@{$self->{args}});
120   return @{$sth->fetchall_arrayref};
121 }
122
123 =head2 reset
124
125 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
126
127 =cut
128
129 sub reset {
130   my ($self) = @_;
131
132   $self->_check_forks_threads;
133   $self->{sth}->finish if $self->{sth}->{Active};
134   $self->_soft_reset;
135 }
136
137 sub _soft_reset {
138   my ($self) = @_;
139
140   delete $self->{sth};
141   $self->{pos} = 0;
142   delete $self->{done};
143   return $self;
144 }
145
146 sub _check_forks_threads {
147   my ($self) = @_;
148
149   if($INC{'threads.pm'} && $self->{tid} != threads->tid) {
150       $self->_soft_reset;
151       $self->{tid} = threads->tid;
152   }
153
154   if($self->{pid} != $$) {
155       $self->_soft_reset;
156       $self->{pid} = $$;
157   }
158 }
159
160 sub DESTROY {
161   my ($self) = @_;
162
163   $self->_check_forks_threads;
164   $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active};
165 }
166
167 1;