Include the DBI error message if a deploy statement fails.
[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,
46 pid => $$,
47 };
48
49 $new->{tid} = threads->tid if $INC{'threads.pm'};
50
cb5f2eea 51 return bless ($new, $class);
28927b50 52}
53
5cf243f6 54=head2 next
55
21b5c39d 56=over 4
57
ebc77b53 58=item Arguments: none
21b5c39d 59
d601dc88 60=item Return Value: \@row_columns
21b5c39d 61
5cf243f6 62=back
63
685dad64 64Advances the cursor to the next row and returns an array of column
65values (the result of L<DBI/fetchrow_array> method).
5cf243f6 66
67=cut
68
28927b50 69sub next {
70 my ($self) = @_;
1346e22d 71
72 $self->_check_forks_threads;
cb5f2eea 73 if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) {
71e65b39 74 $self->{sth}->finish if $self->{sth}->{Active};
cb5f2eea 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];
5c91499f 81 if ($self->{attrs}{software_limit}) {
82 if (my $offset = $self->{attrs}{offset}) {
cb5f2eea 83 $self->{sth}->fetch for 1 .. $offset;
5c91499f 84 }
85 }
28927b50 86 }
cb5f2eea 87 my @row = $self->{sth}->fetchrow_array;
88 if (@row) {
89 $self->{pos}++;
90 } else {
91 delete $self->{sth};
92 $self->{done} = 1;
93 }
28927b50 94 return @row;
95}
96
5cf243f6 97=head2 all
98
21b5c39d 99=over 4
100
ebc77b53 101=item Arguments: none
21b5c39d 102
d601dc88 103=item Return Value: \@row_columns+
21b5c39d 104
5cf243f6 105=back
106
21b5c39d 107Returns a list of arrayrefs of column values for all rows in the
108L<DBIx::Class::ResultSet>.
5cf243f6 109
110=cut
111
1a14aa3f 112sub all {
113 my ($self) = @_;
1346e22d 114
115 $self->_check_forks_threads;
1a14aa3f 116 return $self->SUPER::all if $self->{attrs}{rows};
cb5f2eea 117 $self->{sth}->finish if $self->{sth}->{Active};
118 delete $self->{sth};
119 my ($rv, $sth) = $self->{storage}->_select(@{$self->{args}});
1a14aa3f 120 return @{$sth->fetchall_arrayref};
121}
122
5cf243f6 123=head2 reset
124
5cf243f6 125Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
126
127=cut
128
28927b50 129sub reset {
130 my ($self) = @_;
1346e22d 131
132 $self->_check_forks_threads;
28927b50 133 $self->{sth}->finish if $self->{sth}->{Active};
1346e22d 134 $self->_soft_reset;
135}
136
137sub _soft_reset {
138 my ($self) = @_;
139
cb5f2eea 140 delete $self->{sth};
28927b50 141 $self->{pos} = 0;
cb5f2eea 142 delete $self->{done};
28927b50 143 return $self;
144}
145
1346e22d 146sub _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
28927b50 160sub DESTROY {
161 my ($self) = @_;
1346e22d 162
163 $self->_check_forks_threads;
182fee36 164 $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active};
28927b50 165}
166
1671;