From: Luke Saunders Date: Thu, 11 May 2006 12:21:49 +0000 (+0000) Subject: Merge 'DBIx-Class-current' into 'resultset-new-refactor' X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=cf72e3cfca5c90ef2650c4b6273168a648f52178;p=dbsrgits%2FDBIx-Class-Historic.git Merge 'DBIx-Class-current' into 'resultset-new-refactor' merged recent -current changes into this branch --- cf72e3cfca5c90ef2650c4b6273168a648f52178 diff --cc lib/DBIx/Class/ResultSet.pm index 8916f56,5b2473a..93588e0 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@@ -136,26 -196,32 +136,47 @@@ call it as C 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 @@@ -165,24 -231,22 +186,23 @@@ : {@_})) : 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; @@@ -700,8 -687,10 +724,9 @@@ sub _collapse_result $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; } @@@ -1325,13 -1308,7 +1349,7 @@@ than re-querying the database even if t 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; } diff --cc t/run/01core.tl index 68c2eac,5287124..0c54d42 --- a/t/run/01core.tl +++ b/t/run/01core.tl @@@ -1,7 -1,7 +1,7 @@@ sub run_tests { my $schema = shift; --plan tests => 57; ++plan tests => 59; # figure out if we've got a version of sqlite that is older than 3.2.6, in # which case COUNT(DISTINCT()) doesn't work