[Bast-commits] r3390 - in branches/DBIx-Class/bulk_create:
lib/DBIx/Class t
jnapiorkowski at dev.catalyst.perl.org
jnapiorkowski at dev.catalyst.perl.org
Mon May 28 23:42:18 GMT 2007
Author: jnapiorkowski
Date: 2007-05-28 23:42:17 +0100 (Mon, 28 May 2007)
New Revision: 3390
Modified:
branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
branches/DBIx-Class/bulk_create/t/101populate_rs.t
Log:
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.
Modified: branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
===================================================================
--- branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm 2007-05-28 21:57:08 UTC (rev 3389)
+++ branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm 2007-05-28 22:42:17 UTC (rev 3390)
@@ -1244,7 +1244,7 @@
=over 4
-=item Arguments: $source_name, \@data;
+=item Arguments: \@data;
=back
@@ -1280,18 +1280,18 @@
## 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 @@
} 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;
+ 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(
+ ## 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,
+ );
- $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;
+ }
+ }
- 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 @@
\@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,
Modified: branches/DBIx-Class/bulk_create/t/101populate_rs.t
===================================================================
--- branches/DBIx-Class/bulk_create/t/101populate_rs.t 2007-05-28 21:57:08 UTC (rev 3389)
+++ branches/DBIx-Class/bulk_create/t/101populate_rs.t 2007-05-28 22:42:17 UTC (rev 3390)
@@ -516,8 +516,6 @@
## 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?
-
}
More information about the Bast-commits
mailing list