X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSet.pm;h=ed0f1c709ba5c2ce081e936306178c49c54eca7d;hb=5e30af0dfa7bd294153381d376278ca287c666e3;hp=75090cc1f73252154c43595513b0fea05970b461;hpb=a754e0a91314b75378d86b8b12deaa2dfc607099;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 75090cc..ed0f1c7 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -135,7 +135,10 @@ call it as C. columns => [qw/name artistid/], }); -For a list of attributes that can be passed to C, see L. For more examples of using this function, see L. +For a list of attributes that can be passed to C, see +L. For more examples of using this function, see +L. For a complete +documentation for the first argument, see L. =cut @@ -779,7 +782,8 @@ sub _collapse_result { if (keys %collapse) { my %pri = map { ($_ => 1) } $self->result_source->primary_columns; foreach my $i (0 .. $#construct_as) { - if (delete $pri{$construct_as[$i]}) { + next if defined($construct_as[$i][0]); # only self table + if (delete $pri{$construct_as[$i][1]}) { push(@pri_index, $i); } last unless keys %pri; # short circuit (Johnny Five Is Alive!) @@ -790,14 +794,18 @@ sub _collapse_result { my %pri_vals = map { ($_ => $copy[$_]) } @pri_index; - my %const; + my @const_rows; do { # no need to check anything at the front, we always want the first row + + my %const; foreach my $this_as (@construct_as) { $const{$this_as->[0]||''}{$this_as->[1]} = shift(@copy); } + push(@const_rows, \%const); + } until ( # no pri_index => no collapse => drop straight out !@pri_index or @@ -821,17 +829,49 @@ sub _collapse_result { # THIS BIT STILL NEEDS TO DO THE COLLAPSE my $alias = $self->{attrs}{alias}; - my $info = [ {}, {} ]; - foreach my $key (keys %const) { - if (length $key && $key ne $alias) { - my $target = $info; - my @parts = split(/\./, $key); - foreach my $p (@parts) { - $target = $target->[1]->{$p} ||= []; + my $info = []; + + my %collapse_pos; + + my @const_keys; + + use Data::Dumper; + + foreach my $const (@const_rows) { + scalar @const_keys or do { + @const_keys = sort { length($a) <=> length($b) } keys %$const; + }; + foreach my $key (@const_keys) { + if (length $key) { + my $target = $info; + my @parts = split(/\./, $key); + my $cur = ''; + my $data = $const->{$key}; + foreach my $p (@parts) { + $target = $target->[1]->{$p} ||= []; + $cur .= ".${p}"; + if ($cur eq ".${key}" && (my @ckey = @{$collapse{$cur}||[]})) { + # collapsing at this point and on final part + my $pos = $collapse_pos{$cur}; + CK: foreach my $ck (@ckey) { + if (!defined $pos->{$ck} || $pos->{$ck} ne $data->{$ck}) { + $collapse_pos{$cur} = $data; + delete @collapse_pos{ # clear all positioning for sub-entries + grep { m/^\Q${cur}.\E/ } keys %collapse_pos + }; + push(@$target, []); + last CK; + } + } + } + if (exists $collapse{$cur}) { + $target = $target->[-1]; + } + } + $target->[0] = $data; + } else { + $info->[0] = $const->{$key}; } - $target->[0] = $const{$key}; - } else { - $info->[0] = $const{$key}; } } @@ -1273,9 +1313,10 @@ sub new_result { my %new = ( %{ $self->_remove_alias($values, $alias) }, %{ $self->_remove_alias($collapsed_cond, $alias) }, + -source_handle => $self->_source_handle ); - return $self->result_class->new(\%new,$self->_source_handle); + return $self->result_class->new(\%new); } # _collapse_cond @@ -1825,7 +1866,9 @@ Shortcut to include additional columns in the returned results - for example }); would return all CDs and include a 'name' column to the information -passed to object inflation +passed to object inflation. Note that the 'artist' is the name of the +column (or relationship) accessor, and 'name' is the name of the column +accessor in the related table. =head2 select @@ -1876,8 +1919,14 @@ Indicates additional column names for those added via L<+select>. =back -Indicates column names for object inflation. This is used in conjunction with -C contains one or more function or stored +Indicates column names for object inflation. That is, c< as > +indicates the name that the column can be accessed as via the +C method (or via the object accessor, B). It has nothing to do with the SQL code C< SELECT foo AS bar +>. + +The C< as > attribute is used in conjunction with C contains one or more function or stored procedure names: $rs = $schema->resultset('Employee')->search(undef, {