[Bast-commits] r3319 - in branches/DBIx-Class/bulk_create: lib/DBIx/Class t

jnapiorkowski at dev.catalyst.perl.org jnapiorkowski at dev.catalyst.perl.org
Thu May 17 21:03:37 GMT 2007


Author: jnapiorkowski
Date: 2007-05-17 21:03:35 +0100 (Thu, 17 May 2007)
New Revision: 3319

Modified:
   branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
   branches/DBIx-Class/bulk_create/t/101populate_rs.t
Log:
- void context only uses insert bulk now.
- added a few test for edge cases, like only one row to insert


Modified: branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
===================================================================
--- branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm	2007-05-17 12:56:49 UTC (rev 3318)
+++ branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm	2007-05-17 20:03:35 UTC (rev 3319)
@@ -1280,7 +1280,7 @@
 use Data::Dump qw/dump/;
 
 sub populate {
-  my ($self, $data) = @_;  #warn dump $self->result_source->primary_columns;
+  my ($self, $data) = @_;
   
   if(defined wantarray) {
     my @created;
@@ -1297,12 +1297,13 @@
 	## and relationships similarly to how schema->populate requires a first item
 	## of all the column names.
 	
-    my @names = grep { !$self->result_source->has_relationship($_) } keys %$first;
-	
+    my @names = grep { !ref $first->{$_} } keys %$first;
+	my @values = map { [ map {defined $_ ? $_ : $self->throw_exception("Undefined value for column!")} @$_{@names} ] } @$data;
+		
     $self->result_source->storage->insert_bulk(
 		$self->result_source, 
 		\@names, 
-		[map { [ map {defined $_ ? $_ : $self->throw_exception("Undefined value for column!")} @$_{@names} ] } @$data]
+		\@values,
 	);
 	
 	## Again we assume the first row has to define all the related resultsets
@@ -1310,18 +1311,31 @@
 	my @pks = $self->result_source->primary_columns;
 	
 	## Must have PKs to use this!!!
-	
+
 	foreach my $item (@$data)
 	{
 		## First we need to get a result for each
 		## We need to call populate for each relationship.
-		
+
 		foreach my $rel (@rels)
 		{
-			my $result = $self->find(map {{$_=>$item->{$_}} } @pks);
+			next unless $item->{$rel};
+			my $parent	= $self->find(map {{$_=>$item->{$_}} } @pks)
+			 || next;
 			
-			my @discard = $result->$rel->populate($item->{$rel});
-			#$result->$rel->populate($item->{$rel});
+			my $child	= $parent->$rel;
+
+			my $related = $child->result_source->resolve_condition(
+				
+				$parent->result_source->relationship_info($rel)->{cond},
+				$child,
+				$parent,
+				
+			);
+			
+			my $populate = [map {  {%$_, %$related} } @{$item->{$rel}}];
+
+			$child->populate( $populate );
 		}
 	}
 	

Modified: branches/DBIx-Class/bulk_create/t/101populate_rs.t
===================================================================
--- branches/DBIx-Class/bulk_create/t/101populate_rs.t	2007-05-17 12:56:49 UTC (rev 3318)
+++ branches/DBIx-Class/bulk_create/t/101populate_rs.t	2007-05-17 20:03:35 UTC (rev 3319)
@@ -25,7 +25,12 @@
 
 		]
 	  },
-	  { artistid=>6, name => 'Like I Give a Damn' }
+	  { artistid=>6, name => 'Like I Give a Damn' },
+	  
+	  { artistid => 7, name => 'bbbb', cds => [
+		  { title => 'xxxaaa' ,year => 2005 },
+		]
+	  },
 
 	] );
 	
@@ -45,23 +50,23 @@
 RETURN_VOID: {
 
 	$rs->populate( [
-	  { artistid => 7, name => 'Manufactured CrapB', cds => [ 
+	  { artistid => 8, name => 'Manufactured CrapB', cds => [ 
 		  { title => 'My First CDB', year => 2006 },
 		  { title => 'Yet More Tweeny-Pop crapB', year => 2007 },
 		] 
 	  },
-	  { artistid => 8, name => 'Angsty-Whiny GirlB', cds => [
+	  { artistid => 9, name => 'Angsty-Whiny GirlB', cds => [
 		  { title => 'My parents sold me to a record companyB' ,year => 2005 },
 		  { title => 'Why Am I So Ugly?B', year => 2006 },
 		  { title => 'I Got Surgery and am now PopularB', year => 2007 }
 
 		]
 	  },
-	  {artistid=>9,  name => 'XXXX' }
+	  {artistid=>10,  name => 'XXXX' }
 
 	] );
 	
-	my $artist = $rs->find(7);
+	my $artist = $rs->find(8);
 
 	ok($artist, 'Found artist');
 	is($artist->name, 'Manufactured CrapB');
@@ -75,7 +80,7 @@
 	is($cds[1]->title, 'Yet More Tweeny-Pop crapB', 'Another crap CD');
 	is($cds[1]->year,  2007, 'Published in 2007');
 
-	$artist = $rs->find(8);
+	$artist = $rs->find(9);
 	ok($artist, 'Found artist');
 	is($artist->name, 'Angsty-Whiny GirlB');
 	is($artist->cds->count, 3, 'Has CDs');




More information about the Bast-commits mailing list