use BUILDARGS intead of wrapping new, added make_immutable, removed unnneeded test...
[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'   => "_bool",
8         fallback => 1;
9 use Carp::Clan qw/^DBIx::Class/;
10 use Data::Page;
11 use Storable;
12 use DBIx::Class::ResultSetColumn;
13 use DBIx::Class::ResultSourceHandle;
14 use List::Util ();
15 use base qw/DBIx::Class/;
16
17 __PACKAGE__->mk_group_accessors('simple' => qw/result_class _source_handle/);
18
19 =head1 NAME
20
21 DBIx::Class::ResultSet - Responsible for fetching and creating resultset.
22
23 =head1 SYNOPSIS
24
25   my $rs   = $schema->resultset('User')->search({ registered => 1 });
26   my @rows = $schema->resultset('CD')->search({ year => 2005 })->all();
27
28 =head1 DESCRIPTION
29
30 The resultset is also known as an iterator. It is responsible for handling
31 queries that may return an arbitrary number of rows, e.g. via L</search>
32 or a C<has_many> relationship.
33
34 In the examples below, the following table classes are used:
35
36   package MyApp::Schema::Artist;
37   use base qw/DBIx::Class/;
38   __PACKAGE__->load_components(qw/Core/);
39   __PACKAGE__->table('artist');
40   __PACKAGE__->add_columns(qw/artistid name/);
41   __PACKAGE__->set_primary_key('artistid');
42   __PACKAGE__->has_many(cds => 'MyApp::Schema::CD');
43   1;
44
45   package MyApp::Schema::CD;
46   use base qw/DBIx::Class/;
47   __PACKAGE__->load_components(qw/Core/);
48   __PACKAGE__->table('cd');
49   __PACKAGE__->add_columns(qw/cdid artist title year/);
50   __PACKAGE__->set_primary_key('cdid');
51   __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Artist');
52   1;
53
54 =head1 OVERLOADING
55
56 If a resultset is used in a numeric context it returns the L</count>.
57 However, if it is used in a booleand context it is always true.  So if
58 you want to check if a resultset has any results use C<if $rs != 0>.
59 C<if $rs> will always be true.
60
61 =head1 METHODS
62
63 =head2 new
64
65 =over 4
66
67 =item Arguments: $source, \%$attrs
68
69 =item Return Value: $rs
70
71 =back
72
73 The resultset constructor. Takes a source object (usually a
74 L<DBIx::Class::ResultSourceProxy::Table>) and an attribute hash (see
75 L</ATTRIBUTES> below).  Does not perform any queries -- these are
76 executed as needed by the other methods.
77
78 Generally you won't need to construct a resultset manually.  You'll
79 automatically get one from e.g. a L</search> called in scalar context:
80
81   my $rs = $schema->resultset('CD')->search({ title => '100th Window' });
82
83 IMPORTANT: If called on an object, proxies to new_result instead so
84
85   my $cd = $schema->resultset('CD')->new({ title => 'Spoon' });
86
87 will return a CD object, not a ResultSet.
88
89 =cut
90
91 sub new {
92   my $class = shift;
93   return $class->new_result(@_) if ref $class;
94
95   my ($source, $attrs) = @_;
96   $source = $source->handle 
97     unless $source->isa('DBIx::Class::ResultSourceHandle');
98   $attrs = { %{$attrs||{}} };
99
100   if ($attrs->{page}) {
101     $attrs->{rows} ||= 10;
102   }
103
104   $attrs->{alias} ||= 'me';
105
106   # Creation of {} and bless separated to mitigate RH perl bug
107   # see https://bugzilla.redhat.com/show_bug.cgi?id=196836
108   my $self = {
109     _source_handle => $source,
110     result_class => $attrs->{result_class} || $source->resolve->result_class,
111     cond => $attrs->{where},
112     count => undef,
113     pager => undef,
114     attrs => $attrs
115   };
116
117   bless $self, $class;
118
119   return $self;
120 }
121
122 =head2 search
123
124 =over 4
125
126 =item Arguments: $cond, \%attrs?
127
128 =item Return Value: $resultset (scalar context), @row_objs (list context)
129
130 =back
131
132   my @cds    = $cd_rs->search({ year => 2001 }); # "... WHERE year = 2001"
133   my $new_rs = $cd_rs->search({ year => 2005 });
134
135   my $new_rs = $cd_rs->search([ { year => 2005 }, { year => 2004 } ]);
136                  # year = 2005 OR year = 2004
137
138 If you need to pass in additional attributes but no additional condition,
139 call it as C<search(undef, \%attrs)>.
140
141   # "SELECT name, artistid FROM $artist_table"
142   my @all_artists = $schema->resultset('Artist')->search(undef, {
143     columns => [qw/name artistid/],
144   });
145
146 For a list of attributes that can be passed to C<search>, see
147 L</ATTRIBUTES>. For more examples of using this function, see
148 L<Searching|DBIx::Class::Manual::Cookbook/Searching>. For a complete
149 documentation for the first argument, see L<SQL::Abstract>.
150
151 For more help on using joins with search, see L<DBIx::Class::Manual::Joining>.
152
153 =cut
154
155 sub search {
156   my $self = shift;
157   my $rs = $self->search_rs( @_ );
158   return (wantarray ? $rs->all : $rs);
159 }
160
161 =head2 search_rs
162
163 =over 4
164
165 =item Arguments: $cond, \%attrs?
166
167 =item Return Value: $resultset
168
169 =back
170
171 This method does the same exact thing as search() except it will
172 always return a resultset, even in list context.
173
174 =cut
175
176 sub search_rs {
177   my $self = shift;
178
179   my $attrs = {};
180   $attrs = pop(@_) if @_ > 1 and ref $_[$#_] eq 'HASH';
181   my $our_attrs = { %{$self->{attrs}} };
182   my $having = delete $our_attrs->{having};
183   my $where = delete $our_attrs->{where};
184
185   my $rows;
186
187   my %safe = (alias => 1, cache => 1);
188
189   unless (
190     (@_ && defined($_[0])) # @_ == () or (undef)
191     || 
192     (keys %$attrs # empty attrs or only 'safe' attrs
193     && List::Util::first { !$safe{$_} } keys %$attrs)
194   ) {
195     # no search, effectively just a clone
196     $rows = $self->get_cache;
197   }
198
199   my $new_attrs = { %{$our_attrs}, %{$attrs} };
200
201   # merge new attrs into inherited
202   foreach my $key (qw/join prefetch/) {
203     next unless exists $attrs->{$key};
204     $new_attrs->{$key} = $self->_merge_attr($our_attrs->{$key}, $attrs->{$key});
205   }
206
207   my $cond = (@_
208     ? (
209         (@_ == 1 || ref $_[0] eq "HASH")
210           ? (
211               (ref $_[0] eq 'HASH')
212                 ? (
213                     (keys %{ $_[0] }  > 0)
214                       ? shift
215                       : undef
216                    )
217                 :  shift
218              )
219           : (
220               (@_ % 2)
221                 ? $self->throw_exception("Odd number of arguments to search")
222                 : {@_}
223              )
224       )
225     : undef
226   );
227
228   if (defined $where) {
229     $new_attrs->{where} = (
230       defined $new_attrs->{where}
231         ? { '-and' => [
232               map {
233                 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
234               } $where, $new_attrs->{where}
235             ]
236           }
237         : $where);
238   }
239
240   if (defined $cond) {
241     $new_attrs->{where} = (
242       defined $new_attrs->{where}
243         ? { '-and' => [
244               map {
245                 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
246               } $cond, $new_attrs->{where}
247             ]
248           }
249         : $cond);
250   }
251
252   if (defined $having) {
253     $new_attrs->{having} = (
254       defined $new_attrs->{having}
255         ? { '-and' => [
256               map {
257                 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
258               } $having, $new_attrs->{having}
259             ]
260           }
261         : $having);
262   }
263
264   my $rs = (ref $self)->new($self->result_source, $new_attrs);
265   if ($rows) {
266     $rs->set_cache($rows);
267   }
268   return $rs;
269 }
270
271 =head2 search_literal
272
273 =over 4
274
275 =item Arguments: $sql_fragment, @bind_values
276
277 =item Return Value: $resultset (scalar context), @row_objs (list context)
278
279 =back
280
281   my @cds   = $cd_rs->search_literal('year = ? AND title = ?', qw/2001 Reload/);
282   my $newrs = $artist_rs->search_literal('name = ?', 'Metallica');
283
284 Pass a literal chunk of SQL to be added to the conditional part of the
285 resultset query.
286
287 CAVEAT: C<search_literal> is provided for Class::DBI compatibility and should
288 only be used in that context. There are known problems using C<search_literal>
289 in chained queries; it can result in bind values in the wrong order.  See
290 L<DBIx::Class::Manual::Cookbook/Searching> and
291 L<DBIx::Class::Manual::FAQ/Searching> for searching techniques that do not
292 require C<search_literal>.
293
294 =cut
295
296 sub search_literal {
297   my ($self, $cond, @vals) = @_;
298   my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
299   $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ];
300   return $self->search(\$cond, $attrs);
301 }
302
303 =head2 find
304
305 =over 4
306
307 =item Arguments: @values | \%cols, \%attrs?
308
309 =item Return Value: $row_object
310
311 =back
312
313 Finds a row based on its primary key or unique constraint. For example, to find
314 a row by its primary key:
315
316   my $cd = $schema->resultset('CD')->find(5);
317
318 You can also find a row by a specific unique constraint using the C<key>
319 attribute. For example:
320
321   my $cd = $schema->resultset('CD')->find('Massive Attack', 'Mezzanine', {
322     key => 'cd_artist_title'
323   });
324
325 Additionally, you can specify the columns explicitly by name:
326
327   my $cd = $schema->resultset('CD')->find(
328     {
329       artist => 'Massive Attack',
330       title  => 'Mezzanine',
331     },
332     { key => 'cd_artist_title' }
333   );
334
335 If the C<key> is specified as C<primary>, it searches only on the primary key.
336
337 If no C<key> is specified, it searches on all unique constraints defined on the
338 source for which column data is provided, including the primary key.
339
340 If your table does not have a primary key, you B<must> provide a value for the
341 C<key> attribute matching one of the unique constraints on the source.
342
343 Note: If your query does not return only one row, a warning is generated:
344
345   Query returned more than one row
346
347 See also L</find_or_create> and L</update_or_create>. For information on how to
348 declare unique constraints, see
349 L<DBIx::Class::ResultSource/add_unique_constraint>.
350
351 =cut
352
353 sub find {
354   my $self = shift;
355   my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
356
357   # Default to the primary key, but allow a specific key
358   my @cols = exists $attrs->{key}
359     ? $self->result_source->unique_constraint_columns($attrs->{key})
360     : $self->result_source->primary_columns;
361   $self->throw_exception(
362     "Can't find unless a primary key is defined or unique constraint is specified"
363   ) unless @cols;
364
365   # Parse out a hashref from input
366   my $input_query;
367   if (ref $_[0] eq 'HASH') {
368     $input_query = { %{$_[0]} };
369   }
370   elsif (@_ == @cols) {
371     $input_query = {};
372     @{$input_query}{@cols} = @_;
373   }
374   else {
375     # Compatibility: Allow e.g. find(id => $value)
376     carp "Find by key => value deprecated; please use a hashref instead";
377     $input_query = {@_};
378   }
379
380   my (%related, $info);
381
382   KEY: foreach my $key (keys %$input_query) {
383     if (ref($input_query->{$key})
384         && ($info = $self->result_source->relationship_info($key))) {
385       my $val = delete $input_query->{$key};
386       next KEY if (ref($val) eq 'ARRAY'); # has_many for multi_create
387       my $rel_q = $self->result_source->resolve_condition(
388                     $info->{cond}, $val, $key
389                   );
390       die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY';
391       @related{keys %$rel_q} = values %$rel_q;
392     }
393   }
394   if (my @keys = keys %related) {
395     @{$input_query}{@keys} = values %related;
396   }
397
398
399   # Build the final query: Default to the disjunction of the unique queries,
400   # but allow the input query in case the ResultSet defines the query or the
401   # user is abusing find
402   my $alias = exists $attrs->{alias} ? $attrs->{alias} : $self->{attrs}{alias};
403   my $query;
404   if (exists $attrs->{key}) {
405     my @unique_cols = $self->result_source->unique_constraint_columns($attrs->{key});
406     my $unique_query = $self->_build_unique_query($input_query, \@unique_cols);
407     $query = $self->_add_alias($unique_query, $alias);
408   }
409   else {
410     my @unique_queries = $self->_unique_queries($input_query, $attrs);
411     $query = @unique_queries
412       ? [ map { $self->_add_alias($_, $alias) } @unique_queries ]
413       : $self->_add_alias($input_query, $alias);
414   }
415
416   # Run the query
417   if (keys %$attrs) {
418     my $rs = $self->search($query, $attrs);
419     if (keys %{$rs->_resolved_attrs->{collapse}}) {
420       my $row = $rs->next;
421       carp "Query returned more than one row" if $rs->next;
422       return $row;
423     }
424     else {
425       return $rs->single;
426     }
427   }
428   else {
429     if (keys %{$self->_resolved_attrs->{collapse}}) {
430       my $rs = $self->search($query);
431       my $row = $rs->next;
432       carp "Query returned more than one row" if $rs->next;
433       return $row;
434     }
435     else {
436       return $self->single($query);
437     }
438   }
439 }
440
441 # _add_alias
442 #
443 # Add the specified alias to the specified query hash. A copy is made so the
444 # original query is not modified.
445
446 sub _add_alias {
447   my ($self, $query, $alias) = @_;
448
449   my %aliased = %$query;
450   foreach my $col (grep { ! m/\./ } keys %aliased) {
451     $aliased{"$alias.$col"} = delete $aliased{$col};
452   }
453
454   return \%aliased;
455 }
456
457 # _unique_queries
458 #
459 # Build a list of queries which satisfy unique constraints.
460
461 sub _unique_queries {
462   my ($self, $query, $attrs) = @_;
463
464   my @constraint_names = exists $attrs->{key}
465     ? ($attrs->{key})
466     : $self->result_source->unique_constraint_names;
467
468   my $where = $self->_collapse_cond($self->{attrs}{where} || {});
469   my $num_where = scalar keys %$where;
470
471   my @unique_queries;
472   foreach my $name (@constraint_names) {
473     my @unique_cols = $self->result_source->unique_constraint_columns($name);
474     my $unique_query = $self->_build_unique_query($query, \@unique_cols);
475
476     my $num_cols = scalar @unique_cols;
477     my $num_query = scalar keys %$unique_query;
478
479     my $total = $num_query + $num_where;
480     if ($num_query && ($num_query == $num_cols || $total == $num_cols)) {
481       # The query is either unique on its own or is unique in combination with
482       # the existing where clause
483       push @unique_queries, $unique_query;
484     }
485   }
486
487   return @unique_queries;
488 }
489
490 # _build_unique_query
491 #
492 # Constrain the specified query hash based on the specified column names.
493
494 sub _build_unique_query {
495   my ($self, $query, $unique_cols) = @_;
496
497   return {
498     map  { $_ => $query->{$_} }
499     grep { exists $query->{$_} }
500       @$unique_cols
501   };
502 }
503
504 =head2 search_related
505
506 =over 4
507
508 =item Arguments: $rel, $cond, \%attrs?
509
510 =item Return Value: $new_resultset
511
512 =back
513
514   $new_rs = $cd_rs->search_related('artist', {
515     name => 'Emo-R-Us',
516   });
517
518 Searches the specified relationship, optionally specifying a condition and
519 attributes for matching records. See L</ATTRIBUTES> for more information.
520
521 =cut
522
523 sub search_related {
524   return shift->related_resultset(shift)->search(@_);
525 }
526
527 =head2 search_related_rs
528
529 This method works exactly the same as search_related, except that
530 it guarantees a restultset, even in list context.
531
532 =cut
533
534 sub search_related_rs {
535   return shift->related_resultset(shift)->search_rs(@_);
536 }
537
538 =head2 cursor
539
540 =over 4
541
542 =item Arguments: none
543
544 =item Return Value: $cursor
545
546 =back
547
548 Returns a storage-driven cursor to the given resultset. See
549 L<DBIx::Class::Cursor> for more information.
550
551 =cut
552
553 sub cursor {
554   my ($self) = @_;
555
556   my $attrs = { %{$self->_resolved_attrs} };
557   return $self->{cursor}
558     ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select},
559           $attrs->{where},$attrs);
560 }
561
562 =head2 single
563
564 =over 4
565
566 =item Arguments: $cond?
567
568 =item Return Value: $row_object?
569
570 =back
571
572   my $cd = $schema->resultset('CD')->single({ year => 2001 });
573
574 Inflates the first result without creating a cursor if the resultset has
575 any records in it; if not returns nothing. Used by L</find> as an optimisation.
576
577 Can optionally take an additional condition B<only> - this is a fast-code-path
578 method; if you need to add extra joins or similar call L</search> and then
579 L</single> without a condition on the L<DBIx::Class::ResultSet> returned from
580 that.
581
582 B<Note>: As of 0.08100, this method assumes that the query returns only one
583 row. If more than one row is returned, you will receive a warning:
584
585   Query returned more than one row
586
587 In this case, you should be using L</first> or L</find> instead.
588
589 =cut
590
591 sub single {
592   my ($self, $where) = @_;
593   my $attrs = { %{$self->_resolved_attrs} };
594   if ($where) {
595     if (defined $attrs->{where}) {
596       $attrs->{where} = {
597         '-and' =>
598             [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
599                $where, delete $attrs->{where} ]
600       };
601     } else {
602       $attrs->{where} = $where;
603     }
604   }
605
606 #  XXX: Disabled since it doesn't infer uniqueness in all cases
607 #  unless ($self->_is_unique_query($attrs->{where})) {
608 #    carp "Query not guaranteed to return a single row"
609 #      . "; please declare your unique constraints or use search instead";
610 #  }
611
612   my @data = $self->result_source->storage->select_single(
613     $attrs->{from}, $attrs->{select},
614     $attrs->{where}, $attrs
615   );
616
617   return (@data ? ($self->_construct_object(@data))[0] : undef);
618 }
619
620 # _is_unique_query
621 #
622 # Try to determine if the specified query is guaranteed to be unique, based on
623 # the declared unique constraints.
624
625 sub _is_unique_query {
626   my ($self, $query) = @_;
627
628   my $collapsed = $self->_collapse_query($query);
629   my $alias = $self->{attrs}{alias};
630
631   foreach my $name ($self->result_source->unique_constraint_names) {
632     my @unique_cols = map {
633       "$alias.$_"
634     } $self->result_source->unique_constraint_columns($name);
635
636     # Count the values for each unique column
637     my %seen = map { $_ => 0 } @unique_cols;
638
639     foreach my $key (keys %$collapsed) {
640       my $aliased = $key =~ /\./ ? $key : "$alias.$key";
641       next unless exists $seen{$aliased};  # Additional constraints are okay
642       $seen{$aliased} = scalar keys %{ $collapsed->{$key} };
643     }
644
645     # If we get 0 or more than 1 value for a column, it's not necessarily unique
646     return 1 unless grep { $_ != 1 } values %seen;
647   }
648
649   return 0;
650 }
651
652 # _collapse_query
653 #
654 # Recursively collapse the query, accumulating values for each column.
655
656 sub _collapse_query {
657   my ($self, $query, $collapsed) = @_;
658
659   $collapsed ||= {};
660
661   if (ref $query eq 'ARRAY') {
662     foreach my $subquery (@$query) {
663       next unless ref $subquery;  # -or
664 #      warn "ARRAY: " . Dumper $subquery;
665       $collapsed = $self->_collapse_query($subquery, $collapsed);
666     }
667   }
668   elsif (ref $query eq 'HASH') {
669     if (keys %$query and (keys %$query)[0] eq '-and') {
670       foreach my $subquery (@{$query->{-and}}) {
671 #        warn "HASH: " . Dumper $subquery;
672         $collapsed = $self->_collapse_query($subquery, $collapsed);
673       }
674     }
675     else {
676 #      warn "LEAF: " . Dumper $query;
677       foreach my $col (keys %$query) {
678         my $value = $query->{$col};
679         $collapsed->{$col}{$value}++;
680       }
681     }
682   }
683
684   return $collapsed;
685 }
686
687 =head2 get_column
688
689 =over 4
690
691 =item Arguments: $cond?
692
693 =item Return Value: $resultsetcolumn
694
695 =back
696
697   my $max_length = $rs->get_column('length')->max;
698
699 Returns a L<DBIx::Class::ResultSetColumn> instance for a column of the ResultSet.
700
701 =cut
702
703 sub get_column {
704   my ($self, $column) = @_;
705   my $new = DBIx::Class::ResultSetColumn->new($self, $column);
706   return $new;
707 }
708
709 =head2 search_like
710
711 =over 4
712
713 =item Arguments: $cond, \%attrs?
714
715 =item Return Value: $resultset (scalar context), @row_objs (list context)
716
717 =back
718
719   # WHERE title LIKE '%blue%'
720   $cd_rs = $rs->search_like({ title => '%blue%'});
721
722 Performs a search, but uses C<LIKE> instead of C<=> as the condition. Note
723 that this is simply a convenience method. You most likely want to use
724 L</search> with specific operators.
725
726 For more information, see L<DBIx::Class::Manual::Cookbook>.
727
728 =cut
729
730 sub search_like {
731   my $class = shift;
732   my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
733   my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_};
734   $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
735   return $class->search($query, { %$attrs });
736 }
737
738 =head2 slice
739
740 =over 4
741
742 =item Arguments: $first, $last
743
744 =item Return Value: $resultset (scalar context), @row_objs (list context)
745
746 =back
747
748 Returns a resultset or object list representing a subset of elements from the
749 resultset slice is called on. Indexes are from 0, i.e., to get the first
750 three records, call:
751
752   my ($one, $two, $three) = $rs->slice(0, 2);
753
754 =cut
755
756 sub slice {
757   my ($self, $min, $max) = @_;
758   my $attrs = {}; # = { %{ $self->{attrs} || {} } };
759   $attrs->{offset} = $self->{attrs}{offset} || 0;
760   $attrs->{offset} += $min;
761   $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
762   return $self->search(undef(), $attrs);
763   #my $slice = (ref $self)->new($self->result_source, $attrs);
764   #return (wantarray ? $slice->all : $slice);
765 }
766
767 =head2 next
768
769 =over 4
770
771 =item Arguments: none
772
773 =item Return Value: $result?
774
775 =back
776
777 Returns the next element in the resultset (C<undef> is there is none).
778
779 Can be used to efficiently iterate over records in the resultset:
780
781   my $rs = $schema->resultset('CD')->search;
782   while (my $cd = $rs->next) {
783     print $cd->title;
784   }
785
786 Note that you need to store the resultset object, and call C<next> on it.
787 Calling C<< resultset('Table')->next >> repeatedly will always return the
788 first record from the resultset.
789
790 =cut
791
792 sub next {
793   my ($self) = @_;
794   if (my $cache = $self->get_cache) {
795     $self->{all_cache_position} ||= 0;
796     return $cache->[$self->{all_cache_position}++];
797   }
798   if ($self->{attrs}{cache}) {
799     $self->{all_cache_position} = 1;
800     return ($self->all)[0];
801   }
802   if ($self->{stashed_objects}) {
803     my $obj = shift(@{$self->{stashed_objects}});
804     delete $self->{stashed_objects} unless @{$self->{stashed_objects}};
805     return $obj;
806   }
807   my @row = (
808     exists $self->{stashed_row}
809       ? @{delete $self->{stashed_row}}
810       : $self->cursor->next
811   );
812   return undef unless (@row);
813   my ($row, @more) = $self->_construct_object(@row);
814   $self->{stashed_objects} = \@more if @more;
815   return $row;
816 }
817
818 sub _construct_object {
819   my ($self, @row) = @_;
820   my $info = $self->_collapse_result($self->{_attrs}{as}, \@row);
821   my @new = $self->result_class->inflate_result($self->result_source, @$info);
822   @new = $self->{_attrs}{record_filter}->(@new)
823     if exists $self->{_attrs}{record_filter};
824   return @new;
825 }
826
827 sub _collapse_result {
828   my ($self, $as_proto, $row) = @_;
829
830   my @copy = @$row;
831
832   # 'foo'         => [ undef, 'foo' ]
833   # 'foo.bar'     => [ 'foo', 'bar' ]
834   # 'foo.bar.baz' => [ 'foo.bar', 'baz' ]
835
836   my @construct_as = map { [ (/^(?:(.*)\.)?([^.]+)$/) ] } @$as_proto;
837
838   my %collapse = %{$self->{_attrs}{collapse}||{}};
839
840   my @pri_index;
841
842   # if we're doing collapsing (has_many prefetch) we need to grab records
843   # until the PK changes, so fill @pri_index. if not, we leave it empty so
844   # we know we don't have to bother.
845
846   # the reason for not using the collapse stuff directly is because if you
847   # had for e.g. two artists in a row with no cds, the collapse info for
848   # both would be NULL (undef) so you'd lose the second artist
849
850   # store just the index so we can check the array positions from the row
851   # without having to contruct the full hash
852
853   if (keys %collapse) {
854     my %pri = map { ($_ => 1) } $self->result_source->primary_columns;
855     foreach my $i (0 .. $#construct_as) {
856       next if defined($construct_as[$i][0]); # only self table
857       if (delete $pri{$construct_as[$i][1]}) {
858         push(@pri_index, $i);
859       }
860       last unless keys %pri; # short circuit (Johnny Five Is Alive!)
861     }
862   }
863
864   # no need to do an if, it'll be empty if @pri_index is empty anyway
865
866   my %pri_vals = map { ($_ => $copy[$_]) } @pri_index;
867
868   my @const_rows;
869
870   do { # no need to check anything at the front, we always want the first row
871
872     my %const;
873   
874     foreach my $this_as (@construct_as) {
875       $const{$this_as->[0]||''}{$this_as->[1]} = shift(@copy);
876     }
877
878     push(@const_rows, \%const);
879
880   } until ( # no pri_index => no collapse => drop straight out
881       !@pri_index
882     or
883       do { # get another row, stash it, drop out if different PK
884
885         @copy = $self->cursor->next;
886         $self->{stashed_row} = \@copy;
887
888         # last thing in do block, counts as true if anything doesn't match
889
890         # check xor defined first for NULL vs. NOT NULL then if one is
891         # defined the other must be so check string equality
892
893         grep {
894           (defined $pri_vals{$_} ^ defined $copy[$_])
895           || (defined $pri_vals{$_} && ($pri_vals{$_} ne $copy[$_]))
896         } @pri_index;
897       }
898   );
899
900   my $alias = $self->{attrs}{alias};
901   my $info = [];
902
903   my %collapse_pos;
904
905   my @const_keys;
906
907   foreach my $const (@const_rows) {
908     scalar @const_keys or do {
909       @const_keys = sort { length($a) <=> length($b) } keys %$const;
910     };
911     foreach my $key (@const_keys) {
912       if (length $key) {
913         my $target = $info;
914         my @parts = split(/\./, $key);
915         my $cur = '';
916         my $data = $const->{$key};
917         foreach my $p (@parts) {
918           $target = $target->[1]->{$p} ||= [];
919           $cur .= ".${p}";
920           if ($cur eq ".${key}" && (my @ckey = @{$collapse{$cur}||[]})) { 
921             # collapsing at this point and on final part
922             my $pos = $collapse_pos{$cur};
923             CK: foreach my $ck (@ckey) {
924               if (!defined $pos->{$ck} || $pos->{$ck} ne $data->{$ck}) {
925                 $collapse_pos{$cur} = $data;
926                 delete @collapse_pos{ # clear all positioning for sub-entries
927                   grep { m/^\Q${cur}.\E/ } keys %collapse_pos
928                 };
929                 push(@$target, []);
930                 last CK;
931               }
932             }
933           }
934           if (exists $collapse{$cur}) {
935             $target = $target->[-1];
936           }
937         }
938         $target->[0] = $data;
939       } else {
940         $info->[0] = $const->{$key};
941       }
942     }
943   }
944
945   return $info;
946 }
947
948 =head2 result_source
949
950 =over 4
951
952 =item Arguments: $result_source?
953
954 =item Return Value: $result_source
955
956 =back
957
958 An accessor for the primary ResultSource object from which this ResultSet
959 is derived.
960
961 =head2 result_class
962
963 =over 4
964
965 =item Arguments: $result_class?
966
967 =item Return Value: $result_class
968
969 =back
970
971 An accessor for the class to use when creating row objects. Defaults to 
972 C<< result_source->result_class >> - which in most cases is the name of the 
973 L<"table"|DBIx::Class::Manual::Glossary/"ResultSource"> class.
974
975 =cut
976
977
978 =head2 count
979
980 =over 4
981
982 =item Arguments: $cond, \%attrs??
983
984 =item Return Value: $count
985
986 =back
987
988 Performs an SQL C<COUNT> with the same query as the resultset was built
989 with to find the number of elements. If passed arguments, does a search
990 on the resultset and counts the results of that.
991
992 Note: When using C<count> with C<group_by>, L<DBIx::Class> emulates C<GROUP BY>
993 using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
994 not support C<DISTINCT> with multiple columns. If you are using such a
995 database, you should only use columns from the main table in your C<group_by>
996 clause.
997
998 =cut
999
1000 sub count {
1001   my $self = shift;
1002   return $self->search(@_)->count if @_ and defined $_[0];
1003   return scalar @{ $self->get_cache } if $self->get_cache;
1004   my $count = $self->_count;
1005   return 0 unless $count;
1006
1007   # need to take offset from resolved attrs
1008
1009   $count -= $self->{_attrs}{offset} if $self->{_attrs}{offset};
1010   $count = $self->{attrs}{rows} if
1011     $self->{attrs}{rows} and $self->{attrs}{rows} < $count;
1012   $count = 0 if ($count < 0);
1013   return $count;
1014 }
1015
1016 sub _count { # Separated out so pager can get the full count
1017   my $self = shift;
1018   my $select = { count => '*' };
1019
1020   my $attrs = { %{$self->_resolved_attrs} };
1021   if (my $group_by = delete $attrs->{group_by}) {
1022     delete $attrs->{having};
1023     my @distinct = (ref $group_by ?  @$group_by : ($group_by));
1024     # todo: try CONCAT for multi-column pk
1025     my @pk = $self->result_source->primary_columns;
1026     if (@pk == 1) {
1027       my $alias = $attrs->{alias};
1028       foreach my $column (@distinct) {
1029         if ($column =~ qr/^(?:\Q${alias}.\E)?$pk[0]$/) {
1030           @distinct = ($column);
1031           last;
1032         }
1033       }
1034     }
1035
1036     $select = { count => { distinct => \@distinct } };
1037   }
1038
1039   $attrs->{select} = $select;
1040   $attrs->{as} = [qw/count/];
1041
1042   # offset, order by and page are not needed to count. record_filter is cdbi
1043   delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
1044
1045   my $tmp_rs = (ref $self)->new($self->result_source, $attrs);
1046   my ($count) = $tmp_rs->cursor->next;
1047   return $count;
1048 }
1049
1050 sub _bool {
1051   return 1;
1052 }
1053
1054 =head2 count_literal
1055
1056 =over 4
1057
1058 =item Arguments: $sql_fragment, @bind_values
1059
1060 =item Return Value: $count
1061
1062 =back
1063
1064 Counts the results in a literal query. Equivalent to calling L</search_literal>
1065 with the passed arguments, then L</count>.
1066
1067 =cut
1068
1069 sub count_literal { shift->search_literal(@_)->count; }
1070
1071 =head2 all
1072
1073 =over 4
1074
1075 =item Arguments: none
1076
1077 =item Return Value: @objects
1078
1079 =back
1080
1081 Returns all elements in the resultset. Called implicitly if the resultset
1082 is returned in list context.
1083
1084 =cut
1085
1086 sub all {
1087   my ($self) = @_;
1088   return @{ $self->get_cache } if $self->get_cache;
1089
1090   my @obj;
1091
1092   # TODO: don't call resolve here
1093   if (keys %{$self->_resolved_attrs->{collapse}}) {
1094 #  if ($self->{attrs}{prefetch}) {
1095       # Using $self->cursor->all is really just an optimisation.
1096       # If we're collapsing has_many prefetches it probably makes
1097       # very little difference, and this is cleaner than hacking
1098       # _construct_object to survive the approach
1099     my @row = $self->cursor->next;
1100     while (@row) {
1101       push(@obj, $self->_construct_object(@row));
1102       @row = (exists $self->{stashed_row}
1103                ? @{delete $self->{stashed_row}}
1104                : $self->cursor->next);
1105     }
1106   } else {
1107     @obj = map { $self->_construct_object(@$_) } $self->cursor->all;
1108   }
1109
1110   $self->set_cache(\@obj) if $self->{attrs}{cache};
1111   return @obj;
1112 }
1113
1114 =head2 reset
1115
1116 =over 4
1117
1118 =item Arguments: none
1119
1120 =item Return Value: $self
1121
1122 =back
1123
1124 Resets the resultset's cursor, so you can iterate through the elements again.
1125
1126 =cut
1127
1128 sub reset {
1129   my ($self) = @_;
1130   delete $self->{_attrs} if exists $self->{_attrs};
1131   $self->{all_cache_position} = 0;
1132   $self->cursor->reset;
1133   return $self;
1134 }
1135
1136 =head2 first
1137
1138 =over 4
1139
1140 =item Arguments: none
1141
1142 =item Return Value: $object?
1143
1144 =back
1145
1146 Resets the resultset and returns an object for the first result (if the
1147 resultset returns anything).
1148
1149 =cut
1150
1151 sub first {
1152   return $_[0]->reset->next;
1153 }
1154
1155 # _cond_for_update_delete
1156 #
1157 # update/delete require the condition to be modified to handle
1158 # the differing SQL syntax available.  This transforms the $self->{cond}
1159 # appropriately, returning the new condition.
1160
1161 sub _cond_for_update_delete {
1162   my ($self, $full_cond) = @_;
1163   my $cond = {};
1164
1165   $full_cond ||= $self->{cond};
1166   # No-op. No condition, we're updating/deleting everything
1167   return $cond unless ref $full_cond;
1168
1169   if (ref $full_cond eq 'ARRAY') {
1170     $cond = [
1171       map {
1172         my %hash;
1173         foreach my $key (keys %{$_}) {
1174           $key =~ /([^.]+)$/;
1175           $hash{$1} = $_->{$key};
1176         }
1177         \%hash;
1178       } @{$full_cond}
1179     ];
1180   }
1181   elsif (ref $full_cond eq 'HASH') {
1182     if ((keys %{$full_cond})[0] eq '-and') {
1183       $cond->{-and} = [];
1184
1185       my @cond = @{$full_cond->{-and}};
1186       for (my $i = 0; $i < @cond; $i++) {
1187         my $entry = $cond[$i];
1188
1189         my $hash;
1190         if (ref $entry eq 'HASH') {
1191           $hash = $self->_cond_for_update_delete($entry);
1192         }
1193         else {
1194           $entry =~ /([^.]+)$/;
1195           $hash->{$1} = $cond[++$i];
1196         }
1197
1198         push @{$cond->{-and}}, $hash;
1199       }
1200     }
1201     else {
1202       foreach my $key (keys %{$full_cond}) {
1203         $key =~ /([^.]+)$/;
1204         $cond->{$1} = $full_cond->{$key};
1205       }
1206     }
1207   }
1208   else {
1209     $self->throw_exception(
1210       "Can't update/delete on resultset with condition unless hash or array"
1211     );
1212   }
1213
1214   return $cond;
1215 }
1216
1217
1218 =head2 update
1219
1220 =over 4
1221
1222 =item Arguments: \%values
1223
1224 =item Return Value: $storage_rv
1225
1226 =back
1227
1228 Sets the specified columns in the resultset to the supplied values in a
1229 single query. Return value will be true if the update succeeded or false
1230 if no records were updated; exact type of success value is storage-dependent.
1231
1232 =cut
1233
1234 sub update {
1235   my ($self, $values) = @_;
1236   $self->throw_exception("Values for update must be a hash")
1237     unless ref $values eq 'HASH';
1238
1239   my $cond = $self->_cond_for_update_delete;
1240    
1241   return $self->result_source->storage->update(
1242     $self->result_source, $values, $cond
1243   );
1244 }
1245
1246 =head2 update_all
1247
1248 =over 4
1249
1250 =item Arguments: \%values
1251
1252 =item Return Value: 1
1253
1254 =back
1255
1256 Fetches all objects and updates them one at a time. Note that C<update_all>
1257 will run DBIC cascade triggers, while L</update> will not.
1258
1259 =cut
1260
1261 sub update_all {
1262   my ($self, $values) = @_;
1263   $self->throw_exception("Values for update must be a hash")
1264     unless ref $values eq 'HASH';
1265   foreach my $obj ($self->all) {
1266     $obj->set_columns($values)->update;
1267   }
1268   return 1;
1269 }
1270
1271 =head2 delete
1272
1273 =over 4
1274
1275 =item Arguments: none
1276
1277 =item Return Value: 1
1278
1279 =back
1280
1281 Deletes the contents of the resultset from its result source. Note that this
1282 will not run DBIC cascade triggers. See L</delete_all> if you need triggers
1283 to run. See also L<DBIx::Class::Row/delete>.
1284
1285 =cut
1286
1287 sub delete {
1288   my ($self) = @_;
1289
1290   my $cond = $self->_cond_for_update_delete;
1291
1292   $self->result_source->storage->delete($self->result_source, $cond);
1293   return 1;
1294 }
1295
1296 =head2 delete_all
1297
1298 =over 4
1299
1300 =item Arguments: none
1301
1302 =item Return Value: 1
1303
1304 =back
1305
1306 Fetches all objects and deletes them one at a time. Note that C<delete_all>
1307 will run DBIC cascade triggers, while L</delete> will not.
1308
1309 =cut
1310
1311 sub delete_all {
1312   my ($self) = @_;
1313   $_->delete for $self->all;
1314   return 1;
1315 }
1316
1317 =head2 populate
1318
1319 =over 4
1320
1321 =item Arguments: \@data;
1322
1323 =back
1324
1325 Pass an arrayref of hashrefs. Each hashref should be a structure suitable for
1326 submitting to a $resultset->create(...) method.
1327
1328 In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
1329 to insert the data, as this is a faster method.  
1330
1331 Otherwise, each set of data is inserted into the database using
1332 L<DBIx::Class::ResultSet/create>, and a arrayref of the resulting row
1333 objects is returned.
1334
1335 Example:  Assuming an Artist Class that has many CDs Classes relating:
1336
1337   my $Artist_rs = $schema->resultset("Artist");
1338   
1339   ## Void Context Example 
1340   $Artist_rs->populate([
1341      { artistid => 4, name => 'Manufactured Crap', cds => [ 
1342         { title => 'My First CD', year => 2006 },
1343         { title => 'Yet More Tweeny-Pop crap', year => 2007 },
1344       ],
1345      },
1346      { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
1347         { title => 'My parents sold me to a record company' ,year => 2005 },
1348         { title => 'Why Am I So Ugly?', year => 2006 },
1349         { title => 'I Got Surgery and am now Popular', year => 2007 }
1350       ],
1351      },
1352   ]);
1353   
1354   ## Array Context Example
1355   my ($ArtistOne, $ArtistTwo, $ArtistThree) = $Artist_rs->populate([
1356     { name => "Artist One"},
1357     { name => "Artist Two"},
1358     { name => "Artist Three", cds=> [
1359     { title => "First CD", year => 2007},
1360     { title => "Second CD", year => 2008},
1361   ]}
1362   ]);
1363   
1364   print $ArtistOne->name; ## response is 'Artist One'
1365   print $ArtistThree->cds->count ## reponse is '2'
1366   
1367 Please note an important effect on your data when choosing between void and
1368 wantarray context. Since void context goes straight to C<insert_bulk> in 
1369 L<DBIx::Class::Storage::DBI> this will skip any component that is overriding
1370 c<insert>.  So if you are using something like L<DBIx-Class-UUIDColumns> to 
1371 create primary keys for you, you will find that your PKs are empty.  In this 
1372 case you will have to use the wantarray context in order to create those 
1373 values.
1374
1375 =cut
1376
1377 sub populate {
1378   my ($self, $data) = @_;
1379   
1380   if(defined wantarray) {
1381     my @created;
1382     foreach my $item (@$data) {
1383       push(@created, $self->create($item));
1384     }
1385     return @created;
1386   } else {
1387     my ($first, @rest) = @$data;
1388
1389     my @names = grep {!ref $first->{$_}} keys %$first;
1390     my @rels = grep { $self->result_source->has_relationship($_) } keys %$first;
1391     my @pks = $self->result_source->primary_columns;  
1392
1393     ## do the belongs_to relationships  
1394     foreach my $index (0..$#$data) {
1395       if( grep { !defined $data->[$index]->{$_} } @pks ) {
1396         my @ret = $self->populate($data);
1397         return;
1398       }
1399     
1400       foreach my $rel (@rels) {
1401         next unless $data->[$index]->{$rel} && ref $data->[$index]->{$rel} eq "HASH";
1402         my $result = $self->related_resultset($rel)->create($data->[$index]->{$rel});
1403         my ($reverse) = keys %{$self->result_source->reverse_relationship_info($rel)};
1404         my $related = $result->result_source->resolve_condition(
1405           $result->result_source->relationship_info($reverse)->{cond},
1406           $self,        
1407           $result,        
1408         );
1409
1410         delete $data->[$index]->{$rel};
1411         $data->[$index] = {%{$data->[$index]}, %$related};
1412       
1413         push @names, keys %$related if $index == 0;
1414       }
1415     }
1416
1417     ## do bulk insert on current row
1418     my @values = map { [ @$_{@names} ] } @$data;
1419
1420     $self->result_source->storage->insert_bulk(
1421       $self->result_source, 
1422       \@names, 
1423       \@values,
1424     );
1425
1426     ## do the has_many relationships
1427     foreach my $item (@$data) {
1428
1429       foreach my $rel (@rels) {
1430         next unless $item->{$rel} && ref $item->{$rel} eq "ARRAY";
1431
1432         my $parent = $self->find(map {{$_=>$item->{$_}} } @pks) 
1433      || $self->throw_exception('Cannot find the relating object.');
1434      
1435         my $child = $parent->$rel;
1436     
1437         my $related = $child->result_source->resolve_condition(
1438           $parent->result_source->relationship_info($rel)->{cond},
1439           $child,
1440           $parent,
1441         );
1442
1443         my @rows_to_add = ref $item->{$rel} eq 'ARRAY' ? @{$item->{$rel}} : ($item->{$rel});
1444         my @populate = map { {%$_, %$related} } @rows_to_add;
1445
1446         $child->populate( \@populate );
1447       }
1448     }
1449   }
1450 }
1451
1452 =head2 pager
1453
1454 =over 4
1455
1456 =item Arguments: none
1457
1458 =item Return Value: $pager
1459
1460 =back
1461
1462 Return Value a L<Data::Page> object for the current resultset. Only makes
1463 sense for queries with a C<page> attribute.
1464
1465 =cut
1466
1467 sub pager {
1468   my ($self) = @_;
1469   my $attrs = $self->{attrs};
1470   $self->throw_exception("Can't create pager for non-paged rs")
1471     unless $self->{attrs}{page};
1472   $attrs->{rows} ||= 10;
1473   return $self->{pager} ||= Data::Page->new(
1474     $self->_count, $attrs->{rows}, $self->{attrs}{page});
1475 }
1476
1477 =head2 page
1478
1479 =over 4
1480
1481 =item Arguments: $page_number
1482
1483 =item Return Value: $rs
1484
1485 =back
1486
1487 Returns a resultset for the $page_number page of the resultset on which page
1488 is called, where each page contains a number of rows equal to the 'rows'
1489 attribute set on the resultset (10 by default).
1490
1491 =cut
1492
1493 sub page {
1494   my ($self, $page) = @_;
1495   return (ref $self)->new($self->result_source, { %{$self->{attrs}}, page => $page });
1496 }
1497
1498 =head2 new_result
1499
1500 =over 4
1501
1502 =item Arguments: \%vals
1503
1504 =item Return Value: $object
1505
1506 =back
1507
1508 Creates a new row object in the resultset's result class and returns
1509 it. The row is not inserted into the database at this point, call
1510 L<DBIx::Class::Row/insert> to do that. Calling L<DBIx::Class::Row/in_storage>
1511 will tell you whether the row object has been inserted or not.
1512
1513 Passes the hashref of input on to L<DBIx::Class::Row/new>.
1514
1515 =cut
1516
1517 sub new_result {
1518   my ($self, $values) = @_;
1519   $self->throw_exception( "new_result needs a hash" )
1520     unless (ref $values eq 'HASH');
1521   $self->throw_exception(
1522     "Can't abstract implicit construct, condition not a hash"
1523   ) if ($self->{cond} && !(ref $self->{cond} eq 'HASH'));
1524
1525   my $alias = $self->{attrs}{alias};
1526   my $collapsed_cond = $self->{cond} ? $self->_collapse_cond($self->{cond}) : {};
1527
1528   # precendence must be given to passed values over values inherited from the cond, 
1529   # so the order here is important.
1530   my %new = (
1531     %{ $self->_remove_alias($collapsed_cond, $alias) },
1532     %{ $self->_remove_alias($values, $alias) },
1533     -source_handle => $self->_source_handle,
1534     -result_source => $self->result_source, # DO NOT REMOVE THIS, REQUIRED
1535   );
1536
1537   return $self->result_class->new(\%new);
1538 }
1539
1540 # _collapse_cond
1541 #
1542 # Recursively collapse the condition.
1543
1544 sub _collapse_cond {
1545   my ($self, $cond, $collapsed) = @_;
1546
1547   $collapsed ||= {};
1548
1549   if (ref $cond eq 'ARRAY') {
1550     foreach my $subcond (@$cond) {
1551       next unless ref $subcond;  # -or
1552 #      warn "ARRAY: " . Dumper $subcond;
1553       $collapsed = $self->_collapse_cond($subcond, $collapsed);
1554     }
1555   }
1556   elsif (ref $cond eq 'HASH') {
1557     if (keys %$cond and (keys %$cond)[0] eq '-and') {
1558       foreach my $subcond (@{$cond->{-and}}) {
1559 #        warn "HASH: " . Dumper $subcond;
1560         $collapsed = $self->_collapse_cond($subcond, $collapsed);
1561       }
1562     }
1563     else {
1564 #      warn "LEAF: " . Dumper $cond;
1565       foreach my $col (keys %$cond) {
1566         my $value = $cond->{$col};
1567         $collapsed->{$col} = $value;
1568       }
1569     }
1570   }
1571
1572   return $collapsed;
1573 }
1574
1575 # _remove_alias
1576 #
1577 # Remove the specified alias from the specified query hash. A copy is made so
1578 # the original query is not modified.
1579
1580 sub _remove_alias {
1581   my ($self, $query, $alias) = @_;
1582
1583   my %orig = %{ $query || {} };
1584   my %unaliased;
1585
1586   foreach my $key (keys %orig) {
1587     if ($key !~ /\./) {
1588       $unaliased{$key} = $orig{$key};
1589       next;
1590     }
1591     $unaliased{$1} = $orig{$key}
1592       if $key =~ m/^(?:\Q$alias\E\.)?([^.]+)$/;
1593   }
1594
1595   return \%unaliased;
1596 }
1597
1598 =head2 find_or_new
1599
1600 =over 4
1601
1602 =item Arguments: \%vals, \%attrs?
1603
1604 =item Return Value: $object
1605
1606 =back
1607
1608 Find an existing record from this resultset. If none exists, instantiate a new
1609 result object and return it. The object will not be saved into your storage
1610 until you call L<DBIx::Class::Row/insert> on it.
1611
1612 If you want objects to be saved immediately, use L</find_or_create> instead.
1613
1614 =cut
1615
1616 sub find_or_new {
1617   my $self     = shift;
1618   my $attrs    = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1619   my $hash     = ref $_[0] eq 'HASH' ? shift : {@_};
1620   my $exists   = $self->find($hash, $attrs);
1621   return defined $exists ? $exists : $self->new_result($hash);
1622 }
1623
1624 =head2 create
1625
1626 =over 4
1627
1628 =item Arguments: \%vals
1629
1630 =item Return Value: a L<DBIx::Class::Row> $object
1631
1632 =back
1633
1634 Attempt to create a single new row or a row with multiple related rows
1635 in the table represented by the resultset (and related tables). This
1636 will not check for duplicate rows before inserting, use
1637 L</find_or_create> to do that.
1638
1639 To create one row for this resultset, pass a hashref of key/value
1640 pairs representing the columns of the table and the values you wish to
1641 store. If the appropriate relationships are set up, foreign key fields
1642 can also be passed an object representing the foreign row, and the
1643 value will be set to it's primary key.
1644
1645 To create related objects, pass a hashref for the value if the related
1646 item is a foreign key relationship (L<DBIx::Class::Relationship/belongs_to>),
1647 and use the name of the relationship as the key. (NOT the name of the field,
1648 necessarily). For C<has_many> and C<has_one> relationships, pass an arrayref
1649 of hashrefs containing the data for each of the rows to create in the foreign
1650 tables, again using the relationship name as the key.
1651
1652 Instead of hashrefs of plain related data (key/value pairs), you may
1653 also pass new or inserted objects. New objects (not inserted yet, see
1654 L</new>), will be inserted into their appropriate tables.
1655
1656 Effectively a shortcut for C<< ->new_result(\%vals)->insert >>.
1657
1658 Example of creating a new row.
1659
1660   $person_rs->create({
1661     name=>"Some Person",
1662         email=>"somebody@someplace.com"
1663   });
1664   
1665 Example of creating a new row and also creating rows in a related C<has_many>
1666 or C<has_one> resultset.  Note Arrayref.
1667
1668   $artist_rs->create(
1669      { artistid => 4, name => 'Manufactured Crap', cds => [ 
1670         { title => 'My First CD', year => 2006 },
1671         { title => 'Yet More Tweeny-Pop crap', year => 2007 },
1672       ],
1673      },
1674   );
1675
1676 Example of creating a new row and also creating a row in a related
1677 C<belongs_to>resultset. Note Hashref.
1678
1679   $cd_rs->create({
1680     title=>"Music for Silly Walks",
1681         year=>2000,
1682         artist => {
1683           name=>"Silly Musician",
1684         }
1685   });
1686
1687 =cut
1688
1689 sub create {
1690   my ($self, $attrs) = @_;
1691   $self->throw_exception( "create needs a hashref" )
1692     unless ref $attrs eq 'HASH';
1693   return $self->new_result($attrs)->insert;
1694 }
1695
1696 =head2 find_or_create
1697
1698 =over 4
1699
1700 =item Arguments: \%vals, \%attrs?
1701
1702 =item Return Value: $object
1703
1704 =back
1705
1706   $class->find_or_create({ key => $val, ... });
1707
1708 Tries to find a record based on its primary key or unique constraint; if none
1709 is found, creates one and returns that instead.
1710
1711   my $cd = $schema->resultset('CD')->find_or_create({
1712     cdid   => 5,
1713     artist => 'Massive Attack',
1714     title  => 'Mezzanine',
1715     year   => 2005,
1716   });
1717
1718 Also takes an optional C<key> attribute, to search by a specific key or unique
1719 constraint. For example:
1720
1721   my $cd = $schema->resultset('CD')->find_or_create(
1722     {
1723       artist => 'Massive Attack',
1724       title  => 'Mezzanine',
1725     },
1726     { key => 'cd_artist_title' }
1727   );
1728
1729 See also L</find> and L</update_or_create>. For information on how to declare
1730 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1731
1732 =cut
1733
1734 sub find_or_create {
1735   my $self     = shift;
1736   my $attrs    = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1737   my $hash     = ref $_[0] eq 'HASH' ? shift : {@_};
1738   my $exists   = $self->find($hash, $attrs);
1739   return defined $exists ? $exists : $self->create($hash);
1740 }
1741
1742 =head2 update_or_create
1743
1744 =over 4
1745
1746 =item Arguments: \%col_values, { key => $unique_constraint }?
1747
1748 =item Return Value: $object
1749
1750 =back
1751
1752   $class->update_or_create({ col => $val, ... });
1753
1754 First, searches for an existing row matching one of the unique constraints
1755 (including the primary key) on the source of this resultset. If a row is
1756 found, updates it with the other given column values. Otherwise, creates a new
1757 row.
1758
1759 Takes an optional C<key> attribute to search on a specific unique constraint.
1760 For example:
1761
1762   # In your application
1763   my $cd = $schema->resultset('CD')->update_or_create(
1764     {
1765       artist => 'Massive Attack',
1766       title  => 'Mezzanine',
1767       year   => 1998,
1768     },
1769     { key => 'cd_artist_title' }
1770   );
1771
1772 If no C<key> is specified, it searches on all unique constraints defined on the
1773 source, including the primary key.
1774
1775 If the C<key> is specified as C<primary>, it searches only on the primary key.
1776
1777 See also L</find> and L</find_or_create>. For information on how to declare
1778 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1779
1780 =cut
1781
1782 sub update_or_create {
1783   my $self = shift;
1784   my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1785   my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
1786
1787   my $row = $self->find($cond, $attrs);
1788   if (defined $row) {
1789     $row->update($cond);
1790     return $row;
1791   }
1792
1793   return $self->create($cond);
1794 }
1795
1796 =head2 get_cache
1797
1798 =over 4
1799
1800 =item Arguments: none
1801
1802 =item Return Value: \@cache_objects?
1803
1804 =back
1805
1806 Gets the contents of the cache for the resultset, if the cache is set.
1807
1808 The cache is populated either by using the L</prefetch> attribute to
1809 L</search> or by calling L</set_cache>.
1810
1811 =cut
1812
1813 sub get_cache {
1814   shift->{all_cache};
1815 }
1816
1817 =head2 set_cache
1818
1819 =over 4
1820
1821 =item Arguments: \@cache_objects
1822
1823 =item Return Value: \@cache_objects
1824
1825 =back
1826
1827 Sets the contents of the cache for the resultset. Expects an arrayref
1828 of objects of the same class as those produced by the resultset. Note that
1829 if the cache is set the resultset will return the cached objects rather
1830 than re-querying the database even if the cache attr is not set.
1831
1832 The contents of the cache can also be populated by using the
1833 L</prefetch> attribute to L</search>.
1834
1835 =cut
1836
1837 sub set_cache {
1838   my ( $self, $data ) = @_;
1839   $self->throw_exception("set_cache requires an arrayref")
1840       if defined($data) && (ref $data ne 'ARRAY');
1841   $self->{all_cache} = $data;
1842 }
1843
1844 =head2 clear_cache
1845
1846 =over 4
1847
1848 =item Arguments: none
1849
1850 =item Return Value: []
1851
1852 =back
1853
1854 Clears the cache for the resultset.
1855
1856 =cut
1857
1858 sub clear_cache {
1859   shift->set_cache(undef);
1860 }
1861
1862 =head2 related_resultset
1863
1864 =over 4
1865
1866 =item Arguments: $relationship_name
1867
1868 =item Return Value: $resultset
1869
1870 =back
1871
1872 Returns a related resultset for the supplied relationship name.
1873
1874   $artist_rs = $schema->resultset('CD')->related_resultset('Artist');
1875
1876 =cut
1877
1878 sub related_resultset {
1879   my ($self, $rel) = @_;
1880
1881   $self->{related_resultsets} ||= {};
1882   return $self->{related_resultsets}{$rel} ||= do {
1883     my $rel_obj = $self->result_source->relationship_info($rel);
1884
1885     $self->throw_exception(
1886       "search_related: result source '" . $self->result_source->source_name .
1887         "' has no such relationship $rel")
1888       unless $rel_obj;
1889     
1890     my ($from,$seen) = $self->_resolve_from($rel);
1891
1892     my $join_count = $seen->{$rel};
1893     my $alias = ($join_count > 1 ? join('_', $rel, $join_count) : $rel);
1894
1895     #XXX - temp fix for result_class bug. There likely is a more elegant fix -groditi
1896     my %attrs = %{$self->{attrs}||{}};
1897     delete @attrs{qw(result_class alias)};
1898
1899     my $new_cache;
1900
1901     if (my $cache = $self->get_cache) {
1902       if ($cache->[0] && $cache->[0]->related_resultset($rel)->get_cache) {
1903         $new_cache = [ map { @{$_->related_resultset($rel)->get_cache} }
1904                         @$cache ];
1905       }
1906     }
1907
1908     my $rel_source = $self->result_source->related_source($rel);
1909
1910     my $new = do {
1911
1912       # The reason we do this now instead of passing the alias to the
1913       # search_rs below is that if you wrap/overload resultset on the
1914       # source you need to know what alias it's -going- to have for things
1915       # to work sanely (e.g. RestrictWithObject wants to be able to add
1916       # extra query restrictions, and these may need to be $alias.)
1917
1918       my $attrs = $rel_source->resultset_attributes;
1919       local $attrs->{alias} = $alias;
1920
1921       $rel_source->resultset
1922                  ->search_rs(
1923                      undef, {
1924                        %attrs,
1925                        join => undef,
1926                        prefetch => undef,
1927                        select => undef,
1928                        as => undef,
1929                        where => $self->{cond},
1930                        seen_join => $seen,
1931                        from => $from,
1932                    });
1933     };
1934     $new->set_cache($new_cache) if $new_cache;
1935     $new;
1936   };
1937 }
1938
1939 sub _resolve_from {
1940   my ($self, $extra_join) = @_;
1941   my $source = $self->result_source;
1942   my $attrs = $self->{attrs};
1943   
1944   my $from = $attrs->{from}
1945     || [ { $attrs->{alias} => $source->from } ];
1946     
1947   my $seen = { %{$attrs->{seen_join}||{}} };
1948
1949   my $join = ($attrs->{join}
1950                ? [ $attrs->{join}, $extra_join ]
1951                : $extra_join);
1952
1953   # we need to take the prefetch the attrs into account before we 
1954   # ->resolve_join as otherwise they get lost - captainL
1955   my $merged = $self->_merge_attr( $join, $attrs->{prefetch} );
1956
1957   $from = [
1958     @$from,
1959     ($join ? $source->resolve_join($merged, $attrs->{alias}, $seen) : ()),
1960   ];
1961
1962   return ($from,$seen);
1963 }
1964
1965 sub _resolved_attrs {
1966   my $self = shift;
1967   return $self->{_attrs} if $self->{_attrs};
1968
1969   my $attrs = { %{$self->{attrs}||{}} };
1970   my $source = $self->result_source;
1971   my $alias = $attrs->{alias};
1972
1973   $attrs->{columns} ||= delete $attrs->{cols} if exists $attrs->{cols};
1974   if ($attrs->{columns}) {
1975     delete $attrs->{as};
1976   } elsif (!$attrs->{select}) {
1977     $attrs->{columns} = [ $source->columns ];
1978   }
1979  
1980   $attrs->{select} = 
1981     ($attrs->{select}
1982       ? (ref $attrs->{select} eq 'ARRAY'
1983           ? [ @{$attrs->{select}} ]
1984           : [ $attrs->{select} ])
1985       : [ map { m/\./ ? $_ : "${alias}.$_" } @{delete $attrs->{columns}} ]
1986     );
1987   $attrs->{as} =
1988     ($attrs->{as}
1989       ? (ref $attrs->{as} eq 'ARRAY'
1990           ? [ @{$attrs->{as}} ]
1991           : [ $attrs->{as} ])
1992       : [ map { m/^\Q${alias}.\E(.+)$/ ? $1 : $_ } @{$attrs->{select}} ]
1993     );
1994   
1995   my $adds;
1996   if ($adds = delete $attrs->{include_columns}) {
1997     $adds = [$adds] unless ref $adds eq 'ARRAY';
1998     push(@{$attrs->{select}}, @$adds);
1999     push(@{$attrs->{as}}, map { m/([^.]+)$/; $1 } @$adds);
2000   }
2001   if ($adds = delete $attrs->{'+select'}) {
2002     $adds = [$adds] unless ref $adds eq 'ARRAY';
2003     push(@{$attrs->{select}},
2004            map { /\./ || ref $_ ? $_ : "${alias}.$_" } @$adds);
2005   }
2006   if (my $adds = delete $attrs->{'+as'}) {
2007     $adds = [$adds] unless ref $adds eq 'ARRAY';
2008     push(@{$attrs->{as}}, @$adds);
2009   }
2010
2011   $attrs->{from} ||= [ { 'me' => $source->from } ];
2012
2013   if (exists $attrs->{join} || exists $attrs->{prefetch}) {
2014     my $join = delete $attrs->{join} || {};
2015
2016     if (defined $attrs->{prefetch}) {
2017       $join = $self->_merge_attr(
2018         $join, $attrs->{prefetch}
2019       );
2020       
2021     }
2022
2023     $attrs->{from} =   # have to copy here to avoid corrupting the original
2024       [
2025         @{$attrs->{from}}, 
2026         $source->resolve_join($join, $alias, { %{$attrs->{seen_join}||{}} })
2027       ];
2028
2029   }
2030
2031   $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct};
2032   if ($attrs->{order_by}) {
2033     $attrs->{order_by} = (ref($attrs->{order_by}) eq 'ARRAY'
2034                            ? [ @{$attrs->{order_by}} ]
2035                            : [ $attrs->{order_by} ]);
2036   } else {
2037     $attrs->{order_by} = [];    
2038   }
2039
2040   my $collapse = $attrs->{collapse} || {};
2041   if (my $prefetch = delete $attrs->{prefetch}) {
2042     $prefetch = $self->_merge_attr({}, $prefetch);
2043     my @pre_order;
2044     my $seen = $attrs->{seen_join} || {};
2045     foreach my $p (ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch)) {
2046       # bring joins back to level of current class
2047       my @prefetch = $source->resolve_prefetch(
2048         $p, $alias, $seen, \@pre_order, $collapse
2049       );
2050       push(@{$attrs->{select}}, map { $_->[0] } @prefetch);
2051       push(@{$attrs->{as}}, map { $_->[1] } @prefetch);
2052     }
2053     push(@{$attrs->{order_by}}, @pre_order);
2054   }
2055   $attrs->{collapse} = $collapse;
2056
2057   if ($attrs->{page}) {
2058     $attrs->{offset} ||= 0;
2059     $attrs->{offset} += ($attrs->{rows} * ($attrs->{page} - 1));
2060   }
2061
2062   return $self->{_attrs} = $attrs;
2063 }
2064
2065 sub _rollout_attr {
2066   my ($self, $attr) = @_;
2067   
2068   if (ref $attr eq 'HASH') {
2069     return $self->_rollout_hash($attr);
2070   } elsif (ref $attr eq 'ARRAY') {
2071     return $self->_rollout_array($attr);
2072   } else {
2073     return [$attr];
2074   }
2075 }
2076
2077 sub _rollout_array {
2078   my ($self, $attr) = @_;
2079
2080   my @rolled_array;
2081   foreach my $element (@{$attr}) {
2082     if (ref $element eq 'HASH') {
2083       push( @rolled_array, @{ $self->_rollout_hash( $element ) } );
2084     } elsif (ref $element eq 'ARRAY') {
2085       #  XXX - should probably recurse here
2086       push( @rolled_array, @{$self->_rollout_array($element)} );
2087     } else {
2088       push( @rolled_array, $element );
2089     }
2090   }
2091   return \@rolled_array;
2092 }
2093
2094 sub _rollout_hash {
2095   my ($self, $attr) = @_;
2096
2097   my @rolled_array;
2098   foreach my $key (keys %{$attr}) {
2099     push( @rolled_array, { $key => $attr->{$key} } );
2100   }
2101   return \@rolled_array;
2102 }
2103
2104 sub _calculate_score {
2105   my ($self, $a, $b) = @_;
2106
2107   if (ref $b eq 'HASH') {
2108     my ($b_key) = keys %{$b};
2109     if (ref $a eq 'HASH') {
2110       my ($a_key) = keys %{$a};
2111       if ($a_key eq $b_key) {
2112         return (1 + $self->_calculate_score( $a->{$a_key}, $b->{$b_key} ));
2113       } else {
2114         return 0;
2115       }
2116     } else {
2117       return ($a eq $b_key) ? 1 : 0;
2118     }       
2119   } else {
2120     if (ref $a eq 'HASH') {
2121       my ($a_key) = keys %{$a};
2122       return ($b eq $a_key) ? 1 : 0;
2123     } else {
2124       return ($b eq $a) ? 1 : 0;
2125     }
2126   }
2127 }
2128
2129 sub _merge_attr {
2130   my ($self, $a, $b) = @_;
2131
2132   return $b unless defined($a);
2133   return $a unless defined($b);
2134   
2135   $a = $self->_rollout_attr($a);
2136   $b = $self->_rollout_attr($b);
2137
2138   my $seen_keys;
2139   foreach my $b_element ( @{$b} ) {
2140     # find best candidate from $a to merge $b_element into
2141     my $best_candidate = { position => undef, score => 0 }; my $position = 0;
2142     foreach my $a_element ( @{$a} ) {
2143       my $score = $self->_calculate_score( $a_element, $b_element );
2144       if ($score > $best_candidate->{score}) {
2145         $best_candidate->{position} = $position;
2146         $best_candidate->{score} = $score;
2147       }
2148       $position++;
2149     }
2150     my ($b_key) = ( ref $b_element eq 'HASH' ) ? keys %{$b_element} : ($b_element);
2151
2152     if ($best_candidate->{score} == 0 || exists $seen_keys->{$b_key}) {
2153       push( @{$a}, $b_element );
2154     } else {
2155       my $a_best = $a->[$best_candidate->{position}];
2156       # merge a_best and b_element together and replace original with merged
2157       if (ref $a_best ne 'HASH') {
2158         $a->[$best_candidate->{position}] = $b_element;
2159       } elsif (ref $b_element eq 'HASH') {
2160         my ($key) = keys %{$a_best};
2161         $a->[$best_candidate->{position}] = { $key => $self->_merge_attr($a_best->{$key}, $b_element->{$key}) };
2162       }
2163     }
2164     $seen_keys->{$b_key} = 1; # don't merge the same key twice
2165   }
2166
2167   return $a;
2168 }
2169
2170 sub result_source {
2171     my $self = shift;
2172
2173     if (@_) {
2174         $self->_source_handle($_[0]->handle);
2175     } else {
2176         $self->_source_handle->resolve;
2177     }
2178 }
2179
2180 =head2 throw_exception
2181
2182 See L<DBIx::Class::Schema/throw_exception> for details.
2183
2184 =cut
2185
2186 sub throw_exception {
2187   my $self=shift;
2188   if (ref $self && $self->_source_handle->schema) {
2189     $self->_source_handle->schema->throw_exception(@_)
2190   } else {
2191     croak(@_);
2192   }
2193
2194 }
2195
2196 # XXX: FIXME: Attributes docs need clearing up
2197
2198 =head1 ATTRIBUTES
2199
2200 The resultset takes various attributes that modify its behavior. Here's an
2201 overview of them:
2202
2203 =head2 order_by
2204
2205 =over 4
2206
2207 =item Value: ($order_by | \@order_by)
2208
2209 =back
2210
2211 Which column(s) to order the results by. This is currently passed
2212 through directly to SQL, so you can give e.g. C<year DESC> for a
2213 descending order on the column `year'.
2214
2215 Please note that if you have C<quote_char> enabled (see
2216 L<DBIx::Class::Storage::DBI/connect_info>) you will need to do C<\'year DESC' > to
2217 specify an order. (The scalar ref causes it to be passed as raw sql to the DB,
2218 so you will need to manually quote things as appropriate.)
2219
2220 =head2 columns
2221
2222 =over 4
2223
2224 =item Value: \@columns
2225
2226 =back
2227
2228 Shortcut to request a particular set of columns to be retrieved.  Adds
2229 C<me.> onto the start of any column without a C<.> in it and sets C<select>
2230 from that, then auto-populates C<as> from C<select> as normal. (You may also
2231 use the C<cols> attribute, as in earlier versions of DBIC.)
2232
2233 =head2 include_columns
2234
2235 =over 4
2236
2237 =item Value: \@columns
2238
2239 =back
2240
2241 Shortcut to include additional columns in the returned results - for example
2242
2243   $schema->resultset('CD')->search(undef, {
2244     include_columns => ['artist.name'],
2245     join => ['artist']
2246   });
2247
2248 would return all CDs and include a 'name' column to the information
2249 passed to object inflation. Note that the 'artist' is the name of the
2250 column (or relationship) accessor, and 'name' is the name of the column
2251 accessor in the related table.
2252
2253 =head2 select
2254
2255 =over 4
2256
2257 =item Value: \@select_columns
2258
2259 =back
2260
2261 Indicates which columns should be selected from the storage. You can use
2262 column names, or in the case of RDBMS back ends, function or stored procedure
2263 names:
2264
2265   $rs = $schema->resultset('Employee')->search(undef, {
2266     select => [
2267       'name',
2268       { count => 'employeeid' },
2269       { sum => 'salary' }
2270     ]
2271   });
2272
2273 When you use function/stored procedure names and do not supply an C<as>
2274 attribute, the column names returned are storage-dependent. E.g. MySQL would
2275 return a column named C<count(employeeid)> in the above example.
2276
2277 =head2 +select
2278
2279 =over 4
2280
2281 Indicates additional columns to be selected from storage.  Works the same as
2282 L</select> but adds columns to the selection.
2283
2284 =back
2285
2286 =head2 +as
2287
2288 =over 4
2289
2290 Indicates additional column names for those added via L</+select>.
2291
2292 =back
2293
2294 =head2 as
2295
2296 =over 4
2297
2298 =item Value: \@inflation_names
2299
2300 =back
2301
2302 Indicates column names for object inflation. That is, C<as>
2303 indicates the name that the column can be accessed as via the
2304 C<get_column> method (or via the object accessor, B<if one already
2305 exists>).  It has nothing to do with the SQL code C<SELECT foo AS bar>.
2306
2307 The C<as> attribute is used in conjunction with C<select>,
2308 usually when C<select> contains one or more function or stored
2309 procedure names:
2310
2311   $rs = $schema->resultset('Employee')->search(undef, {
2312     select => [
2313       'name',
2314       { count => 'employeeid' }
2315     ],
2316     as => ['name', 'employee_count'],
2317   });
2318
2319   my $employee = $rs->first(); # get the first Employee
2320
2321 If the object against which the search is performed already has an accessor
2322 matching a column name specified in C<as>, the value can be retrieved using
2323 the accessor as normal:
2324
2325   my $name = $employee->name();
2326
2327 If on the other hand an accessor does not exist in the object, you need to
2328 use C<get_column> instead:
2329
2330   my $employee_count = $employee->get_column('employee_count');
2331
2332 You can create your own accessors if required - see
2333 L<DBIx::Class::Manual::Cookbook> for details.
2334
2335 Please note: This will NOT insert an C<AS employee_count> into the SQL
2336 statement produced, it is used for internal access only. Thus
2337 attempting to use the accessor in an C<order_by> clause or similar
2338 will fail miserably.
2339
2340 To get around this limitation, you can supply literal SQL to your
2341 C<select> attibute that contains the C<AS alias> text, eg:
2342
2343   select => [\'myfield AS alias']
2344
2345 =head2 join
2346
2347 =over 4
2348
2349 =item Value: ($rel_name | \@rel_names | \%rel_names)
2350
2351 =back
2352
2353 Contains a list of relationships that should be joined for this query.  For
2354 example:
2355
2356   # Get CDs by Nine Inch Nails
2357   my $rs = $schema->resultset('CD')->search(
2358     { 'artist.name' => 'Nine Inch Nails' },
2359     { join => 'artist' }
2360   );
2361
2362 Can also contain a hash reference to refer to the other relation's relations.
2363 For example:
2364
2365   package MyApp::Schema::Track;
2366   use base qw/DBIx::Class/;
2367   __PACKAGE__->table('track');
2368   __PACKAGE__->add_columns(qw/trackid cd position title/);
2369   __PACKAGE__->set_primary_key('trackid');
2370   __PACKAGE__->belongs_to(cd => 'MyApp::Schema::CD');
2371   1;
2372
2373   # In your application
2374   my $rs = $schema->resultset('Artist')->search(
2375     { 'track.title' => 'Teardrop' },
2376     {
2377       join     => { cd => 'track' },
2378       order_by => 'artist.name',
2379     }
2380   );
2381
2382 You need to use the relationship (not the table) name in  conditions, 
2383 because they are aliased as such. The current table is aliased as "me", so 
2384 you need to use me.column_name in order to avoid ambiguity. For example:
2385
2386   # Get CDs from 1984 with a 'Foo' track 
2387   my $rs = $schema->resultset('CD')->search(
2388     { 
2389       'me.year' => 1984,
2390       'tracks.name' => 'Foo'
2391     },
2392     { join => 'tracks' }
2393   );
2394   
2395 If the same join is supplied twice, it will be aliased to <rel>_2 (and
2396 similarly for a third time). For e.g.
2397
2398   my $rs = $schema->resultset('Artist')->search({
2399     'cds.title'   => 'Down to Earth',
2400     'cds_2.title' => 'Popular',
2401   }, {
2402     join => [ qw/cds cds/ ],
2403   });
2404
2405 will return a set of all artists that have both a cd with title 'Down
2406 to Earth' and a cd with title 'Popular'.
2407
2408 If you want to fetch related objects from other tables as well, see C<prefetch>
2409 below.
2410
2411 For more help on using joins with search, see L<DBIx::Class::Manual::Joining>.
2412
2413 =head2 prefetch
2414
2415 =over 4
2416
2417 =item Value: ($rel_name | \@rel_names | \%rel_names)
2418
2419 =back
2420
2421 Contains one or more relationships that should be fetched along with
2422 the main query (when they are accessed afterwards the data will
2423 already be available, without extra queries to the database).  This is
2424 useful for when you know you will need the related objects, because it
2425 saves at least one query:
2426
2427   my $rs = $schema->resultset('Tag')->search(
2428     undef,
2429     {
2430       prefetch => {
2431         cd => 'artist'
2432       }
2433     }
2434   );
2435
2436 The initial search results in SQL like the following:
2437
2438   SELECT tag.*, cd.*, artist.* FROM tag
2439   JOIN cd ON tag.cd = cd.cdid
2440   JOIN artist ON cd.artist = artist.artistid
2441
2442 L<DBIx::Class> has no need to go back to the database when we access the
2443 C<cd> or C<artist> relationships, which saves us two SQL statements in this
2444 case.
2445
2446 Simple prefetches will be joined automatically, so there is no need
2447 for a C<join> attribute in the above search. 
2448
2449 C<prefetch> can be used with the following relationship types: C<belongs_to>,
2450 C<has_one> (or if you're using C<add_relationship>, any relationship declared
2451 with an accessor type of 'single' or 'filter'). A more complex example that
2452 prefetches an artists cds, the tracks on those cds, and the tags associted 
2453 with that artist is given below (assuming many-to-many from artists to tags):
2454
2455  my $rs = $schema->resultset('Artist')->search(
2456    undef,
2457    {
2458      prefetch => [
2459        { cds => 'tracks' },
2460        { artist_tags => 'tags' }
2461      ]
2462    }
2463  );
2464  
2465
2466 B<NOTE:> If you specify a C<prefetch> attribute, the C<join> and C<select>
2467 attributes will be ignored.
2468
2469 =head2 page
2470
2471 =over 4
2472
2473 =item Value: $page
2474
2475 =back
2476
2477 Makes the resultset paged and specifies the page to retrieve. Effectively
2478 identical to creating a non-pages resultset and then calling ->page($page)
2479 on it.
2480
2481 If L<rows> attribute is not specified it defualts to 10 rows per page.
2482
2483 =head2 rows
2484
2485 =over 4
2486
2487 =item Value: $rows
2488
2489 =back
2490
2491 Specifes the maximum number of rows for direct retrieval or the number of
2492 rows per page if the page attribute or method is used.
2493
2494 =head2 offset
2495
2496 =over 4
2497
2498 =item Value: $offset
2499
2500 =back
2501
2502 Specifies the (zero-based) row number for the  first row to be returned, or the
2503 of the first row of the first page if paging is used.
2504
2505 =head2 group_by
2506
2507 =over 4
2508
2509 =item Value: \@columns
2510
2511 =back
2512
2513 A arrayref of columns to group by. Can include columns of joined tables.
2514
2515   group_by => [qw/ column1 column2 ... /]
2516
2517 =head2 having
2518
2519 =over 4
2520
2521 =item Value: $condition
2522
2523 =back
2524
2525 HAVING is a select statement attribute that is applied between GROUP BY and
2526 ORDER BY. It is applied to the after the grouping calculations have been
2527 done.
2528
2529   having => { 'count(employee)' => { '>=', 100 } }
2530
2531 =head2 distinct
2532
2533 =over 4
2534
2535 =item Value: (0 | 1)
2536
2537 =back
2538
2539 Set to 1 to group by all columns.
2540
2541 =head2 where
2542
2543 =over 4
2544
2545 Adds to the WHERE clause.
2546
2547   # only return rows WHERE deleted IS NULL for all searches
2548   __PACKAGE__->resultset_attributes({ where => { deleted => undef } }); )
2549
2550 Can be overridden by passing C<{ where => undef }> as an attribute
2551 to a resulset.
2552
2553 =back
2554
2555 =head2 cache
2556
2557 Set to 1 to cache search results. This prevents extra SQL queries if you
2558 revisit rows in your ResultSet:
2559
2560   my $resultset = $schema->resultset('Artist')->search( undef, { cache => 1 } );
2561
2562   while( my $artist = $resultset->next ) {
2563     ... do stuff ...
2564   }
2565
2566   $rs->first; # without cache, this would issue a query
2567
2568 By default, searches are not cached.
2569
2570 For more examples of using these attributes, see
2571 L<DBIx::Class::Manual::Cookbook>.
2572
2573 =head2 from
2574
2575 =over 4
2576
2577 =item Value: \@from_clause
2578
2579 =back
2580
2581 The C<from> attribute gives you manual control over the C<FROM> clause of SQL
2582 statements generated by L<DBIx::Class>, allowing you to express custom C<JOIN>
2583 clauses.
2584
2585 NOTE: Use this on your own risk.  This allows you to shoot off your foot!
2586
2587 C<join> will usually do what you need and it is strongly recommended that you
2588 avoid using C<from> unless you cannot achieve the desired result using C<join>.
2589 And we really do mean "cannot", not just tried and failed. Attempting to use
2590 this because you're having problems with C<join> is like trying to use x86
2591 ASM because you've got a syntax error in your C. Trust us on this.
2592
2593 Now, if you're still really, really sure you need to use this (and if you're
2594 not 100% sure, ask the mailing list first), here's an explanation of how this
2595 works.
2596
2597 The syntax is as follows -
2598
2599   [
2600     { <alias1> => <table1> },
2601     [
2602       { <alias2> => <table2>, -join_type => 'inner|left|right' },
2603       [], # nested JOIN (optional)
2604       { <table1.column1> => <table2.column2>, ... (more conditions) },
2605     ],
2606     # More of the above [ ] may follow for additional joins
2607   ]
2608
2609   <table1> <alias1>
2610   JOIN
2611     <table2> <alias2>
2612     [JOIN ...]
2613   ON <table1.column1> = <table2.column2>
2614   <more joins may follow>
2615
2616 An easy way to follow the examples below is to remember the following:
2617
2618     Anything inside "[]" is a JOIN
2619     Anything inside "{}" is a condition for the enclosing JOIN
2620
2621 The following examples utilize a "person" table in a family tree application.
2622 In order to express parent->child relationships, this table is self-joined:
2623
2624     # Person->belongs_to('father' => 'Person');
2625     # Person->belongs_to('mother' => 'Person');
2626
2627 C<from> can be used to nest joins. Here we return all children with a father,
2628 then search against all mothers of those children:
2629
2630   $rs = $schema->resultset('Person')->search(
2631       undef,
2632       {
2633           alias => 'mother', # alias columns in accordance with "from"
2634           from => [
2635               { mother => 'person' },
2636               [
2637                   [
2638                       { child => 'person' },
2639                       [
2640                           { father => 'person' },
2641                           { 'father.person_id' => 'child.father_id' }
2642                       ]
2643                   ],
2644                   { 'mother.person_id' => 'child.mother_id' }
2645               ],
2646           ]
2647       },
2648   );
2649
2650   # Equivalent SQL:
2651   # SELECT mother.* FROM person mother
2652   # JOIN (
2653   #   person child
2654   #   JOIN person father
2655   #   ON ( father.person_id = child.father_id )
2656   # )
2657   # ON ( mother.person_id = child.mother_id )
2658
2659 The type of any join can be controlled manually. To search against only people
2660 with a father in the person table, we could explicitly use C<INNER JOIN>:
2661
2662     $rs = $schema->resultset('Person')->search(
2663         undef,
2664         {
2665             alias => 'child', # alias columns in accordance with "from"
2666             from => [
2667                 { child => 'person' },
2668                 [
2669                     { father => 'person', -join_type => 'inner' },
2670                     { 'father.id' => 'child.father_id' }
2671                 ],
2672             ]
2673         },
2674     );
2675
2676     # Equivalent SQL:
2677     # SELECT child.* FROM person child
2678     # INNER JOIN person father ON child.father_id = father.id
2679
2680 =head2 for
2681
2682 =over 4
2683
2684 =item Value: ( 'update' | 'shared' )
2685
2686 =back
2687
2688 Set to 'update' for a SELECT ... FOR UPDATE or 'shared' for a SELECT
2689 ... FOR SHARED.
2690
2691 =cut
2692
2693 1;