sub search {
my $self = shift;
-
+ my $rs = $self->search_rs( @_ );
+ return (wantarray ? $rs->all : $rs);
+ }
+
+ =head2 search_rs
+
+ =over 4
+
+ =item Arguments: $cond, \%attrs?
+
+ =item Return Value: $resultset
+
+ =back
+
+ This method does the same exact thing as search() except it will
+ always return a resultset, even in list context.
+
+ =cut
+
+ sub search_rs {
+ my $self = shift;
+
- my $attrs = { %{$self->{attrs}} };
- my $having = delete $attrs->{having};
- $attrs = { %$attrs, %{ pop(@_) } } if @_ > 1 and ref $_[$#_] eq 'HASH';
+ my $our_attrs = { %{$self->{attrs}} };
+ my $having = delete $our_attrs->{having};
+ my $attrs = {};
+ $attrs = pop(@_) if @_ > 1 and ref $_[$#_] eq 'HASH';
+
+ # merge new attrs into old
+ foreach my $key (qw/join prefetch/) {
+ next unless (exists $attrs->{$key});
+ if (exists $our_attrs->{$key}) {
+ $our_attrs->{$key} = [$our_attrs->{$key}] if (ref $our_attrs->{$key} ne 'ARRAY');
+ push(@{$our_attrs->{$key}}, (ref $attrs->{$key} eq 'ARRAY') ? @{$attrs->{$key}} : $attrs->{$key});
+ } else {
+ $our_attrs->{$key} = $attrs->{$key};
+ }
+ delete $attrs->{$key};
+ }
- $our_attrs = { %{$our_attrs}, %{$attrs} };
++ my $new_attrs = { %{$our_attrs}, %{$attrs} };
+ # merge new where and having into old
my $where = (@_
? ((@_ == 1 || ref $_[0] eq "HASH")
? shift
: {@_}))
: undef());
if (defined $where) {
- $our_attrs->{where} = (defined $our_attrs->{where}
- $attrs->{where} = (defined $attrs->{where}
++ $new_attrs->{where} = (defined $new_attrs->{where}
? { '-and' =>
[ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
- $where, $our_attrs->{where} ] }
- $where, $attrs->{where} ] }
++ $where, $new_attrs->{where} ] }
: $where);
}
if (defined $having) {
- $our_attrs->{having} = (defined $our_attrs->{having}
- $attrs->{having} = (defined $attrs->{having}
++ $new_attrs->{having} = (defined $new_attrs->{having}
? { '-and' =>
[ map { ref $_ eq 'ARRAY' ? [ -or => $_ ] : $_ }
- $having, $our_attrs->{having} ] }
- $having, $attrs->{having} ] }
++ $having, $new_attrs->{having} ] }
: $having);
}
- # use Data::Dumper; warn "attrs: " . Dumper($our_attrs);
- my $rs = (ref $self)->new($self->result_source, $our_attrs);
- my $rs = (ref $self)->new($self->result_source, $attrs);
++ my $rs = (ref $self)->new($self->result_source, $new_attrs);
+ $rs->{_parent_rs} = $self->{_parent_rs} if ($self->{_parent_rs}); #XXX - hack to pass through parent of related resultsets
unless (@_) { # no search, effectively just a clone
my $rows = $self->get_cache;
$row = $self->{stashed_row} = \@raw;
$tree = $self->_collapse_result($as, $row, $c_prefix);
}
- @$target = @final;
- @$target = (@final ? @final : [ {}, {} ]);
++ @$target = (@final ? @final : [ {}, {} ]);
+ # single empty result to indicate an empty prefetched has_many
}
-
return $info;
}
sub set_cache {
my ( $self, $data ) = @_;
$self->throw_exception("set_cache requires an arrayref")
- if ref $data ne 'ARRAY';
- my $result_class = $self->result_class;
- foreach( @$data ) {
- $self->throw_exception(
- "cannot cache object of type '$_', expected '$result_class'"
- ) if ref $_ ne $result_class;
- }
- if defined($data) && (ref $data ne 'ARRAY');
++ if defined($data) && (ref $data ne 'ARRAY');
$self->{all_cache} = $data;
}