Lo, doth everything now use resultset_instance
[dbsrgits/DBIx-Class.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,
0a3c5b43 61 cols => $attrs->{cols},
89c0a5a2 62 cond => $attrs->{where},
0a3c5b43 63 from => $attrs->{from},
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
0a3c5b43 72=item search
73
6009260a 74 my @obj = $rs->search({ foo => 3 }); # "... WHERE foo = 3"
75 my $new_rs = $rs->search({ foo => 3 });
76
77If you need to pass in additional attributes but no additional condition,
78call it as ->search(undef, \%attrs);
79
80 my @all = $class->search({}, { cols => [qw/foo bar/] }); # "SELECT foo, bar FROM $class_table"
0a3c5b43 81
82=cut
83
84sub search {
85 my $self = shift;
86
6009260a 87 #use Data::Dumper;warn Dumper(@_);
88
0a3c5b43 89 my $attrs = { %{$self->{attrs}} };
90 if (@_ > 1 && ref $_[$#_] eq 'HASH') {
91 $attrs = { %{ pop(@_) } };
92 }
93
6009260a 94 my $where = ((@_ == 1 || ref $_[0] eq "HASH") ? shift : {@_});
0a3c5b43 95 if (defined $where) {
96 $where = (defined $attrs->{where}
97 ? { '-and' => [ $where, $attrs->{where} ] }
98 : $where);
99 $attrs->{where} = $where;
100 }
101
102 my $rs = $self->new($self->{source}, $attrs);
103
104 return (wantarray ? $rs->all : $rs);
105}
106
6009260a 107=item search_literal
108 my @obj = $rs->search_literal($literal_where_cond, @bind);
109 my $new_rs = $rs->search_literal($literal_where_cond, @bind);
110
111Pass a literal chunk of SQL to be added to the conditional part of the
112resultset
113
114=cut
115sub search_literal {
116 my ($self, $cond, @vals) = @_;
117 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
118 $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ];
119 return $self->search(\$cond, $attrs);
120}
0a3c5b43 121
ee38fa40 122=item cursor
123
38659261 124Return a storage driven cursor to the given resultset.
ee38fa40 125
126=cut
127
73f58123 128sub cursor {
129 my ($self) = @_;
2f5911b2 130 my ($source, $attrs) = @{$self}{qw/source attrs/};
3c5b25c5 131 if ($attrs->{page}) {
132 $attrs->{rows} = $self->pager->entries_per_page;
133 $attrs->{offset} = $self->pager->skipped;
134 }
73f58123 135 return $self->{cursor}
2f5911b2 136 ||= $source->storage->select($self->{from}, $self->{cols},
73f58123 137 $attrs->{where},$attrs);
138}
139
ee38fa40 140=item slice <first> <last>
141
38659261 142return a number of elements from the given resultset.
ee38fa40 143
144=cut
145
89c0a5a2 146sub slice {
147 my ($self, $min, $max) = @_;
148 my $attrs = { %{ $self->{attrs} || {} } };
2f5911b2 149 $self->{source}->throw("Can't slice without where") unless $attrs->{where};
89c0a5a2 150 $attrs->{offset} = $min;
151 $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
2f5911b2 152 my $slice = $self->new($self->{source}, $attrs);
89c0a5a2 153 return (wantarray ? $slice->all : $slice);
154}
155
ee38fa40 156=item next
157
38659261 158Returns the next element in this resultset.
ee38fa40 159
160=cut
161
89c0a5a2 162sub next {
163 my ($self) = @_;
73f58123 164 my @row = $self->cursor->next;
89c0a5a2 165 return unless (@row);
c7ce65e6 166 return $self->_construct_object(@row);
167}
168
169sub _construct_object {
170 my ($self, @row) = @_;
90f3f5ff 171 my @cols = @{ $self->{attrs}{cols} };
172 s/^me\.// for @cols;
3cbddeb1 173 @cols = grep { /\(/ or ! /\./ } @cols;
33ce49d6 174 my $new;
c7ce65e6 175 unless ($self->{attrs}{prefetch}) {
2f5911b2 176 $new = $self->{source}->_row_to_object(\@cols, \@row);
c7ce65e6 177 } else {
178 my @main = splice(@row, 0, scalar @cols);
2f5911b2 179 $new = $self->{source}->_row_to_object(\@cols, \@main);
c7ce65e6 180 PRE: foreach my $pre (@{$self->{attrs}{prefetch}}) {
2f5911b2 181 my $rel_obj = $self->{source}->_relationships->{$pre};
182 my $pre_class = $self->{source}->resolve_class($rel_obj->{class});
dd417d06 183 my @pre_cols = $pre_class->_select_columns;
c7ce65e6 184 my @vals = splice(@row, 0, scalar @pre_cols);
dd417d06 185 my $fetched = $pre_class->_row_to_object(\@pre_cols, \@vals);
2f5911b2 186 $self->{source}->throw("No accessor for prefetched $pre")
c7ce65e6 187 unless defined $rel_obj->{attrs}{accessor};
188 if ($rel_obj->{attrs}{accessor} eq 'single') {
189 foreach my $pri ($rel_obj->{class}->primary_columns) {
7cd300ea 190 unless (defined $fetched->get_column($pri)) {
191 undef $fetched;
192 last;
193 }
c7ce65e6 194 }
195 $new->{_relationship_data}{$pre} = $fetched;
196 } elsif ($rel_obj->{attrs}{accessor} eq 'filter') {
197 $new->{_inflated_column}{$pre} = $fetched;
198 } else {
2f5911b2 199 $self->{source}->throw("Don't know how to store prefetched $pre");
c7ce65e6 200 }
201 }
c7ce65e6 202 }
33ce49d6 203 $new = $self->{attrs}{record_filter}->($new)
204 if exists $self->{attrs}{record_filter};
205 return $new;
89c0a5a2 206}
207
ee38fa40 208=item count
209
210Performs an SQL count with the same query as the resultset was built
6009260a 211with to find the number of elements. If passed arguments, does a search
212on the resultset and counts the results of that.
ee38fa40 213
214=cut
215
89c0a5a2 216sub count {
6009260a 217 my $self = shift;
218 return $self->search(@_)->count if @_ && defined $_[0];
59f8e584 219 my $attrs = { %{ $self->{attrs} } };
3c5b25c5 220 unless ($self->{count}) {
221 # offset and order by are not needed to count
222 delete $attrs->{$_} for qw/offset order_by/;
223
224 my @cols = 'COUNT(*)';
2f5911b2 225 $self->{count} = $self->{source}->storage->select_single(
226 $self->{from}, \@cols, $self->{cond}, $attrs);
3c5b25c5 227 }
228 return 0 unless $self->{count};
9229f20a 229 return $self->{pager}->entries_on_this_page if ($self->{pager});
3c5b25c5 230 return ( $attrs->{rows} && $attrs->{rows} < $self->{count} )
59f8e584 231 ? $attrs->{rows}
3c5b25c5 232 : $self->{count};
89c0a5a2 233}
234
6009260a 235=item count_literal
236
237Calls search_literal with the passed arguments, then count
238
239=cut
240
241sub count_literal { shift->search_literal(@_)->count; }
242
ee38fa40 243=item all
244
38659261 245Returns all elements in the resultset. Is called implictly if the search
ee38fa40 246method is used in list context.
247
248=cut
249
89c0a5a2 250sub all {
251 my ($self) = @_;
c7ce65e6 252 return map { $self->_construct_object(@$_); }
73f58123 253 $self->cursor->all;
89c0a5a2 254}
255
ee38fa40 256=item reset
257
38659261 258Reset this resultset's cursor, so you can iterate through the elements again.
ee38fa40 259
260=cut
261
89c0a5a2 262sub reset {
263 my ($self) = @_;
73f58123 264 $self->cursor->reset;
89c0a5a2 265 return $self;
266}
267
ee38fa40 268=item first
269
38659261 270resets the resultset and returns the first element.
ee38fa40 271
272=cut
273
89c0a5a2 274sub first {
275 return $_[0]->reset->next;
276}
277
ee38fa40 278=item delete
279
38659261 280Deletes all elements in the resultset.
ee38fa40 281
282=cut
283
28927b50 284sub delete {
89c0a5a2 285 my ($self) = @_;
286 $_->delete for $self->all;
287 return 1;
288}
289
28927b50 290*delete_all = \&delete; # Yeah, yeah, yeah ...
291
ee38fa40 292=item pager
293
294Returns a L<Data::Page> object for the current resultset. Only makes
295sense for queries with page turned on.
296
297=cut
298
3c5b25c5 299sub pager {
300 my ($self) = @_;
301 my $attrs = $self->{attrs};
302 delete $attrs->{offset};
303 my $rows_per_page = delete $attrs->{rows} || 10;
304 $self->{pager} ||= Data::Page->new(
305 $self->count, $rows_per_page, $attrs->{page} || 1);
306 $attrs->{rows} = $rows_per_page;
307 return $self->{pager};
308}
309
ee38fa40 310=item page <page>
311
38659261 312Returns a new resultset representing a given page.
ee38fa40 313
314=cut
315
3c5b25c5 316sub page {
317 my ($self, $page) = @_;
318 my $attrs = $self->{attrs};
319 $attrs->{page} = $page;
2f5911b2 320 return $self->new($self->{source}, $attrs);
3c5b25c5 321}
322
ee38fa40 323=back
076652e8 324
325=head1 Attributes
326
38659261 327The resultset is responsible for handling the various attributes that
076652e8 328can be passed in with the search functions. Here's an overview of them:
329
330=over 4
331
332=item order_by
333
334Which column to order the results by.
ee38fa40 335
336=item cols
337
338Which cols should be retrieved on the first search.
339
340=item join
341
342Contains a list of relations that should be joined for this query. Can also
343contain a hash referece to refer to that relation's relations.
344
345=item from
346
347This attribute can contain a arrayref of elements. each element can be another
348arrayref, to nest joins, or it can be a hash which represents the two sides
349of the join.
350
351*NOTE* Use this on your own risk. This allows you to shoot your foot off!
352
076652e8 353=item page
354
355Should the resultset be paged? This can also be enabled by using the
356'page' option.
357
358=item rows
359
360For paged resultsset, how many rows per page
361
362=item offset
363
364For paged resultsset, which page to start on.
365
366=back
367
89c0a5a2 3681;