minor fixup to _merge_attr
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSet.pm
1 package DBIx::Class::ResultSet;
2
3 use strict;
4 use warnings;
5 use overload
6         '0+'     => \&count,
7         'bool'   => sub { 1; },
8         fallback => 1;
9 use Carp::Clan qw/^DBIx::Class/;
10 use Data::Page;
11 use Storable;
12 use DBIx::Class::ResultSetColumn;
13 use base qw/DBIx::Class/;
14
15 __PACKAGE__->load_components(qw/AccessorGroup/);
16 __PACKAGE__->mk_group_accessors('simple' => qw/result_source result_class/);
17
18 =head1 NAME
19
20 DBIx::Class::ResultSet - Responsible for fetching and creating resultset.
21
22 =head1 SYNOPSIS
23
24   my $rs   = $schema->resultset('User')->search(registered => 1);
25   my @rows = $schema->resultset('CD')->search(year => 2005);
26
27 =head1 DESCRIPTION
28
29 The resultset is also known as an iterator. It is responsible for handling
30 queries that may return an arbitrary number of rows, e.g. via L</search>
31 or a C<has_many> relationship.
32
33 In the examples below, the following table classes are used:
34
35   package MyApp::Schema::Artist;
36   use base qw/DBIx::Class/;
37   __PACKAGE__->load_components(qw/Core/);
38   __PACKAGE__->table('artist');
39   __PACKAGE__->add_columns(qw/artistid name/);
40   __PACKAGE__->set_primary_key('artistid');
41   __PACKAGE__->has_many(cds => 'MyApp::Schema::CD');
42   1;
43
44   package MyApp::Schema::CD;
45   use base qw/DBIx::Class/;
46   __PACKAGE__->load_components(qw/Core/);
47   __PACKAGE__->table('cd');
48   __PACKAGE__->add_columns(qw/cdid artist title year/);
49   __PACKAGE__->set_primary_key('cdid');
50   __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Artist');
51   1;
52
53 =head1 METHODS
54
55 =head2 new
56
57 =over 4
58
59 =item Arguments: $source, \%$attrs
60
61 =item Return Value: $rs
62
63 =back
64
65 The resultset constructor. Takes a source object (usually a
66 L<DBIx::Class::ResultSourceProxy::Table>) and an attribute hash (see
67 L</ATTRIBUTES> below).  Does not perform any queries -- these are
68 executed as needed by the other methods.
69
70 Generally you won't need to construct a resultset manually.  You'll
71 automatically get one from e.g. a L</search> called in scalar context:
72
73   my $rs = $schema->resultset('CD')->search({ title => '100th Window' });
74
75 IMPORTANT: If called on an object, proxies to new_result instead so
76
77   my $cd = $schema->resultset('CD')->new({ title => 'Spoon' });
78
79 will return a CD object, not a ResultSet.
80
81 =cut
82
83 sub new {
84   my $class = shift;
85   return $class->new_result(@_) if ref $class;
86
87   my ($source, $attrs) = @_;
88   #weaken $source;
89
90   if ($attrs->{page}) {
91     $attrs->{rows} ||= 10;
92     $attrs->{offset} ||= 0;
93     $attrs->{offset} += ($attrs->{rows} * ($attrs->{page} - 1));
94   }
95
96   $attrs->{alias} ||= 'me';
97
98   bless {
99     result_source => $source,
100     result_class => $attrs->{result_class} || $source->result_class,
101     cond => $attrs->{where},
102     count => undef,
103     pager => undef,
104     attrs => $attrs
105   }, $class;
106 }
107
108 =head2 search
109
110 =over 4
111
112 =item Arguments: $cond, \%attrs?
113
114 =item Return Value: $resultset (scalar context), @row_objs (list context)
115
116 =back
117
118   my @cds    = $cd_rs->search({ year => 2001 }); # "... WHERE year = 2001"
119   my $new_rs = $cd_rs->search({ year => 2005 });
120
121   my $new_rs = $cd_rs->search([ { year => 2005 }, { year => 2004 } ]);
122                  # year = 2005 OR year = 2004
123
124 If you need to pass in additional attributes but no additional condition,
125 call it as C<search(undef, \%attrs)>.
126
127   # "SELECT name, artistid FROM $artist_table"
128   my @all_artists = $schema->resultset('Artist')->search(undef, {
129     columns => [qw/name artistid/],
130   });
131
132 For a list of attributes that can be passed to C<search>, see L</ATTRIBUTES>. For more examples of using this function, see L<Searching|DBIx::Class::Manual::Cookbook/Searching>.
133
134 =cut
135
136 sub search {
137   my $self = shift;
138   my $rs = $self->search_rs( @_ );
139   return (wantarray ? $rs->all : $rs);
140 }
141
142 =head2 search_rs
143
144 =over 4
145
146 =item Arguments: $cond, \%attrs?
147
148 =item Return Value: $resultset
149
150 =back
151
152 This method does the same exact thing as search() except it will
153 always return a resultset, even in list context.
154
155 =cut
156
157 sub search_rs {
158   my $self = shift;
159
160   my $rows;
161
162   unless (@_) {                 # no search, effectively just a clone
163     $rows = $self->get_cache;
164   }
165
166   my $attrs = {};
167   $attrs = pop(@_) if @_ > 1 and ref $_[$#_] eq 'HASH';
168   my $our_attrs = { %{$self->{attrs}} };
169   my $having = delete $our_attrs->{having};
170
171   # merge new attrs into inherited
172   foreach my $key (qw/join prefetch/) {
173     next unless exists $attrs->{$key};
174     $our_attrs->{$key} = $self->_merge_attr($our_attrs->{$key}, delete $attrs->{$key});
175   }
176   
177   my $new_attrs = { %{$our_attrs}, %{$attrs} };
178   my $where = (@_
179     ? (
180         (@_ == 1 || ref $_[0] eq "HASH")
181           ? shift
182           : (
183               (@_ % 2)
184                 ? $self->throw_exception("Odd number of arguments to search")
185                 : {@_}
186              )
187       )
188     : undef
189   );
190
191   if (defined $where) {
192     $new_attrs->{where} = (
193       defined $new_attrs->{where}
194         ? { '-and' => [
195               map {
196                 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
197               } $where, $new_attrs->{where}
198             ]
199           }
200         : $where);
201   }
202
203   if (defined $having) {
204     $new_attrs->{having} = (
205       defined $new_attrs->{having}
206         ? { '-and' => [
207               map {
208                 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
209               } $having, $new_attrs->{having}
210             ]
211           }
212         : $having);
213   }
214
215   my $rs = (ref $self)->new($self->result_source, $new_attrs);
216   if ($rows) {
217     $rs->set_cache($rows);
218   }
219   return $rs;
220 }
221
222 =head2 search_literal
223
224 =over 4
225
226 =item Arguments: $sql_fragment, @bind_values
227
228 =item Return Value: $resultset (scalar context), @row_objs (list context)
229
230 =back
231
232   my @cds   = $cd_rs->search_literal('year = ? AND title = ?', qw/2001 Reload/);
233   my $newrs = $artist_rs->search_literal('name = ?', 'Metallica');
234
235 Pass a literal chunk of SQL to be added to the conditional part of the
236 resultset query.
237
238 =cut
239
240 sub search_literal {
241   my ($self, $cond, @vals) = @_;
242   my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
243   $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ];
244   return $self->search(\$cond, $attrs);
245 }
246
247 =head2 find
248
249 =over 4
250
251 =item Arguments: @values | \%cols, \%attrs?
252
253 =item Return Value: $row_object
254
255 =back
256
257 Finds a row based on its primary key or unique constraint. For example, to find
258 a row by its primary key:
259
260   my $cd = $schema->resultset('CD')->find(5);
261
262 You can also find a row by a specific unique constraint using the C<key>
263 attribute. For example:
264
265   my $cd = $schema->resultset('CD')->find('Massive Attack', 'Mezzanine', {
266     key => 'cd_artist_title'
267   });
268
269 Additionally, you can specify the columns explicitly by name:
270
271   my $cd = $schema->resultset('CD')->find(
272     {
273       artist => 'Massive Attack',
274       title  => 'Mezzanine',
275     },
276     { key => 'cd_artist_title' }
277   );
278
279 If the C<key> is specified as C<primary>, it searches only on the primary key.
280
281 If no C<key> is specified, it searches on all unique constraints defined on the
282 source, including the primary key.
283
284 See also L</find_or_create> and L</update_or_create>. For information on how to
285 declare unique constraints, see
286 L<DBIx::Class::ResultSource/add_unique_constraint>.
287
288 =cut
289
290 sub find {
291   my $self = shift;
292   my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
293
294   # Default to the primary key, but allow a specific key
295   my @cols = exists $attrs->{key}
296     ? $self->result_source->unique_constraint_columns($attrs->{key})
297     : $self->result_source->primary_columns;
298   $self->throw_exception(
299     "Can't find unless a primary key or unique constraint is defined"
300   ) unless @cols;
301
302   # Parse out a hashref from input
303   my $input_query;
304   if (ref $_[0] eq 'HASH') {
305     $input_query = { %{$_[0]} };
306   }
307   elsif (@_ == @cols) {
308     $input_query = {};
309     @{$input_query}{@cols} = @_;
310   }
311   else {
312     # Compatibility: Allow e.g. find(id => $value)
313     carp "Find by key => value deprecated; please use a hashref instead";
314     $input_query = {@_};
315   }
316
317   my @unique_queries = $self->_unique_queries($input_query, $attrs);
318
319   # Handle cases where the ResultSet defines the query, or where the user is
320   # abusing find
321   my $query = @unique_queries ? \@unique_queries : $input_query;
322
323   # Run the query
324   if (keys %$attrs) {
325     my $rs = $self->search($query, $attrs);
326     return keys %{$rs->_resolved_attrs->{collapse}} ? $rs->next : $rs->single;
327   }
328   else {
329     return keys %{$self->_resolved_attrs->{collapse}}
330       ? $self->search($query)->next
331       : $self->single($query);
332   }
333 }
334
335 # _unique_queries
336 #
337 # Build a list of queries which satisfy unique constraints.
338
339 sub _unique_queries {
340   my ($self, $query, $attrs) = @_;
341
342   my $alias = $self->{attrs}{alias};
343   my @constraint_names = exists $attrs->{key}
344     ? ($attrs->{key})
345     : $self->result_source->unique_constraint_names;
346
347   my @unique_queries;
348   foreach my $name (@constraint_names) {
349     my @unique_cols = $self->result_source->unique_constraint_columns($name);
350     my $unique_query = $self->_build_unique_query($query, \@unique_cols);
351
352     my $num_query = scalar keys %$unique_query;
353     next unless $num_query;
354
355     # Add the ResultSet's alias
356     foreach my $col (grep { ! m/\./ } keys %$unique_query) {
357       $unique_query->{"$alias.$col"} = delete $unique_query->{$col};
358     }
359
360     # XXX: Assuming quite a bit about $self->{attrs}{where}
361     my $num_cols = scalar @unique_cols;
362     my $num_where = exists $self->{attrs}{where}
363       ? scalar keys %{ $self->{attrs}{where} }
364       : 0;
365     push @unique_queries, $unique_query
366       if $num_query + $num_where == $num_cols;
367   }
368
369   return @unique_queries;
370 }
371
372 # _build_unique_query
373 #
374 # Constrain the specified query hash based on the specified column names.
375
376 sub _build_unique_query {
377   my ($self, $query, $unique_cols) = @_;
378
379   return {
380     map  { $_ => $query->{$_} }
381     grep { exists $query->{$_} }
382       @$unique_cols
383   };
384 }
385
386 =head2 search_related
387
388 =over 4
389
390 =item Arguments: $rel, $cond, \%attrs?
391
392 =item Return Value: $new_resultset
393
394 =back
395
396   $new_rs = $cd_rs->search_related('artist', {
397     name => 'Emo-R-Us',
398   });
399
400 Searches the specified relationship, optionally specifying a condition and
401 attributes for matching records. See L</ATTRIBUTES> for more information.
402
403 =cut
404
405 sub search_related {
406   return shift->related_resultset(shift)->search(@_);
407 }
408
409 =head2 cursor
410
411 =over 4
412
413 =item Arguments: none
414
415 =item Return Value: $cursor
416
417 =back
418
419 Returns a storage-driven cursor to the given resultset. See
420 L<DBIx::Class::Cursor> for more information.
421
422 =cut
423
424 sub cursor {
425   my ($self) = @_;
426
427   my $attrs = { %{$self->_resolved_attrs} };
428   return $self->{cursor}
429     ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select},
430           $attrs->{where},$attrs);
431 }
432
433 =head2 single
434
435 =over 4
436
437 =item Arguments: $cond?
438
439 =item Return Value: $row_object?
440
441 =back
442
443   my $cd = $schema->resultset('CD')->single({ year => 2001 });
444
445 Inflates the first result without creating a cursor if the resultset has
446 any records in it; if not returns nothing. Used by L</find> as an optimisation.
447
448 Can optionally take an additional condition *only* - this is a fast-code-path
449 method; if you need to add extra joins or similar call ->search and then
450 ->single without a condition on the $rs returned from that.
451
452 =cut
453
454 sub single {
455   my ($self, $where) = @_;
456   my $attrs = { %{$self->_resolved_attrs} };
457   if ($where) {
458     if (defined $attrs->{where}) {
459       $attrs->{where} = {
460         '-and' =>
461             [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
462                $where, delete $attrs->{where} ]
463       };
464     } else {
465       $attrs->{where} = $where;
466     }
467   }
468
469 #  XXX: Disabled since it doesn't infer uniqueness in all cases
470 #  unless ($self->_is_unique_query($attrs->{where})) {
471 #    carp "Query not guaranteed to return a single row"
472 #      . "; please declare your unique constraints or use search instead";
473 #  }
474
475   my @data = $self->result_source->storage->select_single(
476     $attrs->{from}, $attrs->{select},
477     $attrs->{where}, $attrs
478   );
479
480   return (@data ? $self->_construct_object(@data) : ());
481 }
482
483 # _is_unique_query
484 #
485 # Try to determine if the specified query is guaranteed to be unique, based on
486 # the declared unique constraints.
487
488 sub _is_unique_query {
489   my ($self, $query) = @_;
490
491   my $collapsed = $self->_collapse_query($query);
492   my $alias = $self->{attrs}{alias};
493
494   foreach my $name ($self->result_source->unique_constraint_names) {
495     my @unique_cols = map {
496       "$alias.$_"
497     } $self->result_source->unique_constraint_columns($name);
498
499     # Count the values for each unique column
500     my %seen = map { $_ => 0 } @unique_cols;
501
502     foreach my $key (keys %$collapsed) {
503       my $aliased = $key =~ /\./ ? $key : "$alias.$key";
504       next unless exists $seen{$aliased};  # Additional constraints are okay
505       $seen{$aliased} = scalar keys %{ $collapsed->{$key} };
506     }
507
508     # If we get 0 or more than 1 value for a column, it's not necessarily unique
509     return 1 unless grep { $_ != 1 } values %seen;
510   }
511
512   return 0;
513 }
514
515 # _collapse_query
516 #
517 # Recursively collapse the query, accumulating values for each column.
518
519 sub _collapse_query {
520   my ($self, $query, $collapsed) = @_;
521
522   $collapsed ||= {};
523
524   if (ref $query eq 'ARRAY') {
525     foreach my $subquery (@$query) {
526       next unless ref $subquery;  # -or
527 #      warn "ARRAY: " . Dumper $subquery;
528       $collapsed = $self->_collapse_query($subquery, $collapsed);
529     }
530   }
531   elsif (ref $query eq 'HASH') {
532     if (keys %$query and (keys %$query)[0] eq '-and') {
533       foreach my $subquery (@{$query->{-and}}) {
534 #        warn "HASH: " . Dumper $subquery;
535         $collapsed = $self->_collapse_query($subquery, $collapsed);
536       }
537     }
538     else {
539 #      warn "LEAF: " . Dumper $query;
540       foreach my $col (keys %$query) {
541         my $value = $query->{$col};
542         $collapsed->{$col}{$value}++;
543       }
544     }
545   }
546
547   return $collapsed;
548 }
549
550 =head2 get_column
551
552 =over 4
553
554 =item Arguments: $cond?
555
556 =item Return Value: $resultsetcolumn
557
558 =back
559
560   my $max_length = $rs->get_column('length')->max;
561
562 Returns a ResultSetColumn instance for $column based on $self
563
564 =cut
565
566 sub get_column {
567   my ($self, $column) = @_;
568   my $new = DBIx::Class::ResultSetColumn->new($self, $column);
569   return $new;
570 }
571
572 =head2 search_like
573
574 =over 4
575
576 =item Arguments: $cond, \%attrs?
577
578 =item Return Value: $resultset (scalar context), @row_objs (list context)
579
580 =back
581
582   # WHERE title LIKE '%blue%'
583   $cd_rs = $rs->search_like({ title => '%blue%'});
584
585 Performs a search, but uses C<LIKE> instead of C<=> as the condition. Note
586 that this is simply a convenience method. You most likely want to use
587 L</search> with specific operators.
588
589 For more information, see L<DBIx::Class::Manual::Cookbook>.
590
591 =cut
592
593 sub search_like {
594   my $class = shift;
595   my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
596   my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_};
597   $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
598   return $class->search($query, { %$attrs });
599 }
600
601 =head2 slice
602
603 =over 4
604
605 =item Arguments: $first, $last
606
607 =item Return Value: $resultset (scalar context), @row_objs (list context)
608
609 =back
610
611 Returns a resultset or object list representing a subset of elements from the
612 resultset slice is called on. Indexes are from 0, i.e., to get the first
613 three records, call:
614
615   my ($one, $two, $three) = $rs->slice(0, 2);
616
617 =cut
618
619 sub slice {
620   my ($self, $min, $max) = @_;
621   my $attrs = {}; # = { %{ $self->{attrs} || {} } };
622   $attrs->{offset} = $self->{attrs}{offset} || 0;
623   $attrs->{offset} += $min;
624   $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
625   return $self->search(undef(), $attrs);
626   #my $slice = (ref $self)->new($self->result_source, $attrs);
627   #return (wantarray ? $slice->all : $slice);
628 }
629
630 =head2 next
631
632 =over 4
633
634 =item Arguments: none
635
636 =item Return Value: $result?
637
638 =back
639
640 Returns the next element in the resultset (C<undef> is there is none).
641
642 Can be used to efficiently iterate over records in the resultset:
643
644   my $rs = $schema->resultset('CD')->search;
645   while (my $cd = $rs->next) {
646     print $cd->title;
647   }
648
649 Note that you need to store the resultset object, and call C<next> on it.
650 Calling C<< resultset('Table')->next >> repeatedly will always return the
651 first record from the resultset.
652
653 =cut
654
655 sub next {
656   my ($self) = @_;
657   if (my $cache = $self->get_cache) {
658     $self->{all_cache_position} ||= 0;
659     return $cache->[$self->{all_cache_position}++];
660   }
661   if ($self->{attrs}{cache}) {
662     $self->{all_cache_position} = 1;
663     return ($self->all)[0];
664   }
665   my @row = (
666     exists $self->{stashed_row}
667       ? @{delete $self->{stashed_row}}
668       : $self->cursor->next
669   );
670   return unless (@row);
671   return $self->_construct_object(@row);
672 }
673
674 sub _construct_object {
675   my ($self, @row) = @_;
676   my $info = $self->_collapse_result($self->{_attrs}{as}, \@row);
677   my $new = $self->result_class->inflate_result($self->result_source, @$info);
678   $new = $self->{_attrs}{record_filter}->($new)
679     if exists $self->{_attrs}{record_filter};
680   return $new;
681 }
682
683 sub _collapse_result {
684   my ($self, $as, $row, $prefix) = @_;
685
686   my %const;
687   my @copy = @$row;
688   
689   foreach my $this_as (@$as) {
690     my $val = shift @copy;
691     if (defined $prefix) {
692       if ($this_as =~ m/^\Q${prefix}.\E(.+)$/) {
693         my $remain = $1;
694         $remain =~ /^(?:(.*)\.)?([^.]+)$/;
695         $const{$1||''}{$2} = $val;
696       }
697     } else {
698       $this_as =~ /^(?:(.*)\.)?([^.]+)$/;
699       $const{$1||''}{$2} = $val;
700     }
701   }
702
703   my $alias = $self->{attrs}{alias};
704   my $info = [ {}, {} ];
705   foreach my $key (keys %const) {
706     if (length $key && $key ne $alias) {
707       my $target = $info;
708       my @parts = split(/\./, $key);
709       foreach my $p (@parts) {
710         $target = $target->[1]->{$p} ||= [];
711       }
712       $target->[0] = $const{$key};
713     } else {
714       $info->[0] = $const{$key};
715     }
716   }
717   
718   my @collapse;
719   if (defined $prefix) {
720     @collapse = map {
721         m/^\Q${prefix}.\E(.+)$/ ? ($1) : ()
722     } keys %{$self->{_attrs}{collapse}}
723   } else {
724     @collapse = keys %{$self->{_attrs}{collapse}};
725   };
726
727   if (@collapse) {
728     my ($c) = sort { length $a <=> length $b } @collapse;
729     my $target = $info;
730     foreach my $p (split(/\./, $c)) {
731       $target = $target->[1]->{$p} ||= [];
732     }
733     my $c_prefix = (defined($prefix) ? "${prefix}.${c}" : $c);
734     my @co_key = @{$self->{_attrs}{collapse}{$c_prefix}};
735     my $tree = $self->_collapse_result($as, $row, $c_prefix);
736     my %co_check = map { ($_, $tree->[0]->{$_}); } @co_key;
737     my (@final, @raw);
738
739     while (
740       !(
741         grep {
742           !defined($tree->[0]->{$_}) || $co_check{$_} ne $tree->[0]->{$_}
743         } @co_key
744         )
745     ) {
746       push(@final, $tree);
747       last unless (@raw = $self->cursor->next);
748       $row = $self->{stashed_row} = \@raw;
749       $tree = $self->_collapse_result($as, $row, $c_prefix);
750     }
751     @$target = (@final ? @final : [ {}, {} ]);
752       # single empty result to indicate an empty prefetched has_many
753   }
754
755   #print "final info: " . Dumper($info);
756   return $info;
757 }
758
759 =head2 result_source
760
761 =over 4
762
763 =item Arguments: $result_source?
764
765 =item Return Value: $result_source
766
767 =back
768
769 An accessor for the primary ResultSource object from which this ResultSet
770 is derived.
771
772 =head2 result_class
773
774 =over 4
775
776 =item Arguments: $result_class?
777
778 =item Return Value: $result_class
779
780 =back
781
782 An accessor for the class to use when creating row objects. Defaults to 
783 C<< result_source->result_class >> - which in most cases is the name of the 
784 L<"table"|DBIx::Class::Manual::Glossary/"ResultSource"> class.
785
786 =cut
787
788
789 =head2 count
790
791 =over 4
792
793 =item Arguments: $cond, \%attrs??
794
795 =item Return Value: $count
796
797 =back
798
799 Performs an SQL C<COUNT> with the same query as the resultset was built
800 with to find the number of elements. If passed arguments, does a search
801 on the resultset and counts the results of that.
802
803 Note: When using C<count> with C<group_by>, L<DBIX::Class> emulates C<GROUP BY>
804 using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
805 not support C<DISTINCT> with multiple columns. If you are using such a
806 database, you should only use columns from the main table in your C<group_by>
807 clause.
808
809 =cut
810
811 sub count {
812   my $self = shift;
813   return $self->search(@_)->count if @_ and defined $_[0];
814   return scalar @{ $self->get_cache } if $self->get_cache;
815   my $count = $self->_count;
816   return 0 unless $count;
817
818   $count -= $self->{attrs}{offset} if $self->{attrs}{offset};
819   $count = $self->{attrs}{rows} if
820     $self->{attrs}{rows} and $self->{attrs}{rows} < $count;
821   return $count;
822 }
823
824 sub _count { # Separated out so pager can get the full count
825   my $self = shift;
826   my $select = { count => '*' };
827
828   my $attrs = { %{$self->_resolved_attrs} };
829   if (my $group_by = delete $attrs->{group_by}) {
830     delete $attrs->{having};
831     my @distinct = (ref $group_by ?  @$group_by : ($group_by));
832     # todo: try CONCAT for multi-column pk
833     my @pk = $self->result_source->primary_columns;
834     if (@pk == 1) {
835       my $alias = $attrs->{alias};
836       foreach my $column (@distinct) {
837         if ($column =~ qr/^(?:\Q${alias}.\E)?$pk[0]$/) {
838           @distinct = ($column);
839           last;
840         }
841       }
842     }
843
844     $select = { count => { distinct => \@distinct } };
845   }
846
847   $attrs->{select} = $select;
848   $attrs->{as} = [qw/count/];
849
850   # offset, order by and page are not needed to count. record_filter is cdbi
851   delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
852
853   my $tmp_rs = (ref $self)->new($self->result_source, $attrs);
854   my ($count) = $tmp_rs->cursor->next;
855   return $count;
856 }
857
858 =head2 count_literal
859
860 =over 4
861
862 =item Arguments: $sql_fragment, @bind_values
863
864 =item Return Value: $count
865
866 =back
867
868 Counts the results in a literal query. Equivalent to calling L</search_literal>
869 with the passed arguments, then L</count>.
870
871 =cut
872
873 sub count_literal { shift->search_literal(@_)->count; }
874
875 =head2 all
876
877 =over 4
878
879 =item Arguments: none
880
881 =item Return Value: @objects
882
883 =back
884
885 Returns all elements in the resultset. Called implicitly if the resultset
886 is returned in list context.
887
888 =cut
889
890 sub all {
891   my ($self) = @_;
892   return @{ $self->get_cache } if $self->get_cache;
893
894   my @obj;
895
896   # TODO: don't call resolve here
897   if (keys %{$self->_resolved_attrs->{collapse}}) {
898 #  if ($self->{attrs}{prefetch}) {
899       # Using $self->cursor->all is really just an optimisation.
900       # If we're collapsing has_many prefetches it probably makes
901       # very little difference, and this is cleaner than hacking
902       # _construct_object to survive the approach
903     my @row = $self->cursor->next;
904     while (@row) {
905       push(@obj, $self->_construct_object(@row));
906       @row = (exists $self->{stashed_row}
907                ? @{delete $self->{stashed_row}}
908                : $self->cursor->next);
909     }
910   } else {
911     @obj = map { $self->_construct_object(@$_) } $self->cursor->all;
912   }
913
914   $self->set_cache(\@obj) if $self->{attrs}{cache};
915   return @obj;
916 }
917
918 =head2 reset
919
920 =over 4
921
922 =item Arguments: none
923
924 =item Return Value: $self
925
926 =back
927
928 Resets the resultset's cursor, so you can iterate through the elements again.
929
930 =cut
931
932 sub reset {
933   my ($self) = @_;
934   delete $self->{_attrs} if exists $self->{_attrs};
935   $self->{all_cache_position} = 0;
936   $self->cursor->reset;
937   return $self;
938 }
939
940 =head2 first
941
942 =over 4
943
944 =item Arguments: none
945
946 =item Return Value: $object?
947
948 =back
949
950 Resets the resultset and returns an object for the first result (if the
951 resultset returns anything).
952
953 =cut
954
955 sub first {
956   return $_[0]->reset->next;
957 }
958
959 # _cond_for_update_delete
960 #
961 # update/delete require the condition to be modified to handle
962 # the differing SQL syntax available.  This transforms the $self->{cond}
963 # appropriately, returning the new condition.
964
965 sub _cond_for_update_delete {
966   my ($self) = @_;
967   my $cond = {};
968
969   # No-op. No condition, we're updating/deleting everything
970   return $cond unless ref $self->{cond};
971
972   if (ref $self->{cond} eq 'ARRAY') {
973     $cond = [
974       map {
975         my %hash;
976         foreach my $key (keys %{$_}) {
977           $key =~ /([^.]+)$/;
978           $hash{$1} = $_->{$key};
979         }
980         \%hash;
981       } @{$self->{cond}}
982     ];
983   }
984   elsif (ref $self->{cond} eq 'HASH') {
985     if ((keys %{$self->{cond}})[0] eq '-and') {
986       $cond->{-and} = [];
987
988       my @cond = @{$self->{cond}{-and}};
989       for (my $i = 0; $i < @cond; $i++) {
990         my $entry = $cond[$i];
991
992         my %hash;
993         if (ref $entry eq 'HASH') {
994           foreach my $key (keys %{$entry}) {
995             $key =~ /([^.]+)$/;
996             $hash{$1} = $entry->{$key};
997           }
998         }
999         else {
1000           $entry =~ /([^.]+)$/;
1001           $hash{$1} = $cond[++$i];
1002         }
1003
1004         push @{$cond->{-and}}, \%hash;
1005       }
1006     }
1007     else {
1008       foreach my $key (keys %{$self->{cond}}) {
1009         $key =~ /([^.]+)$/;
1010         $cond->{$1} = $self->{cond}{$key};
1011       }
1012     }
1013   }
1014   else {
1015     $self->throw_exception(
1016       "Can't update/delete on resultset with condition unless hash or array"
1017     );
1018   }
1019
1020   return $cond;
1021 }
1022
1023
1024 =head2 update
1025
1026 =over 4
1027
1028 =item Arguments: \%values
1029
1030 =item Return Value: $storage_rv
1031
1032 =back
1033
1034 Sets the specified columns in the resultset to the supplied values in a
1035 single query. Return value will be true if the update succeeded or false
1036 if no records were updated; exact type of success value is storage-dependent.
1037
1038 =cut
1039
1040 sub update {
1041   my ($self, $values) = @_;
1042   $self->throw_exception("Values for update must be a hash")
1043     unless ref $values eq 'HASH';
1044
1045   my $cond = $self->_cond_for_update_delete;
1046
1047   return $self->result_source->storage->update(
1048     $self->result_source->from, $values, $cond
1049   );
1050 }
1051
1052 =head2 update_all
1053
1054 =over 4
1055
1056 =item Arguments: \%values
1057
1058 =item Return Value: 1
1059
1060 =back
1061
1062 Fetches all objects and updates them one at a time. Note that C<update_all>
1063 will run DBIC cascade triggers, while L</update> will not.
1064
1065 =cut
1066
1067 sub update_all {
1068   my ($self, $values) = @_;
1069   $self->throw_exception("Values for update must be a hash")
1070     unless ref $values eq 'HASH';
1071   foreach my $obj ($self->all) {
1072     $obj->set_columns($values)->update;
1073   }
1074   return 1;
1075 }
1076
1077 =head2 delete
1078
1079 =over 4
1080
1081 =item Arguments: none
1082
1083 =item Return Value: 1
1084
1085 =back
1086
1087 Deletes the contents of the resultset from its result source. Note that this
1088 will not run DBIC cascade triggers. See L</delete_all> if you need triggers
1089 to run.
1090
1091 =cut
1092
1093 sub delete {
1094   my ($self) = @_;
1095
1096   my $cond = $self->_cond_for_update_delete;
1097
1098   $self->result_source->storage->delete($self->result_source->from, $cond);
1099   return 1;
1100 }
1101
1102 =head2 delete_all
1103
1104 =over 4
1105
1106 =item Arguments: none
1107
1108 =item Return Value: 1
1109
1110 =back
1111
1112 Fetches all objects and deletes them one at a time. Note that C<delete_all>
1113 will run DBIC cascade triggers, while L</delete> will not.
1114
1115 =cut
1116
1117 sub delete_all {
1118   my ($self) = @_;
1119   $_->delete for $self->all;
1120   return 1;
1121 }
1122
1123 =head2 pager
1124
1125 =over 4
1126
1127 =item Arguments: none
1128
1129 =item Return Value: $pager
1130
1131 =back
1132
1133 Return Value a L<Data::Page> object for the current resultset. Only makes
1134 sense for queries with a C<page> attribute.
1135
1136 =cut
1137
1138 sub pager {
1139   my ($self) = @_;
1140   my $attrs = $self->{attrs};
1141   $self->throw_exception("Can't create pager for non-paged rs")
1142     unless $self->{attrs}{page};
1143   $attrs->{rows} ||= 10;
1144   return $self->{pager} ||= Data::Page->new(
1145     $self->_count, $attrs->{rows}, $self->{attrs}{page});
1146 }
1147
1148 =head2 page
1149
1150 =over 4
1151
1152 =item Arguments: $page_number
1153
1154 =item Return Value: $rs
1155
1156 =back
1157
1158 Returns a resultset for the $page_number page of the resultset on which page
1159 is called, where each page contains a number of rows equal to the 'rows'
1160 attribute set on the resultset (10 by default).
1161
1162 =cut
1163
1164 sub page {
1165   my ($self, $page) = @_;
1166   return (ref $self)->new($self->result_source, { %{$self->{attrs}}, page => $page });
1167 }
1168
1169 =head2 new_result
1170
1171 =over 4
1172
1173 =item Arguments: \%vals
1174
1175 =item Return Value: $object
1176
1177 =back
1178
1179 Creates an object in the resultset's result class and returns it.
1180
1181 =cut
1182
1183 sub new_result {
1184   my ($self, $values) = @_;
1185   $self->throw_exception( "new_result needs a hash" )
1186     unless (ref $values eq 'HASH');
1187   $self->throw_exception(
1188     "Can't abstract implicit construct, condition not a hash"
1189   ) if ($self->{cond} && !(ref $self->{cond} eq 'HASH'));
1190   my %new = %$values;
1191   my $alias = $self->{attrs}{alias};
1192   foreach my $key (keys %{$self->{cond}||{}}) {
1193     $new{$1} = $self->{cond}{$key} if ($key =~ m/^(?:\Q${alias}.\E)?([^.]+)$/);
1194   }
1195   my $obj = $self->result_class->new(\%new);
1196   $obj->result_source($self->result_source) if $obj->can('result_source');
1197   return $obj;
1198 }
1199
1200 =head2 find_or_new
1201
1202 =over 4
1203
1204 =item Arguments: \%vals, \%attrs?
1205
1206 =item Return Value: $object
1207
1208 =back
1209
1210 Find an existing record from this resultset. If none exists, instantiate a new
1211 result object and return it. The object will not be saved into your storage
1212 until you call L<DBIx::Class::Row/insert> on it.
1213
1214 If you want objects to be saved immediately, use L</find_or_create> instead.
1215
1216 =cut
1217
1218 sub find_or_new {
1219   my $self     = shift;
1220   my $attrs    = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1221   my $hash     = ref $_[0] eq 'HASH' ? shift : {@_};
1222   my $exists   = $self->find($hash, $attrs);
1223   return defined $exists ? $exists : $self->new_result($hash);
1224 }
1225
1226 =head2 create
1227
1228 =over 4
1229
1230 =item Arguments: \%vals
1231
1232 =item Return Value: $object
1233
1234 =back
1235
1236 Inserts a record into the resultset and returns the object representing it.
1237
1238 Effectively a shortcut for C<< ->new_result(\%vals)->insert >>.
1239
1240 =cut
1241
1242 sub create {
1243   my ($self, $attrs) = @_;
1244   $self->throw_exception( "create needs a hashref" )
1245     unless ref $attrs eq 'HASH';
1246   return $self->new_result($attrs)->insert;
1247 }
1248
1249 =head2 find_or_create
1250
1251 =over 4
1252
1253 =item Arguments: \%vals, \%attrs?
1254
1255 =item Return Value: $object
1256
1257 =back
1258
1259   $class->find_or_create({ key => $val, ... });
1260
1261 Tries to find a record based on its primary key or unique constraint; if none
1262 is found, creates one and returns that instead.
1263
1264   my $cd = $schema->resultset('CD')->find_or_create({
1265     cdid   => 5,
1266     artist => 'Massive Attack',
1267     title  => 'Mezzanine',
1268     year   => 2005,
1269   });
1270
1271 Also takes an optional C<key> attribute, to search by a specific key or unique
1272 constraint. For example:
1273
1274   my $cd = $schema->resultset('CD')->find_or_create(
1275     {
1276       artist => 'Massive Attack',
1277       title  => 'Mezzanine',
1278     },
1279     { key => 'cd_artist_title' }
1280   );
1281
1282 See also L</find> and L</update_or_create>. For information on how to declare
1283 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1284
1285 =cut
1286
1287 sub find_or_create {
1288   my $self     = shift;
1289   my $attrs    = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1290   my $hash     = ref $_[0] eq 'HASH' ? shift : {@_};
1291   my $exists   = $self->find($hash, $attrs);
1292   return defined $exists ? $exists : $self->create($hash);
1293 }
1294
1295 =head2 update_or_create
1296
1297 =over 4
1298
1299 =item Arguments: \%col_values, { key => $unique_constraint }?
1300
1301 =item Return Value: $object
1302
1303 =back
1304
1305   $class->update_or_create({ col => $val, ... });
1306
1307 First, searches for an existing row matching one of the unique constraints
1308 (including the primary key) on the source of this resultset. If a row is
1309 found, updates it with the other given column values. Otherwise, creates a new
1310 row.
1311
1312 Takes an optional C<key> attribute to search on a specific unique constraint.
1313 For example:
1314
1315   # In your application
1316   my $cd = $schema->resultset('CD')->update_or_create(
1317     {
1318       artist => 'Massive Attack',
1319       title  => 'Mezzanine',
1320       year   => 1998,
1321     },
1322     { key => 'cd_artist_title' }
1323   );
1324
1325 If no C<key> is specified, it searches on all unique constraints defined on the
1326 source, including the primary key.
1327
1328 If the C<key> is specified as C<primary>, it searches only on the primary key.
1329
1330 See also L</find> and L</find_or_create>. For information on how to declare
1331 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1332
1333 =cut
1334
1335 sub update_or_create {
1336   my $self = shift;
1337   my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1338   my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
1339
1340   my $row = $self->find($cond);
1341   if (defined $row) {
1342     $row->update($cond);
1343     return $row;
1344   }
1345
1346   return $self->create($cond);
1347 }
1348
1349 =head2 get_cache
1350
1351 =over 4
1352
1353 =item Arguments: none
1354
1355 =item Return Value: \@cache_objects?
1356
1357 =back
1358
1359 Gets the contents of the cache for the resultset, if the cache is set.
1360
1361 =cut
1362
1363 sub get_cache {
1364   shift->{all_cache};
1365 }
1366
1367 =head2 set_cache
1368
1369 =over 4
1370
1371 =item Arguments: \@cache_objects
1372
1373 =item Return Value: \@cache_objects
1374
1375 =back
1376
1377 Sets the contents of the cache for the resultset. Expects an arrayref
1378 of objects of the same class as those produced by the resultset. Note that
1379 if the cache is set the resultset will return the cached objects rather
1380 than re-querying the database even if the cache attr is not set.
1381
1382 =cut
1383
1384 sub set_cache {
1385   my ( $self, $data ) = @_;
1386   $self->throw_exception("set_cache requires an arrayref")
1387       if defined($data) && (ref $data ne 'ARRAY');
1388   $self->{all_cache} = $data;
1389 }
1390
1391 =head2 clear_cache
1392
1393 =over 4
1394
1395 =item Arguments: none
1396
1397 =item Return Value: []
1398
1399 =back
1400
1401 Clears the cache for the resultset.
1402
1403 =cut
1404
1405 sub clear_cache {
1406   shift->set_cache(undef);
1407 }
1408
1409 =head2 related_resultset
1410
1411 =over 4
1412
1413 =item Arguments: $relationship_name
1414
1415 =item Return Value: $resultset
1416
1417 =back
1418
1419 Returns a related resultset for the supplied relationship name.
1420
1421   $artist_rs = $schema->resultset('CD')->related_resultset('Artist');
1422
1423 =cut
1424
1425 sub related_resultset {
1426   my ($self, $rel) = @_;
1427
1428   $self->{related_resultsets} ||= {};
1429   return $self->{related_resultsets}{$rel} ||= do {
1430     my $rel_obj = $self->result_source->relationship_info($rel);
1431
1432     $self->throw_exception(
1433       "search_related: result source '" . $self->result_source->name .
1434         "' has no such relationship $rel")
1435       unless $rel_obj;
1436     
1437     my ($from,$seen) = $self->_resolve_from($rel);
1438
1439     my $join_count = $seen->{$rel};
1440     my $alias = ($join_count > 1 ? join('_', $rel, $join_count) : $rel);
1441
1442     $self->result_source->schema->resultset($rel_obj->{class})->search_rs(
1443       undef, {
1444         %{$self->{attrs}||{}},
1445         join => undef,
1446         prefetch => undef,
1447         select => undef,
1448         as => undef,
1449         alias => $alias,
1450         where => $self->{cond},
1451         seen_join => $seen,
1452         from => $from,
1453     });
1454   };
1455 }
1456
1457 sub _resolve_from {
1458   my ($self, $extra_join) = @_;
1459   my $source = $self->result_source;
1460   my $attrs = $self->{attrs};
1461   
1462   my $from = $attrs->{from}
1463     || [ { $attrs->{alias} => $source->from } ];
1464     
1465   my $seen = { %{$attrs->{seen_join}||{}} };
1466
1467   my $join = ($attrs->{join}
1468                ? [ $attrs->{join}, $extra_join ]
1469                : $extra_join);
1470   $from = [
1471     @$from,
1472     ($join ? $source->resolve_join($join, $attrs->{alias}, $seen) : ()),
1473   ];
1474
1475   return ($from,$seen);
1476 }
1477
1478 sub _resolved_attrs {
1479   my $self = shift;
1480   return $self->{_attrs} if $self->{_attrs};
1481
1482   my $attrs = { %{$self->{attrs}||{}} };
1483   my $source = $self->{result_source};
1484   my $alias = $attrs->{alias};
1485
1486   $attrs->{columns} ||= delete $attrs->{cols} if exists $attrs->{cols};
1487   if ($attrs->{columns}) {
1488     delete $attrs->{as};
1489   } elsif (!$attrs->{select}) {
1490     $attrs->{columns} = [ $source->columns ];
1491   }
1492  
1493   $attrs->{select} = 
1494     ($attrs->{select}
1495       ? (ref $attrs->{select} eq 'ARRAY'
1496           ? [ @{$attrs->{select}} ]
1497           : [ $attrs->{select} ])
1498       : [ map { m/\./ ? $_ : "${alias}.$_" } @{delete $attrs->{columns}} ]
1499     );
1500   $attrs->{as} =
1501     ($attrs->{as}
1502       ? (ref $attrs->{as} eq 'ARRAY'
1503           ? [ @{$attrs->{as}} ]
1504           : [ $attrs->{as} ])
1505       : [ map { m/^\Q${alias}.\E(.+)$/ ? $1 : $_ } @{$attrs->{select}} ]
1506     );
1507   
1508   my $adds;
1509   if ($adds = delete $attrs->{include_columns}) {
1510     $adds = [$adds] unless ref $adds eq 'ARRAY';
1511     push(@{$attrs->{select}}, @$adds);
1512     push(@{$attrs->{as}}, map { m/([^.]+)$/; $1 } @$adds);
1513   }
1514   if ($adds = delete $attrs->{'+select'}) {
1515     $adds = [$adds] unless ref $adds eq 'ARRAY';
1516     push(@{$attrs->{select}},
1517            map { /\./ || ref $_ ? $_ : "${alias}.$_" } @$adds);
1518   }
1519   if (my $adds = delete $attrs->{'+as'}) {
1520     $adds = [$adds] unless ref $adds eq 'ARRAY';
1521     push(@{$attrs->{as}}, @$adds);
1522   }
1523
1524   $attrs->{from} ||= [ { 'me' => $source->from } ];
1525
1526   if (exists $attrs->{join} || exists $attrs->{prefetch}) {
1527     my $join = delete $attrs->{join} || {};
1528
1529     if (defined $attrs->{prefetch}) {
1530       $join = $self->_merge_attr(
1531         $join, $attrs->{prefetch}
1532       );
1533     }
1534
1535     $attrs->{from} =   # have to copy here to avoid corrupting the original
1536       [
1537         @{$attrs->{from}}, 
1538         $source->resolve_join($join, $alias, { %{$attrs->{seen_join}||{}} })
1539       ];
1540   }
1541
1542   $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct};
1543   if ($attrs->{order_by}) {
1544     $attrs->{order_by} = (ref($attrs->{order_by}) eq 'ARRAY'
1545                            ? [ @{$attrs->{order_by}} ]
1546                            : [ $attrs->{order_by} ]);
1547   } else {
1548     $attrs->{order_by} = [];    
1549   }
1550
1551   my $collapse = $attrs->{collapse} || {};
1552   if (my $prefetch = delete $attrs->{prefetch}) {
1553     my @pre_order;
1554     foreach my $p (ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch)) {
1555       # bring joins back to level of current class
1556       my @prefetch = $source->resolve_prefetch(
1557         $p, $alias, { %{$attrs->{seen_join}||{}} }, \@pre_order, $collapse
1558       );
1559       push(@{$attrs->{select}}, map { $_->[0] } @prefetch);
1560       push(@{$attrs->{as}}, map { $_->[1] } @prefetch);
1561     }
1562     push(@{$attrs->{order_by}}, @pre_order);
1563   }
1564   $attrs->{collapse} = $collapse;
1565
1566   return $self->{_attrs} = $attrs;
1567 }
1568
1569 sub _merge_attr {
1570   my ($self, $a, $b) = @_;
1571   return $b unless defined($a);
1572   return $a unless defined($b);
1573   
1574   if (ref $b eq 'HASH' && ref $a eq 'HASH') {
1575     foreach my $key (keys %{$b}) {
1576       if (exists $a->{$key}) {
1577         $a->{$key} = $self->_merge_attr($a->{$key}, $b->{$key});
1578       } else {
1579         $a->{$key} = $b->{$key};
1580       }
1581     }
1582     return $a;
1583   } else {
1584     $a = [$a] unless ref $a eq 'ARRAY';
1585     $b = [$b] unless ref $b eq 'ARRAY';
1586
1587     my $hash = {};
1588     my @array;
1589     foreach my $x ($a, $b) {
1590       foreach my $element (@{$x}) {
1591         if (ref $element eq 'HASH') {
1592           $hash = $self->_merge_attr($hash, $element);
1593         } elsif (ref $element eq 'ARRAY') {
1594           push(@array, @{$element});
1595         } else {
1596           push(@array, $element) unless $b == $x
1597             && grep { $_ eq $element } @array;
1598         }
1599       }
1600     }
1601     
1602     @array = grep { !exists $hash->{$_} } @array;
1603
1604     return keys %{$hash}
1605       ? ( scalar(@array)
1606             ? [$hash, @array]
1607             : $hash
1608         )
1609       : \@array;
1610   }
1611 }
1612
1613 =head2 throw_exception
1614
1615 See L<DBIx::Class::Schema/throw_exception> for details.
1616
1617 =cut
1618
1619 sub throw_exception {
1620   my $self=shift;
1621   $self->result_source->schema->throw_exception(@_);
1622 }
1623
1624 # XXX: FIXME: Attributes docs need clearing up
1625
1626 =head1 ATTRIBUTES
1627
1628 The resultset takes various attributes that modify its behavior. Here's an
1629 overview of them:
1630
1631 =head2 order_by
1632
1633 =over 4
1634
1635 =item Value: ($order_by | \@order_by)
1636
1637 =back
1638
1639 Which column(s) to order the results by. This is currently passed
1640 through directly to SQL, so you can give e.g. C<year DESC> for a
1641 descending order on the column `year'.
1642
1643 Please note that if you have quoting enabled (see
1644 L<DBIx::Class::Storage/quote_char>) you will need to do C<\'year DESC' > to
1645 specify an order. (The scalar ref causes it to be passed as raw sql to the DB,
1646 so you will need to manually quote things as appropriate.)
1647
1648 =head2 columns
1649
1650 =over 4
1651
1652 =item Value: \@columns
1653
1654 =back
1655
1656 Shortcut to request a particular set of columns to be retrieved.  Adds
1657 C<me.> onto the start of any column without a C<.> in it and sets C<select>
1658 from that, then auto-populates C<as> from C<select> as normal. (You may also
1659 use the C<cols> attribute, as in earlier versions of DBIC.)
1660
1661 =head2 include_columns
1662
1663 =over 4
1664
1665 =item Value: \@columns
1666
1667 =back
1668
1669 Shortcut to include additional columns in the returned results - for example
1670
1671   $schema->resultset('CD')->search(undef, {
1672     include_columns => ['artist.name'],
1673     join => ['artist']
1674   });
1675
1676 would return all CDs and include a 'name' column to the information
1677 passed to object inflation
1678
1679 =head2 select
1680
1681 =over 4
1682
1683 =item Value: \@select_columns
1684
1685 =back
1686
1687 Indicates which columns should be selected from the storage. You can use
1688 column names, or in the case of RDBMS back ends, function or stored procedure
1689 names:
1690
1691   $rs = $schema->resultset('Employee')->search(undef, {
1692     select => [
1693       'name',
1694       { count => 'employeeid' },
1695       { sum => 'salary' }
1696     ]
1697   });
1698
1699 When you use function/stored procedure names and do not supply an C<as>
1700 attribute, the column names returned are storage-dependent. E.g. MySQL would
1701 return a column named C<count(employeeid)> in the above example.
1702
1703 =head2 +select
1704
1705 =over 4
1706
1707 Indicates additional columns to be selected from storage.  Works the same as
1708 L<select> but adds columns to the selection.
1709
1710 =back
1711
1712 =head2 +as
1713
1714 =over 4
1715
1716 Indicates additional column names for those added via L<+select>.
1717
1718 =back
1719
1720 =head2 as
1721
1722 =over 4
1723
1724 =item Value: \@inflation_names
1725
1726 =back
1727
1728 Indicates column names for object inflation. This is used in conjunction with
1729 C<select>, usually when C<select> contains one or more function or stored
1730 procedure names:
1731
1732   $rs = $schema->resultset('Employee')->search(undef, {
1733     select => [
1734       'name',
1735       { count => 'employeeid' }
1736     ],
1737     as => ['name', 'employee_count'],
1738   });
1739
1740   my $employee = $rs->first(); # get the first Employee
1741
1742 If the object against which the search is performed already has an accessor
1743 matching a column name specified in C<as>, the value can be retrieved using
1744 the accessor as normal:
1745
1746   my $name = $employee->name();
1747
1748 If on the other hand an accessor does not exist in the object, you need to
1749 use C<get_column> instead:
1750
1751   my $employee_count = $employee->get_column('employee_count');
1752
1753 You can create your own accessors if required - see
1754 L<DBIx::Class::Manual::Cookbook> for details.
1755
1756 Please note: This will NOT insert an C<AS employee_count> into the SQL
1757 statement produced, it is used for internal access only. Thus
1758 attempting to use the accessor in an C<order_by> clause or similar
1759 will fail miserably.
1760
1761 To get around this limitation, you can supply literal SQL to your
1762 C<select> attibute that contains the C<AS alias> text, eg:
1763
1764   select => [\'myfield AS alias']
1765
1766 =head2 join
1767
1768 =over 4
1769
1770 =item Value: ($rel_name | \@rel_names | \%rel_names)
1771
1772 =back
1773
1774 Contains a list of relationships that should be joined for this query.  For
1775 example:
1776
1777   # Get CDs by Nine Inch Nails
1778   my $rs = $schema->resultset('CD')->search(
1779     { 'artist.name' => 'Nine Inch Nails' },
1780     { join => 'artist' }
1781   );
1782
1783 Can also contain a hash reference to refer to the other relation's relations.
1784 For example:
1785
1786   package MyApp::Schema::Track;
1787   use base qw/DBIx::Class/;
1788   __PACKAGE__->table('track');
1789   __PACKAGE__->add_columns(qw/trackid cd position title/);
1790   __PACKAGE__->set_primary_key('trackid');
1791   __PACKAGE__->belongs_to(cd => 'MyApp::Schema::CD');
1792   1;
1793
1794   # In your application
1795   my $rs = $schema->resultset('Artist')->search(
1796     { 'track.title' => 'Teardrop' },
1797     {
1798       join     => { cd => 'track' },
1799       order_by => 'artist.name',
1800     }
1801   );
1802
1803 If the same join is supplied twice, it will be aliased to <rel>_2 (and
1804 similarly for a third time). For e.g.
1805
1806   my $rs = $schema->resultset('Artist')->search({
1807     'cds.title'   => 'Down to Earth',
1808     'cds_2.title' => 'Popular',
1809   }, {
1810     join => [ qw/cds cds/ ],
1811   });
1812
1813 will return a set of all artists that have both a cd with title 'Down
1814 to Earth' and a cd with title 'Popular'.
1815
1816 If you want to fetch related objects from other tables as well, see C<prefetch>
1817 below.
1818
1819 =head2 prefetch
1820
1821 =over 4
1822
1823 =item Value: ($rel_name | \@rel_names | \%rel_names)
1824
1825 =back
1826
1827 Contains one or more relationships that should be fetched along with the main
1828 query (when they are accessed afterwards they will have already been
1829 "prefetched").  This is useful for when you know you will need the related
1830 objects, because it saves at least one query:
1831
1832   my $rs = $schema->resultset('Tag')->search(
1833     undef,
1834     {
1835       prefetch => {
1836         cd => 'artist'
1837       }
1838     }
1839   );
1840
1841 The initial search results in SQL like the following:
1842
1843   SELECT tag.*, cd.*, artist.* FROM tag
1844   JOIN cd ON tag.cd = cd.cdid
1845   JOIN artist ON cd.artist = artist.artistid
1846
1847 L<DBIx::Class> has no need to go back to the database when we access the
1848 C<cd> or C<artist> relationships, which saves us two SQL statements in this
1849 case.
1850
1851 Simple prefetches will be joined automatically, so there is no need
1852 for a C<join> attribute in the above search. If you're prefetching to
1853 depth (e.g. { cd => { artist => 'label' } or similar), you'll need to
1854 specify the join as well.
1855
1856 C<prefetch> can be used with the following relationship types: C<belongs_to>,
1857 C<has_one> (or if you're using C<add_relationship>, any relationship declared
1858 with an accessor type of 'single' or 'filter').
1859
1860 =head2 page
1861
1862 =over 4
1863
1864 =item Value: $page
1865
1866 =back
1867
1868 Makes the resultset paged and specifies the page to retrieve. Effectively
1869 identical to creating a non-pages resultset and then calling ->page($page)
1870 on it.
1871
1872 If L<rows> attribute is not specified it defualts to 10 rows per page.
1873
1874 =head2 rows
1875
1876 =over 4
1877
1878 =item Value: $rows
1879
1880 =back
1881
1882 Specifes the maximum number of rows for direct retrieval or the number of
1883 rows per page if the page attribute or method is used.
1884
1885 =head2 offset
1886
1887 =over 4
1888
1889 =item Value: $offset
1890
1891 =back
1892
1893 Specifies the (zero-based) row number for the  first row to be returned, or the
1894 of the first row of the first page if paging is used.
1895
1896 =head2 group_by
1897
1898 =over 4
1899
1900 =item Value: \@columns
1901
1902 =back
1903
1904 A arrayref of columns to group by. Can include columns of joined tables.
1905
1906   group_by => [qw/ column1 column2 ... /]
1907
1908 =head2 having
1909
1910 =over 4
1911
1912 =item Value: $condition
1913
1914 =back
1915
1916 HAVING is a select statement attribute that is applied between GROUP BY and
1917 ORDER BY. It is applied to the after the grouping calculations have been
1918 done.
1919
1920   having => { 'count(employee)' => { '>=', 100 } }
1921
1922 =head2 distinct
1923
1924 =over 4
1925
1926 =item Value: (0 | 1)
1927
1928 =back
1929
1930 Set to 1 to group by all columns.
1931
1932 =head2 where
1933
1934 =over 4
1935
1936 Adds to the WHERE clause.
1937
1938   # only return rows WHERE deleted IS NULL for all searches
1939   __PACKAGE__->resultset_attributes({ where => { deleted => undef } }); )
1940
1941 Can be overridden by passing C<{ where => undef }> as an attribute
1942 to a resulset.
1943
1944 =back
1945
1946 =head2 cache
1947
1948 Set to 1 to cache search results. This prevents extra SQL queries if you
1949 revisit rows in your ResultSet:
1950
1951   my $resultset = $schema->resultset('Artist')->search( undef, { cache => 1 } );
1952
1953   while( my $artist = $resultset->next ) {
1954     ... do stuff ...
1955   }
1956
1957   $rs->first; # without cache, this would issue a query
1958
1959 By default, searches are not cached.
1960
1961 For more examples of using these attributes, see
1962 L<DBIx::Class::Manual::Cookbook>.
1963
1964 =head2 from
1965
1966 =over 4
1967
1968 =item Value: \@from_clause
1969
1970 =back
1971
1972 The C<from> attribute gives you manual control over the C<FROM> clause of SQL
1973 statements generated by L<DBIx::Class>, allowing you to express custom C<JOIN>
1974 clauses.
1975
1976 NOTE: Use this on your own risk.  This allows you to shoot off your foot!
1977
1978 C<join> will usually do what you need and it is strongly recommended that you
1979 avoid using C<from> unless you cannot achieve the desired result using C<join>.
1980 And we really do mean "cannot", not just tried and failed. Attempting to use
1981 this because you're having problems with C<join> is like trying to use x86
1982 ASM because you've got a syntax error in your C. Trust us on this.
1983
1984 Now, if you're still really, really sure you need to use this (and if you're
1985 not 100% sure, ask the mailing list first), here's an explanation of how this
1986 works.
1987
1988 The syntax is as follows -
1989
1990   [
1991     { <alias1> => <table1> },
1992     [
1993       { <alias2> => <table2>, -join_type => 'inner|left|right' },
1994       [], # nested JOIN (optional)
1995       { <table1.column1> => <table2.column2>, ... (more conditions) },
1996     ],
1997     # More of the above [ ] may follow for additional joins
1998   ]
1999
2000   <table1> <alias1>
2001   JOIN
2002     <table2> <alias2>
2003     [JOIN ...]
2004   ON <table1.column1> = <table2.column2>
2005   <more joins may follow>
2006
2007 An easy way to follow the examples below is to remember the following:
2008
2009     Anything inside "[]" is a JOIN
2010     Anything inside "{}" is a condition for the enclosing JOIN
2011
2012 The following examples utilize a "person" table in a family tree application.
2013 In order to express parent->child relationships, this table is self-joined:
2014
2015     # Person->belongs_to('father' => 'Person');
2016     # Person->belongs_to('mother' => 'Person');
2017
2018 C<from> can be used to nest joins. Here we return all children with a father,
2019 then search against all mothers of those children:
2020
2021   $rs = $schema->resultset('Person')->search(
2022       undef,
2023       {
2024           alias => 'mother', # alias columns in accordance with "from"
2025           from => [
2026               { mother => 'person' },
2027               [
2028                   [
2029                       { child => 'person' },
2030                       [
2031                           { father => 'person' },
2032                           { 'father.person_id' => 'child.father_id' }
2033                       ]
2034                   ],
2035                   { 'mother.person_id' => 'child.mother_id' }
2036               ],
2037           ]
2038       },
2039   );
2040
2041   # Equivalent SQL:
2042   # SELECT mother.* FROM person mother
2043   # JOIN (
2044   #   person child
2045   #   JOIN person father
2046   #   ON ( father.person_id = child.father_id )
2047   # )
2048   # ON ( mother.person_id = child.mother_id )
2049
2050 The type of any join can be controlled manually. To search against only people
2051 with a father in the person table, we could explicitly use C<INNER JOIN>:
2052
2053     $rs = $schema->resultset('Person')->search(
2054         undef,
2055         {
2056             alias => 'child', # alias columns in accordance with "from"
2057             from => [
2058                 { child => 'person' },
2059                 [
2060                     { father => 'person', -join_type => 'inner' },
2061                     { 'father.id' => 'child.father_id' }
2062                 ],
2063             ]
2064         },
2065     );
2066
2067     # Equivalent SQL:
2068     # SELECT child.* FROM person child
2069     # INNER JOIN person father ON child.father_id = father.id
2070
2071 =cut
2072
2073 1;