Commit | Line | Data |
5cf243f6 |
1 | package DBIx::Class::Storage::DBI::Cursor; |
28927b50 |
2 | |
3 | use base qw/DBIx::Class::Cursor/; |
4 | |
5 | use strict; |
6 | use warnings; |
7 | |
5cf243f6 |
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 | |
5cf243f6 |
33 | Returns a new L<DBIx::Class::Storage::DBI::Cursor> object. |
34 | |
35 | =cut |
36 | |
28927b50 |
37 | sub 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 |
64 | Advances the cursor to the next row and returns an array of column |
65 | values (the result of L<DBI/fetchrow_array> method). |
5cf243f6 |
66 | |
67 | =cut |
68 | |
28927b50 |
69 | sub 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 |
107 | Returns a list of arrayrefs of column values for all rows in the |
108 | L<DBIx::Class::ResultSet>. |
5cf243f6 |
109 | |
110 | =cut |
111 | |
1a14aa3f |
112 | sub 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 |
125 | Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>. |
126 | |
127 | =cut |
128 | |
28927b50 |
129 | sub 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 | |
137 | sub _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 |
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 | |
28927b50 |
160 | sub 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 | |
167 | 1; |