Commit | Line | Data |
89c0a5a2 |
1 | package DBIx::Class::ResultSet; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use overload |
6 | '0+' => 'count', |
7 | fallback => 1; |
3c5b25c5 |
8 | use Data::Page; |
89c0a5a2 |
9 | |
ee38fa40 |
10 | =head1 NAME |
11 | |
12 | DBIX::Class::Recordset - Responsible for fetching and creating recordsets. |
13 | |
14 | =head1 SYNOPSIS; |
15 | |
16 | $rs=MyApp::DB::Class->search(registered=>1); |
17 | |
18 | =head1 DESCRIPTION |
19 | |
20 | The recordset is also know as an iterator. |
21 | |
22 | =head1 METHODS |
23 | |
24 | =over 4 |
25 | |
26 | =item new <db_class> <attrs> |
27 | |
28 | The recordset constructor. Takes a db class and an |
29 | attribute hash (see below for more info on attributes) |
30 | |
31 | =cut |
32 | |
89c0a5a2 |
33 | sub new { |
7624b19f |
34 | my ($it_class, $db_class, $attrs) = @_; |
89c0a5a2 |
35 | #use Data::Dumper; warn Dumper(@_); |
36 | $it_class = ref $it_class if ref $it_class; |
37 | $attrs = { %{ $attrs || {} } }; |
c7ce65e6 |
38 | my %seen; |
39 | $attrs->{cols} ||= [ map { "me.$_" } $db_class->_select_columns ]; |
40 | $attrs->{from} ||= [ { 'me' => $db_class->_table_name } ]; |
fef5d100 |
41 | if ($attrs->{join}) { |
c7ce65e6 |
42 | foreach my $j (ref $attrs->{join} eq 'ARRAY' |
43 | ? (@{$attrs->{join}}) : ($attrs->{join})) { |
44 | if (ref $j eq 'HASH') { |
45 | $seen{$_} = 1 foreach keys %$j; |
46 | } else { |
47 | $seen{$j} = 1; |
48 | } |
49 | } |
50 | push(@{$attrs->{from}}, $db_class->_resolve_join($attrs->{join}, 'me')); |
51 | } |
52 | foreach my $pre (@{$attrs->{prefetch} || []}) { |
53 | push(@{$attrs->{from}}, $db_class->_resolve_join($pre, 'me')) |
54 | unless $seen{$pre}; |
55 | push(@{$attrs->{cols}}, |
56 | map { "$pre.$_" } |
ddab752c |
57 | $db_class->_relationships->{$pre}->{class}->_select_columns); |
fef5d100 |
58 | } |
89c0a5a2 |
59 | my $new = { |
60 | class => $db_class, |
fef5d100 |
61 | cols => $attrs->{cols} || [ $db_class->_select_columns ], |
89c0a5a2 |
62 | cond => $attrs->{where}, |
fef5d100 |
63 | from => $attrs->{from} || $db_class->_table_name, |
3c5b25c5 |
64 | count => undef, |
65 | pager => undef, |
89c0a5a2 |
66 | attrs => $attrs }; |
9229f20a |
67 | bless ($new, $it_class); |
68 | $new->pager if ($attrs->{page}); |
69 | return $new; |
89c0a5a2 |
70 | } |
71 | |
ee38fa40 |
72 | =item cursor |
73 | |
74 | Return a storage driven cursor to the given record set. |
75 | |
76 | =cut |
77 | |
73f58123 |
78 | sub cursor { |
79 | my ($self) = @_; |
80 | my ($db_class, $attrs) = @{$self}{qw/class attrs/}; |
3c5b25c5 |
81 | if ($attrs->{page}) { |
82 | $attrs->{rows} = $self->pager->entries_per_page; |
83 | $attrs->{offset} = $self->pager->skipped; |
84 | } |
73f58123 |
85 | return $self->{cursor} |
fef5d100 |
86 | ||= $db_class->storage->select($self->{from}, $self->{cols}, |
73f58123 |
87 | $attrs->{where},$attrs); |
88 | } |
89 | |
ee38fa40 |
90 | =item slice <first> <last> |
91 | |
92 | return a number of elements from the given record set. |
93 | |
94 | =cut |
95 | |
89c0a5a2 |
96 | sub slice { |
97 | my ($self, $min, $max) = @_; |
98 | my $attrs = { %{ $self->{attrs} || {} } }; |
99 | $self->{class}->throw("Can't slice without where") unless $attrs->{where}; |
100 | $attrs->{offset} = $min; |
101 | $attrs->{rows} = ($max ? ($max - $min + 1) : 1); |
7624b19f |
102 | my $slice = $self->new($self->{class}, $attrs); |
89c0a5a2 |
103 | return (wantarray ? $slice->all : $slice); |
104 | } |
105 | |
ee38fa40 |
106 | =item next |
107 | |
108 | Returns the next element in this record set. |
109 | |
110 | =cut |
111 | |
89c0a5a2 |
112 | sub next { |
113 | my ($self) = @_; |
73f58123 |
114 | my @row = $self->cursor->next; |
89c0a5a2 |
115 | return unless (@row); |
c7ce65e6 |
116 | return $self->_construct_object(@row); |
117 | } |
118 | |
119 | sub _construct_object { |
120 | my ($self, @row) = @_; |
121 | my @cols = $self->{class}->_select_columns; |
33ce49d6 |
122 | my $new; |
c7ce65e6 |
123 | unless ($self->{attrs}{prefetch}) { |
33ce49d6 |
124 | $new = $self->{class}->_row_to_object(\@cols, \@row); |
c7ce65e6 |
125 | } else { |
126 | my @main = splice(@row, 0, scalar @cols); |
33ce49d6 |
127 | $new = $self->{class}->_row_to_object(\@cols, \@main); |
c7ce65e6 |
128 | PRE: foreach my $pre (@{$self->{attrs}{prefetch}}) { |
129 | my $rel_obj = $self->{class}->_relationships->{$pre}; |
dd417d06 |
130 | my $pre_class = $self->{class}->resolve_class($rel_obj->{class}); |
131 | my @pre_cols = $pre_class->_select_columns; |
c7ce65e6 |
132 | my @vals = splice(@row, 0, scalar @pre_cols); |
dd417d06 |
133 | my $fetched = $pre_class->_row_to_object(\@pre_cols, \@vals); |
c7ce65e6 |
134 | $self->{class}->throw("No accessor for prefetched $pre") |
135 | unless defined $rel_obj->{attrs}{accessor}; |
136 | if ($rel_obj->{attrs}{accessor} eq 'single') { |
137 | foreach my $pri ($rel_obj->{class}->primary_columns) { |
138 | next PRE unless defined $fetched->get_column($pri); |
139 | } |
140 | $new->{_relationship_data}{$pre} = $fetched; |
141 | } elsif ($rel_obj->{attrs}{accessor} eq 'filter') { |
142 | $new->{_inflated_column}{$pre} = $fetched; |
143 | } else { |
3125eb1f |
144 | $self->{class}->throw("Don't know how to store prefetched $pre"); |
c7ce65e6 |
145 | } |
146 | } |
c7ce65e6 |
147 | } |
33ce49d6 |
148 | $new = $self->{attrs}{record_filter}->($new) |
149 | if exists $self->{attrs}{record_filter}; |
150 | return $new; |
89c0a5a2 |
151 | } |
152 | |
ee38fa40 |
153 | =item count |
154 | |
155 | Performs an SQL count with the same query as the resultset was built |
156 | with to find the number of elements. |
157 | |
158 | =cut |
159 | |
160 | |
89c0a5a2 |
161 | sub count { |
162 | my ($self) = @_; |
7624b19f |
163 | my $db_class = $self->{class}; |
59f8e584 |
164 | my $attrs = { %{ $self->{attrs} } }; |
3c5b25c5 |
165 | unless ($self->{count}) { |
166 | # offset and order by are not needed to count |
167 | delete $attrs->{$_} for qw/offset order_by/; |
168 | |
169 | my @cols = 'COUNT(*)'; |
fef5d100 |
170 | $self->{count} = $db_class->storage->select_single($self->{from}, \@cols, |
3c5b25c5 |
171 | $self->{cond}, $attrs); |
172 | } |
173 | return 0 unless $self->{count}; |
9229f20a |
174 | return $self->{pager}->entries_on_this_page if ($self->{pager}); |
3c5b25c5 |
175 | return ( $attrs->{rows} && $attrs->{rows} < $self->{count} ) |
59f8e584 |
176 | ? $attrs->{rows} |
3c5b25c5 |
177 | : $self->{count}; |
89c0a5a2 |
178 | } |
179 | |
ee38fa40 |
180 | =item all |
181 | |
182 | Returns all elements in the recordset. Is called implictly if the search |
183 | method is used in list context. |
184 | |
185 | =cut |
186 | |
89c0a5a2 |
187 | sub all { |
188 | my ($self) = @_; |
c7ce65e6 |
189 | return map { $self->_construct_object(@$_); } |
73f58123 |
190 | $self->cursor->all; |
89c0a5a2 |
191 | } |
192 | |
ee38fa40 |
193 | =item reset |
194 | |
195 | Reset this recordset's cursor, so you can iterate through the elements again. |
196 | |
197 | =cut |
198 | |
89c0a5a2 |
199 | sub reset { |
200 | my ($self) = @_; |
73f58123 |
201 | $self->cursor->reset; |
89c0a5a2 |
202 | return $self; |
203 | } |
204 | |
ee38fa40 |
205 | =item first |
206 | |
207 | resets the recordset and returns the first element. |
208 | |
209 | =cut |
210 | |
89c0a5a2 |
211 | sub first { |
212 | return $_[0]->reset->next; |
213 | } |
214 | |
ee38fa40 |
215 | =item delete |
216 | |
217 | Deletes all elements in the recordset. |
218 | |
219 | =cut |
220 | |
28927b50 |
221 | sub delete { |
89c0a5a2 |
222 | my ($self) = @_; |
223 | $_->delete for $self->all; |
224 | return 1; |
225 | } |
226 | |
28927b50 |
227 | *delete_all = \&delete; # Yeah, yeah, yeah ... |
228 | |
ee38fa40 |
229 | =item pager |
230 | |
231 | Returns a L<Data::Page> object for the current resultset. Only makes |
232 | sense for queries with page turned on. |
233 | |
234 | =cut |
235 | |
3c5b25c5 |
236 | sub pager { |
237 | my ($self) = @_; |
238 | my $attrs = $self->{attrs}; |
239 | delete $attrs->{offset}; |
240 | my $rows_per_page = delete $attrs->{rows} || 10; |
241 | $self->{pager} ||= Data::Page->new( |
242 | $self->count, $rows_per_page, $attrs->{page} || 1); |
243 | $attrs->{rows} = $rows_per_page; |
244 | return $self->{pager}; |
245 | } |
246 | |
ee38fa40 |
247 | =item page <page> |
248 | |
249 | Returns a new recordset representing a given page. |
250 | |
251 | =cut |
252 | |
3c5b25c5 |
253 | sub page { |
254 | my ($self, $page) = @_; |
255 | my $attrs = $self->{attrs}; |
256 | $attrs->{page} = $page; |
257 | return $self->new($self->{class}, $attrs); |
258 | } |
259 | |
ee38fa40 |
260 | =back |
076652e8 |
261 | |
262 | =head1 Attributes |
263 | |
264 | The recordset is responsible for handling the various attributes that |
265 | can be passed in with the search functions. Here's an overview of them: |
266 | |
267 | =over 4 |
268 | |
269 | =item order_by |
270 | |
271 | Which column to order the results by. |
ee38fa40 |
272 | |
273 | =item cols |
274 | |
275 | Which cols should be retrieved on the first search. |
276 | |
277 | =item join |
278 | |
279 | Contains a list of relations that should be joined for this query. Can also |
280 | contain a hash referece to refer to that relation's relations. |
281 | |
282 | =item from |
283 | |
284 | This attribute can contain a arrayref of elements. each element can be another |
285 | arrayref, to nest joins, or it can be a hash which represents the two sides |
286 | of the join. |
287 | |
288 | *NOTE* Use this on your own risk. This allows you to shoot your foot off! |
289 | |
076652e8 |
290 | =item page |
291 | |
292 | Should the resultset be paged? This can also be enabled by using the |
293 | 'page' option. |
294 | |
295 | =item rows |
296 | |
297 | For paged resultsset, how many rows per page |
298 | |
299 | =item offset |
300 | |
301 | For paged resultsset, which page to start on. |
302 | |
ee38fa40 |
303 | =item accesor |
304 | |
305 | Tells the recordset how to prefetch relations. Can either be 'single' or |
306 | 'filter'. |
307 | |
076652e8 |
308 | =back |
309 | |
89c0a5a2 |
310 | 1; |