Commit | Line | Data |
5cf243f6 |
1 | package DBIx::Class::Storage::DBI::Cursor; |
28927b50 |
2 | |
28927b50 |
3 | use strict; |
4 | use warnings; |
5 | |
48a76fcf |
6 | use base qw/DBIx::Class::Cursor/; |
a3a526cc |
7 | |
9780718f |
8 | use Try::Tiny; |
fd323bf1 |
9 | use namespace::clean; |
9780718f |
10 | |
a3a526cc |
11 | __PACKAGE__->mk_group_accessors('simple' => |
12 | qw/sth/ |
13 | ); |
2ad62d97 |
14 | |
5cf243f6 |
15 | =head1 NAME |
16 | |
17 | DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a |
18 | resultset. |
19 | |
20 | =head1 SYNOPSIS |
21 | |
22 | my $cursor = $schema->resultset('CD')->cursor(); |
23 | my $first_cd = $cursor->next; |
24 | |
25 | =head1 DESCRIPTION |
26 | |
27 | A Cursor represents a query cursor on a L<DBIx::Class::ResultSet> object. It |
28 | allows for traversing the result set with L</next>, retrieving all results with |
29 | L</all> and resetting the cursor with L</reset>. |
30 | |
31 | Usually, you would use the cursor methods built into L<DBIx::Class::ResultSet> |
32 | to traverse it. See L<DBIx::Class::ResultSet/next>, |
33 | L<DBIx::Class::ResultSet/reset> and L<DBIx::Class::ResultSet/all> for more |
34 | information. |
35 | |
36 | =head1 METHODS |
37 | |
38 | =head2 new |
39 | |
5cf243f6 |
40 | Returns a new L<DBIx::Class::Storage::DBI::Cursor> object. |
41 | |
42 | =cut |
43 | |
28927b50 |
44 | sub new { |
cb5f2eea |
45 | my ($class, $storage, $args, $attrs) = @_; |
cb5f2eea |
46 | $class = ref $class if ref $class; |
2007929b |
47 | |
28927b50 |
48 | my $new = { |
cb5f2eea |
49 | storage => $storage, |
28927b50 |
50 | args => $args, |
51 | pos => 0, |
1346e22d |
52 | attrs => $attrs, |
dbaee748 |
53 | _dbh_gen => $storage->{_dbh_gen}, |
1346e22d |
54 | }; |
55 | |
cb5f2eea |
56 | return bless ($new, $class); |
28927b50 |
57 | } |
58 | |
5cf243f6 |
59 | =head2 next |
60 | |
21b5c39d |
61 | =over 4 |
62 | |
ebc77b53 |
63 | =item Arguments: none |
21b5c39d |
64 | |
d601dc88 |
65 | =item Return Value: \@row_columns |
21b5c39d |
66 | |
5cf243f6 |
67 | =back |
68 | |
685dad64 |
69 | Advances the cursor to the next row and returns an array of column |
70 | values (the result of L<DBI/fetchrow_array> method). |
5cf243f6 |
71 | |
72 | =cut |
73 | |
dbaee748 |
74 | sub _dbh_next { |
75 | my ($storage, $dbh, $self) = @_; |
1346e22d |
76 | |
dbaee748 |
77 | $self->_check_dbh_gen; |
22ed9526 |
78 | if ( |
79 | $self->{attrs}{software_limit} |
80 | && $self->{attrs}{rows} |
81 | && $self->{pos} >= $self->{attrs}{rows} |
82 | ) { |
a3a526cc |
83 | $self->sth->finish if $self->sth->{Active}; |
84 | $self->sth(undef); |
cb5f2eea |
85 | $self->{done} = 1; |
86 | } |
87 | return if $self->{done}; |
a3a526cc |
88 | unless ($self->sth) { |
89 | $self->sth(($storage->_select(@{$self->{args}}))[1]); |
5c91499f |
90 | if ($self->{attrs}{software_limit}) { |
91 | if (my $offset = $self->{attrs}{offset}) { |
a3a526cc |
92 | $self->sth->fetch for 1 .. $offset; |
5c91499f |
93 | } |
94 | } |
28927b50 |
95 | } |
a3a526cc |
96 | my @row = $self->sth->fetchrow_array; |
cb5f2eea |
97 | if (@row) { |
98 | $self->{pos}++; |
99 | } else { |
a3a526cc |
100 | $self->sth(undef); |
cb5f2eea |
101 | $self->{done} = 1; |
102 | } |
28927b50 |
103 | return @row; |
104 | } |
105 | |
dbaee748 |
106 | sub next { |
107 | my ($self) = @_; |
108 | $self->{storage}->dbh_do($self->can('_dbh_next'), $self); |
109 | } |
110 | |
5cf243f6 |
111 | =head2 all |
112 | |
21b5c39d |
113 | =over 4 |
114 | |
ebc77b53 |
115 | =item Arguments: none |
21b5c39d |
116 | |
d601dc88 |
117 | =item Return Value: \@row_columns+ |
21b5c39d |
118 | |
5cf243f6 |
119 | =back |
120 | |
21b5c39d |
121 | Returns a list of arrayrefs of column values for all rows in the |
122 | L<DBIx::Class::ResultSet>. |
5cf243f6 |
123 | |
124 | =cut |
125 | |
dbaee748 |
126 | sub _dbh_all { |
127 | my ($storage, $dbh, $self) = @_; |
1346e22d |
128 | |
dbaee748 |
129 | $self->_check_dbh_gen; |
b4ad6d39 |
130 | $self->sth->finish if $self->sth && $self->sth->{Active}; |
a3a526cc |
131 | $self->sth(undef); |
dbaee748 |
132 | my ($rv, $sth) = $storage->_select(@{$self->{args}}); |
1a14aa3f |
133 | return @{$sth->fetchall_arrayref}; |
134 | } |
135 | |
dbaee748 |
136 | sub all { |
137 | my ($self) = @_; |
6296f45b |
138 | if ($self->{attrs}{software_limit} |
139 | && ($self->{attrs}{offset} || $self->{attrs}{rows})) { |
c3515436 |
140 | return $self->next::method; |
6296f45b |
141 | } |
22ed9526 |
142 | |
dbaee748 |
143 | $self->{storage}->dbh_do($self->can('_dbh_all'), $self); |
144 | } |
145 | |
5cf243f6 |
146 | =head2 reset |
147 | |
5cf243f6 |
148 | Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>. |
149 | |
150 | =cut |
151 | |
28927b50 |
152 | sub reset { |
153 | my ($self) = @_; |
1346e22d |
154 | |
dbaee748 |
155 | # No need to care about failures here |
52b420dd |
156 | try { $self->sth->finish } |
157 | if $self->sth && $self->sth->{Active}; |
1346e22d |
158 | $self->_soft_reset; |
b7c79955 |
159 | return undef; |
1346e22d |
160 | } |
161 | |
162 | sub _soft_reset { |
163 | my ($self) = @_; |
164 | |
a3a526cc |
165 | $self->sth(undef); |
cb5f2eea |
166 | delete $self->{done}; |
dbaee748 |
167 | $self->{pos} = 0; |
28927b50 |
168 | } |
169 | |
dbaee748 |
170 | sub _check_dbh_gen { |
1346e22d |
171 | my ($self) = @_; |
172 | |
dbaee748 |
173 | if($self->{_dbh_gen} != $self->{storage}->{_dbh_gen}) { |
174 | $self->{_dbh_gen} = $self->{storage}->{_dbh_gen}; |
175 | $self->_soft_reset; |
1346e22d |
176 | } |
177 | } |
178 | |
28927b50 |
179 | sub DESTROY { |
dbaee748 |
180 | # None of the reasons this would die matter if we're in DESTROY anyways |
05b22e33 |
181 | if (my $sth = $_[0]->sth) { |
a359de72 |
182 | try { $sth->finish } if $sth->{Active}; |
05b22e33 |
183 | } |
28927b50 |
184 | } |
185 | |
186 | 1; |