1 package DBIx::Class::ResultSet;
9 use Carp::Clan qw/^DBIx::Class/;
12 use DBIx::Class::ResultSetColumn;
13 use DBIx::Class::ResultSourceHandle;
15 use base qw/DBIx::Class/;
17 __PACKAGE__->mk_group_accessors('simple' => qw/result_class _source_handle/);
21 DBIx::Class::ResultSet - Responsible for fetching and creating resultset.
25 my $rs = $schema->resultset('User')->search({ registered => 1 });
26 my @rows = $schema->resultset('CD')->search({ year => 2005 })->all();
30 The resultset is also known as an iterator. It is responsible for handling
31 queries that may return an arbitrary number of rows, e.g. via L</search>
32 or a C<has_many> relationship.
34 In the examples below, the following table classes are used:
36 package MyApp::Schema::Artist;
37 use base qw/DBIx::Class/;
38 __PACKAGE__->load_components(qw/Core/);
39 __PACKAGE__->table('artist');
40 __PACKAGE__->add_columns(qw/artistid name/);
41 __PACKAGE__->set_primary_key('artistid');
42 __PACKAGE__->has_many(cds => 'MyApp::Schema::CD');
45 package MyApp::Schema::CD;
46 use base qw/DBIx::Class/;
47 __PACKAGE__->load_components(qw/Core/);
48 __PACKAGE__->table('cd');
49 __PACKAGE__->add_columns(qw/cdid artist title year/);
50 __PACKAGE__->set_primary_key('cdid');
51 __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Artist');
56 If a resultset is used in a numeric context it returns the L</count>.
57 However, if it is used in a booleand context it is always true. So if
58 you want to check if a resultset has any results use C<if $rs != 0>.
59 C<if $rs> will always be true.
67 =item Arguments: $source, \%$attrs
69 =item Return Value: $rs
73 The resultset constructor. Takes a source object (usually a
74 L<DBIx::Class::ResultSourceProxy::Table>) and an attribute hash (see
75 L</ATTRIBUTES> below). Does not perform any queries -- these are
76 executed as needed by the other methods.
78 Generally you won't need to construct a resultset manually. You'll
79 automatically get one from e.g. a L</search> called in scalar context:
81 my $rs = $schema->resultset('CD')->search({ title => '100th Window' });
83 IMPORTANT: If called on an object, proxies to new_result instead so
85 my $cd = $schema->resultset('CD')->new({ title => 'Spoon' });
87 will return a CD object, not a ResultSet.
93 return $class->new_result(@_) if ref $class;
95 my ($source, $attrs) = @_;
96 $source = $source->handle
97 unless $source->isa('DBIx::Class::ResultSourceHandle');
98 $attrs = { %{$attrs||{}} };
100 if ($attrs->{page}) {
101 $attrs->{rows} ||= 10;
104 $attrs->{alias} ||= 'me';
106 # Creation of {} and bless separated to mitigate RH perl bug
107 # see https://bugzilla.redhat.com/show_bug.cgi?id=196836
109 _source_handle => $source,
110 result_class => $attrs->{result_class} || $source->resolve->result_class,
111 cond => $attrs->{where},
126 =item Arguments: $cond, \%attrs?
128 =item Return Value: $resultset (scalar context), @row_objs (list context)
132 my @cds = $cd_rs->search({ year => 2001 }); # "... WHERE year = 2001"
133 my $new_rs = $cd_rs->search({ year => 2005 });
135 my $new_rs = $cd_rs->search([ { year => 2005 }, { year => 2004 } ]);
136 # year = 2005 OR year = 2004
138 If you need to pass in additional attributes but no additional condition,
139 call it as C<search(undef, \%attrs)>.
141 # "SELECT name, artistid FROM $artist_table"
142 my @all_artists = $schema->resultset('Artist')->search(undef, {
143 columns => [qw/name artistid/],
146 For a list of attributes that can be passed to C<search>, see
147 L</ATTRIBUTES>. For more examples of using this function, see
148 L<Searching|DBIx::Class::Manual::Cookbook/Searching>. For a complete
149 documentation for the first argument, see L<SQL::Abstract>.
151 For more help on using joins with search, see L<DBIx::Class::Manual::Joining>.
157 my $rs = $self->search_rs( @_ );
158 return (wantarray ? $rs->all : $rs);
165 =item Arguments: $cond, \%attrs?
167 =item Return Value: $resultset
171 This method does the same exact thing as search() except it will
172 always return a resultset, even in list context.
180 $attrs = pop(@_) if @_ > 1 and ref $_[$#_] eq 'HASH';
181 my $our_attrs = { %{$self->{attrs}} };
182 my $having = delete $our_attrs->{having};
183 my $where = delete $our_attrs->{where};
187 my %safe = (alias => 1, cache => 1);
190 (@_ && defined($_[0])) # @_ == () or (undef)
192 (keys %$attrs # empty attrs or only 'safe' attrs
193 && List::Util::first { !$safe{$_} } keys %$attrs)
195 # no search, effectively just a clone
196 $rows = $self->get_cache;
199 my $new_attrs = { %{$our_attrs}, %{$attrs} };
201 # merge new attrs into inherited
202 foreach my $key (qw/join prefetch/) {
203 next unless exists $attrs->{$key};
204 $new_attrs->{$key} = $self->_merge_attr($our_attrs->{$key}, $attrs->{$key});
209 (@_ == 1 || ref $_[0] eq "HASH")
211 (ref $_[0] eq 'HASH')
213 (keys %{ $_[0] } > 0)
221 ? $self->throw_exception("Odd number of arguments to search")
228 if (defined $where) {
229 $new_attrs->{where} = (
230 defined $new_attrs->{where}
233 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
234 } $where, $new_attrs->{where}
241 $new_attrs->{where} = (
242 defined $new_attrs->{where}
245 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
246 } $cond, $new_attrs->{where}
252 if (defined $having) {
253 $new_attrs->{having} = (
254 defined $new_attrs->{having}
257 ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_
258 } $having, $new_attrs->{having}
264 my $rs = (ref $self)->new($self->result_source, $new_attrs);
266 $rs->set_cache($rows);
271 =head2 search_literal
275 =item Arguments: $sql_fragment, @bind_values
277 =item Return Value: $resultset (scalar context), @row_objs (list context)
281 my @cds = $cd_rs->search_literal('year = ? AND title = ?', qw/2001 Reload/);
282 my $newrs = $artist_rs->search_literal('name = ?', 'Metallica');
284 Pass a literal chunk of SQL to be added to the conditional part of the
287 CAVEAT: C<search_literal> is provided for Class::DBI compatibility and should
288 only be used in that context. There are known problems using C<search_literal>
289 in chained queries; it can result in bind values in the wrong order. See
290 L<DBIx::Class::Manual::Cookbook/Searching> and
291 L<DBIx::Class::Manual::FAQ/Searching> for searching techniques that do not
292 require C<search_literal>.
297 my ($self, $cond, @vals) = @_;
298 my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
299 $attrs->{bind} = [ @{$self->{attrs}{bind}||[]}, @vals ];
300 return $self->search(\$cond, $attrs);
307 =item Arguments: @values | \%cols, \%attrs?
309 =item Return Value: $row_object
313 Finds a row based on its primary key or unique constraint. For example, to find
314 a row by its primary key:
316 my $cd = $schema->resultset('CD')->find(5);
318 You can also find a row by a specific unique constraint using the C<key>
319 attribute. For example:
321 my $cd = $schema->resultset('CD')->find('Massive Attack', 'Mezzanine', {
322 key => 'cd_artist_title'
325 Additionally, you can specify the columns explicitly by name:
327 my $cd = $schema->resultset('CD')->find(
329 artist => 'Massive Attack',
330 title => 'Mezzanine',
332 { key => 'cd_artist_title' }
335 If the C<key> is specified as C<primary>, it searches only on the primary key.
337 If no C<key> is specified, it searches on all unique constraints defined on the
338 source for which column data is provided, including the primary key.
340 If your table does not have a primary key, you B<must> provide a value for the
341 C<key> attribute matching one of the unique constraints on the source.
343 Note: If your query does not return only one row, a warning is generated:
345 Query returned more than one row
347 See also L</find_or_create> and L</update_or_create>. For information on how to
348 declare unique constraints, see
349 L<DBIx::Class::ResultSource/add_unique_constraint>.
355 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
357 # Default to the primary key, but allow a specific key
358 my @cols = exists $attrs->{key}
359 ? $self->result_source->unique_constraint_columns($attrs->{key})
360 : $self->result_source->primary_columns;
361 $self->throw_exception(
362 "Can't find unless a primary key is defined or unique constraint is specified"
365 # Parse out a hashref from input
367 if (ref $_[0] eq 'HASH') {
368 $input_query = { %{$_[0]} };
370 elsif (@_ == @cols) {
372 @{$input_query}{@cols} = @_;
375 # Compatibility: Allow e.g. find(id => $value)
376 carp "Find by key => value deprecated; please use a hashref instead";
380 my (%related, $info);
382 KEY: foreach my $key (keys %$input_query) {
383 if (ref($input_query->{$key})
384 && ($info = $self->result_source->relationship_info($key))) {
385 my $val = delete $input_query->{$key};
386 next KEY if (ref($val) eq 'ARRAY'); # has_many for multi_create
387 my $rel_q = $self->result_source->resolve_condition(
388 $info->{cond}, $val, $key
390 die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY';
391 @related{keys %$rel_q} = values %$rel_q;
394 if (my @keys = keys %related) {
395 @{$input_query}{@keys} = values %related;
399 # Build the final query: Default to the disjunction of the unique queries,
400 # but allow the input query in case the ResultSet defines the query or the
401 # user is abusing find
402 my $alias = exists $attrs->{alias} ? $attrs->{alias} : $self->{attrs}{alias};
404 if (exists $attrs->{key}) {
405 my @unique_cols = $self->result_source->unique_constraint_columns($attrs->{key});
406 my $unique_query = $self->_build_unique_query($input_query, \@unique_cols);
407 $query = $self->_add_alias($unique_query, $alias);
410 my @unique_queries = $self->_unique_queries($input_query, $attrs);
411 $query = @unique_queries
412 ? [ map { $self->_add_alias($_, $alias) } @unique_queries ]
413 : $self->_add_alias($input_query, $alias);
418 my $rs = $self->search($query, $attrs);
419 if (keys %{$rs->_resolved_attrs->{collapse}}) {
421 carp "Query returned more than one row" if $rs->next;
429 if (keys %{$self->_resolved_attrs->{collapse}}) {
430 my $rs = $self->search($query);
432 carp "Query returned more than one row" if $rs->next;
436 return $self->single($query);
443 # Add the specified alias to the specified query hash. A copy is made so the
444 # original query is not modified.
447 my ($self, $query, $alias) = @_;
449 my %aliased = %$query;
450 foreach my $col (grep { ! m/\./ } keys %aliased) {
451 $aliased{"$alias.$col"} = delete $aliased{$col};
459 # Build a list of queries which satisfy unique constraints.
461 sub _unique_queries {
462 my ($self, $query, $attrs) = @_;
464 my @constraint_names = exists $attrs->{key}
466 : $self->result_source->unique_constraint_names;
468 my $where = $self->_collapse_cond($self->{attrs}{where} || {});
469 my $num_where = scalar keys %$where;
472 foreach my $name (@constraint_names) {
473 my @unique_cols = $self->result_source->unique_constraint_columns($name);
474 my $unique_query = $self->_build_unique_query($query, \@unique_cols);
476 my $num_cols = scalar @unique_cols;
477 my $num_query = scalar keys %$unique_query;
479 my $total = $num_query + $num_where;
480 if ($num_query && ($num_query == $num_cols || $total == $num_cols)) {
481 # The query is either unique on its own or is unique in combination with
482 # the existing where clause
483 push @unique_queries, $unique_query;
487 return @unique_queries;
490 # _build_unique_query
492 # Constrain the specified query hash based on the specified column names.
494 sub _build_unique_query {
495 my ($self, $query, $unique_cols) = @_;
498 map { $_ => $query->{$_} }
499 grep { exists $query->{$_} }
504 =head2 search_related
508 =item Arguments: $rel, $cond, \%attrs?
510 =item Return Value: $new_resultset
514 $new_rs = $cd_rs->search_related('artist', {
518 Searches the specified relationship, optionally specifying a condition and
519 attributes for matching records. See L</ATTRIBUTES> for more information.
524 return shift->related_resultset(shift)->search(@_);
527 =head2 search_related_rs
529 This method works exactly the same as search_related, except that
530 it guarantees a restultset, even in list context.
534 sub search_related_rs {
535 return shift->related_resultset(shift)->search_rs(@_);
542 =item Arguments: none
544 =item Return Value: $cursor
548 Returns a storage-driven cursor to the given resultset. See
549 L<DBIx::Class::Cursor> for more information.
556 my $attrs = { %{$self->_resolved_attrs} };
557 return $self->{cursor}
558 ||= $self->result_source->storage->select($attrs->{from}, $attrs->{select},
559 $attrs->{where},$attrs);
566 =item Arguments: $cond?
568 =item Return Value: $row_object?
572 my $cd = $schema->resultset('CD')->single({ year => 2001 });
574 Inflates the first result without creating a cursor if the resultset has
575 any records in it; if not returns nothing. Used by L</find> as an optimisation.
577 Can optionally take an additional condition B<only> - this is a fast-code-path
578 method; if you need to add extra joins or similar call L</search> and then
579 L</single> without a condition on the L<DBIx::Class::ResultSet> returned from
582 B<Note>: As of 0.08100, this method assumes that the query returns only one
583 row. If more than one row is returned, you will receive a warning:
585 Query returned more than one row
587 In this case, you should be using L</first> or L</find> instead.
592 my ($self, $where) = @_;
593 my $attrs = { %{$self->_resolved_attrs} };
595 if (defined $attrs->{where}) {
598 [ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
599 $where, delete $attrs->{where} ]
602 $attrs->{where} = $where;
606 # XXX: Disabled since it doesn't infer uniqueness in all cases
607 # unless ($self->_is_unique_query($attrs->{where})) {
608 # carp "Query not guaranteed to return a single row"
609 # . "; please declare your unique constraints or use search instead";
612 my @data = $self->result_source->storage->select_single(
613 $attrs->{from}, $attrs->{select},
614 $attrs->{where}, $attrs
617 return (@data ? ($self->_construct_object(@data))[0] : undef);
622 # Try to determine if the specified query is guaranteed to be unique, based on
623 # the declared unique constraints.
625 sub _is_unique_query {
626 my ($self, $query) = @_;
628 my $collapsed = $self->_collapse_query($query);
629 my $alias = $self->{attrs}{alias};
631 foreach my $name ($self->result_source->unique_constraint_names) {
632 my @unique_cols = map {
634 } $self->result_source->unique_constraint_columns($name);
636 # Count the values for each unique column
637 my %seen = map { $_ => 0 } @unique_cols;
639 foreach my $key (keys %$collapsed) {
640 my $aliased = $key =~ /\./ ? $key : "$alias.$key";
641 next unless exists $seen{$aliased}; # Additional constraints are okay
642 $seen{$aliased} = scalar keys %{ $collapsed->{$key} };
645 # If we get 0 or more than 1 value for a column, it's not necessarily unique
646 return 1 unless grep { $_ != 1 } values %seen;
654 # Recursively collapse the query, accumulating values for each column.
656 sub _collapse_query {
657 my ($self, $query, $collapsed) = @_;
661 if (ref $query eq 'ARRAY') {
662 foreach my $subquery (@$query) {
663 next unless ref $subquery; # -or
664 # warn "ARRAY: " . Dumper $subquery;
665 $collapsed = $self->_collapse_query($subquery, $collapsed);
668 elsif (ref $query eq 'HASH') {
669 if (keys %$query and (keys %$query)[0] eq '-and') {
670 foreach my $subquery (@{$query->{-and}}) {
671 # warn "HASH: " . Dumper $subquery;
672 $collapsed = $self->_collapse_query($subquery, $collapsed);
676 # warn "LEAF: " . Dumper $query;
677 foreach my $col (keys %$query) {
678 my $value = $query->{$col};
679 $collapsed->{$col}{$value}++;
691 =item Arguments: $cond?
693 =item Return Value: $resultsetcolumn
697 my $max_length = $rs->get_column('length')->max;
699 Returns a L<DBIx::Class::ResultSetColumn> instance for a column of the ResultSet.
704 my ($self, $column) = @_;
705 my $new = DBIx::Class::ResultSetColumn->new($self, $column);
713 =item Arguments: $cond, \%attrs?
715 =item Return Value: $resultset (scalar context), @row_objs (list context)
719 # WHERE title LIKE '%blue%'
720 $cd_rs = $rs->search_like({ title => '%blue%'});
722 Performs a search, but uses C<LIKE> instead of C<=> as the condition. Note
723 that this is simply a convenience method. You most likely want to use
724 L</search> with specific operators.
726 For more information, see L<DBIx::Class::Manual::Cookbook>.
732 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
733 my $query = ref $_[0] eq 'HASH' ? { %{shift()} }: {@_};
734 $query->{$_} = { 'like' => $query->{$_} } for keys %$query;
735 return $class->search($query, { %$attrs });
742 =item Arguments: $first, $last
744 =item Return Value: $resultset (scalar context), @row_objs (list context)
748 Returns a resultset or object list representing a subset of elements from the
749 resultset slice is called on. Indexes are from 0, i.e., to get the first
752 my ($one, $two, $three) = $rs->slice(0, 2);
757 my ($self, $min, $max) = @_;
758 my $attrs = {}; # = { %{ $self->{attrs} || {} } };
759 $attrs->{offset} = $self->{attrs}{offset} || 0;
760 $attrs->{offset} += $min;
761 $attrs->{rows} = ($max ? ($max - $min + 1) : 1);
762 return $self->search(undef(), $attrs);
763 #my $slice = (ref $self)->new($self->result_source, $attrs);
764 #return (wantarray ? $slice->all : $slice);
771 =item Arguments: none
773 =item Return Value: $result?
777 Returns the next element in the resultset (C<undef> is there is none).
779 Can be used to efficiently iterate over records in the resultset:
781 my $rs = $schema->resultset('CD')->search;
782 while (my $cd = $rs->next) {
786 Note that you need to store the resultset object, and call C<next> on it.
787 Calling C<< resultset('Table')->next >> repeatedly will always return the
788 first record from the resultset.
794 if (my $cache = $self->get_cache) {
795 $self->{all_cache_position} ||= 0;
796 return $cache->[$self->{all_cache_position}++];
798 if ($self->{attrs}{cache}) {
799 $self->{all_cache_position} = 1;
800 return ($self->all)[0];
802 if ($self->{stashed_objects}) {
803 my $obj = shift(@{$self->{stashed_objects}});
804 delete $self->{stashed_objects} unless @{$self->{stashed_objects}};
808 exists $self->{stashed_row}
809 ? @{delete $self->{stashed_row}}
810 : $self->cursor->next
812 return undef unless (@row);
813 my ($row, @more) = $self->_construct_object(@row);
814 $self->{stashed_objects} = \@more if @more;
818 sub _construct_object {
819 my ($self, @row) = @_;
820 my $info = $self->_collapse_result($self->{_attrs}{as}, \@row);
821 my @new = $self->result_class->inflate_result($self->result_source, @$info);
822 @new = $self->{_attrs}{record_filter}->(@new)
823 if exists $self->{_attrs}{record_filter};
827 sub _collapse_result {
828 my ($self, $as_proto, $row) = @_;
832 # 'foo' => [ undef, 'foo' ]
833 # 'foo.bar' => [ 'foo', 'bar' ]
834 # 'foo.bar.baz' => [ 'foo.bar', 'baz' ]
836 my @construct_as = map { [ (/^(?:(.*)\.)?([^.]+)$/) ] } @$as_proto;
838 my %collapse = %{$self->{_attrs}{collapse}||{}};
842 # if we're doing collapsing (has_many prefetch) we need to grab records
843 # until the PK changes, so fill @pri_index. if not, we leave it empty so
844 # we know we don't have to bother.
846 # the reason for not using the collapse stuff directly is because if you
847 # had for e.g. two artists in a row with no cds, the collapse info for
848 # both would be NULL (undef) so you'd lose the second artist
850 # store just the index so we can check the array positions from the row
851 # without having to contruct the full hash
853 if (keys %collapse) {
854 my %pri = map { ($_ => 1) } $self->result_source->primary_columns;
855 foreach my $i (0 .. $#construct_as) {
856 next if defined($construct_as[$i][0]); # only self table
857 if (delete $pri{$construct_as[$i][1]}) {
858 push(@pri_index, $i);
860 last unless keys %pri; # short circuit (Johnny Five Is Alive!)
864 # no need to do an if, it'll be empty if @pri_index is empty anyway
866 my %pri_vals = map { ($_ => $copy[$_]) } @pri_index;
870 do { # no need to check anything at the front, we always want the first row
874 foreach my $this_as (@construct_as) {
875 $const{$this_as->[0]||''}{$this_as->[1]} = shift(@copy);
878 push(@const_rows, \%const);
880 } until ( # no pri_index => no collapse => drop straight out
883 do { # get another row, stash it, drop out if different PK
885 @copy = $self->cursor->next;
886 $self->{stashed_row} = \@copy;
888 # last thing in do block, counts as true if anything doesn't match
890 # check xor defined first for NULL vs. NOT NULL then if one is
891 # defined the other must be so check string equality
894 (defined $pri_vals{$_} ^ defined $copy[$_])
895 || (defined $pri_vals{$_} && ($pri_vals{$_} ne $copy[$_]))
900 my $alias = $self->{attrs}{alias};
907 foreach my $const (@const_rows) {
908 scalar @const_keys or do {
909 @const_keys = sort { length($a) <=> length($b) } keys %$const;
911 foreach my $key (@const_keys) {
914 my @parts = split(/\./, $key);
916 my $data = $const->{$key};
917 foreach my $p (@parts) {
918 $target = $target->[1]->{$p} ||= [];
920 if ($cur eq ".${key}" && (my @ckey = @{$collapse{$cur}||[]})) {
921 # collapsing at this point and on final part
922 my $pos = $collapse_pos{$cur};
923 CK: foreach my $ck (@ckey) {
924 if (!defined $pos->{$ck} || $pos->{$ck} ne $data->{$ck}) {
925 $collapse_pos{$cur} = $data;
926 delete @collapse_pos{ # clear all positioning for sub-entries
927 grep { m/^\Q${cur}.\E/ } keys %collapse_pos
934 if (exists $collapse{$cur}) {
935 $target = $target->[-1];
938 $target->[0] = $data;
940 $info->[0] = $const->{$key};
952 =item Arguments: $result_source?
954 =item Return Value: $result_source
958 An accessor for the primary ResultSource object from which this ResultSet
965 =item Arguments: $result_class?
967 =item Return Value: $result_class
971 An accessor for the class to use when creating row objects. Defaults to
972 C<< result_source->result_class >> - which in most cases is the name of the
973 L<"table"|DBIx::Class::Manual::Glossary/"ResultSource"> class.
982 =item Arguments: $cond, \%attrs??
984 =item Return Value: $count
988 Performs an SQL C<COUNT> with the same query as the resultset was built
989 with to find the number of elements. If passed arguments, does a search
990 on the resultset and counts the results of that.
992 Note: When using C<count> with C<group_by>, L<DBIx::Class> emulates C<GROUP BY>
993 using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
994 not support C<DISTINCT> with multiple columns. If you are using such a
995 database, you should only use columns from the main table in your C<group_by>
1002 return $self->search(@_)->count if @_ and defined $_[0];
1003 return scalar @{ $self->get_cache } if $self->get_cache;
1004 my $count = $self->_count;
1005 return 0 unless $count;
1007 # need to take offset from resolved attrs
1009 $count -= $self->{_attrs}{offset} if $self->{_attrs}{offset};
1010 $count = $self->{attrs}{rows} if
1011 $self->{attrs}{rows} and $self->{attrs}{rows} < $count;
1012 $count = 0 if ($count < 0);
1016 sub _count { # Separated out so pager can get the full count
1018 my $select = { count => '*' };
1020 my $attrs = { %{$self->_resolved_attrs} };
1021 if (my $group_by = delete $attrs->{group_by}) {
1022 delete $attrs->{having};
1023 my @distinct = (ref $group_by ? @$group_by : ($group_by));
1024 # todo: try CONCAT for multi-column pk
1025 my @pk = $self->result_source->primary_columns;
1027 my $alias = $attrs->{alias};
1028 foreach my $column (@distinct) {
1029 if ($column =~ qr/^(?:\Q${alias}.\E)?$pk[0]$/) {
1030 @distinct = ($column);
1036 $select = { count => { distinct => \@distinct } };
1039 $attrs->{select} = $select;
1040 $attrs->{as} = [qw/count/];
1042 # offset, order by and page are not needed to count. record_filter is cdbi
1043 delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
1045 my $tmp_rs = (ref $self)->new($self->result_source, $attrs);
1046 my ($count) = $tmp_rs->cursor->next;
1054 =head2 count_literal
1058 =item Arguments: $sql_fragment, @bind_values
1060 =item Return Value: $count
1064 Counts the results in a literal query. Equivalent to calling L</search_literal>
1065 with the passed arguments, then L</count>.
1069 sub count_literal { shift->search_literal(@_)->count; }
1075 =item Arguments: none
1077 =item Return Value: @objects
1081 Returns all elements in the resultset. Called implicitly if the resultset
1082 is returned in list context.
1088 return @{ $self->get_cache } if $self->get_cache;
1092 # TODO: don't call resolve here
1093 if (keys %{$self->_resolved_attrs->{collapse}}) {
1094 # if ($self->{attrs}{prefetch}) {
1095 # Using $self->cursor->all is really just an optimisation.
1096 # If we're collapsing has_many prefetches it probably makes
1097 # very little difference, and this is cleaner than hacking
1098 # _construct_object to survive the approach
1099 my @row = $self->cursor->next;
1101 push(@obj, $self->_construct_object(@row));
1102 @row = (exists $self->{stashed_row}
1103 ? @{delete $self->{stashed_row}}
1104 : $self->cursor->next);
1107 @obj = map { $self->_construct_object(@$_) } $self->cursor->all;
1110 $self->set_cache(\@obj) if $self->{attrs}{cache};
1118 =item Arguments: none
1120 =item Return Value: $self
1124 Resets the resultset's cursor, so you can iterate through the elements again.
1130 delete $self->{_attrs} if exists $self->{_attrs};
1131 $self->{all_cache_position} = 0;
1132 $self->cursor->reset;
1140 =item Arguments: none
1142 =item Return Value: $object?
1146 Resets the resultset and returns an object for the first result (if the
1147 resultset returns anything).
1152 return $_[0]->reset->next;
1155 # _cond_for_update_delete
1157 # update/delete require the condition to be modified to handle
1158 # the differing SQL syntax available. This transforms the $self->{cond}
1159 # appropriately, returning the new condition.
1161 sub _cond_for_update_delete {
1162 my ($self, $full_cond) = @_;
1165 $full_cond ||= $self->{cond};
1166 # No-op. No condition, we're updating/deleting everything
1167 return $cond unless ref $full_cond;
1169 if (ref $full_cond eq 'ARRAY') {
1173 foreach my $key (keys %{$_}) {
1175 $hash{$1} = $_->{$key};
1181 elsif (ref $full_cond eq 'HASH') {
1182 if ((keys %{$full_cond})[0] eq '-and') {
1185 my @cond = @{$full_cond->{-and}};
1186 for (my $i = 0; $i < @cond; $i++) {
1187 my $entry = $cond[$i];
1190 if (ref $entry eq 'HASH') {
1191 $hash = $self->_cond_for_update_delete($entry);
1194 $entry =~ /([^.]+)$/;
1195 $hash->{$1} = $cond[++$i];
1198 push @{$cond->{-and}}, $hash;
1202 foreach my $key (keys %{$full_cond}) {
1204 $cond->{$1} = $full_cond->{$key};
1209 $self->throw_exception(
1210 "Can't update/delete on resultset with condition unless hash or array"
1222 =item Arguments: \%values
1224 =item Return Value: $storage_rv
1228 Sets the specified columns in the resultset to the supplied values in a
1229 single query. Return value will be true if the update succeeded or false
1230 if no records were updated; exact type of success value is storage-dependent.
1235 my ($self, $values) = @_;
1236 $self->throw_exception("Values for update must be a hash")
1237 unless ref $values eq 'HASH';
1239 my $cond = $self->_cond_for_update_delete;
1241 return $self->result_source->storage->update(
1242 $self->result_source, $values, $cond
1250 =item Arguments: \%values
1252 =item Return Value: 1
1256 Fetches all objects and updates them one at a time. Note that C<update_all>
1257 will run DBIC cascade triggers, while L</update> will not.
1262 my ($self, $values) = @_;
1263 $self->throw_exception("Values for update must be a hash")
1264 unless ref $values eq 'HASH';
1265 foreach my $obj ($self->all) {
1266 $obj->set_columns($values)->update;
1275 =item Arguments: none
1277 =item Return Value: 1
1281 Deletes the contents of the resultset from its result source. Note that this
1282 will not run DBIC cascade triggers. See L</delete_all> if you need triggers
1283 to run. See also L<DBIx::Class::Row/delete>.
1290 my $cond = $self->_cond_for_update_delete;
1292 $self->result_source->storage->delete($self->result_source, $cond);
1300 =item Arguments: none
1302 =item Return Value: 1
1306 Fetches all objects and deletes them one at a time. Note that C<delete_all>
1307 will run DBIC cascade triggers, while L</delete> will not.
1313 $_->delete for $self->all;
1321 =item Arguments: \@data;
1325 Pass an arrayref of hashrefs. Each hashref should be a structure suitable for
1326 submitting to a $resultset->create(...) method.
1328 In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
1329 to insert the data, as this is a faster method.
1331 Otherwise, each set of data is inserted into the database using
1332 L<DBIx::Class::ResultSet/create>, and a arrayref of the resulting row
1333 objects is returned.
1335 Example: Assuming an Artist Class that has many CDs Classes relating:
1337 my $Artist_rs = $schema->resultset("Artist");
1339 ## Void Context Example
1340 $Artist_rs->populate([
1341 { artistid => 4, name => 'Manufactured Crap', cds => [
1342 { title => 'My First CD', year => 2006 },
1343 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
1346 { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
1347 { title => 'My parents sold me to a record company' ,year => 2005 },
1348 { title => 'Why Am I So Ugly?', year => 2006 },
1349 { title => 'I Got Surgery and am now Popular', year => 2007 }
1354 ## Array Context Example
1355 my ($ArtistOne, $ArtistTwo, $ArtistThree) = $Artist_rs->populate([
1356 { name => "Artist One"},
1357 { name => "Artist Two"},
1358 { name => "Artist Three", cds=> [
1359 { title => "First CD", year => 2007},
1360 { title => "Second CD", year => 2008},
1364 print $ArtistOne->name; ## response is 'Artist One'
1365 print $ArtistThree->cds->count ## reponse is '2'
1367 Please note an important effect on your data when choosing between void and
1368 wantarray context. Since void context goes straight to C<insert_bulk> in
1369 L<DBIx::Class::Storage::DBI> this will skip any component that is overriding
1370 c<insert>. So if you are using something like L<DBIx-Class-UUIDColumns> to
1371 create primary keys for you, you will find that your PKs are empty. In this
1372 case you will have to use the wantarray context in order to create those
1378 my ($self, $data) = @_;
1380 if(defined wantarray) {
1382 foreach my $item (@$data) {
1383 push(@created, $self->create($item));
1387 my ($first, @rest) = @$data;
1389 my @names = grep {!ref $first->{$_}} keys %$first;
1390 my @rels = grep { $self->result_source->has_relationship($_) } keys %$first;
1391 my @pks = $self->result_source->primary_columns;
1393 ## do the belongs_to relationships
1394 foreach my $index (0..$#$data) {
1395 if( grep { !defined $data->[$index]->{$_} } @pks ) {
1396 my @ret = $self->populate($data);
1400 foreach my $rel (@rels) {
1401 next unless $data->[$index]->{$rel} && ref $data->[$index]->{$rel} eq "HASH";
1402 my $result = $self->related_resultset($rel)->create($data->[$index]->{$rel});
1403 my ($reverse) = keys %{$self->result_source->reverse_relationship_info($rel)};
1404 my $related = $result->result_source->resolve_condition(
1405 $result->result_source->relationship_info($reverse)->{cond},
1410 delete $data->[$index]->{$rel};
1411 $data->[$index] = {%{$data->[$index]}, %$related};
1413 push @names, keys %$related if $index == 0;
1417 ## do bulk insert on current row
1418 my @values = map { [ @$_{@names} ] } @$data;
1420 $self->result_source->storage->insert_bulk(
1421 $self->result_source,
1426 ## do the has_many relationships
1427 foreach my $item (@$data) {
1429 foreach my $rel (@rels) {
1430 next unless $item->{$rel} && ref $item->{$rel} eq "ARRAY";
1432 my $parent = $self->find(map {{$_=>$item->{$_}} } @pks)
1433 || $self->throw_exception('Cannot find the relating object.');
1435 my $child = $parent->$rel;
1437 my $related = $child->result_source->resolve_condition(
1438 $parent->result_source->relationship_info($rel)->{cond},
1443 my @rows_to_add = ref $item->{$rel} eq 'ARRAY' ? @{$item->{$rel}} : ($item->{$rel});
1444 my @populate = map { {%$_, %$related} } @rows_to_add;
1446 $child->populate( \@populate );
1456 =item Arguments: none
1458 =item Return Value: $pager
1462 Return Value a L<Data::Page> object for the current resultset. Only makes
1463 sense for queries with a C<page> attribute.
1469 my $attrs = $self->{attrs};
1470 $self->throw_exception("Can't create pager for non-paged rs")
1471 unless $self->{attrs}{page};
1472 $attrs->{rows} ||= 10;
1473 return $self->{pager} ||= Data::Page->new(
1474 $self->_count, $attrs->{rows}, $self->{attrs}{page});
1481 =item Arguments: $page_number
1483 =item Return Value: $rs
1487 Returns a resultset for the $page_number page of the resultset on which page
1488 is called, where each page contains a number of rows equal to the 'rows'
1489 attribute set on the resultset (10 by default).
1494 my ($self, $page) = @_;
1495 return (ref $self)->new($self->result_source, { %{$self->{attrs}}, page => $page });
1502 =item Arguments: \%vals
1504 =item Return Value: $object
1508 Creates a new row object in the resultset's result class and returns
1509 it. The row is not inserted into the database at this point, call
1510 L<DBIx::Class::Row/insert> to do that. Calling L<DBIx::Class::Row/in_storage>
1511 will tell you whether the row object has been inserted or not.
1513 Passes the hashref of input on to L<DBIx::Class::Row/new>.
1518 my ($self, $values) = @_;
1519 $self->throw_exception( "new_result needs a hash" )
1520 unless (ref $values eq 'HASH');
1521 $self->throw_exception(
1522 "Can't abstract implicit construct, condition not a hash"
1523 ) if ($self->{cond} && !(ref $self->{cond} eq 'HASH'));
1525 my $alias = $self->{attrs}{alias};
1526 my $collapsed_cond = $self->{cond} ? $self->_collapse_cond($self->{cond}) : {};
1528 # precendence must be given to passed values over values inherited from the cond,
1529 # so the order here is important.
1531 %{ $self->_remove_alias($collapsed_cond, $alias) },
1532 %{ $self->_remove_alias($values, $alias) },
1533 -source_handle => $self->_source_handle,
1534 -result_source => $self->result_source, # DO NOT REMOVE THIS, REQUIRED
1537 return $self->result_class->new(\%new);
1542 # Recursively collapse the condition.
1544 sub _collapse_cond {
1545 my ($self, $cond, $collapsed) = @_;
1549 if (ref $cond eq 'ARRAY') {
1550 foreach my $subcond (@$cond) {
1551 next unless ref $subcond; # -or
1552 # warn "ARRAY: " . Dumper $subcond;
1553 $collapsed = $self->_collapse_cond($subcond, $collapsed);
1556 elsif (ref $cond eq 'HASH') {
1557 if (keys %$cond and (keys %$cond)[0] eq '-and') {
1558 foreach my $subcond (@{$cond->{-and}}) {
1559 # warn "HASH: " . Dumper $subcond;
1560 $collapsed = $self->_collapse_cond($subcond, $collapsed);
1564 # warn "LEAF: " . Dumper $cond;
1565 foreach my $col (keys %$cond) {
1566 my $value = $cond->{$col};
1567 $collapsed->{$col} = $value;
1577 # Remove the specified alias from the specified query hash. A copy is made so
1578 # the original query is not modified.
1581 my ($self, $query, $alias) = @_;
1583 my %orig = %{ $query || {} };
1586 foreach my $key (keys %orig) {
1588 $unaliased{$key} = $orig{$key};
1591 $unaliased{$1} = $orig{$key}
1592 if $key =~ m/^(?:\Q$alias\E\.)?([^.]+)$/;
1602 =item Arguments: \%vals, \%attrs?
1604 =item Return Value: $object
1608 Find an existing record from this resultset. If none exists, instantiate a new
1609 result object and return it. The object will not be saved into your storage
1610 until you call L<DBIx::Class::Row/insert> on it.
1612 If you want objects to be saved immediately, use L</find_or_create> instead.
1618 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1619 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1620 my $exists = $self->find($hash, $attrs);
1621 return defined $exists ? $exists : $self->new_result($hash);
1628 =item Arguments: \%vals
1630 =item Return Value: a L<DBIx::Class::Row> $object
1634 Attempt to create a single new row or a row with multiple related rows
1635 in the table represented by the resultset (and related tables). This
1636 will not check for duplicate rows before inserting, use
1637 L</find_or_create> to do that.
1639 To create one row for this resultset, pass a hashref of key/value
1640 pairs representing the columns of the table and the values you wish to
1641 store. If the appropriate relationships are set up, foreign key fields
1642 can also be passed an object representing the foreign row, and the
1643 value will be set to it's primary key.
1645 To create related objects, pass a hashref for the value if the related
1646 item is a foreign key relationship (L<DBIx::Class::Relationship/belongs_to>),
1647 and use the name of the relationship as the key. (NOT the name of the field,
1648 necessarily). For C<has_many> and C<has_one> relationships, pass an arrayref
1649 of hashrefs containing the data for each of the rows to create in the foreign
1650 tables, again using the relationship name as the key.
1652 Instead of hashrefs of plain related data (key/value pairs), you may
1653 also pass new or inserted objects. New objects (not inserted yet, see
1654 L</new>), will be inserted into their appropriate tables.
1656 Effectively a shortcut for C<< ->new_result(\%vals)->insert >>.
1658 Example of creating a new row.
1660 $person_rs->create({
1661 name=>"Some Person",
1662 email=>"somebody@someplace.com"
1665 Example of creating a new row and also creating rows in a related C<has_many>
1666 or C<has_one> resultset. Note Arrayref.
1669 { artistid => 4, name => 'Manufactured Crap', cds => [
1670 { title => 'My First CD', year => 2006 },
1671 { title => 'Yet More Tweeny-Pop crap', year => 2007 },
1676 Example of creating a new row and also creating a row in a related
1677 C<belongs_to>resultset. Note Hashref.
1680 title=>"Music for Silly Walks",
1683 name=>"Silly Musician",
1690 my ($self, $attrs) = @_;
1691 $self->throw_exception( "create needs a hashref" )
1692 unless ref $attrs eq 'HASH';
1693 return $self->new_result($attrs)->insert;
1696 =head2 find_or_create
1700 =item Arguments: \%vals, \%attrs?
1702 =item Return Value: $object
1706 $class->find_or_create({ key => $val, ... });
1708 Tries to find a record based on its primary key or unique constraint; if none
1709 is found, creates one and returns that instead.
1711 my $cd = $schema->resultset('CD')->find_or_create({
1713 artist => 'Massive Attack',
1714 title => 'Mezzanine',
1718 Also takes an optional C<key> attribute, to search by a specific key or unique
1719 constraint. For example:
1721 my $cd = $schema->resultset('CD')->find_or_create(
1723 artist => 'Massive Attack',
1724 title => 'Mezzanine',
1726 { key => 'cd_artist_title' }
1729 See also L</find> and L</update_or_create>. For information on how to declare
1730 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1734 sub find_or_create {
1736 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1737 my $hash = ref $_[0] eq 'HASH' ? shift : {@_};
1738 my $exists = $self->find($hash, $attrs);
1739 return defined $exists ? $exists : $self->create($hash);
1742 =head2 update_or_create
1746 =item Arguments: \%col_values, { key => $unique_constraint }?
1748 =item Return Value: $object
1752 $class->update_or_create({ col => $val, ... });
1754 First, searches for an existing row matching one of the unique constraints
1755 (including the primary key) on the source of this resultset. If a row is
1756 found, updates it with the other given column values. Otherwise, creates a new
1759 Takes an optional C<key> attribute to search on a specific unique constraint.
1762 # In your application
1763 my $cd = $schema->resultset('CD')->update_or_create(
1765 artist => 'Massive Attack',
1766 title => 'Mezzanine',
1769 { key => 'cd_artist_title' }
1772 If no C<key> is specified, it searches on all unique constraints defined on the
1773 source, including the primary key.
1775 If the C<key> is specified as C<primary>, it searches only on the primary key.
1777 See also L</find> and L</find_or_create>. For information on how to declare
1778 unique constraints, see L<DBIx::Class::ResultSource/add_unique_constraint>.
1782 sub update_or_create {
1784 my $attrs = (@_ > 1 && ref $_[$#_] eq 'HASH' ? pop(@_) : {});
1785 my $cond = ref $_[0] eq 'HASH' ? shift : {@_};
1787 my $row = $self->find($cond, $attrs);
1789 $row->update($cond);
1793 return $self->create($cond);
1800 =item Arguments: none
1802 =item Return Value: \@cache_objects?
1806 Gets the contents of the cache for the resultset, if the cache is set.
1808 The cache is populated either by using the L</prefetch> attribute to
1809 L</search> or by calling L</set_cache>.
1821 =item Arguments: \@cache_objects
1823 =item Return Value: \@cache_objects
1827 Sets the contents of the cache for the resultset. Expects an arrayref
1828 of objects of the same class as those produced by the resultset. Note that
1829 if the cache is set the resultset will return the cached objects rather
1830 than re-querying the database even if the cache attr is not set.
1832 The contents of the cache can also be populated by using the
1833 L</prefetch> attribute to L</search>.
1838 my ( $self, $data ) = @_;
1839 $self->throw_exception("set_cache requires an arrayref")
1840 if defined($data) && (ref $data ne 'ARRAY');
1841 $self->{all_cache} = $data;
1848 =item Arguments: none
1850 =item Return Value: []
1854 Clears the cache for the resultset.
1859 shift->set_cache(undef);
1862 =head2 related_resultset
1866 =item Arguments: $relationship_name
1868 =item Return Value: $resultset
1872 Returns a related resultset for the supplied relationship name.
1874 $artist_rs = $schema->resultset('CD')->related_resultset('Artist');
1878 sub related_resultset {
1879 my ($self, $rel) = @_;
1881 $self->{related_resultsets} ||= {};
1882 return $self->{related_resultsets}{$rel} ||= do {
1883 my $rel_obj = $self->result_source->relationship_info($rel);
1885 $self->throw_exception(
1886 "search_related: result source '" . $self->result_source->source_name .
1887 "' has no such relationship $rel")
1890 my ($from,$seen) = $self->_resolve_from($rel);
1892 my $join_count = $seen->{$rel};
1893 my $alias = ($join_count > 1 ? join('_', $rel, $join_count) : $rel);
1895 #XXX - temp fix for result_class bug. There likely is a more elegant fix -groditi
1896 my %attrs = %{$self->{attrs}||{}};
1897 delete @attrs{qw(result_class alias)};
1901 if (my $cache = $self->get_cache) {
1902 if ($cache->[0] && $cache->[0]->related_resultset($rel)->get_cache) {
1903 $new_cache = [ map { @{$_->related_resultset($rel)->get_cache} }
1908 my $rel_source = $self->result_source->related_source($rel);
1912 # The reason we do this now instead of passing the alias to the
1913 # search_rs below is that if you wrap/overload resultset on the
1914 # source you need to know what alias it's -going- to have for things
1915 # to work sanely (e.g. RestrictWithObject wants to be able to add
1916 # extra query restrictions, and these may need to be $alias.)
1918 my $attrs = $rel_source->resultset_attributes;
1919 local $attrs->{alias} = $alias;
1921 $rel_source->resultset
1929 where => $self->{cond},
1934 $new->set_cache($new_cache) if $new_cache;
1940 my ($self, $extra_join) = @_;
1941 my $source = $self->result_source;
1942 my $attrs = $self->{attrs};
1944 my $from = $attrs->{from}
1945 || [ { $attrs->{alias} => $source->from } ];
1947 my $seen = { %{$attrs->{seen_join}||{}} };
1949 my $join = ($attrs->{join}
1950 ? [ $attrs->{join}, $extra_join ]
1953 # we need to take the prefetch the attrs into account before we
1954 # ->resolve_join as otherwise they get lost - captainL
1955 my $merged = $self->_merge_attr( $join, $attrs->{prefetch} );
1959 ($join ? $source->resolve_join($merged, $attrs->{alias}, $seen) : ()),
1962 return ($from,$seen);
1965 sub _resolved_attrs {
1967 return $self->{_attrs} if $self->{_attrs};
1969 my $attrs = { %{$self->{attrs}||{}} };
1970 my $source = $self->result_source;
1971 my $alias = $attrs->{alias};
1973 $attrs->{columns} ||= delete $attrs->{cols} if exists $attrs->{cols};
1974 if ($attrs->{columns}) {
1975 delete $attrs->{as};
1976 } elsif (!$attrs->{select}) {
1977 $attrs->{columns} = [ $source->columns ];
1982 ? (ref $attrs->{select} eq 'ARRAY'
1983 ? [ @{$attrs->{select}} ]
1984 : [ $attrs->{select} ])
1985 : [ map { m/\./ ? $_ : "${alias}.$_" } @{delete $attrs->{columns}} ]
1989 ? (ref $attrs->{as} eq 'ARRAY'
1990 ? [ @{$attrs->{as}} ]
1992 : [ map { m/^\Q${alias}.\E(.+)$/ ? $1 : $_ } @{$attrs->{select}} ]
1996 if ($adds = delete $attrs->{include_columns}) {
1997 $adds = [$adds] unless ref $adds eq 'ARRAY';
1998 push(@{$attrs->{select}}, @$adds);
1999 push(@{$attrs->{as}}, map { m/([^.]+)$/; $1 } @$adds);
2001 if ($adds = delete $attrs->{'+select'}) {
2002 $adds = [$adds] unless ref $adds eq 'ARRAY';
2003 push(@{$attrs->{select}},
2004 map { /\./ || ref $_ ? $_ : "${alias}.$_" } @$adds);
2006 if (my $adds = delete $attrs->{'+as'}) {
2007 $adds = [$adds] unless ref $adds eq 'ARRAY';
2008 push(@{$attrs->{as}}, @$adds);
2011 $attrs->{from} ||= [ { 'me' => $source->from } ];
2013 if (exists $attrs->{join} || exists $attrs->{prefetch}) {
2014 my $join = delete $attrs->{join} || {};
2016 if (defined $attrs->{prefetch}) {
2017 $join = $self->_merge_attr(
2018 $join, $attrs->{prefetch}
2023 $attrs->{from} = # have to copy here to avoid corrupting the original
2026 $source->resolve_join($join, $alias, { %{$attrs->{seen_join}||{}} })
2031 $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct};
2032 if ($attrs->{order_by}) {
2033 $attrs->{order_by} = (ref($attrs->{order_by}) eq 'ARRAY'
2034 ? [ @{$attrs->{order_by}} ]
2035 : [ $attrs->{order_by} ]);
2037 $attrs->{order_by} = [];
2040 my $collapse = $attrs->{collapse} || {};
2041 if (my $prefetch = delete $attrs->{prefetch}) {
2042 $prefetch = $self->_merge_attr({}, $prefetch);
2044 my $seen = $attrs->{seen_join} || {};
2045 foreach my $p (ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch)) {
2046 # bring joins back to level of current class
2047 my @prefetch = $source->resolve_prefetch(
2048 $p, $alias, $seen, \@pre_order, $collapse
2050 push(@{$attrs->{select}}, map { $_->[0] } @prefetch);
2051 push(@{$attrs->{as}}, map { $_->[1] } @prefetch);
2053 push(@{$attrs->{order_by}}, @pre_order);
2055 $attrs->{collapse} = $collapse;
2057 if ($attrs->{page}) {
2058 $attrs->{offset} ||= 0;
2059 $attrs->{offset} += ($attrs->{rows} * ($attrs->{page} - 1));
2062 return $self->{_attrs} = $attrs;
2066 my ($self, $attr) = @_;
2068 if (ref $attr eq 'HASH') {
2069 return $self->_rollout_hash($attr);
2070 } elsif (ref $attr eq 'ARRAY') {
2071 return $self->_rollout_array($attr);
2077 sub _rollout_array {
2078 my ($self, $attr) = @_;
2081 foreach my $element (@{$attr}) {
2082 if (ref $element eq 'HASH') {
2083 push( @rolled_array, @{ $self->_rollout_hash( $element ) } );
2084 } elsif (ref $element eq 'ARRAY') {
2085 # XXX - should probably recurse here
2086 push( @rolled_array, @{$self->_rollout_array($element)} );
2088 push( @rolled_array, $element );
2091 return \@rolled_array;
2095 my ($self, $attr) = @_;
2098 foreach my $key (keys %{$attr}) {
2099 push( @rolled_array, { $key => $attr->{$key} } );
2101 return \@rolled_array;
2104 sub _calculate_score {
2105 my ($self, $a, $b) = @_;
2107 if (ref $b eq 'HASH') {
2108 my ($b_key) = keys %{$b};
2109 if (ref $a eq 'HASH') {
2110 my ($a_key) = keys %{$a};
2111 if ($a_key eq $b_key) {
2112 return (1 + $self->_calculate_score( $a->{$a_key}, $b->{$b_key} ));
2117 return ($a eq $b_key) ? 1 : 0;
2120 if (ref $a eq 'HASH') {
2121 my ($a_key) = keys %{$a};
2122 return ($b eq $a_key) ? 1 : 0;
2124 return ($b eq $a) ? 1 : 0;
2130 my ($self, $a, $b) = @_;
2132 return $b unless defined($a);
2133 return $a unless defined($b);
2135 $a = $self->_rollout_attr($a);
2136 $b = $self->_rollout_attr($b);
2139 foreach my $b_element ( @{$b} ) {
2140 # find best candidate from $a to merge $b_element into
2141 my $best_candidate = { position => undef, score => 0 }; my $position = 0;
2142 foreach my $a_element ( @{$a} ) {
2143 my $score = $self->_calculate_score( $a_element, $b_element );
2144 if ($score > $best_candidate->{score}) {
2145 $best_candidate->{position} = $position;
2146 $best_candidate->{score} = $score;
2150 my ($b_key) = ( ref $b_element eq 'HASH' ) ? keys %{$b_element} : ($b_element);
2152 if ($best_candidate->{score} == 0 || exists $seen_keys->{$b_key}) {
2153 push( @{$a}, $b_element );
2155 my $a_best = $a->[$best_candidate->{position}];
2156 # merge a_best and b_element together and replace original with merged
2157 if (ref $a_best ne 'HASH') {
2158 $a->[$best_candidate->{position}] = $b_element;
2159 } elsif (ref $b_element eq 'HASH') {
2160 my ($key) = keys %{$a_best};
2161 $a->[$best_candidate->{position}] = { $key => $self->_merge_attr($a_best->{$key}, $b_element->{$key}) };
2164 $seen_keys->{$b_key} = 1; # don't merge the same key twice
2174 $self->_source_handle($_[0]->handle);
2176 $self->_source_handle->resolve;
2180 =head2 throw_exception
2182 See L<DBIx::Class::Schema/throw_exception> for details.
2186 sub throw_exception {
2188 if (ref $self && $self->_source_handle->schema) {
2189 $self->_source_handle->schema->throw_exception(@_)
2196 # XXX: FIXME: Attributes docs need clearing up
2200 The resultset takes various attributes that modify its behavior. Here's an
2207 =item Value: ($order_by | \@order_by)
2211 Which column(s) to order the results by. This is currently passed
2212 through directly to SQL, so you can give e.g. C<year DESC> for a
2213 descending order on the column `year'.
2215 Please note that if you have C<quote_char> enabled (see
2216 L<DBIx::Class::Storage::DBI/connect_info>) you will need to do C<\'year DESC' > to
2217 specify an order. (The scalar ref causes it to be passed as raw sql to the DB,
2218 so you will need to manually quote things as appropriate.)
2224 =item Value: \@columns
2228 Shortcut to request a particular set of columns to be retrieved. Adds
2229 C<me.> onto the start of any column without a C<.> in it and sets C<select>
2230 from that, then auto-populates C<as> from C<select> as normal. (You may also
2231 use the C<cols> attribute, as in earlier versions of DBIC.)
2233 =head2 include_columns
2237 =item Value: \@columns
2241 Shortcut to include additional columns in the returned results - for example
2243 $schema->resultset('CD')->search(undef, {
2244 include_columns => ['artist.name'],
2248 would return all CDs and include a 'name' column to the information
2249 passed to object inflation. Note that the 'artist' is the name of the
2250 column (or relationship) accessor, and 'name' is the name of the column
2251 accessor in the related table.
2257 =item Value: \@select_columns
2261 Indicates which columns should be selected from the storage. You can use
2262 column names, or in the case of RDBMS back ends, function or stored procedure
2265 $rs = $schema->resultset('Employee')->search(undef, {
2268 { count => 'employeeid' },
2273 When you use function/stored procedure names and do not supply an C<as>
2274 attribute, the column names returned are storage-dependent. E.g. MySQL would
2275 return a column named C<count(employeeid)> in the above example.
2281 Indicates additional columns to be selected from storage. Works the same as
2282 L</select> but adds columns to the selection.
2290 Indicates additional column names for those added via L</+select>.
2298 =item Value: \@inflation_names
2302 Indicates column names for object inflation. That is, C<as>
2303 indicates the name that the column can be accessed as via the
2304 C<get_column> method (or via the object accessor, B<if one already
2305 exists>). It has nothing to do with the SQL code C<SELECT foo AS bar>.
2307 The C<as> attribute is used in conjunction with C<select>,
2308 usually when C<select> contains one or more function or stored
2311 $rs = $schema->resultset('Employee')->search(undef, {
2314 { count => 'employeeid' }
2316 as => ['name', 'employee_count'],
2319 my $employee = $rs->first(); # get the first Employee
2321 If the object against which the search is performed already has an accessor
2322 matching a column name specified in C<as>, the value can be retrieved using
2323 the accessor as normal:
2325 my $name = $employee->name();
2327 If on the other hand an accessor does not exist in the object, you need to
2328 use C<get_column> instead:
2330 my $employee_count = $employee->get_column('employee_count');
2332 You can create your own accessors if required - see
2333 L<DBIx::Class::Manual::Cookbook> for details.
2335 Please note: This will NOT insert an C<AS employee_count> into the SQL
2336 statement produced, it is used for internal access only. Thus
2337 attempting to use the accessor in an C<order_by> clause or similar
2338 will fail miserably.
2340 To get around this limitation, you can supply literal SQL to your
2341 C<select> attibute that contains the C<AS alias> text, eg:
2343 select => [\'myfield AS alias']
2349 =item Value: ($rel_name | \@rel_names | \%rel_names)
2353 Contains a list of relationships that should be joined for this query. For
2356 # Get CDs by Nine Inch Nails
2357 my $rs = $schema->resultset('CD')->search(
2358 { 'artist.name' => 'Nine Inch Nails' },
2359 { join => 'artist' }
2362 Can also contain a hash reference to refer to the other relation's relations.
2365 package MyApp::Schema::Track;
2366 use base qw/DBIx::Class/;
2367 __PACKAGE__->table('track');
2368 __PACKAGE__->add_columns(qw/trackid cd position title/);
2369 __PACKAGE__->set_primary_key('trackid');
2370 __PACKAGE__->belongs_to(cd => 'MyApp::Schema::CD');
2373 # In your application
2374 my $rs = $schema->resultset('Artist')->search(
2375 { 'track.title' => 'Teardrop' },
2377 join => { cd => 'track' },
2378 order_by => 'artist.name',
2382 You need to use the relationship (not the table) name in conditions,
2383 because they are aliased as such. The current table is aliased as "me", so
2384 you need to use me.column_name in order to avoid ambiguity. For example:
2386 # Get CDs from 1984 with a 'Foo' track
2387 my $rs = $schema->resultset('CD')->search(
2390 'tracks.name' => 'Foo'
2392 { join => 'tracks' }
2395 If the same join is supplied twice, it will be aliased to <rel>_2 (and
2396 similarly for a third time). For e.g.
2398 my $rs = $schema->resultset('Artist')->search({
2399 'cds.title' => 'Down to Earth',
2400 'cds_2.title' => 'Popular',
2402 join => [ qw/cds cds/ ],
2405 will return a set of all artists that have both a cd with title 'Down
2406 to Earth' and a cd with title 'Popular'.
2408 If you want to fetch related objects from other tables as well, see C<prefetch>
2411 For more help on using joins with search, see L<DBIx::Class::Manual::Joining>.
2417 =item Value: ($rel_name | \@rel_names | \%rel_names)
2421 Contains one or more relationships that should be fetched along with
2422 the main query (when they are accessed afterwards the data will
2423 already be available, without extra queries to the database). This is
2424 useful for when you know you will need the related objects, because it
2425 saves at least one query:
2427 my $rs = $schema->resultset('Tag')->search(
2436 The initial search results in SQL like the following:
2438 SELECT tag.*, cd.*, artist.* FROM tag
2439 JOIN cd ON tag.cd = cd.cdid
2440 JOIN artist ON cd.artist = artist.artistid
2442 L<DBIx::Class> has no need to go back to the database when we access the
2443 C<cd> or C<artist> relationships, which saves us two SQL statements in this
2446 Simple prefetches will be joined automatically, so there is no need
2447 for a C<join> attribute in the above search.
2449 C<prefetch> can be used with the following relationship types: C<belongs_to>,
2450 C<has_one> (or if you're using C<add_relationship>, any relationship declared
2451 with an accessor type of 'single' or 'filter'). A more complex example that
2452 prefetches an artists cds, the tracks on those cds, and the tags associted
2453 with that artist is given below (assuming many-to-many from artists to tags):
2455 my $rs = $schema->resultset('Artist')->search(
2459 { cds => 'tracks' },
2460 { artist_tags => 'tags' }
2466 B<NOTE:> If you specify a C<prefetch> attribute, the C<join> and C<select>
2467 attributes will be ignored.
2477 Makes the resultset paged and specifies the page to retrieve. Effectively
2478 identical to creating a non-pages resultset and then calling ->page($page)
2481 If L<rows> attribute is not specified it defualts to 10 rows per page.
2491 Specifes the maximum number of rows for direct retrieval or the number of
2492 rows per page if the page attribute or method is used.
2498 =item Value: $offset
2502 Specifies the (zero-based) row number for the first row to be returned, or the
2503 of the first row of the first page if paging is used.
2509 =item Value: \@columns
2513 A arrayref of columns to group by. Can include columns of joined tables.
2515 group_by => [qw/ column1 column2 ... /]
2521 =item Value: $condition
2525 HAVING is a select statement attribute that is applied between GROUP BY and
2526 ORDER BY. It is applied to the after the grouping calculations have been
2529 having => { 'count(employee)' => { '>=', 100 } }
2535 =item Value: (0 | 1)
2539 Set to 1 to group by all columns.
2545 Adds to the WHERE clause.
2547 # only return rows WHERE deleted IS NULL for all searches
2548 __PACKAGE__->resultset_attributes({ where => { deleted => undef } }); )
2550 Can be overridden by passing C<{ where => undef }> as an attribute
2557 Set to 1 to cache search results. This prevents extra SQL queries if you
2558 revisit rows in your ResultSet:
2560 my $resultset = $schema->resultset('Artist')->search( undef, { cache => 1 } );
2562 while( my $artist = $resultset->next ) {
2566 $rs->first; # without cache, this would issue a query
2568 By default, searches are not cached.
2570 For more examples of using these attributes, see
2571 L<DBIx::Class::Manual::Cookbook>.
2577 =item Value: \@from_clause
2581 The C<from> attribute gives you manual control over the C<FROM> clause of SQL
2582 statements generated by L<DBIx::Class>, allowing you to express custom C<JOIN>
2585 NOTE: Use this on your own risk. This allows you to shoot off your foot!
2587 C<join> will usually do what you need and it is strongly recommended that you
2588 avoid using C<from> unless you cannot achieve the desired result using C<join>.
2589 And we really do mean "cannot", not just tried and failed. Attempting to use
2590 this because you're having problems with C<join> is like trying to use x86
2591 ASM because you've got a syntax error in your C. Trust us on this.
2593 Now, if you're still really, really sure you need to use this (and if you're
2594 not 100% sure, ask the mailing list first), here's an explanation of how this
2597 The syntax is as follows -
2600 { <alias1> => <table1> },
2602 { <alias2> => <table2>, -join_type => 'inner|left|right' },
2603 [], # nested JOIN (optional)
2604 { <table1.column1> => <table2.column2>, ... (more conditions) },
2606 # More of the above [ ] may follow for additional joins
2613 ON <table1.column1> = <table2.column2>
2614 <more joins may follow>
2616 An easy way to follow the examples below is to remember the following:
2618 Anything inside "[]" is a JOIN
2619 Anything inside "{}" is a condition for the enclosing JOIN
2621 The following examples utilize a "person" table in a family tree application.
2622 In order to express parent->child relationships, this table is self-joined:
2624 # Person->belongs_to('father' => 'Person');
2625 # Person->belongs_to('mother' => 'Person');
2627 C<from> can be used to nest joins. Here we return all children with a father,
2628 then search against all mothers of those children:
2630 $rs = $schema->resultset('Person')->search(
2633 alias => 'mother', # alias columns in accordance with "from"
2635 { mother => 'person' },
2638 { child => 'person' },
2640 { father => 'person' },
2641 { 'father.person_id' => 'child.father_id' }
2644 { 'mother.person_id' => 'child.mother_id' }
2651 # SELECT mother.* FROM person mother
2654 # JOIN person father
2655 # ON ( father.person_id = child.father_id )
2657 # ON ( mother.person_id = child.mother_id )
2659 The type of any join can be controlled manually. To search against only people
2660 with a father in the person table, we could explicitly use C<INNER JOIN>:
2662 $rs = $schema->resultset('Person')->search(
2665 alias => 'child', # alias columns in accordance with "from"
2667 { child => 'person' },
2669 { father => 'person', -join_type => 'inner' },
2670 { 'father.id' => 'child.father_id' }
2677 # SELECT child.* FROM person child
2678 # INNER JOIN person father ON child.father_id = father.id
2684 =item Value: ( 'update' | 'shared' )
2688 Set to 'update' for a SELECT ... FOR UPDATE or 'shared' for a SELECT