1 package DBIx::Class::ResultSet;
9 use Carp::Clan qw/^DBIx::Class/;
13 use Scalar::Util qw/weaken/;
15 use DBIx::Class::ResultSetColumn;
16 use base qw/DBIx::Class/;
17 __PACKAGE__->load_components(qw/AccessorGroup/);
18 __PACKAGE__->mk_group_accessors('simple' => qw/result_source result_class/);
22 DBIx::Class::ResultSet - Responsible for fetching and creating resultset.
26 my $rs = $schema->resultset('User')->search(registered => 1);
27 my @rows = $schema->resultset('CD')->search(year => 2005);
31 The resultset is also known as an iterator. It is responsible for handling
32 queries that may return an arbitrary number of rows, e.g. via L</search>
33 or a C<has_many> relationship.
35 In the examples below, the following table classes are used:
37 package MyApp::Schema::Artist;
38 use base qw/DBIx::Class/;
39 __PACKAGE__->load_components(qw/Core/);
40 __PACKAGE__->table('artist');
41 __PACKAGE__->add_columns(qw/artistid name/);
42 __PACKAGE__->set_primary_key('artistid');
43 __PACKAGE__->has_many(cds => 'MyApp::Schema::CD');
46 package MyApp::Schema::CD;
47 use base qw/DBIx::Class/;
48 __PACKAGE__->load_components(qw/Core/);
49 __PACKAGE__->table('cd');
50 __PACKAGE__->add_columns(qw/cdid artist title year/);
51 __PACKAGE__->set_primary_key('cdid');
52 __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Artist');
61 =item Arguments: $source, \%$attrs
63 =item Return Value: $rs
67 The resultset constructor. Takes a source object (usually a
68 L<DBIx::Class::ResultSourceProxy::Table>) and an attribute hash (see
69 L</ATTRIBUTES> below). Does not perform any queries -- these are
70 executed as needed by the other methods.
72 Generally you won't need to construct a resultset manually. You'll
73 automatically get one from e.g. a L</search> called in scalar context:
75 my $rs = $schema->resultset('CD')->search({ title => '100th Window' });
77 IMPORTANT: If called on an object, proxies to new_result instead so
79 my $cd = $schema->resultset('CD')->new({ title => 'Spoon' });
81 will return a CD object, not a ResultSet.
87 return $class->new_result(@_) if ref $class;
89 my ($source, $attrs) = @_;
93 $attrs->{rows} ||= 10;
94 $attrs->{offset} ||= 0;
95 $attrs->{offset} += ($attrs->{rows} * ($attrs->{page} - 1));
98 $attrs->{alias} ||= 'me';
101 result_source => $source,
102 result_class => $attrs->{result_class} || $source->result_class,
103 cond => $attrs->{where},
104 # from => $attrs->{from},
105 # collapse => $collapse,
107 page => delete $attrs->{page},
117 =item Arguments: $cond, \%attrs?
119 =item Return Value: $resultset (scalar context), @row_objs (list context)
123 my @cds = $cd_rs->search({ year => 2001 }); # "... WHERE year = 2001"
124 my $new_rs = $cd_rs->search({ year => 2005 });
126 my $new_rs = $cd_rs->search([ { year => 2005 }, { year => 2004 } ]);
127 # year = 2005 OR year = 2004
129 If you need to pass in additional attributes but no additional condition,
130 call it as C<search(undef, \%attrs)>.
132 # "SELECT name, artistid FROM $artist_table"
133 my @all_artists = $schema->resultset('Artist')->search(undef, {
134 columns => [qw/name artistid/],
141 my $rs = $self->search_rs( @_ );
142 return (wantarray ? $rs->all : $rs);
149 =item Arguments: $cond, \%attrs?
151 =item Return Value: $resultset
155 This method does the same exact thing as search() except it will
156 always return a resultset, even in list context.
163 my $our_attrs = { %{$self->{attrs}} };
164 my $having = delete $our_attrs->{having};
166 $attrs = pop(@_) if @_ > 1 and ref $_[$#_] eq 'HASH';
168 # merge new attrs into old
169 foreach my $key (qw/join prefetch/) {
170 next unless (exists $attrs->{$key});
171 if (exists $our_attrs->{$key}) {
172 $our_attrs->{$key} = $self->_merge_attr($our_attrs->{$key}, $attrs->{$key});
174 $our_attrs->{$key} = $attrs->{$key};
176 delete $attrs->{$key};
179 if (exists $our_attrs->{prefetch}) {
180 $our_attrs->{join} = $self->_merge_attr($our_attrs->{join}, $our_attrs->{prefetch}, 1);
183 my $new_attrs = { %{$our_attrs}, %{$attrs} };
185 # merge new where and having into old
187 ? ((@_ == 1 || ref $_[0] eq "HASH")
190 ? $self->throw_exception(
191 "Odd number of arguments to search")
194 if (defined $where) {
195 $new_attrs->{where} = (defined $new_attrs->{where}
197 [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
198 $where, $new_attrs->{where} ] }
202 if (defined $having) {
203 $new_attrs->{having} = (defined $new_attrs->{having}
205 [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
206 $having, $new_attrs->{having} ] }
210 my $rs = (ref $self)->new($self->result_source, $new_attrs);
211 $rs->{_parent_rs} = $self->{_parent_rs} if ($self->{_parent_rs}); #XXX - hack to pass through parent of related resultsets
213 unless (@_) { # no search, effectively just a clone
214 my $rows = $self->get_cache;
216 $rs->set_cache($rows);
223 =head2 search_literal
227 =item Arguments: $sql_fragment, @bind_values
229 =item Return Value: $resultset (scalar context), @row_objs (list context)
233 my @cds = $cd_rs->search_literal('year = ? AND title = ?', qw/2001 Reload/);
234 my $newrs = $artist_rs->search_literal('name = ?', 'Metallica');
236 Pass a literal chunk of SQL to be added to the conditional part of the
242 my ($self, $cond, @vals) = @_;
243 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
244 $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ];
245 return $self->search(\$cond, $attrs);
252 =item Arguments: @values | \%cols, \%attrs?
254 =item Return Value: $row_object
258 Finds a row based on its primary key or unique constraint. For example, to find
259 a row by its primary key:
261 my $cd = $schema->resultset('CD')->find(5);
263 You can also find a row by a specific unique constraint using the C<key>
264 attribute. For example:
266 my $cd = $schema->resultset('CD')->find('Massive Attack', 'Mezzanine', { key => 'artist_title' });
268 Additionally, you can specify the columns explicitly by name:
270 my $cd = $schema->resultset('CD')->find(
272 artist => 'Massive Attack',
273 title => 'Mezzanine',
275 { key => 'artist_title' }
278 If the C<key> is specified as C<primary>, it searches only on the primary key.
280 If no C<key> is specified, it searches on all unique constraints defined on the
281 source, including the primary key.
283 See also L</find_or_create> and L</update_or_create>. For information on how to
284 declare unique constraints, see
285 L<DBIx::Class::ResultSource/add_unique_constraint>.
291 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
293 # Default to the primary key, but allow a specific key
294 my @cols = exists $attrs->{key}
295 ? $self->result_source->unique_constraint_columns($attrs->{key})
296 : $self->result_source->primary_columns;
297 $self->throw_exception(
298 "Can't find unless a primary key or unique constraint is defined"
301 # Parse out a hashref from input
303 if (ref $_[0] eq 'HASH') {
304 $input_query = { %{$_[0]} };
306 elsif (@_ == @cols) {
308 @{$input_query}{@cols} = @_;
311 # Compatibility: Allow e.g. find(id => $value)
312 carp "Find by key => value deprecated; please use a hashref instead";
316 my @unique_queries = $self->_unique_queries($input_query, $attrs);
317 # use Data::Dumper; warn Dumper $self->result_source->name, $input_query, \@unique_queries, $self->{attrs}->{where};
320 my $query = \@unique_queries;
321 if (scalar @unique_queries == 0) {
322 if (exists $attrs->{key}) {
323 $self->throw_exception("Required values for the $attrs->{key} key not provided");
326 # Compatibility: Allow broken find usage for now
327 carp "Query not guarnteed to return a single row"
328 . "; please declare your unique constraints or use search instead";
329 $query = $input_query;
335 my $rs = $self->search($query, $attrs);
337 return keys %{$rs->{_attrs}->{collapse}} ? $rs->next : $rs->single;
341 return (keys %{$self->{_attrs}->{collapse}})
342 ? $self->search($query)->next
343 : $self->single($query);
349 # Build a list of queries which satisfy unique constraints.
351 sub _unique_queries {
352 my ($self, $query, $attrs) = @_;
354 my @constraint_names = exists $attrs->{key}
356 : $self->result_source->unique_constraint_names;
359 foreach my $name (@constraint_names) {
360 my @unique_cols = $self->result_source->unique_constraint_columns($name);
361 my $unique_query = $self->_build_unique_query($query, \@unique_cols);
363 next unless scalar keys %$unique_query;
365 # Add the ResultSet's alias
366 foreach my $key (grep { ! m/\./ } keys %$unique_query) {
367 $unique_query->{"$self->{attrs}->{alias}.$key"} = delete $unique_query->{$key};
370 push @unique_queries, $unique_query;
373 return @unique_queries;
376 # _build_unique_query
378 # Constrain the specified query hash based on the specified column names.
380 sub _build_unique_query {
381 my ($self, $query, $unique_cols) = @_;
384 map { $_ => $query->{$_} }
385 grep { exists $query->{$_} }
388 return \%unique_query;
391 =head2 search_related
395 =item Arguments: $cond, \%attrs?
397 =item Return Value: $new_resultset
401 $new_rs = $cd_rs->search_related('artist', {
405 Searches the specified relationship, optionally specifying a condition and
406 attributes for matching records. See L</ATTRIBUTES> for more information.
411 return shift->related_resultset(shift)->search(@_);
418 =item Arguments: none
420 =item Return Value: $cursor
424 Returns a storage-driven cursor to the given resultset. See
425 L<DBIx::Class::Cursor> for more information.
433 my $attrs = { %{$self->{_attrs}} };
434 return $self->{cursor}
435 ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select},
436 $attrs->{where},$attrs);
443 =item Arguments: $cond?
445 =item Return Value: $row_object?
449 my $cd = $schema->resultset('CD')->single({ year => 2001 });
451 Inflates the first result without creating a cursor if the resultset has
452 any records in it; if not returns nothing. Used by L</find> as an optimisation.
454 Can optionally take an additional condition *only* - this is a fast-code-path
455 method; if you need to add extra joins or similar call ->search and then
456 ->single without a condition on the $rs returned from that.
461 my ($self, $where) = @_;
463 my $attrs = { %{$self->{_attrs}} };
465 if (defined $attrs->{where}) {
468 [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
469 $where, delete $attrs->{where} ]
472 $attrs->{where} = $where;
476 my @data = $self->result_source->storage->select_single(
477 $attrs->{from}, $attrs->{select},
478 $attrs->{where},$attrs);
479 return (@data ? $self->_construct_object(@data) : ());
486 =item Arguments: $cond?
488 =item Return Value: $resultsetcolumn
492 my $max_length = $rs->get_column('length')->max;
494 Returns a ResultSetColumn instance for $column based on $self
499 my ($self, $column) = @_;
501 my $new = DBIx::Class::ResultSetColumn->new($self, $column);
509 =item Arguments: $cond, \%attrs?
511 =item Return Value: $resultset (scalar context), @row_objs (list context)
515 # WHERE title LIKE '%blue%'
516 $cd_rs = $rs->search_like({ title => '%blue%'});
518 Performs a search, but uses C<LIKE> instead of C<=> as the condition. Note
519 that this is simply a convenience method. You most likely want to use
520 L</search> with specific operators.
522 For more information, see L<DBIx::Class::Manual::Cookbook>.
528 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
529 my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_};
530 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
531 return $class->search($query, { %$attrs });
538 =item Arguments: $first, $last
540 =item Return Value: $resultset (scalar context), @row_objs (list context)
544 Returns a resultset or object list representing a subset of elements from the
545 resultset slice is called on. Indexes are from 0, i.e., to get the first
548 my ($one, $two, $three) = $rs->slice(0, 2);
553 my ($self, $min, $max) = @_;
554 my $attrs = {}; # = { %{ $self->{attrs} || {} } };
555 $attrs->{offset} = $self->{attrs}{offset} || 0;
556 $attrs->{offset} += $min;
557 $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
558 return $self->search(undef(), $attrs);
559 #my $slice = (ref $self)->new($self->result_source, $attrs);
560 #return (wantarray ? $slice->all : $slice);
567 =item Arguments: none
569 =item Return Value: $result?
573 Returns the next element in the resultset (C<undef> is there is none).
575 Can be used to efficiently iterate over records in the resultset:
577 my $rs = $schema->resultset('CD')->search;
578 while (my $cd = $rs->next) {
582 Note that you need to store the resultset object, and call C<next> on it.
583 Calling C<< resultset('Table')->next >> repeatedly will always return the
584 first record from the resultset.
590 if (my $cache = $self->get_cache) {
591 $self->{all_cache_position} ||= 0;
592 return $cache->[$self->{all_cache_position}++];
594 if ($self->{attrs}{cache}) {
595 $self->{all_cache_position} = 1;
596 return ($self->all)[0];
598 my @row = (exists $self->{stashed_row} ?
599 @{delete $self->{stashed_row}} :
602 return unless (@row);
603 return $self->_construct_object(@row);
609 return if(exists $self->{_attrs}); #return if _resolve has already been called
611 my $attrs = $self->{attrs};
612 my $source = ($self->{_parent_rs}) ? $self->{_parent_rs} : $self->{result_source};
614 # XXX - lose storable dclone
615 my $record_filter = delete $attrs->{record_filter} if (defined $attrs->{record_filter});
616 $attrs = Storable::dclone($attrs || {}); # { %{ $attrs || {} } };
617 $attrs->{record_filter} = $record_filter if ($record_filter);
618 $self->{attrs}->{record_filter} = $record_filter if ($record_filter);
620 my $alias = $attrs->{alias};
622 $attrs->{columns} ||= delete $attrs->{cols} if $attrs->{cols};
623 delete $attrs->{as} if $attrs->{columns};
624 $attrs->{columns} ||= [ $self->{result_source}->columns ] unless $attrs->{select};
625 my $select_alias = ($self->{_parent_rs}) ? $self->{attrs}->{_live_join} : $alias;
627 map { m/\./ ? $_ : "${select_alias}.$_" } @{delete $attrs->{columns}}
628 ] if $attrs->{columns};
630 map { m/^\Q$alias.\E(.+)$/ ? $1 : $_ } @{$attrs->{select}}
632 if (my $include = delete $attrs->{include_columns}) {
633 push(@{$attrs->{select}}, @$include);
634 push(@{$attrs->{as}}, map { m/([^.]+)$/; $1; } @$include);
637 $attrs->{from} ||= [ { $alias => $source->from } ];
638 $attrs->{seen_join} ||= {};
640 if (my $join = delete $attrs->{join}) {
641 foreach my $j (ref $join eq 'ARRAY' ? @$join : ($join)) {
642 if (ref $j eq 'HASH') {
643 $seen{$_} = 1 foreach keys %$j;
649 push(@{$attrs->{from}}, $source->resolve_join($join, $attrs->{alias}, $attrs->{seen_join}));
651 $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct};
652 $attrs->{order_by} = [ $attrs->{order_by} ] if
653 $attrs->{order_by} and !ref($attrs->{order_by});
654 $attrs->{order_by} ||= [];
656 if(my $seladds = delete($attrs->{'+select'})) {
657 my @seladds = (ref($seladds) eq 'ARRAY' ? @$seladds : ($seladds));
659 @{ $attrs->{select} },
660 map { (m/\./ || ref($_)) ? $_ : "${alias}.$_" } $seladds
663 if(my $asadds = delete($attrs->{'+as'})) {
664 my @asadds = (ref($asadds) eq 'ARRAY' ? @$asadds : ($asadds));
665 $attrs->{as} = [ @{ $attrs->{as} }, @asadds ];
668 my $collapse = $attrs->{collapse} || {};
669 if (my $prefetch = delete $attrs->{prefetch}) {
671 foreach my $p (ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch)) {
672 if ( ref $p eq 'HASH' ) {
673 foreach my $key (keys %$p) {
674 push(@{$attrs->{from}}, $source->resolve_join($p, $attrs->{alias}))
678 push(@{$attrs->{from}}, $source->resolve_join($p, $attrs->{alias}))
681 my @prefetch = $source->resolve_prefetch(
682 $p, $attrs->{alias}, {}, \@pre_order, $collapse);
683 push(@{$attrs->{select}}, map { $_->[0] } @prefetch);
684 push(@{$attrs->{as}}, map { $_->[1] } @prefetch);
686 push(@{$attrs->{order_by}}, @pre_order);
688 $attrs->{collapse} = $collapse;
689 $self->{_attrs} = $attrs;
693 my ($self, $a, $b, $is_prefetch) = @_;
696 if (ref $b eq 'HASH' && ref $a eq 'HASH') {
697 foreach my $key (keys %{$b}) {
698 if (exists $a->{$key}) {
699 $a->{$key} = $self->_merge_attr($a->{$key}, $b->{$key}, $is_prefetch);
701 $a->{$key} = delete $b->{$key};
706 $a = [$a] unless (ref $a eq 'ARRAY');
707 $b = [$b] unless (ref $b eq 'ARRAY');
712 foreach my $element (@{$_}) {
713 if (ref $element eq 'HASH') {
714 $hash = $self->_merge_attr($hash, $element, $is_prefetch);
715 } elsif (ref $element eq 'ARRAY') {
716 $array = [@{$array}, @{$element}];
718 if (($b == $_) && $is_prefetch) {
719 $self->_merge_array($array, $element, $is_prefetch);
721 push(@{$array}, $element);
727 if ((keys %{$hash}) && (scalar(@{$array} > 0))) {
728 return [$hash, @{$array}];
730 return (keys %{$hash}) ? $hash : $array;
736 my ($self, $a, $b) = @_;
738 $b = [$b] unless (ref $b eq 'ARRAY');
739 # add elements from @{$b} to @{$a} which aren't already in @{$a}
740 foreach my $b_element (@{$b}) {
741 push(@{$a}, $b_element) unless grep {$b_element eq $_} @{$a};
745 sub _construct_object {
746 my ($self, @row) = @_;
747 my @as = @{ $self->{_attrs}{as} };
749 my $info = $self->_collapse_result(\@as, \@row);
750 my $new = $self->result_class->inflate_result($self->result_source, @$info);
751 $new = $self->{_attrs}{record_filter}->($new)
752 if exists $self->{_attrs}{record_filter};
756 sub _collapse_result {
757 my ($self, $as, $row, $prefix) = @_;
759 my $live_join = $self->{attrs}->{_live_join} ||="";
763 foreach my $this_as (@$as) {
764 my $val = shift @copy;
765 if (defined $prefix) {
766 if ($this_as =~ m/^\Q${prefix}.\E(.+)$/) {
768 $remain =~ /^(?:(.*)\.)?([^.]+)$/;
769 $const{$1||''}{$2} = $val;
772 $this_as =~ /^(?:(.*)\.)?([^.]+)$/;
773 $const{$1||''}{$2} = $val;
777 my $info = [ {}, {} ];
778 foreach my $key (keys %const) {
779 if (length $key && $key ne $live_join) {
781 my @parts = split(/\./, $key);
782 foreach my $p (@parts) {
783 $target = $target->[1]->{$p} ||= [];
785 $target->[0] = $const{$key};
787 $info->[0] = $const{$key};
792 if (defined $prefix) {
794 m/^\Q${prefix}.\E(.+)$/ ? ($1) : ()
795 } keys %{$self->{_attrs}->{collapse}}
797 @collapse = keys %{$self->{_attrs}->{collapse}};
801 my ($c) = sort { length $a <=> length $b } @collapse;
803 foreach my $p (split(/\./, $c)) {
804 $target = $target->[1]->{$p} ||= [];
806 my $c_prefix = (defined($prefix) ? "${prefix}.${c}" : $c);
807 my @co_key = @{$self->{_attrs}->{collapse}{$c_prefix}};
808 my %co_check = map { ($_, $target->[0]->{$_}); } @co_key;
809 my $tree = $self->_collapse_result($as, $row, $c_prefix);
812 !defined($tree->[0]->{$_}) ||
813 $co_check{$_} ne $tree->[0]->{$_}
816 last unless (@raw = $self->cursor->next);
817 $row = $self->{stashed_row} = \@raw;
818 $tree = $self->_collapse_result($as, $row, $c_prefix);
820 @$target = (@final ? @final : [ {}, {} ]);
821 # single empty result to indicate an empty prefetched has_many
830 =item Arguments: $result_source?
832 =item Return Value: $result_source
836 An accessor for the primary ResultSource object from which this ResultSet
846 =item Arguments: $cond, \%attrs??
848 =item Return Value: $count
852 Performs an SQL C<COUNT> with the same query as the resultset was built
853 with to find the number of elements. If passed arguments, does a search
854 on the resultset and counts the results of that.
856 Note: When using C<count> with C<group_by>, L<DBIX::Class> emulates C<GROUP BY>
857 using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
858 not support C<DISTINCT> with multiple columns. If you are using such a
859 database, you should only use columns from the main table in your C<group_by>
866 return $self->search(@_)->count if @_ and defined $_[0];
867 return scalar @{ $self->get_cache } if $self->get_cache;
869 my $count = $self->_count;
870 return 0 unless $count;
872 $count -= $self->{attrs}{offset} if $self->{attrs}{offset};
873 $count = $self->{attrs}{rows} if
874 $self->{attrs}{rows} and $self->{attrs}{rows} < $count;
878 sub _count { # Separated out so pager can get the full count
880 my $select = { count => '*' };
883 my $attrs = { %{ $self->{_attrs} } };
884 if (my $group_by = delete $attrs->{group_by}) {
885 delete $attrs->{having};
886 my @distinct = (ref $group_by ? @$group_by : ($group_by));
887 # todo: try CONCAT for multi-column pk
888 my @pk = $self->result_source->primary_columns;
890 foreach my $column (@distinct) {
891 if ($column =~ qr/^(?:\Q$attrs->{alias}.\E)?$pk[0]$/) {
892 @distinct = ($column);
898 $select = { count => { distinct => \@distinct } };
901 $attrs->{select} = $select;
902 $attrs->{as} = [qw/count/];
904 # offset, order by and page are not needed to count. record_filter is cdbi
905 delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
906 my ($count) = (ref $self)->new($self->result_source, $attrs)->cursor->next;
914 =item Arguments: $sql_fragment, @bind_values
916 =item Return Value: $count
920 Counts the results in a literal query. Equivalent to calling L</search_literal>
921 with the passed arguments, then L</count>.
925 sub count_literal { shift->search_literal(@_)->count; }
931 =item Arguments: none
933 =item Return Value: @objects
937 Returns all elements in the resultset. Called implicitly if the resultset
938 is returned in list context.
944 return @{ $self->get_cache } if $self->get_cache;
948 # TODO: don't call resolve here
950 if (keys %{$self->{_attrs}->{collapse}}) {
951 # if ($self->{attrs}->{prefetch}) {
952 # Using $self->cursor->all is really just an optimisation.
953 # If we're collapsing has_many prefetches it probably makes
954 # very little difference, and this is cleaner than hacking
955 # _construct_object to survive the approach
956 my @row = $self->cursor->next;
958 push(@obj, $self->_construct_object(@row));
959 @row = (exists $self->{stashed_row}
960 ? @{delete $self->{stashed_row}}
961 : $self->cursor->next);
964 @obj = map { $self->_construct_object(@$_) } $self->cursor->all;
967 $self->set_cache(\@obj) if $self->{attrs}{cache};
975 =item Arguments: none
977 =item Return Value: $self
981 Resets the resultset's cursor, so you can iterate through the elements again.
987 delete $self->{_attrs} if (exists $self->{_attrs});
989 $self->{all_cache_position} = 0;
990 $self->cursor->reset;
998 =item Arguments: none
1000 =item Return Value: $object?
1004 Resets the resultset and returns an object for the first result (if the
1005 resultset returns anything).
1010 return $_[0]->reset->next;
1013 # _cond_for_update_delete
1015 # update/delete require the condition to be modified to handle
1016 # the differing SQL syntax available. This transforms the $self->{cond}
1017 # appropriately, returning the new condition.
1019 sub _cond_for_update_delete {
1023 if (!ref($self->{cond})) {
1024 # No-op. No condition, we're updating/deleting everything
1026 elsif (ref $self->{cond} eq 'ARRAY') {
1030 foreach my $key (keys %{$_}) {
1032 $hash{$1} = $_->{$key};
1038 elsif (ref $self->{cond} eq 'HASH') {
1039 if ((keys %{$self->{cond}})[0] eq '-and') {
1042 my @cond = @{$self->{cond}{-and}};
1043 for (my $i = 0; $i <= @cond - 1; $i++) {
1044 my $entry = $cond[$i];
1047 if (ref $entry eq 'HASH') {
1048 foreach my $key (keys %{$entry}) {
1050 $hash{$1} = $entry->{$key};
1054 $entry =~ /([^.]+)$/;
1055 $hash{$1} = $cond[++$i];
1058 push @{$cond->{-and}}, \%hash;
1062 foreach my $key (keys %{$self->{cond}}) {
1064 $cond->{$1} = $self->{cond}{$key};
1069 $self->throw_exception(
1070 "Can't update/delete on resultset with condition unless hash or array"
1082 =item Arguments: \%values
1084 =item Return Value: $storage_rv
1088 Sets the specified columns in the resultset to the supplied values in a
1089 single query. Return value will be true if the update succeeded or false
1090 if no records were updated; exact type of success value is storage-dependent.
1095 my ($self, $values) = @_;
1096 $self->throw_exception("Values for update must be a hash")
1097 unless ref $values eq 'HASH';
1099 my $cond = $self->_cond_for_update_delete;
1101 return $self->result_source->storage->update(
1102 $self->result_source->from, $values, $cond
1110 =item Arguments: \%values
1112 =item Return Value: 1
1116 Fetches all objects and updates them one at a time. Note that C<update_all>
1117 will run DBIC cascade triggers, while L</update> will not.
1122 my ($self, $values) = @_;
1123 $self->throw_exception("Values for update must be a hash")
1124 unless ref $values eq 'HASH';
1125 foreach my $obj ($self->all) {
1126 $obj->set_columns($values)->update;
1135 =item Arguments: none
1137 =item Return Value: 1
1141 Deletes the contents of the resultset from its result source. Note that this
1142 will not run DBIC cascade triggers. See L</delete_all> if you need triggers
1151 my $cond = $self->_cond_for_update_delete;
1153 $self->result_source->storage->delete($self->result_source->from, $cond);
1161 =item Arguments: none
1163 =item Return Value: 1
1167 Fetches all objects and deletes them one at a time. Note that C<delete_all>
1168 will run DBIC cascade triggers, while L</delete> will not.
1174 $_->delete for $self->all;
1182 =item Arguments: none
1184 =item Return Value: $pager
1188 Return Value a L<Data::Page> object for the current resultset. Only makes
1189 sense for queries with a C<page> attribute.
1195 my $attrs = $self->{attrs};
1196 $self->throw_exception("Can't create pager for non-paged rs")
1197 unless $self->{page};
1198 $attrs->{rows} ||= 10;
1199 return $self->{pager} ||= Data::Page->new(
1200 $self->_count, $attrs->{rows}, $self->{page});
1207 =item Arguments: $page_number
1209 =item Return Value: $rs
1213 Returns a resultset for the $page_number page of the resultset on which page
1214 is called, where each page contains a number of rows equal to the 'rows'
1215 attribute set on the resultset (10 by default).
1220 my ($self, $page) = @_;
1221 my $attrs = { %{$self->{attrs}} };
1222 $attrs->{page} = $page;
1223 return (ref $self)->new($self->result_source, $attrs);
1230 =item Arguments: \%vals
1232 =item Return Value: $object
1236 Creates an object in the resultset's result class and returns it.
1241 my ($self, $values) = @_;
1242 $self->throw_exception( "new_result needs a hash" )
1243 unless (ref $values eq 'HASH');
1244 $self->throw_exception(
1245 "Can't abstract implicit construct, condition not a hash"
1246 ) if ($self->{cond} && !(ref $self->{cond} eq 'HASH'));
1248 my $alias = $self->{attrs}{alias};
1249 foreach my $key (keys %{$self->{cond}||{}}) {
1250 $new{$1} = $self->{cond}{$key} if ($key =~ m/^(?:\Q${alias}.\E)?([^.]+)$/);
1252 my $obj = $self->result_class->new(\%new);
1253 $obj->result_source($self->result_source) if $obj->can('result_source');
1261 =item Arguments: \%vals, \%attrs?
1263 =item Return Value: $object
1267 Find an existing record from this resultset. If none exists, instantiate a new
1268 result object and return it. The object will not be saved into your storage
1269 until you call L<DBIx::Class::Row/insert> on it.
1271 If you want objects to be saved immediately, use L</find_or_create> instead.
1277 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1278 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1279 my $exists = $self->find($hash, $attrs);
1280 return defined $exists ? $exists : $self->new_result($hash);
1287 =item Arguments: \%vals
1289 =item Return Value: $object
1293 Inserts a record into the resultset and returns the object representing it.
1295 Effectively a shortcut for C<< ->new_result(\%vals)->insert >>.
1300 my ($self, $attrs) = @_;
1301 $self->throw_exception( "create needs a hashref" )
1302 unless ref $attrs eq 'HASH';
1303 return $self->new_result($attrs)->insert;
1306 =head2 find_or_create
1310 =item Arguments: \%vals, \%attrs?
1312 =item Return Value: $object
1316 $class->find_or_create({ key => $val, ... });
1318 Tries to find a record based on its primary key or unique constraint; if none
1319 is found, creates one and returns that instead.
1321 my $cd = $schema->resultset('CD')->find_or_create({
1323 artist => 'Massive Attack',
1324 title => 'Mezzanine',
1328 Also takes an optional C<key> attribute, to search by a specific key or unique
1329 constraint. For example:
1331 my $cd = $schema->resultset('CD')->find_or_create(
1333 artist => 'Massive Attack',
1334 title => 'Mezzanine',
1336 { key => 'artist_title' }
1339 See also L</find> and L</update_or_create>. For information on how to declare
1340 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1344 sub find_or_create {
1346 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1347 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1348 my $exists = $self->find($hash, $attrs);
1349 return defined $exists ? $exists : $self->create($hash);
1352 =head2 update_or_create
1356 =item Arguments: \%col_values, { key => $unique_constraint }?
1358 =item Return Value: $object
1362 $class->update_or_create({ col => $val, ... });
1364 First, searches for an existing row matching one of the unique constraints
1365 (including the primary key) on the source of this resultset. If a row is
1366 found, updates it with the other given column values. Otherwise, creates a new
1369 Takes an optional C<key> attribute to search on a specific unique constraint.
1372 # In your application
1373 my $cd = $schema->resultset('CD')->update_or_create(
1375 artist => 'Massive Attack',
1376 title => 'Mezzanine',
1379 { key => 'artist_title' }
1382 If no C<key> is specified, it searches on all unique constraints defined on the
1383 source, including the primary key.
1385 If the C<key> is specified as C<primary>, it searches only on the primary key.
1387 See also L</find> and L</find_or_create>. For information on how to declare
1388 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1392 sub update_or_create {
1394 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1395 my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
1397 my $row = $self->find($cond);
1399 $row->update($cond);
1403 return $self->create($cond);
1410 =item Arguments: none
1412 =item Return Value: \@cache_objects?
1416 Gets the contents of the cache for the resultset, if the cache is set.
1428 =item Arguments: \@cache_objects
1430 =item Return Value: \@cache_objects
1434 Sets the contents of the cache for the resultset. Expects an arrayref
1435 of objects of the same class as those produced by the resultset. Note that
1436 if the cache is set the resultset will return the cached objects rather
1437 than re-querying the database even if the cache attr is not set.
1442 my ( $self, $data ) = @_;
1443 $self->throw_exception("set_cache requires an arrayref")
1444 if defined($data) && (ref $data ne 'ARRAY');
1445 $self->{all_cache} = $data;
1452 =item Arguments: none
1454 =item Return Value: []
1458 Clears the cache for the resultset.
1463 shift->set_cache(undef);
1466 =head2 related_resultset
1470 =item Arguments: $relationship_name
1472 =item Return Value: $resultset
1476 Returns a related resultset for the supplied relationship name.
1478 $artist_rs = $schema->resultset('CD')->related_resultset('Artist');
1482 sub related_resultset {
1483 my ( $self, $rel ) = @_;
1485 $self->{related_resultsets} ||= {};
1486 return $self->{related_resultsets}{$rel} ||= do {
1487 #warn "fetching related resultset for rel '$rel' " . $self->result_source->{name};
1488 my $rel_obj = $self->result_source->relationship_info($rel);
1489 $self->throw_exception(
1490 "search_related: result source '" . $self->result_source->name .
1491 "' has no such relationship ${rel}")
1492 unless $rel_obj; #die Dumper $self->{attrs};
1494 my $rs = $self->result_source->schema->resultset($rel_obj->{class}
1496 { %{$self->{attrs}},
1500 _live_join => $rel }
1503 # keep reference of the original resultset
1504 $rs->{_parent_rs} = $self->result_source;
1509 =head2 throw_exception
1511 See L<DBIx::Class::Schema/throw_exception> for details.
1515 sub throw_exception {
1517 $self->result_source->schema->throw_exception(@_);
1520 # XXX: FIXME: Attributes docs need clearing up
1524 The resultset takes various attributes that modify its behavior. Here's an
1531 =item Value: ($order_by | \@order_by)
1535 Which column(s) to order the results by. This is currently passed
1536 through directly to SQL, so you can give e.g. C<year DESC> for a
1537 descending order on the column `year'.
1539 Please note that if you have quoting enabled (see
1540 L<DBIx::Class::Storage/quote_char>) you will need to do C<\'year DESC' > to
1541 specify an order. (The scalar ref causes it to be passed as raw sql to the DB,
1542 so you will need to manually quote things as appropriate.)
1548 =item Value: \@columns
1552 Shortcut to request a particular set of columns to be retrieved. Adds
1553 C<me.> onto the start of any column without a C<.> in it and sets C<select>
1554 from that, then auto-populates C<as> from C<select> as normal. (You may also
1555 use the C<cols> attribute, as in earlier versions of DBIC.)
1557 =head2 include_columns
1561 =item Value: \@columns
1565 Shortcut to include additional columns in the returned results - for example
1567 $schema->resultset('CD')->search(undef, {
1568 include_columns => ['artist.name'],
1572 would return all CDs and include a 'name' column to the information
1573 passed to object inflation
1579 =item Value: \@select_columns
1583 Indicates which columns should be selected from the storage. You can use
1584 column names, or in the case of RDBMS back ends, function or stored procedure
1587 $rs = $schema->resultset('Employee')->search(undef, {
1590 { count => 'employeeid' },
1595 When you use function/stored procedure names and do not supply an C<as>
1596 attribute, the column names returned are storage-dependent. E.g. MySQL would
1597 return a column named C<count(employeeid)> in the above example.
1603 Indicates additional columns to be selected from storage. Works the same as
1604 L<select> but adds columns to the selection.
1612 Indicates additional column names for those added via L<+select>.
1620 =item Value: \@inflation_names
1624 Indicates column names for object inflation. This is used in conjunction with
1625 C<select>, usually when C<select> contains one or more function or stored
1628 $rs = $schema->resultset('Employee')->search(undef, {
1631 { count => 'employeeid' }
1633 as => ['name', 'employee_count'],
1636 my $employee = $rs->first(); # get the first Employee
1638 If the object against which the search is performed already has an accessor
1639 matching a column name specified in C<as>, the value can be retrieved using
1640 the accessor as normal:
1642 my $name = $employee->name();
1644 If on the other hand an accessor does not exist in the object, you need to
1645 use C<get_column> instead:
1647 my $employee_count = $employee->get_column('employee_count');
1649 You can create your own accessors if required - see
1650 L<DBIx::Class::Manual::Cookbook> for details.
1652 Please note: This will NOT insert an C<AS employee_count> into the SQL statement
1653 produced, it is used for internal access only. Thus attempting to use the accessor
1654 in an C<order_by> clause or similar will fail misrably.
1660 =item Value: ($rel_name | \@rel_names | \%rel_names)
1664 Contains a list of relationships that should be joined for this query. For
1667 # Get CDs by Nine Inch Nails
1668 my $rs = $schema->resultset('CD')->search(
1669 { 'artist.name' => 'Nine Inch Nails' },
1670 { join => 'artist' }
1673 Can also contain a hash reference to refer to the other relation's relations.
1676 package MyApp::Schema::Track;
1677 use base qw/DBIx::Class/;
1678 __PACKAGE__->table('track');
1679 __PACKAGE__->add_columns(qw/trackid cd position title/);
1680 __PACKAGE__->set_primary_key('trackid');
1681 __PACKAGE__->belongs_to(cd => 'MyApp::Schema::CD');
1684 # In your application
1685 my $rs = $schema->resultset('Artist')->search(
1686 { 'track.title' => 'Teardrop' },
1688 join => { cd => 'track' },
1689 order_by => 'artist.name',
1693 If the same join is supplied twice, it will be aliased to <rel>_2 (and
1694 similarly for a third time). For e.g.
1696 my $rs = $schema->resultset('Artist')->search({
1697 'cds.title' => 'Down to Earth',
1698 'cds_2.title' => 'Popular',
1700 join => [ qw/cds cds/ ],
1703 will return a set of all artists that have both a cd with title 'Down
1704 to Earth' and a cd with title 'Popular'.
1706 If you want to fetch related objects from other tables as well, see C<prefetch>
1713 =item Value: ($rel_name | \@rel_names | \%rel_names)
1717 Contains one or more relationships that should be fetched along with the main
1718 query (when they are accessed afterwards they will have already been
1719 "prefetched"). This is useful for when you know you will need the related
1720 objects, because it saves at least one query:
1722 my $rs = $schema->resultset('Tag')->search(
1731 The initial search results in SQL like the following:
1733 SELECT tag.*, cd.*, artist.* FROM tag
1734 JOIN cd ON tag.cd = cd.cdid
1735 JOIN artist ON cd.artist = artist.artistid
1737 L<DBIx::Class> has no need to go back to the database when we access the
1738 C<cd> or C<artist> relationships, which saves us two SQL statements in this
1741 Simple prefetches will be joined automatically, so there is no need
1742 for a C<join> attribute in the above search. If you're prefetching to
1743 depth (e.g. { cd => { artist => 'label' } or similar), you'll need to
1744 specify the join as well.
1746 C<prefetch> can be used with the following relationship types: C<belongs_to>,
1747 C<has_one> (or if you're using C<add_relationship>, any relationship declared
1748 with an accessor type of 'single' or 'filter').
1758 Makes the resultset paged and specifies the page to retrieve. Effectively
1759 identical to creating a non-pages resultset and then calling ->page($page)
1762 If L<rows> attribute is not specified it defualts to 10 rows per page.
1772 Specifes the maximum number of rows for direct retrieval or the number of
1773 rows per page if the page attribute or method is used.
1779 =item Value: $offset
1783 Specifies the (zero-based) row number for the first row to be returned, or the
1784 of the first row of the first page if paging is used.
1790 =item Value: \@columns
1794 A arrayref of columns to group by. Can include columns of joined tables.
1796 group_by => [qw/ column1 column2 ... /]
1802 =item Value: $condition
1806 HAVING is a select statement attribute that is applied between GROUP BY and
1807 ORDER BY. It is applied to the after the grouping calculations have been
1810 having => { 'count(employee)' => { '>=', 100 } }
1816 =item Value: (0 | 1)
1820 Set to 1 to group by all columns.
1824 Set to 1 to cache search results. This prevents extra SQL queries if you
1825 revisit rows in your ResultSet:
1827 my $resultset = $schema->resultset('Artist')->search( undef, { cache => 1 } );
1829 while( my $artist = $resultset->next ) {
1833 $rs->first; # without cache, this would issue a query
1835 By default, searches are not cached.
1837 For more examples of using these attributes, see
1838 L<DBIx::Class::Manual::Cookbook>.
1844 =item Value: \@from_clause
1848 The C<from> attribute gives you manual control over the C<FROM> clause of SQL
1849 statements generated by L<DBIx::Class>, allowing you to express custom C<JOIN>
1852 NOTE: Use this on your own risk. This allows you to shoot off your foot!
1854 C<join> will usually do what you need and it is strongly recommended that you
1855 avoid using C<from> unless you cannot achieve the desired result using C<join>.
1856 And we really do mean "cannot", not just tried and failed. Attempting to use
1857 this because you're having problems with C<join> is like trying to use x86
1858 ASM because you've got a syntax error in your C. Trust us on this.
1860 Now, if you're still really, really sure you need to use this (and if you're
1861 not 100% sure, ask the mailing list first), here's an explanation of how this
1864 The syntax is as follows -
1867 { <alias1> => <table1> },
1869 { <alias2> => <table2>, -join_type => 'inner|left|right' },
1870 [], # nested JOIN (optional)
1871 { <table1.column1> => <table2.column2>, ... (more conditions) },
1873 # More of the above [ ] may follow for additional joins
1880 ON <table1.column1> = <table2.column2>
1881 <more joins may follow>
1883 An easy way to follow the examples below is to remember the following:
1885 Anything inside "[]" is a JOIN
1886 Anything inside "{}" is a condition for the enclosing JOIN
1888 The following examples utilize a "person" table in a family tree application.
1889 In order to express parent->child relationships, this table is self-joined:
1891 # Person->belongs_to('father' => 'Person');
1892 # Person->belongs_to('mother' => 'Person');
1894 C<from> can be used to nest joins. Here we return all children with a father,
1895 then search against all mothers of those children:
1897 $rs = $schema->resultset('Person')->search(
1900 alias => 'mother', # alias columns in accordance with "from"
1902 { mother => 'person' },
1905 { child => 'person' },
1907 { father => 'person' },
1908 { 'father.person_id' => 'child.father_id' }
1911 { 'mother.person_id' => 'child.mother_id' }
1918 # SELECT mother.* FROM person mother
1921 # JOIN person father
1922 # ON ( father.person_id = child.father_id )
1924 # ON ( mother.person_id = child.mother_id )
1926 The type of any join can be controlled manually. To search against only people
1927 with a father in the person table, we could explicitly use C<INNER JOIN>:
1929 $rs = $schema->resultset('Person')->search(
1932 alias => 'child', # alias columns in accordance with "from"
1934 { child => 'person' },
1936 { father => 'person', -join_type => 'inner' },
1937 { 'father.id' => 'child.father_id' }
1944 # SELECT child.* FROM person child
1945 # INNER JOIN person father ON child.father_id = father.id