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