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) = @_; |
cb5f2eea |
39 | $class = ref $class if ref $class; |
2007929b |
40 | |
28927b50 |
41 | my $new = { |
cb5f2eea |
42 | storage => $storage, |
28927b50 |
43 | args => $args, |
44 | pos => 0, |
1346e22d |
45 | attrs => $attrs, |
dbaee748 |
46 | _dbh_gen => $storage->{_dbh_gen}, |
1346e22d |
47 | }; |
48 | |
cb5f2eea |
49 | return bless ($new, $class); |
28927b50 |
50 | } |
51 | |
59af6677 |
52 | =head2 as_query |
53 | |
658fa250 |
54 | =over 4 |
55 | |
56 | =item Arguments: none |
57 | |
4dc99a01 |
58 | =item Return Value: \[ $sql, @bind ] |
658fa250 |
59 | |
60 | =back |
61 | |
59af6677 |
62 | Returns the SQL statement and bind vars associated with the invocant. |
63 | |
64 | =cut |
65 | |
66 | sub as_query { |
67 | my $self = shift; |
68 | |
69 | my $storage = $self->{storage}; |
70 | my $sql_maker = $storage->sql_maker; |
71 | local $sql_maker->{for}; |
72 | |
73 | my @args = $storage->_select_args(@{$self->{args}}); |
74 | my ($sql, $bind) = $storage->_prep_for_execute(@args[0 .. 2], [@args[4 .. $#args]]); |
da0b073b |
75 | return \[ "($sql)", @$bind ]; |
59af6677 |
76 | } |
77 | |
5cf243f6 |
78 | =head2 next |
79 | |
21b5c39d |
80 | =over 4 |
81 | |
ebc77b53 |
82 | =item Arguments: none |
21b5c39d |
83 | |
d601dc88 |
84 | =item Return Value: \@row_columns |
21b5c39d |
85 | |
5cf243f6 |
86 | =back |
87 | |
685dad64 |
88 | Advances the cursor to the next row and returns an array of column |
89 | values (the result of L<DBI/fetchrow_array> method). |
5cf243f6 |
90 | |
91 | =cut |
92 | |
dbaee748 |
93 | sub _dbh_next { |
94 | my ($storage, $dbh, $self) = @_; |
1346e22d |
95 | |
dbaee748 |
96 | $self->_check_dbh_gen; |
cb5f2eea |
97 | if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) { |
71e65b39 |
98 | $self->{sth}->finish if $self->{sth}->{Active}; |
cb5f2eea |
99 | delete $self->{sth}; |
100 | $self->{done} = 1; |
101 | } |
102 | return if $self->{done}; |
103 | unless ($self->{sth}) { |
dbaee748 |
104 | $self->{sth} = ($storage->_select(@{$self->{args}}))[1]; |
5c91499f |
105 | if ($self->{attrs}{software_limit}) { |
106 | if (my $offset = $self->{attrs}{offset}) { |
cb5f2eea |
107 | $self->{sth}->fetch for 1 .. $offset; |
5c91499f |
108 | } |
109 | } |
28927b50 |
110 | } |
cb5f2eea |
111 | my @row = $self->{sth}->fetchrow_array; |
112 | if (@row) { |
113 | $self->{pos}++; |
114 | } else { |
115 | delete $self->{sth}; |
116 | $self->{done} = 1; |
117 | } |
28927b50 |
118 | return @row; |
119 | } |
120 | |
dbaee748 |
121 | sub next { |
122 | my ($self) = @_; |
123 | $self->{storage}->dbh_do($self->can('_dbh_next'), $self); |
124 | } |
125 | |
5cf243f6 |
126 | =head2 all |
127 | |
21b5c39d |
128 | =over 4 |
129 | |
ebc77b53 |
130 | =item Arguments: none |
21b5c39d |
131 | |
d601dc88 |
132 | =item Return Value: \@row_columns+ |
21b5c39d |
133 | |
5cf243f6 |
134 | =back |
135 | |
21b5c39d |
136 | Returns a list of arrayrefs of column values for all rows in the |
137 | L<DBIx::Class::ResultSet>. |
5cf243f6 |
138 | |
139 | =cut |
140 | |
dbaee748 |
141 | sub _dbh_all { |
142 | my ($storage, $dbh, $self) = @_; |
1346e22d |
143 | |
dbaee748 |
144 | $self->_check_dbh_gen; |
cb5f2eea |
145 | $self->{sth}->finish if $self->{sth}->{Active}; |
146 | delete $self->{sth}; |
dbaee748 |
147 | my ($rv, $sth) = $storage->_select(@{$self->{args}}); |
1a14aa3f |
148 | return @{$sth->fetchall_arrayref}; |
149 | } |
150 | |
dbaee748 |
151 | sub all { |
152 | my ($self) = @_; |
6296f45b |
153 | if ($self->{attrs}{software_limit} |
154 | && ($self->{attrs}{offset} || $self->{attrs}{rows})) { |
155 | return $self->SUPER::all; |
156 | } |
dbaee748 |
157 | $self->{storage}->dbh_do($self->can('_dbh_all'), $self); |
158 | } |
159 | |
5cf243f6 |
160 | =head2 reset |
161 | |
5cf243f6 |
162 | Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>. |
163 | |
164 | =cut |
165 | |
28927b50 |
166 | sub reset { |
167 | my ($self) = @_; |
1346e22d |
168 | |
dbaee748 |
169 | # No need to care about failures here |
170 | eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} }; |
1346e22d |
171 | $self->_soft_reset; |
172 | } |
173 | |
174 | sub _soft_reset { |
175 | my ($self) = @_; |
176 | |
cb5f2eea |
177 | delete $self->{sth}; |
cb5f2eea |
178 | delete $self->{done}; |
dbaee748 |
179 | $self->{pos} = 0; |
28927b50 |
180 | return $self; |
181 | } |
182 | |
dbaee748 |
183 | sub _check_dbh_gen { |
1346e22d |
184 | my ($self) = @_; |
185 | |
dbaee748 |
186 | if($self->{_dbh_gen} != $self->{storage}->{_dbh_gen}) { |
187 | $self->{_dbh_gen} = $self->{storage}->{_dbh_gen}; |
188 | $self->_soft_reset; |
1346e22d |
189 | } |
190 | } |
191 | |
28927b50 |
192 | sub DESTROY { |
193 | my ($self) = @_; |
1346e22d |
194 | |
dbaee748 |
195 | # None of the reasons this would die matter if we're in DESTROY anyways |
19fb8520 |
196 | local $@; |
dbaee748 |
197 | eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} }; |
28927b50 |
198 | } |
199 | |
200 | 1; |