X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSet.pm;h=cdb2cf33064fcd77665e3c43cf52b48859fe8771;hb=daeb1865acd969682c941fffd2ce51d9238d9cad;hp=3805812baac33d94ec9666d69807727f5e265179;hpb=336feb8ef85d16d22ae0b131b0c7a85abc9e9435;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 3805812..cdb2cf3 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -57,7 +57,12 @@ represents. The query that the ResultSet represents is B executed against the database when these methods are called: -L L L L L L +L, L, L, L, L, L. + +If a resultset is used in a numeric context it returns the L. +However, if it is used in a boolean context it is B true. So if +you want to check if a resultset has any results, you must use C. =head1 EXAMPLES @@ -101,7 +106,7 @@ attributes with the same keys need resolving. L, L, L, L attributes are merged into the existing ones from the original resultset. -The L, L attribute, and any search conditions are +The L and L attributes, and any search conditions, are merged with an SQL C to the existing condition from the original resultset. @@ -142,13 +147,6 @@ Which is the same as: See: L, L, L, L, L. -=head1 OVERLOADING - -If a resultset is used in a numeric context it returns the L. -However, if it is used in a boolean context it is always true. So if -you want to check if a resultset has any results use C. -C will always be true. - =head1 METHODS =head2 new @@ -199,7 +197,6 @@ sub new { my $self = { _source_handle => $source, cond => $attrs->{where}, - count => undef, pager => undef, attrs => $attrs }; @@ -538,8 +535,8 @@ sub find { : $self->_add_alias($input_query, $alias); } - # Run the query - my $rs = $self->search ($query, $attrs); + # Run the query, passing the result_class since it should propagate for find + my $rs = $self->search ($query, {result_class => $self->result_class, %$attrs}); if (keys %{$rs->_resolved_attrs->{collapse}}) { my $row = $rs->next; carp "Query returned more than one row" if $rs->next; @@ -1138,9 +1135,14 @@ in the original source class will not run. sub result_class { my ($self, $result_class) = @_; if ($result_class) { - $self->ensure_class_loaded($result_class); + unless (ref $result_class) { # don't fire this for an object + $self->ensure_class_loaded($result_class); + } $self->_result_class($result_class); - $self->{attrs}{result_class} = $result_class if ref $self; + # THIS LINE WOULD BE A BUG - this accessor specifically exists to + # permit the user to set result class on one result set only; it only + # chains if provided to search() + #$self->{attrs}{result_class} = $result_class if ref $self; } $self->_result_class; } @@ -1235,14 +1237,12 @@ sub _count_rs { my $rsrc = $self->result_source; $attrs ||= $self->_resolved_attrs; - # only take pieces we need for a simple count - my $tmp_attrs = { map - { $_ => $attrs->{$_} } - qw/ alias from where bind join / - }; + my $tmp_attrs = { %$attrs }; + # take off any limits, record_filter is cdbi, and no point of ordering nor locking a count + delete @{$tmp_attrs}{qw/rows offset order_by record_filter for/}; # overwrite the selector (supplied by the storage) - $tmp_attrs->{select} = $rsrc->storage->_count_select ($rsrc, $tmp_attrs); + $tmp_attrs->{select} = $rsrc->storage->_count_select ($rsrc, $attrs); $tmp_attrs->{as} = 'count'; my $tmp_rs = $rsrc->resultset_class->new($rsrc, $tmp_attrs)->get_column ('count'); @@ -1259,10 +1259,9 @@ sub _count_subq_rs { my $rsrc = $self->result_source; $attrs ||= $self->_resolved_attrs; - my $sub_attrs = { map - { $_ => $attrs->{$_} } - qw/ alias from where bind join group_by having rows offset / - }; + my $sub_attrs = { %$attrs }; + # extra selectors do not go in the subquery and there is no point of ordering it, nor locking it + delete @{$sub_attrs}{qw/collapse select _prefetch_select as order_by for/}; # if we multi-prefetch we group_by primary keys only as this is what we would # get out of the rs via ->next/->all. We *DO WANT* to clobber old group_by regardless @@ -1289,16 +1288,11 @@ sub _count_subq_rs { $sub_attrs->{select} = @pcols ? \@pcols : [ 1 ]; } - - # this is so that the query can be simplified e.g. - # * ordering can be thrown away in things like Top limit - $sub_attrs->{-for_count_only} = 1; - return $rsrc->resultset_class ->new ($rsrc, $sub_attrs) ->as_subselect_rs ->search ({}, { columns => { count => $rsrc->storage->_count_select ($rsrc, $attrs) } }) - -> get_column ('count'); + ->get_column ('count'); } sub _bool { @@ -1429,14 +1423,15 @@ sub _rs_update_delete { my $cond = $rsrc->schema->storage->_strip_cond_qualifiers ($self->{cond}); my $needs_group_by_subq = $self->_has_resolved_attr (qw/collapse group_by -join/); - my $needs_subq = $needs_group_by_subq || (not defined $cond) || $self->_has_resolved_attr(qw/row offset/); + my $needs_subq = $needs_group_by_subq || (not defined $cond) || $self->_has_resolved_attr(qw/rows offset/); if ($needs_group_by_subq or $needs_subq) { # make a new $rs selecting only the PKs (that's all we really need) my $attrs = $self->_resolved_attrs_copy; - delete $attrs->{$_} for qw/collapse select as/; + + delete $attrs->{$_} for qw/collapse _collapse_order_by select _prefetch_select as/; $attrs->{columns} = [ map { "$attrs->{alias}.$_" } ($self->result_source->_pri_cols) ]; if ($needs_group_by_subq) { @@ -1470,7 +1465,6 @@ sub _rs_update_delete { } my $subrs = (ref $self)->new($rsrc, $attrs); - return $self->result_source->storage->_subq_update_delete($subrs, $op, $values); } else { @@ -2684,15 +2678,22 @@ sub as_subselect_rs { my $attrs = $self->_resolved_attrs; - return $self->result_source->resultset->search( undef, { + my $fresh_rs = (ref $self)->new ( + $self->result_source + ); + + # these pieces will be locked in the subquery + delete $fresh_rs->{cond}; + delete @{$fresh_rs->{attrs}}{qw/where bind/}; + + return $fresh_rs->search( {}, { from => [{ $attrs->{alias} => $self->as_query, -alias => $attrs->{alias}, -source_handle => $self->result_source->handle, }], - map { $_ => $attrs->{$_} } qw/select as alias/ - - }); + alias => $attrs->{alias}, + }); } # This code is called by search_related, and makes sure there