From: John Napiorkowski Date: Mon, 28 May 2007 22:42:17 +0000 (+0000) Subject: Added hack to force wantarray mode if bulk insert can't complete due to missing relat... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4adcfc20c2acf62efe7b1fd46a27915112307fef;p=dbsrgits%2FDBIx-Class-Historic.git Added hack to force wantarray mode if bulk insert can't complete due to missing relating keys in the data. Updated tests to reflect above. --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 111e038..246c7f9 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1244,7 +1244,7 @@ sub delete_all { =over 4 -=item Arguments: $source_name, \@data; +=item Arguments: \@data; =back @@ -1280,18 +1280,18 @@ Example: Assuming an Artist Class that has many CDs Classes relating: ## Array Context Example my ($ArtistOne, $ArtistTwo, $ArtistThree) = $Artist_rs->populate([ { name => "Artist One"}, - { name => "Artist Two"}, - { name => "Artist Three", cds=> [ - { title => "First CD", year => 2007}, - { title => "Second CD", year => 2008}, - ]} + { name => "Artist Two"}, + { name => "Artist Three", cds=> [ + { title => "First CD", year => 2007}, + { title => "Second CD", year => 2008}, + ]} ]); print $ArtistOne->name; ## response is 'Artist One' print $ArtistThree->cds->count ## reponse is '2' =cut -use Data::Dump qw/dump/; + sub populate { my ($self, $data) = @_; @@ -1304,35 +1304,35 @@ sub populate { } else { my ($first, @rest) = @$data; - my @names = grep {!ref $first->{$_}} keys %$first; + my @names = grep {!ref $first->{$_}} keys %$first; my @rels = grep { $self->result_source->has_relationship($_) } keys %$first; - my @pks = $self->result_source->primary_columns; - - ## do the belongs_to relationships - foreach my $index (0..$#{@$data}) - { - foreach my $rel (@rels) - { - next unless $data->[$index]->{$rel} && ref $data->[$index]->{$rel} eq "HASH"; - - my $result = $self->related_resultset($rel)->create($data->[$index]->{$rel}); - - my ($reverse) = keys %{$self->result_source->reverse_relationship_info($rel)}; - - my $related = $result->result_source->resolve_condition( - - $result->result_source->relationship_info($reverse)->{cond}, - $self, - $result, - ); - - delete $data->[$index]->{$rel}; - $data->[$index] = {%{$data->[$index]}, %$related}; - - push @names, keys %$related if $index == 0; - } - } - + my @pks = $self->result_source->primary_columns; + + ## do the belongs_to relationships + foreach my $index (0..$#{@$data}) { + if( grep { !defined $data->[$index]->{$_} } @pks ) { + my @ret = $self->populate($data); + return; + } + + foreach my $rel (@rels) { + next unless $data->[$index]->{$rel} && ref $data->[$index]->{$rel} eq "HASH"; + my $result = $self->related_resultset($rel)->create($data->[$index]->{$rel}); + my ($reverse) = keys %{$self->result_source->reverse_relationship_info($rel)}; + my $related = $result->result_source->resolve_condition( + $result->result_source->relationship_info($reverse)->{cond}, + $self, + $result, + ); + + delete $data->[$index]->{$rel}; + $data->[$index] = {%{$data->[$index]}, %$related}; + + push @names, keys %$related if $index == 0; + } + } + + ## do bulk insert on current row my @values = map { [ map { defined $_ ? $_ : $self->throw_exception("Undefined value for column!") @@ -1345,15 +1345,17 @@ sub populate { \@values, ); - ## do the has_many relationships + ## do the has_many relationships foreach my $item (@$data) { foreach my $rel (@rels) { next unless $item->{$rel} && ref $item->{$rel} eq "ARRAY"; - my $parent = $self->find(map {{$_=>$item->{$_}} } @pks) || next; + my $parent = $self->find(map {{$_=>$item->{$_}} } @pks) + || $self->throw_exception('Cannot find the relating object.'); + my $child = $parent->$rel; - + my $related = $child->result_source->resolve_condition( $parent->result_source->relationship_info($rel)->{cond}, $child, diff --git a/t/101populate_rs.t b/t/101populate_rs.t index aedff87..7362397 100644 --- a/t/101populate_rs.t +++ b/t/101populate_rs.t @@ -516,8 +516,6 @@ SHOULD_CAUSE_ERRORS: { ## changing columns ## basically errors for non well formed data ## check for the first incomplete problem - ## can we solve the problem of void context and no PKs? - }