1 package DBIx::Class::ResultSet;
9 use Carp::Clan qw/^DBIx::Class/;
12 use DBIx::Class::ResultSetColumn;
13 use base qw/DBIx::Class/;
15 use Data::Dumper; $Data::Dumper::Indent = 1;
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,
116 =item Arguments: $cond, \%attrs?
118 =item Return Value: $resultset (scalar context), @row_objs (list context)
122 my @cds = $cd_rs->search({ year => 2001 }); # "... WHERE year = 2001"
123 my $new_rs = $cd_rs->search({ year => 2005 });
125 my $new_rs = $cd_rs->search([ { year => 2005 }, { year => 2004 } ]);
126 # year = 2005 OR year = 2004
128 If you need to pass in additional attributes but no additional condition,
129 call it as C<search(undef, \%attrs)>.
131 # "SELECT name, artistid FROM $artist_table"
132 my @all_artists = $schema->resultset('Artist')->search(undef, {
133 columns => [qw/name artistid/],
140 my $rs = $self->search_rs( @_ );
141 return (wantarray ? $rs->all : $rs);
148 =item Arguments: $cond, \%attrs?
150 =item Return Value: $resultset
154 This method does the same exact thing as search() except it will
155 always return a resultset, even in list context.
164 unless (@_) { # no search, effectively just a clone
165 $rows = $self->get_cache;
169 $attrs = pop(@_) if @_ > 1 and ref $_[$#_] eq 'HASH';
170 my $our_attrs = exists $attrs->{_parent_attrs}
171 ? { %{delete $attrs->{_parent_attrs}} }
172 : { %{$self->{attrs}} };
173 my $having = delete $our_attrs->{having};
175 # merge new attrs into inherited
176 foreach my $key (qw/join prefetch/) {
177 next unless exists $attrs->{$key};
178 $our_attrs->{$key} = $self->_merge_attr($our_attrs->{$key}, delete $attrs->{$key});
181 my $new_attrs = { %{$our_attrs}, %{$attrs} };
184 (@_ == 1 || ref $_[0] eq "HASH")
188 ? $self->throw_exception("Odd number of arguments to search")
195 if (defined $where) {
196 $new_attrs->{where} = (
197 defined $new_attrs->{where}
200 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
201 } $where, $new_attrs->{where}
207 if (defined $having) {
208 $new_attrs->{having} = (
209 defined $new_attrs->{having}
212 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
213 } $having, $new_attrs->{having}
219 my $rs = (ref $self)->new($self->result_source, $new_attrs);
221 $rs->set_cache($rows);
226 =head2 search_literal
230 =item Arguments: $sql_fragment, @bind_values
232 =item Return Value: $resultset (scalar context), @row_objs (list context)
236 my @cds = $cd_rs->search_literal('year = ? AND title = ?', qw/2001 Reload/);
237 my $newrs = $artist_rs->search_literal('name = ?', 'Metallica');
239 Pass a literal chunk of SQL to be added to the conditional part of the
245 my ($self, $cond, @vals) = @_;
246 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
247 $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ];
248 return $self->search(\$cond, $attrs);
255 =item Arguments: @values | \%cols, \%attrs?
257 =item Return Value: $row_object
261 Finds a row based on its primary key or unique constraint. For example, to find
262 a row by its primary key:
264 my $cd = $schema->resultset('CD')->find(5);
266 You can also find a row by a specific unique constraint using the C<key>
267 attribute. For example:
269 my $cd = $schema->resultset('CD')->find('Massive Attack', 'Mezzanine', {
270 key => 'cd_artist_title'
273 Additionally, you can specify the columns explicitly by name:
275 my $cd = $schema->resultset('CD')->find(
277 artist => 'Massive Attack',
278 title => 'Mezzanine',
280 { key => 'cd_artist_title' }
283 If the C<key> is specified as C<primary>, it searches only on the primary key.
285 If no C<key> is specified, it searches on all unique constraints defined on the
286 source, including the primary key.
288 See also L</find_or_create> and L</update_or_create>. For information on how to
289 declare unique constraints, see
290 L<DBIx::Class::ResultSource/add_unique_constraint>.
296 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
298 # Default to the primary key, but allow a specific key
299 my @cols = exists $attrs->{key}
300 ? $self->result_source->unique_constraint_columns($attrs->{key})
301 : $self->result_source->primary_columns;
302 $self->throw_exception(
303 "Can't find unless a primary key or unique constraint is defined"
306 # Parse out a hashref from input
308 if (ref $_[0] eq 'HASH') {
309 $input_query = { %{$_[0]} };
311 elsif (@_ == @cols) {
313 @{$input_query}{@cols} = @_;
316 # Compatibility: Allow e.g. find(id => $value)
317 carp "Find by key => value deprecated; please use a hashref instead";
321 my @unique_queries = $self->_unique_queries($input_query, $attrs);
323 # Handle cases where the ResultSet defines the query, or where the user is
325 my $query = @unique_queries ? \@unique_queries : $input_query;
329 my $rs = $self->search($query, $attrs);
330 return keys %{$rs->_resolved_attrs->{collapse}} ? $rs->next : $rs->single;
333 return keys %{$self->_resolved_attrs->{collapse}}
334 ? $self->search($query)->next
335 : $self->single($query);
341 # Build a list of queries which satisfy unique constraints.
343 sub _unique_queries {
344 my ($self, $query, $attrs) = @_;
346 my $alias = $self->{attrs}{alias};
347 my @constraint_names = exists $attrs->{key}
349 : $self->result_source->unique_constraint_names;
352 foreach my $name (@constraint_names) {
353 my @unique_cols = $self->result_source->unique_constraint_columns($name);
354 my $unique_query = $self->_build_unique_query($query, \@unique_cols);
356 my $num_query = scalar keys %$unique_query;
357 next unless $num_query;
359 # Add the ResultSet's alias
360 foreach my $col (grep { ! m/\./ } keys %$unique_query) {
361 $unique_query->{"$alias.$col"} = delete $unique_query->{$col};
364 # XXX: Assuming quite a bit about $self->{attrs}{where}
365 my $num_cols = scalar @unique_cols;
366 my $num_where = exists $self->{attrs}{where}
367 ? scalar keys %{ $self->{attrs}{where} }
369 push @unique_queries, $unique_query
370 if $num_query + $num_where == $num_cols;
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->{$_} }
390 =head2 search_related
394 =item Arguments: $rel, $cond, \%attrs?
396 =item Return Value: $new_resultset
400 $new_rs = $cd_rs->search_related('artist', {
404 Searches the specified relationship, optionally specifying a condition and
405 attributes for matching records. See L</ATTRIBUTES> for more information.
410 return shift->related_resultset(shift)->search(@_);
417 =item Arguments: none
419 =item Return Value: $cursor
423 Returns a storage-driven cursor to the given resultset. See
424 L<DBIx::Class::Cursor> for more information.
431 my $attrs = { %{$self->_resolved_attrs} };
432 return $self->{cursor}
433 ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select},
434 $attrs->{where},$attrs);
441 =item Arguments: $cond?
443 =item Return Value: $row_object?
447 my $cd = $schema->resultset('CD')->single({ year => 2001 });
449 Inflates the first result without creating a cursor if the resultset has
450 any records in it; if not returns nothing. Used by L</find> as an optimisation.
452 Can optionally take an additional condition *only* - this is a fast-code-path
453 method; if you need to add extra joins or similar call ->search and then
454 ->single without a condition on the $rs returned from that.
459 my ($self, $where) = @_;
460 my $attrs = { %{$self->_resolved_attrs} };
462 if (defined $attrs->{where}) {
465 [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
466 $where, delete $attrs->{where} ]
469 $attrs->{where} = $where;
473 unless ($self->_is_unique_query($attrs->{where})) {
474 carp "Query not guaranteed to return a single row"
475 . "; please declare your unique constraints or use search instead";
478 my @data = $self->result_source->storage->select_single(
479 $attrs->{from}, $attrs->{select},
480 $attrs->{where}, $attrs
483 return (@data ? $self->_construct_object(@data) : ());
488 # Try to determine if the specified query is guaranteed to be unique, based on
489 # the declared unique constraints.
491 sub _is_unique_query {
492 my ($self, $query) = @_;
494 my $collapsed = $self->_collapse_query($query);
495 my $alias = $self->{attrs}{alias};
497 foreach my $name ($self->result_source->unique_constraint_names) {
498 my @unique_cols = map {
500 } $self->result_source->unique_constraint_columns($name);
502 # Count the values for each unique column
503 my %seen = map { $_ => 0 } @unique_cols;
505 foreach my $key (keys %$collapsed) {
506 my $aliased = $key =~ /\./ ? $key : "$alias.$key";
507 next unless exists $seen{$aliased}; # Additional constraints are okay
508 $seen{$aliased} = scalar keys %{ $collapsed->{$key} };
511 # If we get 0 or more than 1 value for a column, it's not necessarily unique
512 return 1 unless grep { $_ != 1 } values %seen;
520 # Recursively collapse the query, accumulating values for each column.
522 sub _collapse_query {
523 my ($self, $query, $collapsed) = @_;
527 if (ref $query eq 'ARRAY') {
528 foreach my $subquery (@$query) {
529 next unless ref $subquery; # -or
530 # warn "ARRAY: " . Dumper $subquery;
531 $collapsed = $self->_collapse_query($subquery, $collapsed);
534 elsif (ref $query eq 'HASH') {
535 if (keys %$query and (keys %$query)[0] eq '-and') {
536 foreach my $subquery (@{$query->{-and}}) {
537 # warn "HASH: " . Dumper $subquery;
538 $collapsed = $self->_collapse_query($subquery, $collapsed);
542 # warn "LEAF: " . Dumper $query;
543 foreach my $col (keys %$query) {
544 my $value = $query->{$col};
545 $collapsed->{$col}{$value}++;
557 =item Arguments: $cond?
559 =item Return Value: $resultsetcolumn
563 my $max_length = $rs->get_column('length')->max;
565 Returns a ResultSetColumn instance for $column based on $self
570 my ($self, $column) = @_;
571 my $new = DBIx::Class::ResultSetColumn->new($self, $column);
579 =item Arguments: $cond, \%attrs?
581 =item Return Value: $resultset (scalar context), @row_objs (list context)
585 # WHERE title LIKE '%blue%'
586 $cd_rs = $rs->search_like({ title => '%blue%'});
588 Performs a search, but uses C<LIKE> instead of C<=> as the condition. Note
589 that this is simply a convenience method. You most likely want to use
590 L</search> with specific operators.
592 For more information, see L<DBIx::Class::Manual::Cookbook>.
598 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
599 my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_};
600 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
601 return $class->search($query, { %$attrs });
608 =item Arguments: $first, $last
610 =item Return Value: $resultset (scalar context), @row_objs (list context)
614 Returns a resultset or object list representing a subset of elements from the
615 resultset slice is called on. Indexes are from 0, i.e., to get the first
618 my ($one, $two, $three) = $rs->slice(0, 2);
623 my ($self, $min, $max) = @_;
624 my $attrs = {}; # = { %{ $self->{attrs} || {} } };
625 $attrs->{offset} = $self->{attrs}{offset} || 0;
626 $attrs->{offset} += $min;
627 $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
628 return $self->search(undef(), $attrs);
629 #my $slice = (ref $self)->new($self->result_source, $attrs);
630 #return (wantarray ? $slice->all : $slice);
637 =item Arguments: none
639 =item Return Value: $result?
643 Returns the next element in the resultset (C<undef> is there is none).
645 Can be used to efficiently iterate over records in the resultset:
647 my $rs = $schema->resultset('CD')->search;
648 while (my $cd = $rs->next) {
652 Note that you need to store the resultset object, and call C<next> on it.
653 Calling C<< resultset('Table')->next >> repeatedly will always return the
654 first record from the resultset.
660 if (my $cache = $self->get_cache) {
661 $self->{all_cache_position} ||= 0;
662 return $cache->[$self->{all_cache_position}++];
664 if ($self->{attrs}{cache}) {
665 $self->{all_cache_position} = 1;
666 return ($self->all)[0];
669 exists $self->{stashed_row}
670 ? @{delete $self->{stashed_row}}
671 : $self->cursor->next
673 return unless (@row);
674 return $self->_construct_object(@row);
677 sub _resolved_attrs {
679 return $self->{_attrs} if $self->{_attrs};
681 my $attrs = { %{$self->{attrs}||{}} };
682 my $source = $self->{result_source};
683 my $alias = $attrs->{alias};
685 # XXX - lose storable dclone
686 my $record_filter = delete $attrs->{record_filter};
687 #$attrs = Storable::dclone($attrs || {}); # { %{ $attrs || {} } };
689 $attrs->{record_filter} = $record_filter if $record_filter;
691 $attrs->{columns} ||= delete $attrs->{cols} if exists $attrs->{cols};
692 if ($attrs->{columns}) {
694 } elsif (!$attrs->{select}) {
695 $attrs->{columns} = [ $self->{result_source}->columns ];
698 my $select_alias = $self->{attrs}{alias};
699 $attrs->{select} ||= [
700 map { m/\./ ? $_ : "${select_alias}.$_" } @{delete $attrs->{columns}}
703 map { m/^\Q${alias}.\E(.+)$/ ? $1 : $_ } @{$attrs->{select}}
707 if ($adds = delete $attrs->{include_columns}) {
708 $adds = [$adds] unless ref $adds eq 'ARRAY';
709 push(@{$attrs->{select}}, @$adds);
710 push(@{$attrs->{as}}, map { m/([^.]+)$/; $1 } @$adds);
712 if ($adds = delete $attrs->{'+select'}) {
713 $adds = [$adds] unless ref $adds eq 'ARRAY';
714 push(@{$attrs->{select}}, map { /\./ || ref $_ ? $_ : "${alias}.$_" } @$adds);
716 if (my $adds = delete $attrs->{'+as'}) {
717 $adds = [$adds] unless ref $adds eq 'ARRAY';
718 push(@{$attrs->{as}}, @$adds);
722 $attrs->{from} ||= $attrs->{_parent_from}
723 || [ { $alias => $source->from } ];
724 #$attrs->{from} ||= [ { $alias => $source->from } ];
726 if (exists $attrs->{join} || exists $attrs->{prefetch}) {
728 my $join = delete $attrs->{join} || {};
730 if (defined $attrs->{prefetch}) {
731 $join = $self->_merge_attr(
732 $join, $attrs->{prefetch}
736 push(@{$attrs->{from}},
737 $source->resolve_join($join, $alias, { %{$self->{_parent_seen_join}||{}} })
741 $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct};
742 if ($attrs->{order_by}) {
743 $attrs->{order_by} = [ $attrs->{order_by} ] unless ref $attrs->{order_by};
745 $attrs->{order_by} ||= [];
748 my $collapse = $attrs->{collapse} || {};
749 if (my $prefetch = delete $attrs->{prefetch}) {
751 foreach my $p (ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch)) {
752 # bring joins back to level of current class
753 my @prefetch = $source->resolve_prefetch(
754 $p, $alias, { %{$attrs->{_parent_seen_join}||{}} }, \@pre_order, $collapse
756 push(@{$attrs->{select}}, map { $_->[0] } @prefetch);
757 push(@{$attrs->{as}}, map { $_->[1] } @prefetch);
759 push(@{$attrs->{order_by}}, @pre_order);
761 $attrs->{collapse} = $collapse;
763 return $self->{_attrs} = $attrs;
768 my $source = $self->result_source;
769 my $attrs = $self->{attrs};
771 my $from = $attrs->{_parent_from}
772 || [ { $attrs->{alias} => $source->from } ];
774 my $seen = { %{$attrs->{_parent_seen_join}||{}} };
776 if ($attrs->{join}) {
778 $source->resolve_join($attrs->{join}, $attrs->{alias}, $seen)
782 return ($from,$seen);
786 my ($self, $a, $b) = @_;
789 if (ref $b eq 'HASH' && ref $a eq 'HASH') {
790 foreach my $key (keys %{$b}) {
791 if (exists $a->{$key}) {
792 $a->{$key} = $self->_merge_attr($a->{$key}, $b->{$key});
794 $a->{$key} = $b->{$key};
799 $a = [$a] unless ref $a eq 'ARRAY';
800 $b = [$b] unless ref $b eq 'ARRAY';
804 foreach my $x ($a, $b) {
805 foreach my $element (@{$x}) {
806 if (ref $element eq 'HASH') {
807 $hash = $self->_merge_attr($hash, $element);
808 } elsif (ref $element eq 'ARRAY') {
809 push(@array, @{$element});
811 push(@array, $element) unless $b == $x
812 && grep { $_ eq $element } @array;
817 @array = grep { !exists $hash->{$_} } @array;
828 sub _construct_object {
829 my ($self, @row) = @_;
830 my $info = $self->_collapse_result($self->{_attrs}{as}, \@row);
831 my $new = $self->result_class->inflate_result($self->result_source, @$info);
832 $new = $self->{_attrs}{record_filter}->($new)
833 if exists $self->{_attrs}{record_filter};
837 sub _collapse_result {
838 my ($self, $as, $row, $prefix) = @_;
843 foreach my $this_as (@$as) {
844 my $val = shift @copy;
845 if (defined $prefix) {
846 if ($this_as =~ m/^\Q${prefix}.\E(.+)$/) {
848 $remain =~ /^(?:(.*)\.)?([^.]+)$/;
849 $const{$1||''}{$2} = $val;
852 $this_as =~ /^(?:(.*)\.)?([^.]+)$/;
853 $const{$1||''}{$2} = $val;
857 my $alias = $self->{attrs}{alias};
858 my $info = [ {}, {} ];
859 foreach my $key (keys %const) {
860 if (length $key && $key ne $alias) {
862 my @parts = split(/\./, $key);
863 foreach my $p (@parts) {
864 $target = $target->[1]->{$p} ||= [];
866 $target->[0] = $const{$key};
868 $info->[0] = $const{$key};
873 if (defined $prefix) {
875 m/^\Q${prefix}.\E(.+)$/ ? ($1) : ()
876 } keys %{$self->{_attrs}{collapse}}
878 @collapse = keys %{$self->{_attrs}{collapse}};
882 my ($c) = sort { length $a <=> length $b } @collapse;
884 foreach my $p (split(/\./, $c)) {
885 $target = $target->[1]->{$p} ||= [];
887 my $c_prefix = (defined($prefix) ? "${prefix}.${c}" : $c);
888 my @co_key = @{$self->{_attrs}{collapse}{$c_prefix}};
889 my $tree = $self->_collapse_result($as, $row, $c_prefix);
890 my %co_check = map { ($_, $tree->[0]->{$_}); } @co_key;
896 !defined($tree->[0]->{$_}) || $co_check{$_} ne $tree->[0]->{$_}
901 last unless (@raw = $self->cursor->next);
902 $row = $self->{stashed_row} = \@raw;
903 $tree = $self->_collapse_result($as, $row, $c_prefix);
905 @$target = (@final ? @final : [ {}, {} ]);
906 # single empty result to indicate an empty prefetched has_many
909 #print "final info: " . Dumper($info);
917 =item Arguments: $result_source?
919 =item Return Value: $result_source
923 An accessor for the primary ResultSource object from which this ResultSet
933 =item Arguments: $cond, \%attrs??
935 =item Return Value: $count
939 Performs an SQL C<COUNT> with the same query as the resultset was built
940 with to find the number of elements. If passed arguments, does a search
941 on the resultset and counts the results of that.
943 Note: When using C<count> with C<group_by>, L<DBIX::Class> emulates C<GROUP BY>
944 using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
945 not support C<DISTINCT> with multiple columns. If you are using such a
946 database, you should only use columns from the main table in your C<group_by>
953 return $self->search(@_)->count if @_ and defined $_[0];
954 return scalar @{ $self->get_cache } if $self->get_cache;
955 my $count = $self->_count;
956 return 0 unless $count;
958 $count -= $self->{attrs}{offset} if $self->{attrs}{offset};
959 $count = $self->{attrs}{rows} if
960 $self->{attrs}{rows} and $self->{attrs}{rows} < $count;
964 sub _count { # Separated out so pager can get the full count
966 my $select = { count => '*' };
968 my $attrs = { %{$self->_resolved_attrs} };
969 if (my $group_by = delete $attrs->{group_by}) {
970 delete $attrs->{having};
971 my @distinct = (ref $group_by ? @$group_by : ($group_by));
972 # todo: try CONCAT for multi-column pk
973 my @pk = $self->result_source->primary_columns;
975 my $alias = $attrs->{alias};
976 foreach my $column (@distinct) {
977 if ($column =~ qr/^(?:\Q${alias}.\E)?$pk[0]$/) {
978 @distinct = ($column);
984 $select = { count => { distinct => \@distinct } };
987 $attrs->{select} = $select;
988 $attrs->{as} = [qw/count/];
990 # offset, order by and page are not needed to count. record_filter is cdbi
991 delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
993 my $tmp_rs = (ref $self)->new($self->result_source, $attrs);
994 my ($count) = $tmp_rs->cursor->next;
1002 =item Arguments: $sql_fragment, @bind_values
1004 =item Return Value: $count
1008 Counts the results in a literal query. Equivalent to calling L</search_literal>
1009 with the passed arguments, then L</count>.
1013 sub count_literal { shift->search_literal(@_)->count; }
1019 =item Arguments: none
1021 =item Return Value: @objects
1025 Returns all elements in the resultset. Called implicitly if the resultset
1026 is returned in list context.
1032 return @{ $self->get_cache } if $self->get_cache;
1036 # TODO: don't call resolve here
1037 if (keys %{$self->_resolved_attrs->{collapse}}) {
1038 # if ($self->{attrs}{prefetch}) {
1039 # Using $self->cursor->all is really just an optimisation.
1040 # If we're collapsing has_many prefetches it probably makes
1041 # very little difference, and this is cleaner than hacking
1042 # _construct_object to survive the approach
1043 my @row = $self->cursor->next;
1045 push(@obj, $self->_construct_object(@row));
1046 @row = (exists $self->{stashed_row}
1047 ? @{delete $self->{stashed_row}}
1048 : $self->cursor->next);
1051 @obj = map { $self->_construct_object(@$_) } $self->cursor->all;
1054 $self->set_cache(\@obj) if $self->{attrs}{cache};
1062 =item Arguments: none
1064 =item Return Value: $self
1068 Resets the resultset's cursor, so you can iterate through the elements again.
1074 delete $self->{_attrs} if exists $self->{_attrs};
1075 $self->{all_cache_position} = 0;
1076 $self->cursor->reset;
1084 =item Arguments: none
1086 =item Return Value: $object?
1090 Resets the resultset and returns an object for the first result (if the
1091 resultset returns anything).
1096 return $_[0]->reset->next;
1099 # _cond_for_update_delete
1101 # update/delete require the condition to be modified to handle
1102 # the differing SQL syntax available. This transforms the $self->{cond}
1103 # appropriately, returning the new condition.
1105 sub _cond_for_update_delete {
1109 # No-op. No condition, we're updating/deleting everything
1110 return $cond unless ref $self->{cond};
1112 if (ref $self->{cond} eq 'ARRAY') {
1116 foreach my $key (keys %{$_}) {
1118 $hash{$1} = $_->{$key};
1124 elsif (ref $self->{cond} eq 'HASH') {
1125 if ((keys %{$self->{cond}})[0] eq '-and') {
1128 my @cond = @{$self->{cond}{-and}};
1129 for (my $i = 0; $i < @cond; $i++) {
1130 my $entry = $cond[$i];
1133 if (ref $entry eq 'HASH') {
1134 foreach my $key (keys %{$entry}) {
1136 $hash{$1} = $entry->{$key};
1140 $entry =~ /([^.]+)$/;
1141 $hash{$1} = $cond[++$i];
1144 push @{$cond->{-and}}, \%hash;
1148 foreach my $key (keys %{$self->{cond}}) {
1150 $cond->{$1} = $self->{cond}{$key};
1155 $self->throw_exception(
1156 "Can't update/delete on resultset with condition unless hash or array"
1168 =item Arguments: \%values
1170 =item Return Value: $storage_rv
1174 Sets the specified columns in the resultset to the supplied values in a
1175 single query. Return value will be true if the update succeeded or false
1176 if no records were updated; exact type of success value is storage-dependent.
1181 my ($self, $values) = @_;
1182 $self->throw_exception("Values for update must be a hash")
1183 unless ref $values eq 'HASH';
1185 my $cond = $self->_cond_for_update_delete;
1187 return $self->result_source->storage->update(
1188 $self->result_source->from, $values, $cond
1196 =item Arguments: \%values
1198 =item Return Value: 1
1202 Fetches all objects and updates them one at a time. Note that C<update_all>
1203 will run DBIC cascade triggers, while L</update> will not.
1208 my ($self, $values) = @_;
1209 $self->throw_exception("Values for update must be a hash")
1210 unless ref $values eq 'HASH';
1211 foreach my $obj ($self->all) {
1212 $obj->set_columns($values)->update;
1221 =item Arguments: none
1223 =item Return Value: 1
1227 Deletes the contents of the resultset from its result source. Note that this
1228 will not run DBIC cascade triggers. See L</delete_all> if you need triggers
1236 my $cond = $self->_cond_for_update_delete;
1238 $self->result_source->storage->delete($self->result_source->from, $cond);
1246 =item Arguments: none
1248 =item Return Value: 1
1252 Fetches all objects and deletes them one at a time. Note that C<delete_all>
1253 will run DBIC cascade triggers, while L</delete> will not.
1259 $_->delete for $self->all;
1267 =item Arguments: none
1269 =item Return Value: $pager
1273 Return Value a L<Data::Page> object for the current resultset. Only makes
1274 sense for queries with a C<page> attribute.
1280 my $attrs = $self->{attrs};
1281 $self->throw_exception("Can't create pager for non-paged rs")
1282 unless $self->{attrs}{page};
1283 $attrs->{rows} ||= 10;
1284 return $self->{pager} ||= Data::Page->new(
1285 $self->_count, $attrs->{rows}, $self->{attrs}{page});
1292 =item Arguments: $page_number
1294 =item Return Value: $rs
1298 Returns a resultset for the $page_number page of the resultset on which page
1299 is called, where each page contains a number of rows equal to the 'rows'
1300 attribute set on the resultset (10 by default).
1305 my ($self, $page) = @_;
1306 return (ref $self)->new($self->result_source, { %{$self->{attrs}}, page => $page });
1313 =item Arguments: \%vals
1315 =item Return Value: $object
1319 Creates an object in the resultset's result class and returns it.
1324 my ($self, $values) = @_;
1325 $self->throw_exception( "new_result needs a hash" )
1326 unless (ref $values eq 'HASH');
1327 $self->throw_exception(
1328 "Can't abstract implicit construct, condition not a hash"
1329 ) if ($self->{cond} && !(ref $self->{cond} eq 'HASH'));
1331 my $alias = $self->{attrs}{alias};
1332 foreach my $key (keys %{$self->{cond}||{}}) {
1333 $new{$1} = $self->{cond}{$key} if ($key =~ m/^(?:\Q${alias}.\E)?([^.]+)$/);
1335 my $obj = $self->result_class->new(\%new);
1336 $obj->result_source($self->result_source) if $obj->can('result_source');
1344 =item Arguments: \%vals, \%attrs?
1346 =item Return Value: $object
1350 Find an existing record from this resultset. If none exists, instantiate a new
1351 result object and return it. The object will not be saved into your storage
1352 until you call L<DBIx::Class::Row/insert> on it.
1354 If you want objects to be saved immediately, use L</find_or_create> instead.
1360 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1361 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1362 my $exists = $self->find($hash, $attrs);
1363 return defined $exists ? $exists : $self->new_result($hash);
1370 =item Arguments: \%vals
1372 =item Return Value: $object
1376 Inserts a record into the resultset and returns the object representing it.
1378 Effectively a shortcut for C<< ->new_result(\%vals)->insert >>.
1383 my ($self, $attrs) = @_;
1384 $self->throw_exception( "create needs a hashref" )
1385 unless ref $attrs eq 'HASH';
1386 return $self->new_result($attrs)->insert;
1389 =head2 find_or_create
1393 =item Arguments: \%vals, \%attrs?
1395 =item Return Value: $object
1399 $class->find_or_create({ key => $val, ... });
1401 Tries to find a record based on its primary key or unique constraint; if none
1402 is found, creates one and returns that instead.
1404 my $cd = $schema->resultset('CD')->find_or_create({
1406 artist => 'Massive Attack',
1407 title => 'Mezzanine',
1411 Also takes an optional C<key> attribute, to search by a specific key or unique
1412 constraint. For example:
1414 my $cd = $schema->resultset('CD')->find_or_create(
1416 artist => 'Massive Attack',
1417 title => 'Mezzanine',
1419 { key => 'cd_artist_title' }
1422 See also L</find> and L</update_or_create>. For information on how to declare
1423 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1427 sub find_or_create {
1429 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1430 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1431 my $exists = $self->find($hash, $attrs);
1432 return defined $exists ? $exists : $self->create($hash);
1435 =head2 update_or_create
1439 =item Arguments: \%col_values, { key => $unique_constraint }?
1441 =item Return Value: $object
1445 $class->update_or_create({ col => $val, ... });
1447 First, searches for an existing row matching one of the unique constraints
1448 (including the primary key) on the source of this resultset. If a row is
1449 found, updates it with the other given column values. Otherwise, creates a new
1452 Takes an optional C<key> attribute to search on a specific unique constraint.
1455 # In your application
1456 my $cd = $schema->resultset('CD')->update_or_create(
1458 artist => 'Massive Attack',
1459 title => 'Mezzanine',
1462 { key => 'cd_artist_title' }
1465 If no C<key> is specified, it searches on all unique constraints defined on the
1466 source, including the primary key.
1468 If the C<key> is specified as C<primary>, it searches only on the primary key.
1470 See also L</find> and L</find_or_create>. For information on how to declare
1471 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1475 sub update_or_create {
1477 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1478 my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
1480 my $row = $self->find($cond);
1482 $row->update($cond);
1486 return $self->create($cond);
1493 =item Arguments: none
1495 =item Return Value: \@cache_objects?
1499 Gets the contents of the cache for the resultset, if the cache is set.
1511 =item Arguments: \@cache_objects
1513 =item Return Value: \@cache_objects
1517 Sets the contents of the cache for the resultset. Expects an arrayref
1518 of objects of the same class as those produced by the resultset. Note that
1519 if the cache is set the resultset will return the cached objects rather
1520 than re-querying the database even if the cache attr is not set.
1525 my ( $self, $data ) = @_;
1526 $self->throw_exception("set_cache requires an arrayref")
1527 if defined($data) && (ref $data ne 'ARRAY');
1528 $self->{all_cache} = $data;
1535 =item Arguments: none
1537 =item Return Value: []
1541 Clears the cache for the resultset.
1546 shift->set_cache(undef);
1549 =head2 related_resultset
1553 =item Arguments: $relationship_name
1555 =item Return Value: $resultset
1559 Returns a related resultset for the supplied relationship name.
1561 $artist_rs = $schema->resultset('CD')->related_resultset('Artist');
1565 sub related_resultset {
1566 my ($self, $rel) = @_;
1568 $self->{related_resultsets} ||= {};
1569 return $self->{related_resultsets}{$rel} ||= do {
1570 my $rel_obj = $self->result_source->relationship_info($rel);
1572 $self->throw_exception(
1573 "search_related: result source '" . $self->result_source->name .
1574 "' has no such relationship $rel")
1577 my ($from,$seen) = $self->_resolve_from;
1579 my $join_count = $self->{_parent_seen_join}{$rel};
1580 my $alias = $join_count ? join('_', $rel, $join_count+1) : $rel;
1582 my $rs = $self->result_source->schema->resultset($rel_obj->{class})->search(
1587 _parent_from => $from,
1588 _parent_seen_join => $seen,
1589 _parent_attrs => $self->{attrs},
1596 =head2 throw_exception
1598 See L<DBIx::Class::Schema/throw_exception> for details.
1602 sub throw_exception {
1604 $self->result_source->schema->throw_exception(@_);
1607 # XXX: FIXME: Attributes docs need clearing up
1611 The resultset takes various attributes that modify its behavior. Here's an
1618 =item Value: ($order_by | \@order_by)
1622 Which column(s) to order the results by. This is currently passed
1623 through directly to SQL, so you can give e.g. C<year DESC> for a
1624 descending order on the column `year'.
1626 Please note that if you have quoting enabled (see
1627 L<DBIx::Class::Storage/quote_char>) you will need to do C<\'year DESC' > to
1628 specify an order. (The scalar ref causes it to be passed as raw sql to the DB,
1629 so you will need to manually quote things as appropriate.)
1635 =item Value: \@columns
1639 Shortcut to request a particular set of columns to be retrieved. Adds
1640 C<me.> onto the start of any column without a C<.> in it and sets C<select>
1641 from that, then auto-populates C<as> from C<select> as normal. (You may also
1642 use the C<cols> attribute, as in earlier versions of DBIC.)
1644 =head2 include_columns
1648 =item Value: \@columns
1652 Shortcut to include additional columns in the returned results - for example
1654 $schema->resultset('CD')->search(undef, {
1655 include_columns => ['artist.name'],
1659 would return all CDs and include a 'name' column to the information
1660 passed to object inflation
1666 =item Value: \@select_columns
1670 Indicates which columns should be selected from the storage. You can use
1671 column names, or in the case of RDBMS back ends, function or stored procedure
1674 $rs = $schema->resultset('Employee')->search(undef, {
1677 { count => 'employeeid' },
1682 When you use function/stored procedure names and do not supply an C<as>
1683 attribute, the column names returned are storage-dependent. E.g. MySQL would
1684 return a column named C<count(employeeid)> in the above example.
1690 Indicates additional columns to be selected from storage. Works the same as
1691 L<select> but adds columns to the selection.
1699 Indicates additional column names for those added via L<+select>.
1707 =item Value: \@inflation_names
1711 Indicates column names for object inflation. This is used in conjunction with
1712 C<select>, usually when C<select> contains one or more function or stored
1715 $rs = $schema->resultset('Employee')->search(undef, {
1718 { count => 'employeeid' }
1720 as => ['name', 'employee_count'],
1723 my $employee = $rs->first(); # get the first Employee
1725 If the object against which the search is performed already has an accessor
1726 matching a column name specified in C<as>, the value can be retrieved using
1727 the accessor as normal:
1729 my $name = $employee->name();
1731 If on the other hand an accessor does not exist in the object, you need to
1732 use C<get_column> instead:
1734 my $employee_count = $employee->get_column('employee_count');
1736 You can create your own accessors if required - see
1737 L<DBIx::Class::Manual::Cookbook> for details.
1739 Please note: This will NOT insert an C<AS employee_count> into the SQL statement
1740 produced, it is used for internal access only. Thus attempting to use the accessor
1741 in an C<order_by> clause or similar will fail misrably.
1747 =item Value: ($rel_name | \@rel_names | \%rel_names)
1751 Contains a list of relationships that should be joined for this query. For
1754 # Get CDs by Nine Inch Nails
1755 my $rs = $schema->resultset('CD')->search(
1756 { 'artist.name' => 'Nine Inch Nails' },
1757 { join => 'artist' }
1760 Can also contain a hash reference to refer to the other relation's relations.
1763 package MyApp::Schema::Track;
1764 use base qw/DBIx::Class/;
1765 __PACKAGE__->table('track');
1766 __PACKAGE__->add_columns(qw/trackid cd position title/);
1767 __PACKAGE__->set_primary_key('trackid');
1768 __PACKAGE__->belongs_to(cd => 'MyApp::Schema::CD');
1771 # In your application
1772 my $rs = $schema->resultset('Artist')->search(
1773 { 'track.title' => 'Teardrop' },
1775 join => { cd => 'track' },
1776 order_by => 'artist.name',
1780 If the same join is supplied twice, it will be aliased to <rel>_2 (and
1781 similarly for a third time). For e.g.
1783 my $rs = $schema->resultset('Artist')->search({
1784 'cds.title' => 'Down to Earth',
1785 'cds_2.title' => 'Popular',
1787 join => [ qw/cds cds/ ],
1790 will return a set of all artists that have both a cd with title 'Down
1791 to Earth' and a cd with title 'Popular'.
1793 If you want to fetch related objects from other tables as well, see C<prefetch>
1800 =item Value: ($rel_name | \@rel_names | \%rel_names)
1804 Contains one or more relationships that should be fetched along with the main
1805 query (when they are accessed afterwards they will have already been
1806 "prefetched"). This is useful for when you know you will need the related
1807 objects, because it saves at least one query:
1809 my $rs = $schema->resultset('Tag')->search(
1818 The initial search results in SQL like the following:
1820 SELECT tag.*, cd.*, artist.* FROM tag
1821 JOIN cd ON tag.cd = cd.cdid
1822 JOIN artist ON cd.artist = artist.artistid
1824 L<DBIx::Class> has no need to go back to the database when we access the
1825 C<cd> or C<artist> relationships, which saves us two SQL statements in this
1828 Simple prefetches will be joined automatically, so there is no need
1829 for a C<join> attribute in the above search. If you're prefetching to
1830 depth (e.g. { cd => { artist => 'label' } or similar), you'll need to
1831 specify the join as well.
1833 C<prefetch> can be used with the following relationship types: C<belongs_to>,
1834 C<has_one> (or if you're using C<add_relationship>, any relationship declared
1835 with an accessor type of 'single' or 'filter').
1845 Makes the resultset paged and specifies the page to retrieve. Effectively
1846 identical to creating a non-pages resultset and then calling ->page($page)
1849 If L<rows> attribute is not specified it defualts to 10 rows per page.
1859 Specifes the maximum number of rows for direct retrieval or the number of
1860 rows per page if the page attribute or method is used.
1866 =item Value: $offset
1870 Specifies the (zero-based) row number for the first row to be returned, or the
1871 of the first row of the first page if paging is used.
1877 =item Value: \@columns
1881 A arrayref of columns to group by. Can include columns of joined tables.
1883 group_by => [qw/ column1 column2 ... /]
1889 =item Value: $condition
1893 HAVING is a select statement attribute that is applied between GROUP BY and
1894 ORDER BY. It is applied to the after the grouping calculations have been
1897 having => { 'count(employee)' => { '>=', 100 } }
1903 =item Value: (0 | 1)
1907 Set to 1 to group by all columns.
1913 Adds to the WHERE clause.
1915 # only return rows WHERE deleted IS NULL for all searches
1916 __PACKAGE__->resultset_attributes({ where => { deleted => undef } }); )
1918 Can be overridden by passing C<{ where => undef }> as an attribute
1925 Set to 1 to cache search results. This prevents extra SQL queries if you
1926 revisit rows in your ResultSet:
1928 my $resultset = $schema->resultset('Artist')->search( undef, { cache => 1 } );
1930 while( my $artist = $resultset->next ) {
1934 $rs->first; # without cache, this would issue a query
1936 By default, searches are not cached.
1938 For more examples of using these attributes, see
1939 L<DBIx::Class::Manual::Cookbook>.
1945 =item Value: \@from_clause
1949 The C<from> attribute gives you manual control over the C<FROM> clause of SQL
1950 statements generated by L<DBIx::Class>, allowing you to express custom C<JOIN>
1953 NOTE: Use this on your own risk. This allows you to shoot off your foot!
1955 C<join> will usually do what you need and it is strongly recommended that you
1956 avoid using C<from> unless you cannot achieve the desired result using C<join>.
1957 And we really do mean "cannot", not just tried and failed. Attempting to use
1958 this because you're having problems with C<join> is like trying to use x86
1959 ASM because you've got a syntax error in your C. Trust us on this.
1961 Now, if you're still really, really sure you need to use this (and if you're
1962 not 100% sure, ask the mailing list first), here's an explanation of how this
1965 The syntax is as follows -
1968 { <alias1> => <table1> },
1970 { <alias2> => <table2>, -join_type => 'inner|left|right' },
1971 [], # nested JOIN (optional)
1972 { <table1.column1> => <table2.column2>, ... (more conditions) },
1974 # More of the above [ ] may follow for additional joins
1981 ON <table1.column1> = <table2.column2>
1982 <more joins may follow>
1984 An easy way to follow the examples below is to remember the following:
1986 Anything inside "[]" is a JOIN
1987 Anything inside "{}" is a condition for the enclosing JOIN
1989 The following examples utilize a "person" table in a family tree application.
1990 In order to express parent->child relationships, this table is self-joined:
1992 # Person->belongs_to('father' => 'Person');
1993 # Person->belongs_to('mother' => 'Person');
1995 C<from> can be used to nest joins. Here we return all children with a father,
1996 then search against all mothers of those children:
1998 $rs = $schema->resultset('Person')->search(
2001 alias => 'mother', # alias columns in accordance with "from"
2003 { mother => 'person' },
2006 { child => 'person' },
2008 { father => 'person' },
2009 { 'father.person_id' => 'child.father_id' }
2012 { 'mother.person_id' => 'child.mother_id' }
2019 # SELECT mother.* FROM person mother
2022 # JOIN person father
2023 # ON ( father.person_id = child.father_id )
2025 # ON ( mother.person_id = child.mother_id )
2027 The type of any join can be controlled manually. To search against only people
2028 with a father in the person table, we could explicitly use C<INNER JOIN>:
2030 $rs = $schema->resultset('Person')->search(
2033 alias => 'child', # alias columns in accordance with "from"
2035 { child => 'person' },
2037 { father => 'person', -join_type => 'inner' },
2038 { 'father.id' => 'child.father_id' }
2045 # SELECT child.* FROM person child
2046 # INNER JOIN person father ON child.father_id = father.id