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