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 result_class => $attrs->{result_class} || $source->resolve->result_class,
112 cond => $attrs->{where},
127 =item Arguments: $cond, \%attrs?
129 =item Return Value: $resultset (scalar context), @row_objs (list context)
133 my @cds = $cd_rs->search({ year => 2001 }); # "... WHERE year = 2001"
134 my $new_rs = $cd_rs->search({ year => 2005 });
136 my $new_rs = $cd_rs->search([ { year => 2005 }, { year => 2004 } ]);
137 # year = 2005 OR year = 2004
139 If you need to pass in additional attributes but no additional condition,
140 call it as C<search(undef, \%attrs)>.
142 # "SELECT name, artistid FROM $artist_table"
143 my @all_artists = $schema->resultset('Artist')->search(undef, {
144 columns => [qw/name artistid/],
147 For a list of attributes that can be passed to C<search>, see
148 L</ATTRIBUTES>. For more examples of using this function, see
149 L<Searching|DBIx::Class::Manual::Cookbook/Searching>. For a complete
150 documentation for the first argument, see L<SQL::Abstract>.
152 For more help on using joins with search, see L<DBIx::Class::Manual::Joining>.
158 my $rs = $self->search_rs( @_ );
159 return (wantarray ? $rs->all : $rs);
166 =item Arguments: $cond, \%attrs?
168 =item Return Value: $resultset
172 This method does the same exact thing as search() except it will
173 always return a resultset, even in list context.
181 $attrs = pop(@_) if @_ > 1 and ref $_[$#_] eq 'HASH';
182 my $our_attrs = { %{$self->{attrs}} };
183 my $having = delete $our_attrs->{having};
184 my $where = delete $our_attrs->{where};
188 my %safe = (alias => 1, cache => 1);
191 (@_ && defined($_[0])) # @_ == () or (undef)
193 (keys %$attrs # empty attrs or only 'safe' attrs
194 && List::Util::first { !$safe{$_} } keys %$attrs)
196 # no search, effectively just a clone
197 $rows = $self->get_cache;
200 my $new_attrs = { %{$our_attrs}, %{$attrs} };
202 # merge new attrs into inherited
203 foreach my $key (qw/join prefetch/) {
204 next unless exists $attrs->{$key};
205 $new_attrs->{$key} = $self->_merge_attr($our_attrs->{$key}, $attrs->{$key});
210 (@_ == 1 || ref $_[0] eq "HASH")
212 (ref $_[0] eq 'HASH')
214 (keys %{ $_[0] } > 0)
222 ? $self->throw_exception("Odd number of arguments to search")
229 if (defined $where) {
230 $new_attrs->{where} = (
231 defined $new_attrs->{where}
234 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
235 } $where, $new_attrs->{where}
242 $new_attrs->{where} = (
243 defined $new_attrs->{where}
246 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
247 } $cond, $new_attrs->{where}
253 if (defined $having) {
254 $new_attrs->{having} = (
255 defined $new_attrs->{having}
258 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
259 } $having, $new_attrs->{having}
265 my $rs = (ref $self)->new($self->result_source, $new_attrs);
267 $rs->set_cache($rows);
272 =head2 search_literal
276 =item Arguments: $sql_fragment, @bind_values
278 =item Return Value: $resultset (scalar context), @row_objs (list context)
282 my @cds = $cd_rs->search_literal('year = ? AND title = ?', qw/2001 Reload/);
283 my $newrs = $artist_rs->search_literal('name = ?', 'Metallica');
285 Pass a literal chunk of SQL to be added to the conditional part of the
288 CAVEAT: C<search_literal> is provided for Class::DBI compatibility and should
289 only be used in that context. There are known problems using C<search_literal>
290 in chained queries; it can result in bind values in the wrong order. See
291 L<DBIx::Class::Manual::Cookbook/Searching> and
292 L<DBIx::Class::Manual::FAQ/Searching> for searching techniques that do not
293 require C<search_literal>.
298 my ($self, $cond, @vals) = @_;
299 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
300 $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ];
301 return $self->search(\$cond, $attrs);
308 =item Arguments: @values | \%cols, \%attrs?
310 =item Return Value: $row_object | undef
314 Finds a row based on its primary key or unique constraint. For example, to find
315 a row by its primary key:
317 my $cd = $schema->resultset('CD')->find(5);
319 You can also find a row by a specific unique constraint using the C<key>
320 attribute. For example:
322 my $cd = $schema->resultset('CD')->find('Massive Attack', 'Mezzanine', {
323 key => 'cd_artist_title'
326 Additionally, you can specify the columns explicitly by name:
328 my $cd = $schema->resultset('CD')->find(
330 artist => 'Massive Attack',
331 title => 'Mezzanine',
333 { key => 'cd_artist_title' }
336 If the C<key> is specified as C<primary>, it searches only on the primary key.
338 If no C<key> is specified, it searches on all unique constraints defined on the
339 source for which column data is provided, including the primary key.
341 If your table does not have a primary key, you B<must> provide a value for the
342 C<key> attribute matching one of the unique constraints on the source.
344 Note: If your query does not return only one row, a warning is generated:
346 Query returned more than one row
348 See also L</find_or_create> and L</update_or_create>. For information on how to
349 declare unique constraints, see
350 L<DBIx::Class::ResultSource/add_unique_constraint>.
356 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
358 # Default to the primary key, but allow a specific key
359 my @cols = exists $attrs->{key}
360 ? $self->result_source->unique_constraint_columns($attrs->{key})
361 : $self->result_source->primary_columns;
362 $self->throw_exception(
363 "Can't find unless a primary key is defined or unique constraint is specified"
366 # Parse out a hashref from input
368 if (ref $_[0] eq 'HASH') {
369 $input_query = { %{$_[0]} };
371 elsif (@_ == @cols) {
373 @{$input_query}{@cols} = @_;
376 # Compatibility: Allow e.g. find(id => $value)
377 carp "Find by key => value deprecated; please use a hashref instead";
381 my (%related, $info);
383 KEY: foreach my $key (keys %$input_query) {
384 if (ref($input_query->{$key})
385 && ($info = $self->result_source->relationship_info($key))) {
386 my $val = delete $input_query->{$key};
387 next KEY if (ref($val) eq 'ARRAY'); # has_many for multi_create
388 my $rel_q = $self->result_source->resolve_condition(
389 $info->{cond}, $val, $key
391 die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY';
392 @related{keys %$rel_q} = values %$rel_q;
395 if (my @keys = keys %related) {
396 @{$input_query}{@keys} = values %related;
400 # Build the final query: Default to the disjunction of the unique queries,
401 # but allow the input query in case the ResultSet defines the query or the
402 # user is abusing find
403 my $alias = exists $attrs->{alias} ? $attrs->{alias} : $self->{attrs}{alias};
405 if (exists $attrs->{key}) {
406 my @unique_cols = $self->result_source->unique_constraint_columns($attrs->{key});
407 my $unique_query = $self->_build_unique_query($input_query, \@unique_cols);
408 $query = $self->_add_alias($unique_query, $alias);
411 my @unique_queries = $self->_unique_queries($input_query, $attrs);
412 $query = @unique_queries
413 ? [ map { $self->_add_alias($_, $alias) } @unique_queries ]
414 : $self->_add_alias($input_query, $alias);
419 my $rs = $self->search($query, $attrs);
420 if (keys %{$rs->_resolved_attrs->{collapse}}) {
422 carp "Query returned more than one row" if $rs->next;
430 if (keys %{$self->_resolved_attrs->{collapse}}) {
431 my $rs = $self->search($query);
433 carp "Query returned more than one row" if $rs->next;
437 return $self->single($query);
444 # Add the specified alias to the specified query hash. A copy is made so the
445 # original query is not modified.
448 my ($self, $query, $alias) = @_;
450 my %aliased = %$query;
451 foreach my $col (grep { ! m/\./ } keys %aliased) {
452 $aliased{"$alias.$col"} = delete $aliased{$col};
460 # Build a list of queries which satisfy unique constraints.
462 sub _unique_queries {
463 my ($self, $query, $attrs) = @_;
465 my @constraint_names = exists $attrs->{key}
467 : $self->result_source->unique_constraint_names;
469 my $where = $self->_collapse_cond($self->{attrs}{where} || {});
470 my $num_where = scalar keys %$where;
473 foreach my $name (@constraint_names) {
474 my @unique_cols = $self->result_source->unique_constraint_columns($name);
475 my $unique_query = $self->_build_unique_query($query, \@unique_cols);
477 my $num_cols = scalar @unique_cols;
478 my $num_query = scalar keys %$unique_query;
480 my $total = $num_query + $num_where;
481 if ($num_query && ($num_query == $num_cols || $total == $num_cols)) {
482 # The query is either unique on its own or is unique in combination with
483 # the existing where clause
484 push @unique_queries, $unique_query;
488 return @unique_queries;
491 # _build_unique_query
493 # Constrain the specified query hash based on the specified column names.
495 sub _build_unique_query {
496 my ($self, $query, $unique_cols) = @_;
499 map { $_ => $query->{$_} }
500 grep { exists $query->{$_} }
505 =head2 search_related
509 =item Arguments: $rel, $cond, \%attrs?
511 =item Return Value: $new_resultset
515 $new_rs = $cd_rs->search_related('artist', {
519 Searches the specified relationship, optionally specifying a condition and
520 attributes for matching records. See L</ATTRIBUTES> for more information.
525 return shift->related_resultset(shift)->search(@_);
528 =head2 search_related_rs
530 This method works exactly the same as search_related, except that
531 it guarantees a restultset, even in list context.
535 sub search_related_rs {
536 return shift->related_resultset(shift)->search_rs(@_);
543 =item Arguments: none
545 =item Return Value: $cursor
549 Returns a storage-driven cursor to the given resultset. See
550 L<DBIx::Class::Cursor> for more information.
557 my $attrs = { %{$self->_resolved_attrs} };
558 return $self->{cursor}
559 ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select},
560 $attrs->{where},$attrs);
567 =item Arguments: $cond?
569 =item Return Value: $row_object?
573 my $cd = $schema->resultset('CD')->single({ year => 2001 });
575 Inflates the first result without creating a cursor if the resultset has
576 any records in it; if not returns nothing. Used by L</find> as a lean version of
579 While this method can take an optional search condition (just like L</search>)
580 being a fast-code-path it does not recognize search attributes. If you need to
581 add extra joins or similar, call L</search> and then chain-call L</single> on the
582 L<DBIx::Class::ResultSet> returned.
588 As of 0.08100, this method enforces the assumption that the preceeding
589 query returns only one row. If more than one row is returned, you will receive
592 Query returned more than one row
594 In this case, you should be using L</first> or L</find> instead, or if you really
595 know what you are doing, use the L</rows> attribute to explicitly limit the size
603 my ($self, $where) = @_;
604 my $attrs = { %{$self->_resolved_attrs} };
606 if (defined $attrs->{where}) {
609 [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
610 $where, delete $attrs->{where} ]
613 $attrs->{where} = $where;
617 # XXX: Disabled since it doesn't infer uniqueness in all cases
618 # unless ($self->_is_unique_query($attrs->{where})) {
619 # carp "Query not guaranteed to return a single row"
620 # . "; please declare your unique constraints or use search instead";
623 my @data = $self->result_source->storage->select_single(
624 $attrs->{from}, $attrs->{select},
625 $attrs->{where}, $attrs
628 return (@data ? ($self->_construct_object(@data))[0] : undef);
633 # Try to determine if the specified query is guaranteed to be unique, based on
634 # the declared unique constraints.
636 sub _is_unique_query {
637 my ($self, $query) = @_;
639 my $collapsed = $self->_collapse_query($query);
640 my $alias = $self->{attrs}{alias};
642 foreach my $name ($self->result_source->unique_constraint_names) {
643 my @unique_cols = map {
645 } $self->result_source->unique_constraint_columns($name);
647 # Count the values for each unique column
648 my %seen = map { $_ => 0 } @unique_cols;
650 foreach my $key (keys %$collapsed) {
651 my $aliased = $key =~ /\./ ? $key : "$alias.$key";
652 next unless exists $seen{$aliased}; # Additional constraints are okay
653 $seen{$aliased} = scalar keys %{ $collapsed->{$key} };
656 # If we get 0 or more than 1 value for a column, it's not necessarily unique
657 return 1 unless grep { $_ != 1 } values %seen;
665 # Recursively collapse the query, accumulating values for each column.
667 sub _collapse_query {
668 my ($self, $query, $collapsed) = @_;
672 if (ref $query eq 'ARRAY') {
673 foreach my $subquery (@$query) {
674 next unless ref $subquery; # -or
675 # warn "ARRAY: " . Dumper $subquery;
676 $collapsed = $self->_collapse_query($subquery, $collapsed);
679 elsif (ref $query eq 'HASH') {
680 if (keys %$query and (keys %$query)[0] eq '-and') {
681 foreach my $subquery (@{$query->{-and}}) {
682 # warn "HASH: " . Dumper $subquery;
683 $collapsed = $self->_collapse_query($subquery, $collapsed);
687 # warn "LEAF: " . Dumper $query;
688 foreach my $col (keys %$query) {
689 my $value = $query->{$col};
690 $collapsed->{$col}{$value}++;
702 =item Arguments: $cond?
704 =item Return Value: $resultsetcolumn
708 my $max_length = $rs->get_column('length')->max;
710 Returns a L<DBIx::Class::ResultSetColumn> instance for a column of the ResultSet.
715 my ($self, $column) = @_;
716 my $new = DBIx::Class::ResultSetColumn->new($self, $column);
724 =item Arguments: $cond, \%attrs?
726 =item Return Value: $resultset (scalar context), @row_objs (list context)
730 # WHERE title LIKE '%blue%'
731 $cd_rs = $rs->search_like({ title => '%blue%'});
733 Performs a search, but uses C<LIKE> instead of C<=> as the condition. Note
734 that this is simply a convenience method. You most likely want to use
735 L</search> with specific operators.
737 For more information, see L<DBIx::Class::Manual::Cookbook>.
743 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
744 my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_};
745 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
746 return $class->search($query, { %$attrs });
753 =item Arguments: $first, $last
755 =item Return Value: $resultset (scalar context), @row_objs (list context)
759 Returns a resultset or object list representing a subset of elements from the
760 resultset slice is called on. Indexes are from 0, i.e., to get the first
763 my ($one, $two, $three) = $rs->slice(0, 2);
768 my ($self, $min, $max) = @_;
769 my $attrs = {}; # = { %{ $self->{attrs} || {} } };
770 $attrs->{offset} = $self->{attrs}{offset} || 0;
771 $attrs->{offset} += $min;
772 $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
773 return $self->search(undef(), $attrs);
774 #my $slice = (ref $self)->new($self->result_source, $attrs);
775 #return (wantarray ? $slice->all : $slice);
782 =item Arguments: none
784 =item Return Value: $result?
788 Returns the next element in the resultset (C<undef> is there is none).
790 Can be used to efficiently iterate over records in the resultset:
792 my $rs = $schema->resultset('CD')->search;
793 while (my $cd = $rs->next) {
797 Note that you need to store the resultset object, and call C<next> on it.
798 Calling C<< resultset('Table')->next >> repeatedly will always return the
799 first record from the resultset.
805 if (my $cache = $self->get_cache) {
806 $self->{all_cache_position} ||= 0;
807 return $cache->[$self->{all_cache_position}++];
809 if ($self->{attrs}{cache}) {
810 $self->{all_cache_position} = 1;
811 return ($self->all)[0];
813 if ($self->{stashed_objects}) {
814 my $obj = shift(@{$self->{stashed_objects}});
815 delete $self->{stashed_objects} unless @{$self->{stashed_objects}};
819 exists $self->{stashed_row}
820 ? @{delete $self->{stashed_row}}
821 : $self->cursor->next
823 return undef unless (@row);
824 my ($row, @more) = $self->_construct_object(@row);
825 $self->{stashed_objects} = \@more if @more;
829 sub _construct_object {
830 my ($self, @row) = @_;
831 my $info = $self->_collapse_result($self->{_attrs}{as}, \@row);
832 my @new = $self->result_class->inflate_result($self->result_source, @$info);
833 @new = $self->{_attrs}{record_filter}->(@new)
834 if exists $self->{_attrs}{record_filter};
838 sub _collapse_result {
839 my ($self, $as_proto, $row) = @_;
843 # 'foo' => [ undef, 'foo' ]
844 # 'foo.bar' => [ 'foo', 'bar' ]
845 # 'foo.bar.baz' => [ 'foo.bar', 'baz' ]
847 my @construct_as = map { [ (/^(?:(.*)\.)?([^.]+)$/) ] } @$as_proto;
849 my %collapse = %{$self->{_attrs}{collapse}||{}};
853 # if we're doing collapsing (has_many prefetch) we need to grab records
854 # until the PK changes, so fill @pri_index. if not, we leave it empty so
855 # we know we don't have to bother.
857 # the reason for not using the collapse stuff directly is because if you
858 # had for e.g. two artists in a row with no cds, the collapse info for
859 # both would be NULL (undef) so you'd lose the second artist
861 # store just the index so we can check the array positions from the row
862 # without having to contruct the full hash
864 if (keys %collapse) {
865 my %pri = map { ($_ => 1) } $self->result_source->primary_columns;
866 foreach my $i (0 .. $#construct_as) {
867 next if defined($construct_as[$i][0]); # only self table
868 if (delete $pri{$construct_as[$i][1]}) {
869 push(@pri_index, $i);
871 last unless keys %pri; # short circuit (Johnny Five Is Alive!)
875 # no need to do an if, it'll be empty if @pri_index is empty anyway
877 my %pri_vals = map { ($_ => $copy[$_]) } @pri_index;
881 do { # no need to check anything at the front, we always want the first row
885 foreach my $this_as (@construct_as) {
886 $const{$this_as->[0]||''}{$this_as->[1]} = shift(@copy);
889 push(@const_rows, \%const);
891 } until ( # no pri_index => no collapse => drop straight out
894 do { # get another row, stash it, drop out if different PK
896 @copy = $self->cursor->next;
897 $self->{stashed_row} = \@copy;
899 # last thing in do block, counts as true if anything doesn't match
901 # check xor defined first for NULL vs. NOT NULL then if one is
902 # defined the other must be so check string equality
905 (defined $pri_vals{$_} ^ defined $copy[$_])
906 || (defined $pri_vals{$_} && ($pri_vals{$_} ne $copy[$_]))
911 my $alias = $self->{attrs}{alias};
918 foreach my $const (@const_rows) {
919 scalar @const_keys or do {
920 @const_keys = sort { length($a) <=> length($b) } keys %$const;
922 foreach my $key (@const_keys) {
925 my @parts = split(/\./, $key);
927 my $data = $const->{$key};
928 foreach my $p (@parts) {
929 $target = $target->[1]->{$p} ||= [];
931 if ($cur eq ".${key}" && (my @ckey = @{$collapse{$cur}||[]})) {
932 # collapsing at this point and on final part
933 my $pos = $collapse_pos{$cur};
934 CK: foreach my $ck (@ckey) {
935 if (!defined $pos->{$ck} || $pos->{$ck} ne $data->{$ck}) {
936 $collapse_pos{$cur} = $data;
937 delete @collapse_pos{ # clear all positioning for sub-entries
938 grep { m/^\Q${cur}.\E/ } keys %collapse_pos
945 if (exists $collapse{$cur}) {
946 $target = $target->[-1];
949 $target->[0] = $data;
951 $info->[0] = $const->{$key};
963 =item Arguments: $result_source?
965 =item Return Value: $result_source
969 An accessor for the primary ResultSource object from which this ResultSet
976 =item Arguments: $result_class?
978 =item Return Value: $result_class
982 An accessor for the class to use when creating row objects. Defaults to
983 C<< result_source->result_class >> - which in most cases is the name of the
984 L<"table"|DBIx::Class::Manual::Glossary/"ResultSource"> class.
993 =item Arguments: $cond, \%attrs??
995 =item Return Value: $count
999 Performs an SQL C<COUNT> with the same query as the resultset was built
1000 with to find the number of elements. If passed arguments, does a search
1001 on the resultset and counts the results of that.
1003 Note: When using C<count> with C<group_by>, L<DBIx::Class> emulates C<GROUP BY>
1004 using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
1005 not support C<DISTINCT> with multiple columns. If you are using such a
1006 database, you should only use columns from the main table in your C<group_by>
1013 return $self->search(@_)->count if @_ and defined $_[0];
1014 return scalar @{ $self->get_cache } if $self->get_cache;
1015 my $count = $self->_count;
1016 return 0 unless $count;
1018 # need to take offset from resolved attrs
1020 $count -= $self->{_attrs}{offset} if $self->{_attrs}{offset};
1021 $count = $self->{attrs}{rows} if
1022 $self->{attrs}{rows} and $self->{attrs}{rows} < $count;
1023 $count = 0 if ($count < 0);
1027 sub _count { # Separated out so pager can get the full count
1029 my $select = { count => '*' };
1031 my $attrs = { %{$self->_resolved_attrs} };
1032 if (my $group_by = delete $attrs->{group_by}) {
1033 delete $attrs->{having};
1034 my @distinct = (ref $group_by ? @$group_by : ($group_by));
1035 # todo: try CONCAT for multi-column pk
1036 my @pk = $self->result_source->primary_columns;
1038 my $alias = $attrs->{alias};
1039 foreach my $column (@distinct) {
1040 if ($column =~ qr/^(?:\Q${alias}.\E)?$pk[0]$/) {
1041 @distinct = ($column);
1047 $select = { count => { distinct => \@distinct } };
1050 $attrs->{select} = $select;
1051 $attrs->{as} = [qw/count/];
1053 # offset, order by and page are not needed to count. record_filter is cdbi
1054 delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
1056 my $tmp_rs = (ref $self)->new($self->result_source, $attrs);
1057 my ($count) = $tmp_rs->cursor->next;
1065 =head2 count_literal
1069 =item Arguments: $sql_fragment, @bind_values
1071 =item Return Value: $count
1075 Counts the results in a literal query. Equivalent to calling L</search_literal>
1076 with the passed arguments, then L</count>.
1080 sub count_literal { shift->search_literal(@_)->count; }
1086 =item Arguments: none
1088 =item Return Value: @objects
1092 Returns all elements in the resultset. Called implicitly if the resultset
1093 is returned in list context.
1099 return @{ $self->get_cache } if $self->get_cache;
1103 # TODO: don't call resolve here
1104 if (keys %{$self->_resolved_attrs->{collapse}}) {
1105 # if ($self->{attrs}{prefetch}) {
1106 # Using $self->cursor->all is really just an optimisation.
1107 # If we're collapsing has_many prefetches it probably makes
1108 # very little difference, and this is cleaner than hacking
1109 # _construct_object to survive the approach
1110 my @row = $self->cursor->next;
1112 push(@obj, $self->_construct_object(@row));
1113 @row = (exists $self->{stashed_row}
1114 ? @{delete $self->{stashed_row}}
1115 : $self->cursor->next);
1118 @obj = map { $self->_construct_object(@$_) } $self->cursor->all;
1121 $self->set_cache(\@obj) if $self->{attrs}{cache};
1129 =item Arguments: none
1131 =item Return Value: $self
1135 Resets the resultset's cursor, so you can iterate through the elements again.
1141 delete $self->{_attrs} if exists $self->{_attrs};
1142 $self->{all_cache_position} = 0;
1143 $self->cursor->reset;
1151 =item Arguments: none
1153 =item Return Value: $object?
1157 Resets the resultset and returns an object for the first result (if the
1158 resultset returns anything).
1163 return $_[0]->reset->next;
1166 # _cond_for_update_delete
1168 # update/delete require the condition to be modified to handle
1169 # the differing SQL syntax available. This transforms the $self->{cond}
1170 # appropriately, returning the new condition.
1172 sub _cond_for_update_delete {
1173 my ($self, $full_cond) = @_;
1176 $full_cond ||= $self->{cond};
1177 # No-op. No condition, we're updating/deleting everything
1178 return $cond unless ref $full_cond;
1180 if (ref $full_cond eq 'ARRAY') {
1184 foreach my $key (keys %{$_}) {
1186 $hash{$1} = $_->{$key};
1192 elsif (ref $full_cond eq 'HASH') {
1193 if ((keys %{$full_cond})[0] eq '-and') {
1196 my @cond = @{$full_cond->{-and}};
1197 for (my $i = 0; $i < @cond; $i++) {
1198 my $entry = $cond[$i];
1201 if (ref $entry eq 'HASH') {
1202 $hash = $self->_cond_for_update_delete($entry);
1205 $entry =~ /([^.]+)$/;
1206 $hash->{$1} = $cond[++$i];
1209 push @{$cond->{-and}}, $hash;
1213 foreach my $key (keys %{$full_cond}) {
1215 $cond->{$1} = $full_cond->{$key};
1220 $self->throw_exception(
1221 "Can't update/delete on resultset with condition unless hash or array"
1233 =item Arguments: \%values
1235 =item Return Value: $storage_rv
1239 Sets the specified columns in the resultset to the supplied values in a
1240 single query. Return value will be true if the update succeeded or false
1241 if no records were updated; exact type of success value is storage-dependent.
1246 my ($self, $values) = @_;
1247 $self->throw_exception("Values for update must be a hash")
1248 unless ref $values eq 'HASH';
1250 my $cond = $self->_cond_for_update_delete;
1252 return $self->result_source->storage->update(
1253 $self->result_source, $values, $cond
1261 =item Arguments: \%values
1263 =item Return Value: 1
1267 Fetches all objects and updates them one at a time. Note that C<update_all>
1268 will run DBIC cascade triggers, while L</update> will not.
1273 my ($self, $values) = @_;
1274 $self->throw_exception("Values for update must be a hash")
1275 unless ref $values eq 'HASH';
1276 foreach my $obj ($self->all) {
1277 $obj->set_columns($values)->update;
1286 =item Arguments: none
1288 =item Return Value: 1
1292 Deletes the contents of the resultset from its result source. Note that this
1293 will not run DBIC cascade triggers. See L</delete_all> if you need triggers
1294 to run. See also L<DBIx::Class::Row/delete>.
1301 my $cond = $self->_cond_for_update_delete;
1303 $self->result_source->storage->delete($self->result_source, $cond);
1311 =item Arguments: none
1313 =item Return Value: 1
1317 Fetches all objects and deletes them one at a time. Note that C<delete_all>
1318 will run DBIC cascade triggers, while L</delete> will not.
1324 $_->delete for $self->all;
1332 =item Arguments: \@data;
1336 Pass an arrayref of hashrefs. Each hashref should be a structure suitable for
1337 submitting to a $resultset->create(...) method.
1339 In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
1340 to insert the data, as this is a faster method.
1342 Otherwise, each set of data is inserted into the database using
1343 L<DBIx::Class::ResultSet/create>, and a arrayref of the resulting row
1344 objects is returned.
1346 Example: Assuming an Artist Class that has many CDs Classes relating:
1348 my $Artist_rs = $schema->resultset("Artist");
1350 ## Void Context Example
1351 $Artist_rs->populate([
1352 { artistid => 4, name => 'Manufactured Crap', cds => [
1353 { title => 'My First CD', year => 2006 },
1354 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
1357 { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
1358 { title => 'My parents sold me to a record company' ,year => 2005 },
1359 { title => 'Why Am I So Ugly?', year => 2006 },
1360 { title => 'I Got Surgery and am now Popular', year => 2007 }
1365 ## Array Context Example
1366 my ($ArtistOne, $ArtistTwo, $ArtistThree) = $Artist_rs->populate([
1367 { name => "Artist One"},
1368 { name => "Artist Two"},
1369 { name => "Artist Three", cds=> [
1370 { title => "First CD", year => 2007},
1371 { title => "Second CD", year => 2008},
1375 print $ArtistOne->name; ## response is 'Artist One'
1376 print $ArtistThree->cds->count ## reponse is '2'
1378 Please note an important effect on your data when choosing between void and
1379 wantarray context. Since void context goes straight to C<insert_bulk> in
1380 L<DBIx::Class::Storage::DBI> this will skip any component that is overriding
1381 c<insert>. So if you are using something like L<DBIx-Class-UUIDColumns> to
1382 create primary keys for you, you will find that your PKs are empty. In this
1383 case you will have to use the wantarray context in order to create those
1389 my ($self, $data) = @_;
1391 if(defined wantarray) {
1393 foreach my $item (@$data) {
1394 push(@created, $self->create($item));
1398 my ($first, @rest) = @$data;
1400 my @names = grep {!ref $first->{$_}} keys %$first;
1401 my @rels = grep { $self->result_source->has_relationship($_) } keys %$first;
1402 my @pks = $self->result_source->primary_columns;
1404 ## do the belongs_to relationships
1405 foreach my $index (0..$#$data) {
1406 if( grep { !defined $data->[$index]->{$_} } @pks ) {
1407 my @ret = $self->populate($data);
1411 foreach my $rel (@rels) {
1412 next unless $data->[$index]->{$rel} && ref $data->[$index]->{$rel} eq "HASH";
1413 my $result = $self->related_resultset($rel)->create($data->[$index]->{$rel});
1414 my ($reverse) = keys %{$self->result_source->reverse_relationship_info($rel)};
1415 my $related = $result->result_source->resolve_condition(
1416 $result->result_source->relationship_info($reverse)->{cond},
1421 delete $data->[$index]->{$rel};
1422 $data->[$index] = {%{$data->[$index]}, %$related};
1424 push @names, keys %$related if $index == 0;
1428 ## do bulk insert on current row
1429 my @values = map { [ @$_{@names} ] } @$data;
1431 $self->result_source->storage->insert_bulk(
1432 $self->result_source,
1437 ## do the has_many relationships
1438 foreach my $item (@$data) {
1440 foreach my $rel (@rels) {
1441 next unless $item->{$rel} && ref $item->{$rel} eq "ARRAY";
1443 my $parent = $self->find(map {{$_=>$item->{$_}} } @pks)
1444 || $self->throw_exception('Cannot find the relating object.');
1446 my $child = $parent->$rel;
1448 my $related = $child->result_source->resolve_condition(
1449 $parent->result_source->relationship_info($rel)->{cond},
1454 my @rows_to_add = ref $item->{$rel} eq 'ARRAY' ? @{$item->{$rel}} : ($item->{$rel});
1455 my @populate = map { {%$_, %$related} } @rows_to_add;
1457 $child->populate( \@populate );
1467 =item Arguments: none
1469 =item Return Value: $pager
1473 Return Value a L<Data::Page> object for the current resultset. Only makes
1474 sense for queries with a C<page> attribute.
1480 my $attrs = $self->{attrs};
1481 $self->throw_exception("Can't create pager for non-paged rs")
1482 unless $self->{attrs}{page};
1483 $attrs->{rows} ||= 10;
1484 return $self->{pager} ||= Data::Page->new(
1485 $self->_count, $attrs->{rows}, $self->{attrs}{page});
1492 =item Arguments: $page_number
1494 =item Return Value: $rs
1498 Returns a resultset for the $page_number page of the resultset on which page
1499 is called, where each page contains a number of rows equal to the 'rows'
1500 attribute set on the resultset (10 by default).
1505 my ($self, $page) = @_;
1506 return (ref $self)->new($self->result_source, { %{$self->{attrs}}, page => $page });
1513 =item Arguments: \%vals
1515 =item Return Value: $rowobject
1519 Creates a new row object in the resultset's result class and returns
1520 it. The row is not inserted into the database at this point, call
1521 L<DBIx::Class::Row/insert> to do that. Calling L<DBIx::Class::Row/in_storage>
1522 will tell you whether the row object has been inserted or not.
1524 Passes the hashref of input on to L<DBIx::Class::Row/new>.
1529 my ($self, $values) = @_;
1530 $self->throw_exception( "new_result needs a hash" )
1531 unless (ref $values eq 'HASH');
1534 my $alias = $self->{attrs}{alias};
1537 defined $self->{cond}
1538 && $self->{cond} eq $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION
1540 %new = %{$self->{attrs}{related_objects}};
1542 $self->throw_exception(
1543 "Can't abstract implicit construct, condition not a hash"
1544 ) if ($self->{cond} && !(ref $self->{cond} eq 'HASH'));
1546 my $collapsed_cond = (
1548 ? $self->_collapse_cond($self->{cond})
1552 # precendence must be given to passed values over values inherited from
1553 # the cond, so the order here is important.
1554 my %implied = %{$self->_remove_alias($collapsed_cond, $alias)};
1555 while( my($col,$value) = each %implied ){
1556 if(ref($value) eq 'HASH' && keys(%$value) && (keys %$value)[0] eq '='){
1557 $new{$col} = $value->{'='};
1560 $new{$col} = $value if $self->_is_deterministic_value($value);
1566 %{ $self->_remove_alias($values, $alias) },
1567 -source_handle => $self->_source_handle,
1568 -result_source => $self->result_source, # DO NOT REMOVE THIS, REQUIRED
1571 return $self->result_class->new(\%new);
1574 # _is_deterministic_value
1576 # Make an effor to strip non-deterministic values from the condition,
1577 # to make sure new_result chokes less
1579 sub _is_deterministic_value {
1582 my $ref_type = ref $value;
1583 return 1 if $ref_type eq '' || $ref_type eq 'SCALAR';
1584 return 1 if Scalar::Util::blessed($value);
1590 # Recursively collapse the condition.
1592 sub _collapse_cond {
1593 my ($self, $cond, $collapsed) = @_;
1597 if (ref $cond eq 'ARRAY') {
1598 foreach my $subcond (@$cond) {
1599 next unless ref $subcond; # -or
1600 # warn "ARRAY: " . Dumper $subcond;
1601 $collapsed = $self->_collapse_cond($subcond, $collapsed);
1604 elsif (ref $cond eq 'HASH') {
1605 if (keys %$cond and (keys %$cond)[0] eq '-and') {
1606 foreach my $subcond (@{$cond->{-and}}) {
1607 # warn "HASH: " . Dumper $subcond;
1608 $collapsed = $self->_collapse_cond($subcond, $collapsed);
1612 # warn "LEAF: " . Dumper $cond;
1613 foreach my $col (keys %$cond) {
1614 my $value = $cond->{$col};
1615 $collapsed->{$col} = $value;
1625 # Remove the specified alias from the specified query hash. A copy is made so
1626 # the original query is not modified.
1629 my ($self, $query, $alias) = @_;
1631 my %orig = %{ $query || {} };
1634 foreach my $key (keys %orig) {
1636 $unaliased{$key} = $orig{$key};
1639 $unaliased{$1} = $orig{$key}
1640 if $key =~ m/^(?:\Q$alias\E\.)?([^.]+)$/;
1650 =item Arguments: \%vals, \%attrs?
1652 =item Return Value: $rowobject
1656 my $artist = $schema->resultset('Artist')->find_or_new(
1657 { artist => 'fred' }, { key => 'artists' });
1659 $cd->cd_to_producer->find_or_new({ producer => $producer },
1660 { key => 'primary });
1662 Find an existing record from this resultset, based on it's primary
1663 key, or a unique constraint. If none exists, instantiate a new result
1664 object and return it. The object will not be saved into your storage
1665 until you call L<DBIx::Class::Row/insert> on it.
1667 You most likely want this method when looking for existing rows using
1668 a unique constraint that is not the primary key, or looking for
1671 If you want objects to be saved immediately, use L</find_or_create> instead.
1673 B<Note>: C<find_or_new> is probably not what you want when creating a
1674 new row in a table that uses primary keys supplied by the
1675 database. Passing in a primary key column with a value of I<undef>
1676 will cause L</find> to attempt to search for a row with a value of
1683 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1684 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1685 my $exists = $self->find($hash, $attrs);
1686 return defined $exists ? $exists : $self->new_result($hash);
1693 =item Arguments: \%vals
1695 =item Return Value: a L<DBIx::Class::Row> $object
1699 Attempt to create a single new row or a row with multiple related rows
1700 in the table represented by the resultset (and related tables). This
1701 will not check for duplicate rows before inserting, use
1702 L</find_or_create> to do that.
1704 To create one row for this resultset, pass a hashref of key/value
1705 pairs representing the columns of the table and the values you wish to
1706 store. If the appropriate relationships are set up, foreign key fields
1707 can also be passed an object representing the foreign row, and the
1708 value will be set to it's primary key.
1710 To create related objects, pass a hashref for the value if the related
1711 item is a foreign key relationship (L<DBIx::Class::Relationship/belongs_to>),
1712 and use the name of the relationship as the key. (NOT the name of the field,
1713 necessarily). For C<has_many> and C<has_one> relationships, pass an arrayref
1714 of hashrefs containing the data for each of the rows to create in the foreign
1715 tables, again using the relationship name as the key.
1717 Instead of hashrefs of plain related data (key/value pairs), you may
1718 also pass new or inserted objects. New objects (not inserted yet, see
1719 L</new>), will be inserted into their appropriate tables.
1721 Effectively a shortcut for C<< ->new_result(\%vals)->insert >>.
1723 Example of creating a new row.
1725 $person_rs->create({
1726 name=>"Some Person",
1727 email=>"somebody@someplace.com"
1730 Example of creating a new row and also creating rows in a related C<has_many>
1731 or C<has_one> resultset. Note Arrayref.
1734 { artistid => 4, name => 'Manufactured Crap', cds => [
1735 { title => 'My First CD', year => 2006 },
1736 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
1741 Example of creating a new row and also creating a row in a related
1742 C<belongs_to>resultset. Note Hashref.
1745 title=>"Music for Silly Walks",
1748 name=>"Silly Musician",
1755 my ($self, $attrs) = @_;
1756 $self->throw_exception( "create needs a hashref" )
1757 unless ref $attrs eq 'HASH';
1758 return $self->new_result($attrs)->insert;
1761 =head2 find_or_create
1765 =item Arguments: \%vals, \%attrs?
1767 =item Return Value: $rowobject
1771 $cd->cd_to_producer->find_or_create({ producer => $producer },
1772 { key => 'primary });
1774 Tries to find a record based on its primary key or unique constraints; if none
1775 is found, creates one and returns that instead.
1777 my $cd = $schema->resultset('CD')->find_or_create({
1779 artist => 'Massive Attack',
1780 title => 'Mezzanine',
1784 Also takes an optional C<key> attribute, to search by a specific key or unique
1785 constraint. For example:
1787 my $cd = $schema->resultset('CD')->find_or_create(
1789 artist => 'Massive Attack',
1790 title => 'Mezzanine',
1792 { key => 'cd_artist_title' }
1795 B<Note>: Because find_or_create() reads from the database and then
1796 possibly inserts based on the result, this method is subject to a race
1797 condition. Another process could create a record in the table after
1798 the find has completed and before the create has started. To avoid
1799 this problem, use find_or_create() inside a transaction.
1801 B<Note>: C<find_or_create> is probably not what you want when creating
1802 a new row in a table that uses primary keys supplied by the
1803 database. Passing in a primary key column with a value of I<undef>
1804 will cause L</find> to attempt to search for a row with a value of
1807 See also L</find> and L</update_or_create>. For information on how to declare
1808 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1812 sub find_or_create {
1814 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1815 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1816 my $exists = $self->find($hash, $attrs);
1817 return defined $exists ? $exists : $self->create($hash);
1820 =head2 update_or_create
1824 =item Arguments: \%col_values, { key => $unique_constraint }?
1826 =item Return Value: $rowobject
1830 $resultset->update_or_create({ col => $val, ... });
1832 First, searches for an existing row matching one of the unique constraints
1833 (including the primary key) on the source of this resultset. If a row is
1834 found, updates it with the other given column values. Otherwise, creates a new
1837 Takes an optional C<key> attribute to search on a specific unique constraint.
1840 # In your application
1841 my $cd = $schema->resultset('CD')->update_or_create(
1843 artist => 'Massive Attack',
1844 title => 'Mezzanine',
1847 { key => 'cd_artist_title' }
1850 $cd->cd_to_producer->update_or_create({
1851 producer => $producer,
1858 If no C<key> is specified, it searches on all unique constraints defined on the
1859 source, including the primary key.
1861 If the C<key> is specified as C<primary>, it searches only on the primary key.
1863 See also L</find> and L</find_or_create>. For information on how to declare
1864 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1866 B<Note>: C<update_or_create> is probably not what you want when
1867 looking for a row in a table that uses primary keys supplied by the
1868 database, unless you actually have a key value. Passing in a primary
1869 key column with a value of I<undef> will cause L</find> to attempt to
1870 search for a row with a value of I<NULL>.
1874 sub update_or_create {
1876 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1877 my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
1879 my $row = $self->find($cond, $attrs);
1881 $row->update($cond);
1885 return $self->create($cond);
1892 =item Arguments: none
1894 =item Return Value: \@cache_objects?
1898 Gets the contents of the cache for the resultset, if the cache is set.
1900 The cache is populated either by using the L</prefetch> attribute to
1901 L</search> or by calling L</set_cache>.
1913 =item Arguments: \@cache_objects
1915 =item Return Value: \@cache_objects
1919 Sets the contents of the cache for the resultset. Expects an arrayref
1920 of objects of the same class as those produced by the resultset. Note that
1921 if the cache is set the resultset will return the cached objects rather
1922 than re-querying the database even if the cache attr is not set.
1924 The contents of the cache can also be populated by using the
1925 L</prefetch> attribute to L</search>.
1930 my ( $self, $data ) = @_;
1931 $self->throw_exception("set_cache requires an arrayref")
1932 if defined($data) && (ref $data ne 'ARRAY');
1933 $self->{all_cache} = $data;
1940 =item Arguments: none
1942 =item Return Value: []
1946 Clears the cache for the resultset.
1951 shift->set_cache(undef);
1954 =head2 related_resultset
1958 =item Arguments: $relationship_name
1960 =item Return Value: $resultset
1964 Returns a related resultset for the supplied relationship name.
1966 $artist_rs = $schema->resultset('CD')->related_resultset('Artist');
1970 sub related_resultset {
1971 my ($self, $rel) = @_;
1973 $self->{related_resultsets} ||= {};
1974 return $self->{related_resultsets}{$rel} ||= do {
1975 my $rel_obj = $self->result_source->relationship_info($rel);
1977 $self->throw_exception(
1978 "search_related: result source '" . $self->result_source->source_name .
1979 "' has no such relationship $rel")
1982 my ($from,$seen) = $self->_resolve_from($rel);
1984 my $join_count = $seen->{$rel};
1985 my $alias = ($join_count > 1 ? join('_', $rel, $join_count) : $rel);
1987 #XXX - temp fix for result_class bug. There likely is a more elegant fix -groditi
1988 my %attrs = %{$self->{attrs}||{}};
1989 delete @attrs{qw(result_class alias)};
1993 if (my $cache = $self->get_cache) {
1994 if ($cache->[0] && $cache->[0]->related_resultset($rel)->get_cache) {
1995 $new_cache = [ map { @{$_->related_resultset($rel)->get_cache} }
2000 my $rel_source = $self->result_source->related_source($rel);
2004 # The reason we do this now instead of passing the alias to the
2005 # search_rs below is that if you wrap/overload resultset on the
2006 # source you need to know what alias it's -going- to have for things
2007 # to work sanely (e.g. RestrictWithObject wants to be able to add
2008 # extra query restrictions, and these may need to be $alias.)
2010 my $attrs = $rel_source->resultset_attributes;
2011 local $attrs->{alias} = $alias;
2013 $rel_source->resultset
2021 where => $self->{cond},
2026 $new->set_cache($new_cache) if $new_cache;
2032 my ($self, $extra_join) = @_;
2033 my $source = $self->result_source;
2034 my $attrs = $self->{attrs};
2036 my $from = $attrs->{from}
2037 || [ { $attrs->{alias} => $source->from } ];
2039 my $seen = { %{$attrs->{seen_join}||{}} };
2041 my $join = ($attrs->{join}
2042 ? [ $attrs->{join}, $extra_join ]
2045 # we need to take the prefetch the attrs into account before we
2046 # ->resolve_join as otherwise they get lost - captainL
2047 my $merged = $self->_merge_attr( $join, $attrs->{prefetch} );
2051 ($join ? $source->resolve_join($merged, $attrs->{alias}, $seen) : ()),
2054 return ($from,$seen);
2057 sub _resolved_attrs {
2059 return $self->{_attrs} if $self->{_attrs};
2061 my $attrs = { %{$self->{attrs}||{}} };
2062 my $source = $self->result_source;
2063 my $alias = $attrs->{alias};
2065 $attrs->{columns} ||= delete $attrs->{cols} if exists $attrs->{cols};
2066 if ($attrs->{columns}) {
2067 delete $attrs->{as};
2068 } elsif (!$attrs->{select}) {
2069 $attrs->{columns} = [ $source->columns ];
2074 ? (ref $attrs->{select} eq 'ARRAY'
2075 ? [ @{$attrs->{select}} ]
2076 : [ $attrs->{select} ])
2077 : [ map { m/\./ ? $_ : "${alias}.$_" } @{delete $attrs->{columns}} ]
2081 ? (ref $attrs->{as} eq 'ARRAY'
2082 ? [ @{$attrs->{as}} ]
2084 : [ map { m/^\Q${alias}.\E(.+)$/ ? $1 : $_ } @{$attrs->{select}} ]
2088 if ($adds = delete $attrs->{include_columns}) {
2089 $adds = [$adds] unless ref $adds eq 'ARRAY';
2090 push(@{$attrs->{select}}, @$adds);
2091 push(@{$attrs->{as}}, map { m/([^.]+)$/; $1 } @$adds);
2093 if ($adds = delete $attrs->{'+select'}) {
2094 $adds = [$adds] unless ref $adds eq 'ARRAY';
2095 push(@{$attrs->{select}},
2096 map { /\./ || ref $_ ? $_ : "${alias}.$_" } @$adds);
2098 if (my $adds = delete $attrs->{'+as'}) {
2099 $adds = [$adds] unless ref $adds eq 'ARRAY';
2100 push(@{$attrs->{as}}, @$adds);
2103 $attrs->{from} ||= [ { 'me' => $source->from } ];
2105 if (exists $attrs->{join} || exists $attrs->{prefetch}) {
2106 my $join = delete $attrs->{join} || {};
2108 if (defined $attrs->{prefetch}) {
2109 $join = $self->_merge_attr(
2110 $join, $attrs->{prefetch}
2115 $attrs->{from} = # have to copy here to avoid corrupting the original
2118 $source->resolve_join($join, $alias, { %{$attrs->{seen_join}||{}} })
2123 $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct};
2124 if ($attrs->{order_by}) {
2125 $attrs->{order_by} = (ref($attrs->{order_by}) eq 'ARRAY'
2126 ? [ @{$attrs->{order_by}} ]
2127 : [ $attrs->{order_by} ]);
2129 $attrs->{order_by} = [];
2132 my $collapse = $attrs->{collapse} || {};
2133 if (my $prefetch = delete $attrs->{prefetch}) {
2134 $prefetch = $self->_merge_attr({}, $prefetch);
2136 my $seen = $attrs->{seen_join} || {};
2137 foreach my $p (ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch)) {
2138 # bring joins back to level of current class
2139 my @prefetch = $source->resolve_prefetch(
2140 $p, $alias, $seen, \@pre_order, $collapse
2142 push(@{$attrs->{select}}, map { $_->[0] } @prefetch);
2143 push(@{$attrs->{as}}, map { $_->[1] } @prefetch);
2145 push(@{$attrs->{order_by}}, @pre_order);
2147 $attrs->{collapse} = $collapse;
2149 if ($attrs->{page}) {
2150 $attrs->{offset} ||= 0;
2151 $attrs->{offset} += ($attrs->{rows} * ($attrs->{page} - 1));
2154 return $self->{_attrs} = $attrs;
2158 my ($self, $attr) = @_;
2160 if (ref $attr eq 'HASH') {
2161 return $self->_rollout_hash($attr);
2162 } elsif (ref $attr eq 'ARRAY') {
2163 return $self->_rollout_array($attr);
2169 sub _rollout_array {
2170 my ($self, $attr) = @_;
2173 foreach my $element (@{$attr}) {
2174 if (ref $element eq 'HASH') {
2175 push( @rolled_array, @{ $self->_rollout_hash( $element ) } );
2176 } elsif (ref $element eq 'ARRAY') {
2177 # XXX - should probably recurse here
2178 push( @rolled_array, @{$self->_rollout_array($element)} );
2180 push( @rolled_array, $element );
2183 return \@rolled_array;
2187 my ($self, $attr) = @_;
2190 foreach my $key (keys %{$attr}) {
2191 push( @rolled_array, { $key => $attr->{$key} } );
2193 return \@rolled_array;
2196 sub _calculate_score {
2197 my ($self, $a, $b) = @_;
2199 if (ref $b eq 'HASH') {
2200 my ($b_key) = keys %{$b};
2201 if (ref $a eq 'HASH') {
2202 my ($a_key) = keys %{$a};
2203 if ($a_key eq $b_key) {
2204 return (1 + $self->_calculate_score( $a->{$a_key}, $b->{$b_key} ));
2209 return ($a eq $b_key) ? 1 : 0;
2212 if (ref $a eq 'HASH') {
2213 my ($a_key) = keys %{$a};
2214 return ($b eq $a_key) ? 1 : 0;
2216 return ($b eq $a) ? 1 : 0;
2222 my ($self, $orig, $import) = @_;
2224 return $import unless defined($orig);
2225 return $orig unless defined($import);
2227 $orig = $self->_rollout_attr($orig);
2228 $import = $self->_rollout_attr($import);
2231 foreach my $import_element ( @{$import} ) {
2232 # find best candidate from $orig to merge $b_element into
2233 my $best_candidate = { position => undef, score => 0 }; my $position = 0;
2234 foreach my $orig_element ( @{$orig} ) {
2235 my $score = $self->_calculate_score( $orig_element, $import_element );
2236 if ($score > $best_candidate->{score}) {
2237 $best_candidate->{position} = $position;
2238 $best_candidate->{score} = $score;
2242 my ($import_key) = ( ref $import_element eq 'HASH' ) ? keys %{$import_element} : ($import_element);
2244 if ($best_candidate->{score} == 0 || exists $seen_keys->{$import_key}) {
2245 push( @{$orig}, $import_element );
2247 my $orig_best = $orig->[$best_candidate->{position}];
2248 # merge orig_best and b_element together and replace original with merged
2249 if (ref $orig_best ne 'HASH') {
2250 $orig->[$best_candidate->{position}] = $import_element;
2251 } elsif (ref $import_element eq 'HASH') {
2252 my ($key) = keys %{$orig_best};
2253 $orig->[$best_candidate->{position}] = { $key => $self->_merge_attr($orig_best->{$key}, $import_element->{$key}) };
2256 $seen_keys->{$import_key} = 1; # don't merge the same key twice
2266 $self->_source_handle($_[0]->handle);
2268 $self->_source_handle->resolve;
2272 =head2 throw_exception
2274 See L<DBIx::Class::Schema/throw_exception> for details.
2278 sub throw_exception {
2280 if (ref $self && $self->_source_handle->schema) {
2281 $self->_source_handle->schema->throw_exception(@_)
2288 # XXX: FIXME: Attributes docs need clearing up
2292 The resultset takes various attributes that modify its behavior. Here's an
2299 =item Value: ($order_by | \@order_by)
2303 Which column(s) to order the results by. This is currently passed
2304 through directly to SQL, so you can give e.g. C<year DESC> for a
2305 descending order on the column `year'.
2307 Please note that if you have C<quote_char> enabled (see
2308 L<DBIx::Class::Storage::DBI/connect_info>) you will need to do C<\'year DESC' > to
2309 specify an order. (The scalar ref causes it to be passed as raw sql to the DB,
2310 so you will need to manually quote things as appropriate.)
2316 =item Value: \@columns
2320 Shortcut to request a particular set of columns to be retrieved. Adds
2321 C<me.> onto the start of any column without a C<.> in it and sets C<select>
2322 from that, then auto-populates C<as> from C<select> as normal. (You may also
2323 use the C<cols> attribute, as in earlier versions of DBIC.)
2325 =head2 include_columns
2329 =item Value: \@columns
2333 Shortcut to include additional columns in the returned results - for example
2335 $schema->resultset('CD')->search(undef, {
2336 include_columns => ['artist.name'],
2340 would return all CDs and include a 'name' column to the information
2341 passed to object inflation. Note that the 'artist' is the name of the
2342 column (or relationship) accessor, and 'name' is the name of the column
2343 accessor in the related table.
2349 =item Value: \@select_columns
2353 Indicates which columns should be selected from the storage. You can use
2354 column names, or in the case of RDBMS back ends, function or stored procedure
2357 $rs = $schema->resultset('Employee')->search(undef, {
2360 { count => 'employeeid' },
2365 When you use function/stored procedure names and do not supply an C<as>
2366 attribute, the column names returned are storage-dependent. E.g. MySQL would
2367 return a column named C<count(employeeid)> in the above example.
2373 Indicates additional columns to be selected from storage. Works the same as
2374 L</select> but adds columns to the selection.
2382 Indicates additional column names for those added via L</+select>.
2390 =item Value: \@inflation_names
2394 Indicates column names for object inflation. That is, C<as>
2395 indicates the name that the column can be accessed as via the
2396 C<get_column> method (or via the object accessor, B<if one already
2397 exists>). It has nothing to do with the SQL code C<SELECT foo AS bar>.
2399 The C<as> attribute is used in conjunction with C<select>,
2400 usually when C<select> contains one or more function or stored
2403 $rs = $schema->resultset('Employee')->search(undef, {
2406 { count => 'employeeid' }
2408 as => ['name', 'employee_count'],
2411 my $employee = $rs->first(); # get the first Employee
2413 If the object against which the search is performed already has an accessor
2414 matching a column name specified in C<as>, the value can be retrieved using
2415 the accessor as normal:
2417 my $name = $employee->name();
2419 If on the other hand an accessor does not exist in the object, you need to
2420 use C<get_column> instead:
2422 my $employee_count = $employee->get_column('employee_count');
2424 You can create your own accessors if required - see
2425 L<DBIx::Class::Manual::Cookbook> for details.
2427 Please note: This will NOT insert an C<AS employee_count> into the SQL
2428 statement produced, it is used for internal access only. Thus
2429 attempting to use the accessor in an C<order_by> clause or similar
2430 will fail miserably.
2432 To get around this limitation, you can supply literal SQL to your
2433 C<select> attibute that contains the C<AS alias> text, eg:
2435 select => [\'myfield AS alias']
2441 =item Value: ($rel_name | \@rel_names | \%rel_names)
2445 Contains a list of relationships that should be joined for this query. For
2448 # Get CDs by Nine Inch Nails
2449 my $rs = $schema->resultset('CD')->search(
2450 { 'artist.name' => 'Nine Inch Nails' },
2451 { join => 'artist' }
2454 Can also contain a hash reference to refer to the other relation's relations.
2457 package MyApp::Schema::Track;
2458 use base qw/DBIx::Class/;
2459 __PACKAGE__->table('track');
2460 __PACKAGE__->add_columns(qw/trackid cd position title/);
2461 __PACKAGE__->set_primary_key('trackid');
2462 __PACKAGE__->belongs_to(cd => 'MyApp::Schema::CD');
2465 # In your application
2466 my $rs = $schema->resultset('Artist')->search(
2467 { 'track.title' => 'Teardrop' },
2469 join => { cd => 'track' },
2470 order_by => 'artist.name',
2474 You need to use the relationship (not the table) name in conditions,
2475 because they are aliased as such. The current table is aliased as "me", so
2476 you need to use me.column_name in order to avoid ambiguity. For example:
2478 # Get CDs from 1984 with a 'Foo' track
2479 my $rs = $schema->resultset('CD')->search(
2482 'tracks.name' => 'Foo'
2484 { join => 'tracks' }
2487 If the same join is supplied twice, it will be aliased to <rel>_2 (and
2488 similarly for a third time). For e.g.
2490 my $rs = $schema->resultset('Artist')->search({
2491 'cds.title' => 'Down to Earth',
2492 'cds_2.title' => 'Popular',
2494 join => [ qw/cds cds/ ],
2497 will return a set of all artists that have both a cd with title 'Down
2498 to Earth' and a cd with title 'Popular'.
2500 If you want to fetch related objects from other tables as well, see C<prefetch>
2503 For more help on using joins with search, see L<DBIx::Class::Manual::Joining>.
2509 =item Value: ($rel_name | \@rel_names | \%rel_names)
2513 Contains one or more relationships that should be fetched along with
2514 the main query (when they are accessed afterwards the data will
2515 already be available, without extra queries to the database). This is
2516 useful for when you know you will need the related objects, because it
2517 saves at least one query:
2519 my $rs = $schema->resultset('Tag')->search(
2528 The initial search results in SQL like the following:
2530 SELECT tag.*, cd.*, artist.* FROM tag
2531 JOIN cd ON tag.cd = cd.cdid
2532 JOIN artist ON cd.artist = artist.artistid
2534 L<DBIx::Class> has no need to go back to the database when we access the
2535 C<cd> or C<artist> relationships, which saves us two SQL statements in this
2538 Simple prefetches will be joined automatically, so there is no need
2539 for a C<join> attribute in the above search.
2541 C<prefetch> can be used with the following relationship types: C<belongs_to>,
2542 C<has_one> (or if you're using C<add_relationship>, any relationship declared
2543 with an accessor type of 'single' or 'filter'). A more complex example that
2544 prefetches an artists cds, the tracks on those cds, and the tags associted
2545 with that artist is given below (assuming many-to-many from artists to tags):
2547 my $rs = $schema->resultset('Artist')->search(
2551 { cds => 'tracks' },
2552 { artist_tags => 'tags' }
2558 B<NOTE:> If you specify a C<prefetch> attribute, the C<join> and C<select>
2559 attributes will be ignored.
2569 Makes the resultset paged and specifies the page to retrieve. Effectively
2570 identical to creating a non-pages resultset and then calling ->page($page)
2573 If L<rows> attribute is not specified it defualts to 10 rows per page.
2583 Specifes the maximum number of rows for direct retrieval or the number of
2584 rows per page if the page attribute or method is used.
2590 =item Value: $offset
2594 Specifies the (zero-based) row number for the first row to be returned, or the
2595 of the first row of the first page if paging is used.
2601 =item Value: \@columns
2605 A arrayref of columns to group by. Can include columns of joined tables.
2607 group_by => [qw/ column1 column2 ... /]
2613 =item Value: $condition
2617 HAVING is a select statement attribute that is applied between GROUP BY and
2618 ORDER BY. It is applied to the after the grouping calculations have been
2621 having => { 'count(employee)' => { '>=', 100 } }
2627 =item Value: (0 | 1)
2631 Set to 1 to group by all columns.
2637 Adds to the WHERE clause.
2639 # only return rows WHERE deleted IS NULL for all searches
2640 __PACKAGE__->resultset_attributes({ where => { deleted => undef } }); )
2642 Can be overridden by passing C<{ where => undef }> as an attribute
2649 Set to 1 to cache search results. This prevents extra SQL queries if you
2650 revisit rows in your ResultSet:
2652 my $resultset = $schema->resultset('Artist')->search( undef, { cache => 1 } );
2654 while( my $artist = $resultset->next ) {
2658 $rs->first; # without cache, this would issue a query
2660 By default, searches are not cached.
2662 For more examples of using these attributes, see
2663 L<DBIx::Class::Manual::Cookbook>.
2669 =item Value: \@from_clause
2673 The C<from> attribute gives you manual control over the C<FROM> clause of SQL
2674 statements generated by L<DBIx::Class>, allowing you to express custom C<JOIN>
2677 NOTE: Use this on your own risk. This allows you to shoot off your foot!
2679 C<join> will usually do what you need and it is strongly recommended that you
2680 avoid using C<from> unless you cannot achieve the desired result using C<join>.
2681 And we really do mean "cannot", not just tried and failed. Attempting to use
2682 this because you're having problems with C<join> is like trying to use x86
2683 ASM because you've got a syntax error in your C. Trust us on this.
2685 Now, if you're still really, really sure you need to use this (and if you're
2686 not 100% sure, ask the mailing list first), here's an explanation of how this
2689 The syntax is as follows -
2692 { <alias1> => <table1> },
2694 { <alias2> => <table2>, -join_type => 'inner|left|right' },
2695 [], # nested JOIN (optional)
2696 { <table1.column1> => <table2.column2>, ... (more conditions) },
2698 # More of the above [ ] may follow for additional joins
2705 ON <table1.column1> = <table2.column2>
2706 <more joins may follow>
2708 An easy way to follow the examples below is to remember the following:
2710 Anything inside "[]" is a JOIN
2711 Anything inside "{}" is a condition for the enclosing JOIN
2713 The following examples utilize a "person" table in a family tree application.
2714 In order to express parent->child relationships, this table is self-joined:
2716 # Person->belongs_to('father' => 'Person');
2717 # Person->belongs_to('mother' => 'Person');
2719 C<from> can be used to nest joins. Here we return all children with a father,
2720 then search against all mothers of those children:
2722 $rs = $schema->resultset('Person')->search(
2725 alias => 'mother', # alias columns in accordance with "from"
2727 { mother => 'person' },
2730 { child => 'person' },
2732 { father => 'person' },
2733 { 'father.person_id' => 'child.father_id' }
2736 { 'mother.person_id' => 'child.mother_id' }
2743 # SELECT mother.* FROM person mother
2746 # JOIN person father
2747 # ON ( father.person_id = child.father_id )
2749 # ON ( mother.person_id = child.mother_id )
2751 The type of any join can be controlled manually. To search against only people
2752 with a father in the person table, we could explicitly use C<INNER JOIN>:
2754 $rs = $schema->resultset('Person')->search(
2757 alias => 'child', # alias columns in accordance with "from"
2759 { child => 'person' },
2761 { father => 'person', -join_type => 'inner' },
2762 { 'father.id' => 'child.father_id' }
2769 # SELECT child.* FROM person child
2770 # INNER JOIN person father ON child.father_id = father.id
2776 =item Value: ( 'update' | 'shared' )
2780 Set to 'update' for a SELECT ... FOR UPDATE or 'shared' for a SELECT