1 package DBIx::Class::ResultSet;
9 use Carp::Clan qw/^DBIx::Class/;
12 use DBIx::Class::ResultSetColumn;
13 use DBIx::Class::ResultSourceHandle;
16 use base qw/DBIx::Class/;
18 __PACKAGE__->mk_group_accessors('simple' => qw/_result_class _source_handle/);
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 })->all();
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');
57 If a resultset is used in a numeric context it returns the L</count>.
58 However, if it is used in a booleand context it is always true. So if
59 you want to check if a resultset has any results use C<if $rs != 0>.
60 C<if $rs> will always be true.
68 =item Arguments: $source, \%$attrs
70 =item Return Value: $rs
74 The resultset constructor. Takes a source object (usually a
75 L<DBIx::Class::ResultSourceProxy::Table>) and an attribute hash (see
76 L</ATTRIBUTES> below). Does not perform any queries -- these are
77 executed as needed by the other methods.
79 Generally you won't need to construct a resultset manually. You'll
80 automatically get one from e.g. a L</search> called in scalar context:
82 my $rs = $schema->resultset('CD')->search({ title => '100th Window' });
84 IMPORTANT: If called on an object, proxies to new_result instead so
86 my $cd = $schema->resultset('CD')->new({ title => 'Spoon' });
88 will return a CD object, not a ResultSet.
94 return $class->new_result(@_) if ref $class;
96 my ($source, $attrs) = @_;
97 $source = $source->handle
98 unless $source->isa('DBIx::Class::ResultSourceHandle');
99 $attrs = { %{$attrs||{}} };
101 if ($attrs->{page}) {
102 $attrs->{rows} ||= 10;
105 $attrs->{alias} ||= 'me';
107 # Creation of {} and bless separated to mitigate RH perl bug
108 # see https://bugzilla.redhat.com/show_bug.cgi?id=196836
110 _source_handle => $source,
111 cond => $attrs->{where},
120 $attrs->{result_class} || $source->resolve->result_class
130 =item Arguments: $cond, \%attrs?
132 =item Return Value: $resultset (scalar context), @row_objs (list context)
136 my @cds = $cd_rs->search({ year => 2001 }); # "... WHERE year = 2001"
137 my $new_rs = $cd_rs->search({ year => 2005 });
139 my $new_rs = $cd_rs->search([ { year => 2005 }, { year => 2004 } ]);
140 # year = 2005 OR year = 2004
142 If you need to pass in additional attributes but no additional condition,
143 call it as C<search(undef, \%attrs)>.
145 # "SELECT name, artistid FROM $artist_table"
146 my @all_artists = $schema->resultset('Artist')->search(undef, {
147 columns => [qw/name artistid/],
150 For a list of attributes that can be passed to C<search>, see
151 L</ATTRIBUTES>. For more examples of using this function, see
152 L<Searching|DBIx::Class::Manual::Cookbook/Searching>. For a complete
153 documentation for the first argument, see L<SQL::Abstract>.
155 For more help on using joins with search, see L<DBIx::Class::Manual::Joining>.
161 my $rs = $self->search_rs( @_ );
162 return (wantarray ? $rs->all : $rs);
169 =item Arguments: $cond, \%attrs?
171 =item Return Value: $resultset
175 This method does the same exact thing as search() except it will
176 always return a resultset, even in list context.
184 $attrs = pop(@_) if @_ > 1 and ref $_[$#_] eq 'HASH';
185 my $our_attrs = { %{$self->{attrs}} };
186 my $having = delete $our_attrs->{having};
187 my $where = delete $our_attrs->{where};
191 my %safe = (alias => 1, cache => 1);
194 (@_ && defined($_[0])) # @_ == () or (undef)
196 (keys %$attrs # empty attrs or only 'safe' attrs
197 && List::Util::first { !$safe{$_} } keys %$attrs)
199 # no search, effectively just a clone
200 $rows = $self->get_cache;
203 my $new_attrs = { %{$our_attrs}, %{$attrs} };
205 # merge new attrs into inherited
206 foreach my $key (qw/join prefetch +select +as/) {
207 next unless exists $attrs->{$key};
208 $new_attrs->{$key} = $self->_merge_attr($our_attrs->{$key}, $attrs->{$key});
213 (@_ == 1 || ref $_[0] eq "HASH")
215 (ref $_[0] eq 'HASH')
217 (keys %{ $_[0] } > 0)
225 ? $self->throw_exception("Odd number of arguments to search")
232 if (defined $where) {
233 $new_attrs->{where} = (
234 defined $new_attrs->{where}
237 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
238 } $where, $new_attrs->{where}
245 $new_attrs->{where} = (
246 defined $new_attrs->{where}
249 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
250 } $cond, $new_attrs->{where}
256 if (defined $having) {
257 $new_attrs->{having} = (
258 defined $new_attrs->{having}
261 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
262 } $having, $new_attrs->{having}
268 my $rs = (ref $self)->new($self->result_source, $new_attrs);
270 $rs->set_cache($rows);
275 =head2 search_literal
279 =item Arguments: $sql_fragment, @bind_values
281 =item Return Value: $resultset (scalar context), @row_objs (list context)
285 my @cds = $cd_rs->search_literal('year = ? AND title = ?', qw/2001 Reload/);
286 my $newrs = $artist_rs->search_literal('name = ?', 'Metallica');
288 Pass a literal chunk of SQL to be added to the conditional part of the
291 CAVEAT: C<search_literal> is provided for Class::DBI compatibility and should
292 only be used in that context. There are known problems using C<search_literal>
293 in chained queries; it can result in bind values in the wrong order. See
294 L<DBIx::Class::Manual::Cookbook/Searching> and
295 L<DBIx::Class::Manual::FAQ/Searching> for searching techniques that do not
296 require C<search_literal>.
301 my ($self, $cond, @vals) = @_;
302 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
303 $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ];
304 return $self->search(\$cond, $attrs);
311 =item Arguments: @values | \%cols, \%attrs?
313 =item Return Value: $row_object | undef
317 Finds a row based on its primary key or unique constraint. For example, to find
318 a row by its primary key:
320 my $cd = $schema->resultset('CD')->find(5);
322 You can also find a row by a specific unique constraint using the C<key>
323 attribute. For example:
325 my $cd = $schema->resultset('CD')->find('Massive Attack', 'Mezzanine', {
326 key => 'cd_artist_title'
329 Additionally, you can specify the columns explicitly by name:
331 my $cd = $schema->resultset('CD')->find(
333 artist => 'Massive Attack',
334 title => 'Mezzanine',
336 { key => 'cd_artist_title' }
339 If the C<key> is specified as C<primary>, it searches only on the primary key.
341 If no C<key> is specified, it searches on all unique constraints defined on the
342 source for which column data is provided, including the primary key.
344 If your table does not have a primary key, you B<must> provide a value for the
345 C<key> attribute matching one of the unique constraints on the source.
347 In addition to C<key>, L</find> recognizes and applies standard
348 L<resultset attributes|/ATTRIBUTES> in the same way as L</search> does.
350 Note: If your query does not return only one row, a warning is generated:
352 Query returned more than one row
354 See also L</find_or_create> and L</update_or_create>. For information on how to
355 declare unique constraints, see
356 L<DBIx::Class::ResultSource/add_unique_constraint>.
362 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
364 # Default to the primary key, but allow a specific key
365 my @cols = exists $attrs->{key}
366 ? $self->result_source->unique_constraint_columns($attrs->{key})
367 : $self->result_source->primary_columns;
368 $self->throw_exception(
369 "Can't find unless a primary key is defined or unique constraint is specified"
372 # Parse out a hashref from input
374 if (ref $_[0] eq 'HASH') {
375 $input_query = { %{$_[0]} };
377 elsif (@_ == @cols) {
379 @{$input_query}{@cols} = @_;
382 # Compatibility: Allow e.g. find(id => $value)
383 carp "Find by key => value deprecated; please use a hashref instead";
387 my (%related, $info);
389 KEY: foreach my $key (keys %$input_query) {
390 if (ref($input_query->{$key})
391 && ($info = $self->result_source->relationship_info($key))) {
392 my $val = delete $input_query->{$key};
393 next KEY if (ref($val) eq 'ARRAY'); # has_many for multi_create
394 my $rel_q = $self->result_source->resolve_condition(
395 $info->{cond}, $val, $key
397 die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY';
398 @related{keys %$rel_q} = values %$rel_q;
401 if (my @keys = keys %related) {
402 @{$input_query}{@keys} = values %related;
406 # Build the final query: Default to the disjunction of the unique queries,
407 # but allow the input query in case the ResultSet defines the query or the
408 # user is abusing find
409 my $alias = exists $attrs->{alias} ? $attrs->{alias} : $self->{attrs}{alias};
411 if (exists $attrs->{key}) {
412 my @unique_cols = $self->result_source->unique_constraint_columns($attrs->{key});
413 my $unique_query = $self->_build_unique_query($input_query, \@unique_cols);
414 $query = $self->_add_alias($unique_query, $alias);
417 my @unique_queries = $self->_unique_queries($input_query, $attrs);
418 $query = @unique_queries
419 ? [ map { $self->_add_alias($_, $alias) } @unique_queries ]
420 : $self->_add_alias($input_query, $alias);
425 my $rs = $self->search($query, $attrs);
426 if (keys %{$rs->_resolved_attrs->{collapse}}) {
428 carp "Query returned more than one row" if $rs->next;
436 if (keys %{$self->_resolved_attrs->{collapse}}) {
437 my $rs = $self->search($query);
439 carp "Query returned more than one row" if $rs->next;
443 return $self->single($query);
450 # Add the specified alias to the specified query hash. A copy is made so the
451 # original query is not modified.
454 my ($self, $query, $alias) = @_;
456 my %aliased = %$query;
457 foreach my $col (grep { ! m/\./ } keys %aliased) {
458 $aliased{"$alias.$col"} = delete $aliased{$col};
466 # Build a list of queries which satisfy unique constraints.
468 sub _unique_queries {
469 my ($self, $query, $attrs) = @_;
471 my @constraint_names = exists $attrs->{key}
473 : $self->result_source->unique_constraint_names;
475 my $where = $self->_collapse_cond($self->{attrs}{where} || {});
476 my $num_where = scalar keys %$where;
479 foreach my $name (@constraint_names) {
480 my @unique_cols = $self->result_source->unique_constraint_columns($name);
481 my $unique_query = $self->_build_unique_query($query, \@unique_cols);
483 my $num_cols = scalar @unique_cols;
484 my $num_query = scalar keys %$unique_query;
486 my $total = $num_query + $num_where;
487 if ($num_query && ($num_query == $num_cols || $total == $num_cols)) {
488 # The query is either unique on its own or is unique in combination with
489 # the existing where clause
490 push @unique_queries, $unique_query;
494 return @unique_queries;
497 # _build_unique_query
499 # Constrain the specified query hash based on the specified column names.
501 sub _build_unique_query {
502 my ($self, $query, $unique_cols) = @_;
505 map { $_ => $query->{$_} }
506 grep { exists $query->{$_} }
511 =head2 search_related
515 =item Arguments: $rel, $cond, \%attrs?
517 =item Return Value: $new_resultset
521 $new_rs = $cd_rs->search_related('artist', {
525 Searches the specified relationship, optionally specifying a condition and
526 attributes for matching records. See L</ATTRIBUTES> for more information.
531 return shift->related_resultset(shift)->search(@_);
534 =head2 search_related_rs
536 This method works exactly the same as search_related, except that
537 it guarantees a restultset, even in list context.
541 sub search_related_rs {
542 return shift->related_resultset(shift)->search_rs(@_);
549 =item Arguments: none
551 =item Return Value: $cursor
555 Returns a storage-driven cursor to the given resultset. See
556 L<DBIx::Class::Cursor> for more information.
563 my $attrs = { %{$self->_resolved_attrs} };
564 return $self->{cursor}
565 ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select},
566 $attrs->{where},$attrs);
573 =item Arguments: $cond?
575 =item Return Value: $row_object?
579 my $cd = $schema->resultset('CD')->single({ year => 2001 });
581 Inflates the first result without creating a cursor if the resultset has
582 any records in it; if not returns nothing. Used by L</find> as a lean version of
585 While this method can take an optional search condition (just like L</search>)
586 being a fast-code-path it does not recognize search attributes. If you need to
587 add extra joins or similar, call L</search> and then chain-call L</single> on the
588 L<DBIx::Class::ResultSet> returned.
594 As of 0.08100, this method enforces the assumption that the preceeding
595 query returns only one row. If more than one row is returned, you will receive
598 Query returned more than one row
600 In this case, you should be using L</first> or L</find> instead, or if you really
601 know what you are doing, use the L</rows> attribute to explicitly limit the size
609 my ($self, $where) = @_;
611 $self->throw_exception('single() only takes search conditions, no attributes. You want ->search( $cond, $attrs )->single()');
614 my $attrs = { %{$self->_resolved_attrs} };
616 if (defined $attrs->{where}) {
619 [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
620 $where, delete $attrs->{where} ]
623 $attrs->{where} = $where;
627 # XXX: Disabled since it doesn't infer uniqueness in all cases
628 # unless ($self->_is_unique_query($attrs->{where})) {
629 # carp "Query not guaranteed to return a single row"
630 # . "; please declare your unique constraints or use search instead";
633 my @data = $self->result_source->storage->select_single(
634 $attrs->{from}, $attrs->{select},
635 $attrs->{where}, $attrs
638 return (@data ? ($self->_construct_object(@data))[0] : undef);
643 # Try to determine if the specified query is guaranteed to be unique, based on
644 # the declared unique constraints.
646 sub _is_unique_query {
647 my ($self, $query) = @_;
649 my $collapsed = $self->_collapse_query($query);
650 my $alias = $self->{attrs}{alias};
652 foreach my $name ($self->result_source->unique_constraint_names) {
653 my @unique_cols = map {
655 } $self->result_source->unique_constraint_columns($name);
657 # Count the values for each unique column
658 my %seen = map { $_ => 0 } @unique_cols;
660 foreach my $key (keys %$collapsed) {
661 my $aliased = $key =~ /\./ ? $key : "$alias.$key";
662 next unless exists $seen{$aliased}; # Additional constraints are okay
663 $seen{$aliased} = scalar keys %{ $collapsed->{$key} };
666 # If we get 0 or more than 1 value for a column, it's not necessarily unique
667 return 1 unless grep { $_ != 1 } values %seen;
675 # Recursively collapse the query, accumulating values for each column.
677 sub _collapse_query {
678 my ($self, $query, $collapsed) = @_;
682 if (ref $query eq 'ARRAY') {
683 foreach my $subquery (@$query) {
684 next unless ref $subquery; # -or
685 # warn "ARRAY: " . Dumper $subquery;
686 $collapsed = $self->_collapse_query($subquery, $collapsed);
689 elsif (ref $query eq 'HASH') {
690 if (keys %$query and (keys %$query)[0] eq '-and') {
691 foreach my $subquery (@{$query->{-and}}) {
692 # warn "HASH: " . Dumper $subquery;
693 $collapsed = $self->_collapse_query($subquery, $collapsed);
697 # warn "LEAF: " . Dumper $query;
698 foreach my $col (keys %$query) {
699 my $value = $query->{$col};
700 $collapsed->{$col}{$value}++;
712 =item Arguments: $cond?
714 =item Return Value: $resultsetcolumn
718 my $max_length = $rs->get_column('length')->max;
720 Returns a L<DBIx::Class::ResultSetColumn> instance for a column of the ResultSet.
725 my ($self, $column) = @_;
726 my $new = DBIx::Class::ResultSetColumn->new($self, $column);
734 =item Arguments: $cond, \%attrs?
736 =item Return Value: $resultset (scalar context), @row_objs (list context)
740 # WHERE title LIKE '%blue%'
741 $cd_rs = $rs->search_like({ title => '%blue%'});
743 Performs a search, but uses C<LIKE> instead of C<=> as the condition. Note
744 that this is simply a convenience method retained for ex Class::DBI users.
745 You most likely want to use L</search> with specific operators.
747 For more information, see L<DBIx::Class::Manual::Cookbook>.
753 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
754 my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_};
755 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
756 return $class->search($query, { %$attrs });
763 =item Arguments: $first, $last
765 =item Return Value: $resultset (scalar context), @row_objs (list context)
769 Returns a resultset or object list representing a subset of elements from the
770 resultset slice is called on. Indexes are from 0, i.e., to get the first
773 my ($one, $two, $three) = $rs->slice(0, 2);
778 my ($self, $min, $max) = @_;
779 my $attrs = {}; # = { %{ $self->{attrs} || {} } };
780 $attrs->{offset} = $self->{attrs}{offset} || 0;
781 $attrs->{offset} += $min;
782 $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
783 return $self->search(undef(), $attrs);
784 #my $slice = (ref $self)->new($self->result_source, $attrs);
785 #return (wantarray ? $slice->all : $slice);
792 =item Arguments: none
794 =item Return Value: $result?
798 Returns the next element in the resultset (C<undef> is there is none).
800 Can be used to efficiently iterate over records in the resultset:
802 my $rs = $schema->resultset('CD')->search;
803 while (my $cd = $rs->next) {
807 Note that you need to store the resultset object, and call C<next> on it.
808 Calling C<< resultset('Table')->next >> repeatedly will always return the
809 first record from the resultset.
815 if (my $cache = $self->get_cache) {
816 $self->{all_cache_position} ||= 0;
817 return $cache->[$self->{all_cache_position}++];
819 if ($self->{attrs}{cache}) {
820 $self->{all_cache_position} = 1;
821 return ($self->all)[0];
823 if ($self->{stashed_objects}) {
824 my $obj = shift(@{$self->{stashed_objects}});
825 delete $self->{stashed_objects} unless @{$self->{stashed_objects}};
829 exists $self->{stashed_row}
830 ? @{delete $self->{stashed_row}}
831 : $self->cursor->next
833 return undef unless (@row);
834 my ($row, @more) = $self->_construct_object(@row);
835 $self->{stashed_objects} = \@more if @more;
839 sub _construct_object {
840 my ($self, @row) = @_;
841 my $info = $self->_collapse_result($self->{_attrs}{as}, \@row);
842 my @new = $self->result_class->inflate_result($self->result_source, @$info);
843 @new = $self->{_attrs}{record_filter}->(@new)
844 if exists $self->{_attrs}{record_filter};
848 sub _collapse_result {
849 my ($self, $as_proto, $row) = @_;
853 # 'foo' => [ undef, 'foo' ]
854 # 'foo.bar' => [ 'foo', 'bar' ]
855 # 'foo.bar.baz' => [ 'foo.bar', 'baz' ]
857 my @construct_as = map { [ (/^(?:(.*)\.)?([^.]+)$/) ] } @$as_proto;
859 my %collapse = %{$self->{_attrs}{collapse}||{}};
863 # if we're doing collapsing (has_many prefetch) we need to grab records
864 # until the PK changes, so fill @pri_index. if not, we leave it empty so
865 # we know we don't have to bother.
867 # the reason for not using the collapse stuff directly is because if you
868 # had for e.g. two artists in a row with no cds, the collapse info for
869 # both would be NULL (undef) so you'd lose the second artist
871 # store just the index so we can check the array positions from the row
872 # without having to contruct the full hash
874 if (keys %collapse) {
875 my %pri = map { ($_ => 1) } $self->result_source->primary_columns;
876 foreach my $i (0 .. $#construct_as) {
877 next if defined($construct_as[$i][0]); # only self table
878 if (delete $pri{$construct_as[$i][1]}) {
879 push(@pri_index, $i);
881 last unless keys %pri; # short circuit (Johnny Five Is Alive!)
885 # no need to do an if, it'll be empty if @pri_index is empty anyway
887 my %pri_vals = map { ($_ => $copy[$_]) } @pri_index;
891 do { # no need to check anything at the front, we always want the first row
895 foreach my $this_as (@construct_as) {
896 $const{$this_as->[0]||''}{$this_as->[1]} = shift(@copy);
899 push(@const_rows, \%const);
901 } until ( # no pri_index => no collapse => drop straight out
904 do { # get another row, stash it, drop out if different PK
906 @copy = $self->cursor->next;
907 $self->{stashed_row} = \@copy;
909 # last thing in do block, counts as true if anything doesn't match
911 # check xor defined first for NULL vs. NOT NULL then if one is
912 # defined the other must be so check string equality
915 (defined $pri_vals{$_} ^ defined $copy[$_])
916 || (defined $pri_vals{$_} && ($pri_vals{$_} ne $copy[$_]))
921 my $alias = $self->{attrs}{alias};
928 foreach my $const (@const_rows) {
929 scalar @const_keys or do {
930 @const_keys = sort { length($a) <=> length($b) } keys %$const;
932 foreach my $key (@const_keys) {
935 my @parts = split(/\./, $key);
937 my $data = $const->{$key};
938 foreach my $p (@parts) {
939 $target = $target->[1]->{$p} ||= [];
941 if ($cur eq ".${key}" && (my @ckey = @{$collapse{$cur}||[]})) {
942 # collapsing at this point and on final part
943 my $pos = $collapse_pos{$cur};
944 CK: foreach my $ck (@ckey) {
945 if (!defined $pos->{$ck} || $pos->{$ck} ne $data->{$ck}) {
946 $collapse_pos{$cur} = $data;
947 delete @collapse_pos{ # clear all positioning for sub-entries
948 grep { m/^\Q${cur}.\E/ } keys %collapse_pos
955 if (exists $collapse{$cur}) {
956 $target = $target->[-1];
959 $target->[0] = $data;
961 $info->[0] = $const->{$key};
973 =item Arguments: $result_source?
975 =item Return Value: $result_source
979 An accessor for the primary ResultSource object from which this ResultSet
986 =item Arguments: $result_class?
988 =item Return Value: $result_class
992 An accessor for the class to use when creating row objects. Defaults to
993 C<< result_source->result_class >> - which in most cases is the name of the
994 L<"table"|DBIx::Class::Manual::Glossary/"ResultSource"> class.
999 my ($self, $result_class) = @_;
1000 if ($result_class) {
1001 $self->ensure_class_loaded($result_class);
1002 $self->_result_class($result_class);
1004 $self->_result_class;
1011 =item Arguments: $cond, \%attrs??
1013 =item Return Value: $count
1017 Performs an SQL C<COUNT> with the same query as the resultset was built
1018 with to find the number of elements. If passed arguments, does a search
1019 on the resultset and counts the results of that.
1021 Note: When using C<count> with C<group_by>, L<DBIx::Class> emulates C<GROUP BY>
1022 using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
1023 not support C<DISTINCT> with multiple columns. If you are using such a
1024 database, you should only use columns from the main table in your C<group_by>
1031 return $self->search(@_)->count if @_ and defined $_[0];
1032 return scalar @{ $self->get_cache } if $self->get_cache;
1033 my $count = $self->_count;
1034 return 0 unless $count;
1036 # need to take offset from resolved attrs
1038 $count -= $self->{_attrs}{offset} if $self->{_attrs}{offset};
1039 $count = $self->{attrs}{rows} if
1040 $self->{attrs}{rows} and $self->{attrs}{rows} < $count;
1041 $count = 0 if ($count < 0);
1045 sub _count { # Separated out so pager can get the full count
1047 my $select = { count => '*' };
1049 my $attrs = { %{$self->_resolved_attrs} };
1050 if (my $group_by = delete $attrs->{group_by}) {
1051 delete $attrs->{having};
1052 my @distinct = (ref $group_by ? @$group_by : ($group_by));
1053 # todo: try CONCAT for multi-column pk
1054 my @pk = $self->result_source->primary_columns;
1056 my $alias = $attrs->{alias};
1057 foreach my $column (@distinct) {
1058 if ($column =~ qr/^(?:\Q${alias}.\E)?$pk[0]$/) {
1059 @distinct = ($column);
1065 $select = { count => { distinct => \@distinct } };
1068 $attrs->{select} = $select;
1069 $attrs->{as} = [qw/count/];
1071 # offset, order by and page are not needed to count. record_filter is cdbi
1072 delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
1074 my $tmp_rs = (ref $self)->new($self->result_source, $attrs);
1075 my ($count) = $tmp_rs->cursor->next;
1083 =head2 count_literal
1087 =item Arguments: $sql_fragment, @bind_values
1089 =item Return Value: $count
1093 Counts the results in a literal query. Equivalent to calling L</search_literal>
1094 with the passed arguments, then L</count>.
1098 sub count_literal { shift->search_literal(@_)->count; }
1104 =item Arguments: none
1106 =item Return Value: @objects
1110 Returns all elements in the resultset. Called implicitly if the resultset
1111 is returned in list context.
1118 $self->throw_exception("all() doesn't take any arguments, you probably wanted ->search(...)->all()");
1121 return @{ $self->get_cache } if $self->get_cache;
1125 # TODO: don't call resolve here
1126 if (keys %{$self->_resolved_attrs->{collapse}}) {
1127 # if ($self->{attrs}{prefetch}) {
1128 # Using $self->cursor->all is really just an optimisation.
1129 # If we're collapsing has_many prefetches it probably makes
1130 # very little difference, and this is cleaner than hacking
1131 # _construct_object to survive the approach
1132 my @row = $self->cursor->next;
1134 push(@obj, $self->_construct_object(@row));
1135 @row = (exists $self->{stashed_row}
1136 ? @{delete $self->{stashed_row}}
1137 : $self->cursor->next);
1140 @obj = map { $self->_construct_object(@$_) } $self->cursor->all;
1143 $self->set_cache(\@obj) if $self->{attrs}{cache};
1151 =item Arguments: none
1153 =item Return Value: $self
1157 Resets the resultset's cursor, so you can iterate through the elements again.
1163 delete $self->{_attrs} if exists $self->{_attrs};
1164 $self->{all_cache_position} = 0;
1165 $self->cursor->reset;
1173 =item Arguments: none
1175 =item Return Value: $object?
1179 Resets the resultset and returns an object for the first result (if the
1180 resultset returns anything).
1185 return $_[0]->reset->next;
1188 # _cond_for_update_delete
1190 # update/delete require the condition to be modified to handle
1191 # the differing SQL syntax available. This transforms the $self->{cond}
1192 # appropriately, returning the new condition.
1194 sub _cond_for_update_delete {
1195 my ($self, $full_cond) = @_;
1198 $full_cond ||= $self->{cond};
1199 # No-op. No condition, we're updating/deleting everything
1200 return $cond unless ref $full_cond;
1202 if (ref $full_cond eq 'ARRAY') {
1206 foreach my $key (keys %{$_}) {
1208 $hash{$1} = $_->{$key};
1214 elsif (ref $full_cond eq 'HASH') {
1215 if ((keys %{$full_cond})[0] eq '-and') {
1218 my @cond = @{$full_cond->{-and}};
1219 for (my $i = 0; $i < @cond; $i++) {
1220 my $entry = $cond[$i];
1223 if (ref $entry eq 'HASH') {
1224 $hash = $self->_cond_for_update_delete($entry);
1227 $entry =~ /([^.]+)$/;
1228 $hash->{$1} = $cond[++$i];
1231 push @{$cond->{-and}}, $hash;
1235 foreach my $key (keys %{$full_cond}) {
1237 $cond->{$1} = $full_cond->{$key};
1242 $self->throw_exception(
1243 "Can't update/delete on resultset with condition unless hash or array"
1255 =item Arguments: \%values
1257 =item Return Value: $storage_rv
1261 Sets the specified columns in the resultset to the supplied values in a
1262 single query. Return value will be true if the update succeeded or false
1263 if no records were updated; exact type of success value is storage-dependent.
1268 my ($self, $values) = @_;
1269 $self->throw_exception("Values for update must be a hash")
1270 unless ref $values eq 'HASH';
1272 my $cond = $self->_cond_for_update_delete;
1274 return $self->result_source->storage->update(
1275 $self->result_source, $values, $cond
1283 =item Arguments: \%values
1285 =item Return Value: 1
1289 Fetches all objects and updates them one at a time. Note that C<update_all>
1290 will run DBIC cascade triggers, while L</update> will not.
1295 my ($self, $values) = @_;
1296 $self->throw_exception("Values for update must be a hash")
1297 unless ref $values eq 'HASH';
1298 foreach my $obj ($self->all) {
1299 $obj->set_columns($values)->update;
1308 =item Arguments: none
1310 =item Return Value: 1
1314 Deletes the contents of the resultset from its result source. Note that this
1315 will not run DBIC cascade triggers. See L</delete_all> if you need triggers
1316 to run. See also L<DBIx::Class::Row/delete>.
1318 delete may not generate correct SQL for a query with joins or a resultset
1319 chained from a related resultset. In this case it will generate a warning:-
1321 WARNING! Currently $rs->delete() does not generate proper SQL on
1322 joined resultsets, and may delete rows well outside of the contents
1323 of $rs. Use at your own risk
1325 In these cases you may find that delete_all is more appropriate, or you
1326 need to respecify your query in a way that can be expressed without a join.
1332 $self->throw_exception("Delete should not be passed any arguments")
1334 carp( 'WARNING! Currently $rs->delete() does not generate proper SQL'
1335 . ' on joined resultsets, and may delete rows well outside of the'
1336 . ' contents of $rs. Use at your own risk' )
1337 if ( $self->{attrs}{seen_join} );
1338 my $cond = $self->_cond_for_update_delete;
1340 $self->result_source->storage->delete($self->result_source, $cond);
1348 =item Arguments: none
1350 =item Return Value: 1
1354 Fetches all objects and deletes them one at a time. Note that C<delete_all>
1355 will run DBIC cascade triggers, while L</delete> will not.
1361 $_->delete for $self->all;
1369 =item Arguments: \@data;
1373 Accepts either an arrayref of hashrefs or alternatively an arrayref of arrayrefs.
1374 For the arrayref of hashrefs style each hashref should be a structure suitable
1375 forsubmitting to a $resultset->create(...) method.
1377 In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
1378 to insert the data, as this is a faster method.
1380 Otherwise, each set of data is inserted into the database using
1381 L<DBIx::Class::ResultSet/create>, and a arrayref of the resulting row
1382 objects is returned.
1384 Example: Assuming an Artist Class that has many CDs Classes relating:
1386 my $Artist_rs = $schema->resultset("Artist");
1388 ## Void Context Example
1389 $Artist_rs->populate([
1390 { artistid => 4, name => 'Manufactured Crap', cds => [
1391 { title => 'My First CD', year => 2006 },
1392 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
1395 { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
1396 { title => 'My parents sold me to a record company' ,year => 2005 },
1397 { title => 'Why Am I So Ugly?', year => 2006 },
1398 { title => 'I Got Surgery and am now Popular', year => 2007 }
1403 ## Array Context Example
1404 my ($ArtistOne, $ArtistTwo, $ArtistThree) = $Artist_rs->populate([
1405 { name => "Artist One"},
1406 { name => "Artist Two"},
1407 { name => "Artist Three", cds=> [
1408 { title => "First CD", year => 2007},
1409 { title => "Second CD", year => 2008},
1413 print $ArtistOne->name; ## response is 'Artist One'
1414 print $ArtistThree->cds->count ## reponse is '2'
1416 For the arrayref of arrayrefs style, the first element should be a list of the
1417 fieldsnames to which the remaining elements are rows being inserted. For
1420 $Arstist_rs->populate([
1421 [qw/artistid name/],
1422 [100, 'A Formally Unknown Singer'],
1423 [101, 'A singer that jumped the shark two albums ago'],
1424 [102, 'An actually cool singer.'],
1427 Please note an important effect on your data when choosing between void and
1428 wantarray context. Since void context goes straight to C<insert_bulk> in
1429 L<DBIx::Class::Storage::DBI> this will skip any component that is overriding
1430 c<insert>. So if you are using something like L<DBIx-Class-UUIDColumns> to
1431 create primary keys for you, you will find that your PKs are empty. In this
1432 case you will have to use the wantarray context in order to create those
1438 my $self = shift @_;
1439 my $data = ref $_[0][0] eq 'HASH'
1440 ? $_[0] : ref $_[0][0] eq 'ARRAY' ? $self->_normalize_populate_args($_[0]) :
1441 $self->throw_exception('Populate expects an arrayref of hashes or arrayref of arrayrefs');
1443 if(defined wantarray) {
1445 foreach my $item (@$data) {
1446 push(@created, $self->create($item));
1450 my ($first, @rest) = @$data;
1452 my @names = grep {!ref $first->{$_}} keys %$first;
1453 my @rels = grep { $self->result_source->has_relationship($_) } keys %$first;
1454 my @pks = $self->result_source->primary_columns;
1456 ## do the belongs_to relationships
1457 foreach my $index (0..$#$data) {
1458 if( grep { !defined $data->[$index]->{$_} } @pks ) {
1459 my @ret = $self->populate($data);
1463 foreach my $rel (@rels) {
1464 next unless $data->[$index]->{$rel} && ref $data->[$index]->{$rel} eq "HASH";
1465 my $result = $self->related_resultset($rel)->create($data->[$index]->{$rel});
1466 my ($reverse) = keys %{$self->result_source->reverse_relationship_info($rel)};
1467 my $related = $result->result_source->resolve_condition(
1468 $result->result_source->relationship_info($reverse)->{cond},
1473 delete $data->[$index]->{$rel};
1474 $data->[$index] = {%{$data->[$index]}, %$related};
1476 push @names, keys %$related if $index == 0;
1480 ## do bulk insert on current row
1481 my @values = map { [ @$_{@names} ] } @$data;
1483 $self->result_source->storage->insert_bulk(
1484 $self->result_source,
1489 ## do the has_many relationships
1490 foreach my $item (@$data) {
1492 foreach my $rel (@rels) {
1493 next unless $item->{$rel} && ref $item->{$rel} eq "ARRAY";
1495 my $parent = $self->find(map {{$_=>$item->{$_}} } @pks)
1496 || $self->throw_exception('Cannot find the relating object.');
1498 my $child = $parent->$rel;
1500 my $related = $child->result_source->resolve_condition(
1501 $parent->result_source->relationship_info($rel)->{cond},
1506 my @rows_to_add = ref $item->{$rel} eq 'ARRAY' ? @{$item->{$rel}} : ($item->{$rel});
1507 my @populate = map { {%$_, %$related} } @rows_to_add;
1509 $child->populate( \@populate );
1515 =head2 _normalize_populate_args ($args)
1517 Private method used by L</populate> to normalize its incoming arguments. Factored
1518 out in case you want to subclass and accept new argument structures to the
1519 L</populate> method.
1523 sub _normalize_populate_args {
1524 my ($self, $data) = @_;
1525 my @names = @{shift(@$data)};
1526 my @results_to_create;
1527 foreach my $datum (@$data) {
1528 my %result_to_create;
1529 foreach my $index (0..$#names) {
1530 $result_to_create{$names[$index]} = $$datum[$index];
1532 push @results_to_create, \%result_to_create;
1534 return \@results_to_create;
1541 =item Arguments: none
1543 =item Return Value: $pager
1547 Return Value a L<Data::Page> object for the current resultset. Only makes
1548 sense for queries with a C<page> attribute.
1554 my $attrs = $self->{attrs};
1555 $self->throw_exception("Can't create pager for non-paged rs")
1556 unless $self->{attrs}{page};
1557 $attrs->{rows} ||= 10;
1558 return $self->{pager} ||= Data::Page->new(
1559 $self->_count, $attrs->{rows}, $self->{attrs}{page});
1566 =item Arguments: $page_number
1568 =item Return Value: $rs
1572 Returns a resultset for the $page_number page of the resultset on which page
1573 is called, where each page contains a number of rows equal to the 'rows'
1574 attribute set on the resultset (10 by default).
1579 my ($self, $page) = @_;
1580 return (ref $self)->new($self->result_source, { %{$self->{attrs}}, page => $page });
1587 =item Arguments: \%vals
1589 =item Return Value: $rowobject
1593 Creates a new row object in the resultset's result class and returns
1594 it. The row is not inserted into the database at this point, call
1595 L<DBIx::Class::Row/insert> to do that. Calling L<DBIx::Class::Row/in_storage>
1596 will tell you whether the row object has been inserted or not.
1598 Passes the hashref of input on to L<DBIx::Class::Row/new>.
1603 my ($self, $values) = @_;
1604 $self->throw_exception( "new_result needs a hash" )
1605 unless (ref $values eq 'HASH');
1608 my $alias = $self->{attrs}{alias};
1611 defined $self->{cond}
1612 && $self->{cond} eq $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION
1614 %new = %{$self->{attrs}{related_objects}};
1616 $self->throw_exception(
1617 "Can't abstract implicit construct, condition not a hash"
1618 ) if ($self->{cond} && !(ref $self->{cond} eq 'HASH'));
1620 my $collapsed_cond = (
1622 ? $self->_collapse_cond($self->{cond})
1626 # precendence must be given to passed values over values inherited from
1627 # the cond, so the order here is important.
1628 my %implied = %{$self->_remove_alias($collapsed_cond, $alias)};
1629 while( my($col,$value) = each %implied ){
1630 if(ref($value) eq 'HASH' && keys(%$value) && (keys %$value)[0] eq '='){
1631 $new{$col} = $value->{'='};
1634 $new{$col} = $value if $self->_is_deterministic_value($value);
1640 %{ $self->_remove_alias($values, $alias) },
1641 -source_handle => $self->_source_handle,
1642 -result_source => $self->result_source, # DO NOT REMOVE THIS, REQUIRED
1645 return $self->result_class->new(\%new);
1648 # _is_deterministic_value
1650 # Make an effor to strip non-deterministic values from the condition,
1651 # to make sure new_result chokes less
1653 sub _is_deterministic_value {
1656 my $ref_type = ref $value;
1657 return 1 if $ref_type eq '' || $ref_type eq 'SCALAR';
1658 return 1 if Scalar::Util::blessed($value);
1664 # Recursively collapse the condition.
1666 sub _collapse_cond {
1667 my ($self, $cond, $collapsed) = @_;
1671 if (ref $cond eq 'ARRAY') {
1672 foreach my $subcond (@$cond) {
1673 next unless ref $subcond; # -or
1674 # warn "ARRAY: " . Dumper $subcond;
1675 $collapsed = $self->_collapse_cond($subcond, $collapsed);
1678 elsif (ref $cond eq 'HASH') {
1679 if (keys %$cond and (keys %$cond)[0] eq '-and') {
1680 foreach my $subcond (@{$cond->{-and}}) {
1681 # warn "HASH: " . Dumper $subcond;
1682 $collapsed = $self->_collapse_cond($subcond, $collapsed);
1686 # warn "LEAF: " . Dumper $cond;
1687 foreach my $col (keys %$cond) {
1688 my $value = $cond->{$col};
1689 $collapsed->{$col} = $value;
1699 # Remove the specified alias from the specified query hash. A copy is made so
1700 # the original query is not modified.
1703 my ($self, $query, $alias) = @_;
1705 my %orig = %{ $query || {} };
1708 foreach my $key (keys %orig) {
1710 $unaliased{$key} = $orig{$key};
1713 $unaliased{$1} = $orig{$key}
1714 if $key =~ m/^(?:\Q$alias\E\.)?([^.]+)$/;
1724 =item Arguments: \%vals, \%attrs?
1726 =item Return Value: $rowobject
1730 my $artist = $schema->resultset('Artist')->find_or_new(
1731 { artist => 'fred' }, { key => 'artists' });
1733 $cd->cd_to_producer->find_or_new({ producer => $producer },
1734 { key => 'primary });
1736 Find an existing record from this resultset, based on its primary
1737 key, or a unique constraint. If none exists, instantiate a new result
1738 object and return it. The object will not be saved into your storage
1739 until you call L<DBIx::Class::Row/insert> on it.
1741 You most likely want this method when looking for existing rows using
1742 a unique constraint that is not the primary key, or looking for
1745 If you want objects to be saved immediately, use L</find_or_create> instead.
1747 B<Note>: C<find_or_new> is probably not what you want when creating a
1748 new row in a table that uses primary keys supplied by the
1749 database. Passing in a primary key column with a value of I<undef>
1750 will cause L</find> to attempt to search for a row with a value of
1757 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1758 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1759 my $exists = $self->find($hash, $attrs);
1760 return defined $exists ? $exists : $self->new_result($hash);
1767 =item Arguments: \%vals
1769 =item Return Value: a L<DBIx::Class::Row> $object
1773 Attempt to create a single new row or a row with multiple related rows
1774 in the table represented by the resultset (and related tables). This
1775 will not check for duplicate rows before inserting, use
1776 L</find_or_create> to do that.
1778 To create one row for this resultset, pass a hashref of key/value
1779 pairs representing the columns of the table and the values you wish to
1780 store. If the appropriate relationships are set up, foreign key fields
1781 can also be passed an object representing the foreign row, and the
1782 value will be set to its primary key.
1784 To create related objects, pass a hashref for the value if the related
1785 item is a foreign key relationship (L<DBIx::Class::Relationship/belongs_to>),
1786 and use the name of the relationship as the key. (NOT the name of the field,
1787 necessarily). For C<has_many> and C<has_one> relationships, pass an arrayref
1788 of hashrefs containing the data for each of the rows to create in the foreign
1789 tables, again using the relationship name as the key.
1791 Instead of hashrefs of plain related data (key/value pairs), you may
1792 also pass new or inserted objects. New objects (not inserted yet, see
1793 L</new>), will be inserted into their appropriate tables.
1795 Effectively a shortcut for C<< ->new_result(\%vals)->insert >>.
1797 Example of creating a new row.
1799 $person_rs->create({
1800 name=>"Some Person",
1801 email=>"somebody@someplace.com"
1804 Example of creating a new row and also creating rows in a related C<has_many>
1805 or C<has_one> resultset. Note Arrayref.
1808 { artistid => 4, name => 'Manufactured Crap', cds => [
1809 { title => 'My First CD', year => 2006 },
1810 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
1815 Example of creating a new row and also creating a row in a related
1816 C<belongs_to>resultset. Note Hashref.
1819 title=>"Music for Silly Walks",
1822 name=>"Silly Musician",
1829 my ($self, $attrs) = @_;
1830 $self->throw_exception( "create needs a hashref" )
1831 unless ref $attrs eq 'HASH';
1832 return $self->new_result($attrs)->insert;
1835 =head2 find_or_create
1839 =item Arguments: \%vals, \%attrs?
1841 =item Return Value: $rowobject
1845 $cd->cd_to_producer->find_or_create({ producer => $producer },
1846 { key => 'primary });
1848 Tries to find a record based on its primary key or unique constraints; if none
1849 is found, creates one and returns that instead.
1851 my $cd = $schema->resultset('CD')->find_or_create({
1853 artist => 'Massive Attack',
1854 title => 'Mezzanine',
1858 Also takes an optional C<key> attribute, to search by a specific key or unique
1859 constraint. For example:
1861 my $cd = $schema->resultset('CD')->find_or_create(
1863 artist => 'Massive Attack',
1864 title => 'Mezzanine',
1866 { key => 'cd_artist_title' }
1869 B<Note>: Because find_or_create() reads from the database and then
1870 possibly inserts based on the result, this method is subject to a race
1871 condition. Another process could create a record in the table after
1872 the find has completed and before the create has started. To avoid
1873 this problem, use find_or_create() inside a transaction.
1875 B<Note>: C<find_or_create> is probably not what you want when creating
1876 a new row in a table that uses primary keys supplied by the
1877 database. Passing in a primary key column with a value of I<undef>
1878 will cause L</find> to attempt to search for a row with a value of
1881 See also L</find> and L</update_or_create>. For information on how to declare
1882 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1886 sub find_or_create {
1888 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1889 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1890 my $exists = $self->find($hash, $attrs);
1891 return defined $exists ? $exists : $self->create($hash);
1894 =head2 update_or_create
1898 =item Arguments: \%col_values, { key => $unique_constraint }?
1900 =item Return Value: $rowobject
1904 $resultset->update_or_create({ col => $val, ... });
1906 First, searches for an existing row matching one of the unique constraints
1907 (including the primary key) on the source of this resultset. If a row is
1908 found, updates it with the other given column values. Otherwise, creates a new
1911 Takes an optional C<key> attribute to search on a specific unique constraint.
1914 # In your application
1915 my $cd = $schema->resultset('CD')->update_or_create(
1917 artist => 'Massive Attack',
1918 title => 'Mezzanine',
1921 { key => 'cd_artist_title' }
1924 $cd->cd_to_producer->update_or_create({
1925 producer => $producer,
1932 If no C<key> is specified, it searches on all unique constraints defined on the
1933 source, including the primary key.
1935 If the C<key> is specified as C<primary>, it searches only on the primary key.
1937 See also L</find> and L</find_or_create>. For information on how to declare
1938 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1940 B<Note>: C<update_or_create> is probably not what you want when
1941 looking for a row in a table that uses primary keys supplied by the
1942 database, unless you actually have a key value. Passing in a primary
1943 key column with a value of I<undef> will cause L</find> to attempt to
1944 search for a row with a value of I<NULL>.
1948 sub update_or_create {
1950 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1951 my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
1953 my $row = $self->find($cond, $attrs);
1955 $row->update($cond);
1959 return $self->create($cond);
1966 =item Arguments: none
1968 =item Return Value: \@cache_objects?
1972 Gets the contents of the cache for the resultset, if the cache is set.
1974 The cache is populated either by using the L</prefetch> attribute to
1975 L</search> or by calling L</set_cache>.
1987 =item Arguments: \@cache_objects
1989 =item Return Value: \@cache_objects
1993 Sets the contents of the cache for the resultset. Expects an arrayref
1994 of objects of the same class as those produced by the resultset. Note that
1995 if the cache is set the resultset will return the cached objects rather
1996 than re-querying the database even if the cache attr is not set.
1998 The contents of the cache can also be populated by using the
1999 L</prefetch> attribute to L</search>.
2004 my ( $self, $data ) = @_;
2005 $self->throw_exception("set_cache requires an arrayref")
2006 if defined($data) && (ref $data ne 'ARRAY');
2007 $self->{all_cache} = $data;
2014 =item Arguments: none
2016 =item Return Value: []
2020 Clears the cache for the resultset.
2025 shift->set_cache(undef);
2028 =head2 related_resultset
2032 =item Arguments: $relationship_name
2034 =item Return Value: $resultset
2038 Returns a related resultset for the supplied relationship name.
2040 $artist_rs = $schema->resultset('CD')->related_resultset('Artist');
2044 sub related_resultset {
2045 my ($self, $rel) = @_;
2047 $self->{related_resultsets} ||= {};
2048 return $self->{related_resultsets}{$rel} ||= do {
2049 my $rel_obj = $self->result_source->relationship_info($rel);
2051 $self->throw_exception(
2052 "search_related: result source '" . $self->result_source->source_name .
2053 "' has no such relationship $rel")
2056 my ($from,$seen) = $self->_resolve_from($rel);
2058 my $join_count = $seen->{$rel};
2059 my $alias = ($join_count > 1 ? join('_', $rel, $join_count) : $rel);
2061 #XXX - temp fix for result_class bug. There likely is a more elegant fix -groditi
2062 my %attrs = %{$self->{attrs}||{}};
2063 delete @attrs{qw(result_class alias)};
2067 if (my $cache = $self->get_cache) {
2068 if ($cache->[0] && $cache->[0]->related_resultset($rel)->get_cache) {
2069 $new_cache = [ map { @{$_->related_resultset($rel)->get_cache} }
2074 my $rel_source = $self->result_source->related_source($rel);
2078 # The reason we do this now instead of passing the alias to the
2079 # search_rs below is that if you wrap/overload resultset on the
2080 # source you need to know what alias it's -going- to have for things
2081 # to work sanely (e.g. RestrictWithObject wants to be able to add
2082 # extra query restrictions, and these may need to be $alias.)
2084 my $attrs = $rel_source->resultset_attributes;
2085 local $attrs->{alias} = $alias;
2087 $rel_source->resultset
2095 where => $self->{cond},
2100 $new->set_cache($new_cache) if $new_cache;
2105 =head2 current_source_alias
2109 =item Arguments: none
2111 =item Return Value: $source_alias
2115 Returns the current table alias for the result source this resultset is built
2116 on, that will be used in the SQL query. Usually it is C<me>.
2118 Currently the source alias that refers to the result set returned by a
2119 L</search>/L</find> family method depends on how you got to the resultset: it's
2120 C<me> by default, but eg. L</search_related> aliases it to the related result
2121 source name (and keeps C<me> referring to the original result set). The long
2122 term goal is to make L<DBIx::Class> always alias the current resultset as C<me>
2123 (and make this method unnecessary).
2125 Thus it's currently necessary to use this method in predefined queries (see
2126 L<DBIx::Class::Manual::Cookbook/Predefined searches>) when referring to the
2127 source alias of the current result set:
2129 # in a result set class
2131 my ($self, $user) = @_;
2133 my $me = $self->current_source_alias;
2135 return $self->search(
2136 "$me.modified" => $user->id,
2142 sub current_source_alias {
2145 return ($self->{attrs} || {})->{alias} || 'me';
2149 my ($self, $extra_join) = @_;
2150 my $source = $self->result_source;
2151 my $attrs = $self->{attrs};
2153 my $from = $attrs->{from}
2154 || [ { $attrs->{alias} => $source->from } ];
2156 my $seen = { %{$attrs->{seen_join}||{}} };
2158 my $join = ($attrs->{join}
2159 ? [ $attrs->{join}, $extra_join ]
2162 # we need to take the prefetch the attrs into account before we
2163 # ->resolve_join as otherwise they get lost - captainL
2164 my $merged = $self->_merge_attr( $join, $attrs->{prefetch} );
2168 ($join ? $source->resolve_join($merged, $attrs->{alias}, $seen) : ()),
2171 return ($from,$seen);
2174 sub _resolved_attrs {
2176 return $self->{_attrs} if $self->{_attrs};
2178 my $attrs = { %{$self->{attrs}||{}} };
2179 my $source = $self->result_source;
2180 my $alias = $attrs->{alias};
2182 $attrs->{columns} ||= delete $attrs->{cols} if exists $attrs->{cols};
2183 if ($attrs->{columns}) {
2184 delete $attrs->{as};
2185 } elsif (!$attrs->{select}) {
2186 $attrs->{columns} = [ $source->columns ];
2191 ? (ref $attrs->{select} eq 'ARRAY'
2192 ? [ @{$attrs->{select}} ]
2193 : [ $attrs->{select} ])
2194 : [ map { m/\./ ? $_ : "${alias}.$_" } @{delete $attrs->{columns}} ]
2198 ? (ref $attrs->{as} eq 'ARRAY'
2199 ? [ @{$attrs->{as}} ]
2201 : [ map { m/^\Q${alias}.\E(.+)$/ ? $1 : $_ } @{$attrs->{select}} ]
2205 if ($adds = delete $attrs->{include_columns}) {
2206 $adds = [$adds] unless ref $adds eq 'ARRAY';
2207 push(@{$attrs->{select}}, @$adds);
2208 push(@{$attrs->{as}}, map { m/([^.]+)$/; $1 } @$adds);
2210 if ($adds = delete $attrs->{'+select'}) {
2211 $adds = [$adds] unless ref $adds eq 'ARRAY';
2212 push(@{$attrs->{select}},
2213 map { /\./ || ref $_ ? $_ : "${alias}.$_" } @$adds);
2215 if (my $adds = delete $attrs->{'+as'}) {
2216 $adds = [$adds] unless ref $adds eq 'ARRAY';
2217 push(@{$attrs->{as}}, @$adds);
2220 $attrs->{from} ||= [ { 'me' => $source->from } ];
2222 if (exists $attrs->{join} || exists $attrs->{prefetch}) {
2223 my $join = delete $attrs->{join} || {};
2225 if (defined $attrs->{prefetch}) {
2226 $join = $self->_merge_attr(
2227 $join, $attrs->{prefetch}
2232 $attrs->{from} = # have to copy here to avoid corrupting the original
2235 $source->resolve_join($join, $alias, { %{$attrs->{seen_join}||{}} })
2240 $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct};
2241 if ($attrs->{order_by}) {
2242 $attrs->{order_by} = (ref($attrs->{order_by}) eq 'ARRAY'
2243 ? [ @{$attrs->{order_by}} ]
2244 : [ $attrs->{order_by} ]);
2246 $attrs->{order_by} = [];
2249 my $collapse = $attrs->{collapse} || {};
2250 if (my $prefetch = delete $attrs->{prefetch}) {
2251 $prefetch = $self->_merge_attr({}, $prefetch);
2253 my $seen = { %{ $attrs->{seen_join} || {} } };
2254 foreach my $p (ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch)) {
2255 # bring joins back to level of current class
2256 my @prefetch = $source->resolve_prefetch(
2257 $p, $alias, $seen, \@pre_order, $collapse
2259 push(@{$attrs->{select}}, map { $_->[0] } @prefetch);
2260 push(@{$attrs->{as}}, map { $_->[1] } @prefetch);
2262 push(@{$attrs->{order_by}}, @pre_order);
2264 $attrs->{collapse} = $collapse;
2266 if ($attrs->{page}) {
2267 $attrs->{offset} ||= 0;
2268 $attrs->{offset} += ($attrs->{rows} * ($attrs->{page} - 1));
2271 return $self->{_attrs} = $attrs;
2275 my ($self, $attr) = @_;
2277 if (ref $attr eq 'HASH') {
2278 return $self->_rollout_hash($attr);
2279 } elsif (ref $attr eq 'ARRAY') {
2280 return $self->_rollout_array($attr);
2286 sub _rollout_array {
2287 my ($self, $attr) = @_;
2290 foreach my $element (@{$attr}) {
2291 if (ref $element eq 'HASH') {
2292 push( @rolled_array, @{ $self->_rollout_hash( $element ) } );
2293 } elsif (ref $element eq 'ARRAY') {
2294 # XXX - should probably recurse here
2295 push( @rolled_array, @{$self->_rollout_array($element)} );
2297 push( @rolled_array, $element );
2300 return \@rolled_array;
2304 my ($self, $attr) = @_;
2307 foreach my $key (keys %{$attr}) {
2308 push( @rolled_array, { $key => $attr->{$key} } );
2310 return \@rolled_array;
2313 sub _calculate_score {
2314 my ($self, $a, $b) = @_;
2316 if (ref $b eq 'HASH') {
2317 my ($b_key) = keys %{$b};
2318 if (ref $a eq 'HASH') {
2319 my ($a_key) = keys %{$a};
2320 if ($a_key eq $b_key) {
2321 return (1 + $self->_calculate_score( $a->{$a_key}, $b->{$b_key} ));
2326 return ($a eq $b_key) ? 1 : 0;
2329 if (ref $a eq 'HASH') {
2330 my ($a_key) = keys %{$a};
2331 return ($b eq $a_key) ? 1 : 0;
2333 return ($b eq $a) ? 1 : 0;
2339 my ($self, $orig, $import) = @_;
2341 return $import unless defined($orig);
2342 return $orig unless defined($import);
2344 $orig = $self->_rollout_attr($orig);
2345 $import = $self->_rollout_attr($import);
2348 foreach my $import_element ( @{$import} ) {
2349 # find best candidate from $orig to merge $b_element into
2350 my $best_candidate = { position => undef, score => 0 }; my $position = 0;
2351 foreach my $orig_element ( @{$orig} ) {
2352 my $score = $self->_calculate_score( $orig_element, $import_element );
2353 if ($score > $best_candidate->{score}) {
2354 $best_candidate->{position} = $position;
2355 $best_candidate->{score} = $score;
2359 my ($import_key) = ( ref $import_element eq 'HASH' ) ? keys %{$import_element} : ($import_element);
2361 if ($best_candidate->{score} == 0 || exists $seen_keys->{$import_key}) {
2362 push( @{$orig}, $import_element );
2364 my $orig_best = $orig->[$best_candidate->{position}];
2365 # merge orig_best and b_element together and replace original with merged
2366 if (ref $orig_best ne 'HASH') {
2367 $orig->[$best_candidate->{position}] = $import_element;
2368 } elsif (ref $import_element eq 'HASH') {
2369 my ($key) = keys %{$orig_best};
2370 $orig->[$best_candidate->{position}] = { $key => $self->_merge_attr($orig_best->{$key}, $import_element->{$key}) };
2373 $seen_keys->{$import_key} = 1; # don't merge the same key twice
2383 $self->_source_handle($_[0]->handle);
2385 $self->_source_handle->resolve;
2389 =head2 throw_exception
2391 See L<DBIx::Class::Schema/throw_exception> for details.
2395 sub throw_exception {
2397 if (ref $self && $self->_source_handle->schema) {
2398 $self->_source_handle->schema->throw_exception(@_)
2405 # XXX: FIXME: Attributes docs need clearing up
2409 Attributes are used to refine a ResultSet in various ways when
2410 searching for data. They can be passed to any method which takes an
2411 C<\%attrs> argument. See L</search>, L</search_rs>, L</find>,
2414 These are in no particular order:
2420 =item Value: ($order_by | \@order_by)
2424 Which column(s) to order the results by. This is currently passed
2425 through directly to SQL, so you can give e.g. C<year DESC> for a
2426 descending order on the column `year'.
2428 Please note that if you have C<quote_char> enabled (see
2429 L<DBIx::Class::Storage::DBI/connect_info>) you will need to do C<\'year DESC' > to
2430 specify an order. (The scalar ref causes it to be passed as raw sql to the DB,
2431 so you will need to manually quote things as appropriate.)
2433 If your L<SQL::Abstract> version supports it (>=1.50), you can also use
2434 C<{-desc => 'year'}>, which takes care of the quoting for you. This is the
2441 =item Value: \@columns
2445 Shortcut to request a particular set of columns to be retrieved. Adds
2446 C<me.> onto the start of any column without a C<.> in it and sets C<select>
2447 from that, then auto-populates C<as> from C<select> as normal. (You may also
2448 use the C<cols> attribute, as in earlier versions of DBIC.)
2450 =head2 include_columns
2454 =item Value: \@columns
2458 Shortcut to include additional columns in the returned results - for example
2460 $schema->resultset('CD')->search(undef, {
2461 include_columns => ['artist.name'],
2465 would return all CDs and include a 'name' column to the information
2466 passed to object inflation. Note that the 'artist' is the name of the
2467 column (or relationship) accessor, and 'name' is the name of the column
2468 accessor in the related table.
2474 =item Value: \@select_columns
2478 Indicates which columns should be selected from the storage. You can use
2479 column names, or in the case of RDBMS back ends, function or stored procedure
2482 $rs = $schema->resultset('Employee')->search(undef, {
2485 { count => 'employeeid' },
2490 When you use function/stored procedure names and do not supply an C<as>
2491 attribute, the column names returned are storage-dependent. E.g. MySQL would
2492 return a column named C<count(employeeid)> in the above example.
2498 Indicates additional columns to be selected from storage. Works the same as
2499 L</select> but adds columns to the selection.
2507 Indicates additional column names for those added via L</+select>. See L</as>.
2515 =item Value: \@inflation_names
2519 Indicates column names for object inflation. That is, C<as>
2520 indicates the name that the column can be accessed as via the
2521 C<get_column> method (or via the object accessor, B<if one already
2522 exists>). It has nothing to do with the SQL code C<SELECT foo AS bar>.
2524 The C<as> attribute is used in conjunction with C<select>,
2525 usually when C<select> contains one or more function or stored
2528 $rs = $schema->resultset('Employee')->search(undef, {
2531 { count => 'employeeid' }
2533 as => ['name', 'employee_count'],
2536 my $employee = $rs->first(); # get the first Employee
2538 If the object against which the search is performed already has an accessor
2539 matching a column name specified in C<as>, the value can be retrieved using
2540 the accessor as normal:
2542 my $name = $employee->name();
2544 If on the other hand an accessor does not exist in the object, you need to
2545 use C<get_column> instead:
2547 my $employee_count = $employee->get_column('employee_count');
2549 You can create your own accessors if required - see
2550 L<DBIx::Class::Manual::Cookbook> for details.
2552 Please note: This will NOT insert an C<AS employee_count> into the SQL
2553 statement produced, it is used for internal access only. Thus
2554 attempting to use the accessor in an C<order_by> clause or similar
2555 will fail miserably.
2557 To get around this limitation, you can supply literal SQL to your
2558 C<select> attibute that contains the C<AS alias> text, eg:
2560 select => [\'myfield AS alias']
2566 =item Value: ($rel_name | \@rel_names | \%rel_names)
2570 Contains a list of relationships that should be joined for this query. For
2573 # Get CDs by Nine Inch Nails
2574 my $rs = $schema->resultset('CD')->search(
2575 { 'artist.name' => 'Nine Inch Nails' },
2576 { join => 'artist' }
2579 Can also contain a hash reference to refer to the other relation's relations.
2582 package MyApp::Schema::Track;
2583 use base qw/DBIx::Class/;
2584 __PACKAGE__->table('track');
2585 __PACKAGE__->add_columns(qw/trackid cd position title/);
2586 __PACKAGE__->set_primary_key('trackid');
2587 __PACKAGE__->belongs_to(cd => 'MyApp::Schema::CD');
2590 # In your application
2591 my $rs = $schema->resultset('Artist')->search(
2592 { 'track.title' => 'Teardrop' },
2594 join => { cd => 'track' },
2595 order_by => 'artist.name',
2599 You need to use the relationship (not the table) name in conditions,
2600 because they are aliased as such. The current table is aliased as "me", so
2601 you need to use me.column_name in order to avoid ambiguity. For example:
2603 # Get CDs from 1984 with a 'Foo' track
2604 my $rs = $schema->resultset('CD')->search(
2607 'tracks.name' => 'Foo'
2609 { join => 'tracks' }
2612 If the same join is supplied twice, it will be aliased to <rel>_2 (and
2613 similarly for a third time). For e.g.
2615 my $rs = $schema->resultset('Artist')->search({
2616 'cds.title' => 'Down to Earth',
2617 'cds_2.title' => 'Popular',
2619 join => [ qw/cds cds/ ],
2622 will return a set of all artists that have both a cd with title 'Down
2623 to Earth' and a cd with title 'Popular'.
2625 If you want to fetch related objects from other tables as well, see C<prefetch>
2628 For more help on using joins with search, see L<DBIx::Class::Manual::Joining>.
2634 =item Value: ($rel_name | \@rel_names | \%rel_names)
2638 Contains one or more relationships that should be fetched along with
2639 the main query (when they are accessed afterwards the data will
2640 already be available, without extra queries to the database). This is
2641 useful for when you know you will need the related objects, because it
2642 saves at least one query:
2644 my $rs = $schema->resultset('Tag')->search(
2653 The initial search results in SQL like the following:
2655 SELECT tag.*, cd.*, artist.* FROM tag
2656 JOIN cd ON tag.cd = cd.cdid
2657 JOIN artist ON cd.artist = artist.artistid
2659 L<DBIx::Class> has no need to go back to the database when we access the
2660 C<cd> or C<artist> relationships, which saves us two SQL statements in this
2663 Simple prefetches will be joined automatically, so there is no need
2664 for a C<join> attribute in the above search.
2666 C<prefetch> can be used with the following relationship types: C<belongs_to>,
2667 C<has_one> (or if you're using C<add_relationship>, any relationship declared
2668 with an accessor type of 'single' or 'filter'). A more complex example that
2669 prefetches an artists cds, the tracks on those cds, and the tags associted
2670 with that artist is given below (assuming many-to-many from artists to tags):
2672 my $rs = $schema->resultset('Artist')->search(
2676 { cds => 'tracks' },
2677 { artist_tags => 'tags' }
2683 B<NOTE:> If you specify a C<prefetch> attribute, the C<join> and C<select>
2684 attributes will be ignored.
2694 Makes the resultset paged and specifies the page to retrieve. Effectively
2695 identical to creating a non-pages resultset and then calling ->page($page)
2698 If L<rows> attribute is not specified it defualts to 10 rows per page.
2708 Specifes the maximum number of rows for direct retrieval or the number of
2709 rows per page if the page attribute or method is used.
2715 =item Value: $offset
2719 Specifies the (zero-based) row number for the first row to be returned, or the
2720 of the first row of the first page if paging is used.
2726 =item Value: \@columns
2730 A arrayref of columns to group by. Can include columns of joined tables.
2732 group_by => [qw/ column1 column2 ... /]
2738 =item Value: $condition
2742 HAVING is a select statement attribute that is applied between GROUP BY and
2743 ORDER BY. It is applied to the after the grouping calculations have been
2746 having => { 'count(employee)' => { '>=', 100 } }
2752 =item Value: (0 | 1)
2756 Set to 1 to group by all columns.
2762 Adds to the WHERE clause.
2764 # only return rows WHERE deleted IS NULL for all searches
2765 __PACKAGE__->resultset_attributes({ where => { deleted => undef } }); )
2767 Can be overridden by passing C<{ where => undef }> as an attribute
2774 Set to 1 to cache search results. This prevents extra SQL queries if you
2775 revisit rows in your ResultSet:
2777 my $resultset = $schema->resultset('Artist')->search( undef, { cache => 1 } );
2779 while( my $artist = $resultset->next ) {
2783 $rs->first; # without cache, this would issue a query
2785 By default, searches are not cached.
2787 For more examples of using these attributes, see
2788 L<DBIx::Class::Manual::Cookbook>.
2794 =item Value: \@from_clause
2798 The C<from> attribute gives you manual control over the C<FROM> clause of SQL
2799 statements generated by L<DBIx::Class>, allowing you to express custom C<JOIN>
2802 NOTE: Use this on your own risk. This allows you to shoot off your foot!
2804 C<join> will usually do what you need and it is strongly recommended that you
2805 avoid using C<from> unless you cannot achieve the desired result using C<join>.
2806 And we really do mean "cannot", not just tried and failed. Attempting to use
2807 this because you're having problems with C<join> is like trying to use x86
2808 ASM because you've got a syntax error in your C. Trust us on this.
2810 Now, if you're still really, really sure you need to use this (and if you're
2811 not 100% sure, ask the mailing list first), here's an explanation of how this
2814 The syntax is as follows -
2817 { <alias1> => <table1> },
2819 { <alias2> => <table2>, -join_type => 'inner|left|right' },
2820 [], # nested JOIN (optional)
2821 { <table1.column1> => <table2.column2>, ... (more conditions) },
2823 # More of the above [ ] may follow for additional joins
2830 ON <table1.column1> = <table2.column2>
2831 <more joins may follow>
2833 An easy way to follow the examples below is to remember the following:
2835 Anything inside "[]" is a JOIN
2836 Anything inside "{}" is a condition for the enclosing JOIN
2838 The following examples utilize a "person" table in a family tree application.
2839 In order to express parent->child relationships, this table is self-joined:
2841 # Person->belongs_to('father' => 'Person');
2842 # Person->belongs_to('mother' => 'Person');
2844 C<from> can be used to nest joins. Here we return all children with a father,
2845 then search against all mothers of those children:
2847 $rs = $schema->resultset('Person')->search(
2850 alias => 'mother', # alias columns in accordance with "from"
2852 { mother => 'person' },
2855 { child => 'person' },
2857 { father => 'person' },
2858 { 'father.person_id' => 'child.father_id' }
2861 { 'mother.person_id' => 'child.mother_id' }
2868 # SELECT mother.* FROM person mother
2871 # JOIN person father
2872 # ON ( father.person_id = child.father_id )
2874 # ON ( mother.person_id = child.mother_id )
2876 The type of any join can be controlled manually. To search against only people
2877 with a father in the person table, we could explicitly use C<INNER JOIN>:
2879 $rs = $schema->resultset('Person')->search(
2882 alias => 'child', # alias columns in accordance with "from"
2884 { child => 'person' },
2886 { father => 'person', -join_type => 'inner' },
2887 { 'father.id' => 'child.father_id' }
2894 # SELECT child.* FROM person child
2895 # INNER JOIN person father ON child.father_id = father.id
2897 If you need to express really complex joins or you need a subselect, you
2898 can supply literal SQL to C<from> via a scalar reference. In this case
2899 the contents of the scalar will replace the table name asscoiated with the
2902 WARNING: This technique might very well not work as expected on chained
2903 searches - you have been warned.
2905 # Assuming the Event resultsource is defined as:
2907 MySchema::Event->add_columns (
2910 is_auto_increment => 1,
2919 MySchema::Event->set_primary_key ('sequence');
2921 # This will get back the latest event for every location. The column
2922 # selector is still provided by DBIC, all we do is add a JOIN/WHERE
2923 # combo to limit the resultset
2925 $rs = $schema->resultset('Event');
2926 $table = $rs->result_source->name;
2927 $latest = $rs->search (
2930 (SELECT e1.* FROM $table e1
2932 ON e1.location = e2.location
2933 AND e1.sequence < e2.sequence
2934 WHERE e2.sequence is NULL
2939 # Equivalent SQL (with the DBIC chunks added):
2941 SELECT me.sequence, me.location, me.type FROM
2942 (SELECT e1.* FROM events e1
2944 ON e1.location = e2.location
2945 AND e1.sequence < e2.sequence
2946 WHERE e2.sequence is NULL
2953 =item Value: ( 'update' | 'shared' )
2957 Set to 'update' for a SELECT ... FOR UPDATE or 'shared' for a SELECT