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

jnapiorkowski at dev.catalyst.perl.org jnapiorkowski at dev.catalyst.perl.org
Mon May 14 23:45:29 GMT 2007


Author: jnapiorkowski
Date: 2007-05-14 23:45:29 +0100 (Mon, 14 May 2007)
New Revision: 3314

Modified:
   branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
   branches/DBIx-Class/bulk_create/t/101populate_rs.t
Log:
First go at supporting the populate syntax for resultset objects.  Got non void context worked, but insert_bulk not done yet.  Updated the test for this to test both void and nonvoid context.

Modified: branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm
===================================================================
--- branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm	2007-05-13 00:56:19 UTC (rev 3313)
+++ branches/DBIx-Class/bulk_create/lib/DBIx/Class/ResultSet.pm	2007-05-14 22:45:29 UTC (rev 3314)
@@ -1240,6 +1240,70 @@
   return 1;
 }
 
+=head2 populate
+
+=over 4
+
+=item Arguments: $source_name, \@data;
+
+=back
+
+Pass an arrayref of hashrefs. Each hashref should be a structure suitable for
+submitting to a $resultset->create(...) method.
+
+In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
+to insert the data, as this is a fast method.
+
+Otherwise, each set of data is inserted into the database using
+L<DBIx::Class::ResultSet/create>, and a arrayref of the resulting row
+objects is returned.
+
+i.e.,
+
+  $rs->populate( [
+	  { artistid => 4, name => 'Manufactured Crap', cds => [ 
+		  { title => 'My First CD', year => 2006 },
+		  { title => 'Yet More Tweeny-Pop crap', year => 2007 },
+		] 
+	  },
+	  { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
+		  { title => 'My parents sold me to a record company' ,year => 2005 },
+		  { title => 'Why Am I So Ugly?', year => 2006 },
+		  { title => 'I Got Surgery and am now Popular', year => 2007 }
+		]
+	  },
+	  { name => 'Like I Give a Damn' }
+
+	] );
+
+=cut
+use Data::Dump qw/dump/;
+
+sub populate {
+  my ($self, $data) = @_;
+  
+  if(defined wantarray) {
+    my @created;
+    foreach my $item (@$data) {
+      push(@created, $self->create($item));
+    }
+    return @created;
+  } 
+  else
+  {
+    my ($first, @rest) = @$data;
+    my @names = keys %$first;
+	
+	warn dump keys %$_ for @$data;
+	
+	#@$data = map { my %unit = %$_; warn dump @unit{qw/cds artistid/}; warn dump %unit; } @$data;
+	
+	die "Void mode not supported yet :)";
+	
+    #$self->result_source->storage->insert_bulk($self->result_source, \@names, $data);
+  }
+}
+
 =head2 pager
 
 =over 4

Modified: branches/DBIx-Class/bulk_create/t/101populate_rs.t
===================================================================
--- branches/DBIx-Class/bulk_create/t/101populate_rs.t	2007-05-13 00:56:19 UTC (rev 3313)
+++ branches/DBIx-Class/bulk_create/t/101populate_rs.t	2007-05-14 22:45:29 UTC (rev 3314)
@@ -10,56 +10,90 @@
 my $schema = DBICTest->init_schema();
 my $rs = $schema->resultset('Artist');
 
-$rs->populate( [
-  { artistid => 4, name => 'Manufactured Crap', cds => [ 
-      { title => 'My First CD', year => 2006 },
-      { title => 'Yet More Tweeny-Pop crap', year => 2007 },
-    ] 
-  },
-  { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
-      { title => 'My parents sold me to a record company' ,year => 2005 },
-      { title => 'Why Am I So Ugly?', year => 2006 },
-      { title => 'I Got Surgery and am now Popular', year => 2007 }
+RETURN_RESULTSETS: {
 
-    ]
-  },
-  { name => 'Like I Give a Damn' }
+	my ($crap, $girl) = $rs->populate( [
+	  { artistid => 4, name => 'Manufactured Crap', cds => [ 
+		  { title => 'My First CD', year => 2006 },
+		  { title => 'Yet More Tweeny-Pop crap', year => 2007 },
+		] 
+	  },
+	  { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
+		  { title => 'My parents sold me to a record company' ,year => 2005 },
+		  { title => 'Why Am I So Ugly?', year => 2006 },
+		  { title => 'I Got Surgery and am now Popular', year => 2007 }
 
-] );
+		]
+	  },
+	  { name => 'Like I Give a Damn' }
 
-my $artist = $rs->find(4);
+	] );
+	
+	isa_ok( $crap, 'DBICTest::Artist', "Got 'Artist'");
+	isa_ok( $girl, 'DBICTest::Artist', "Got 'Artist'");
+	
+	ok( $crap->name eq 'Manufactured Crap', "Got Correct name for result object");
+	ok( $girl->name eq 'Angsty-Whiny Girl', "Got Correct name for result object");
+	
+	use Data::Dump qw/dump/;
+	
+	ok( $crap->cds->count == 2, "got Expected Number of Cds");
+	ok( $girl->cds->count == 3, "got Expected Number of Cds");
+}
 
-ok($artist, 'Found artist');
-is($artist->name, 'Manufactured Crap');
-is($artist->cds->count, 2, 'Has CDs');
+RETURN_VOID: {
 
-my @cds = $artist->cds;
+	$rs->populate( [
+	  { artistid => 4, name => 'Manufactured Crap', cds => [ 
+		  { title => 'My First CD', year => 2006 },
+		  { title => 'Yet More Tweeny-Pop crap', year => 2007 },
+		] 
+	  },
+	  { artistid => 5, name => 'Angsty-Whiny Girl', cds => [
+		  { title => 'My parents sold me to a record company' ,year => 2005 },
+		  { title => 'Why Am I So Ugly?', year => 2006 },
+		  { title => 'I Got Surgery and am now Popular', year => 2007 }
 
-is($cds[0]->title, 'My First CD', 'A CD');
-is($cds[0]->year,  2006, 'Published in 2006');
+		]
+	  },
+	  { name => 'Like I Give a Damn' }
 
-is($cds[1]->title, 'Yet More Tweeny-Pop crap', 'Another crap CD');
-is($cds[1]->year,  2007, 'Published in 2007');
+	] );
 
-$artist = $rs->find(5);
-ok($artist, 'Found artist');
-is($artist->name, 'Angsty-Whiny Girl');
-is($artist->cds->count, 3, 'Has CDs');
+	my $artist = $rs->find(4);
 
- at cds = $artist->cds;
+	ok($artist, 'Found artist');
+	is($artist->name, 'Manufactured Crap');
+	is($artist->cds->count, 2, 'Has CDs');
 
+	my @cds = $artist->cds;
 
-is($cds[0]->title, 'My parents sold me to a record company', 'A CD');
-is($cds[0]->year,  2005, 'Published in 2005');
+	is($cds[0]->title, 'My First CD', 'A CD');
+	is($cds[0]->year,  2006, 'Published in 2006');
 
-is($cds[1]->title, 'Why Am I So Ugly?', 'A Coaster');
-is($cds[1]->year,  2006, 'Published in 2006');
+	is($cds[1]->title, 'Yet More Tweeny-Pop crap', 'Another crap CD');
+	is($cds[1]->year,  2007, 'Published in 2007');
 
-is($cds[2]->title, 'I Got Surgery and am now Popular', 'Selling un-attainable dreams');
-is($cds[2]->year,  2007, 'Published in 2007');
+	$artist = $rs->find(5);
+	ok($artist, 'Found artist');
+	is($artist->name, 'Angsty-Whiny Girl');
+	is($artist->cds->count, 3, 'Has CDs');
 
-$artist = $rs->search({name => 'Like I Give A Damn'})->single;
-ok($artist);
+	@cds = $artist->cds;
 
-is($artist->cds->count, 0, 'No CDs');
 
+	is($cds[0]->title, 'My parents sold me to a record company', 'A CD');
+	is($cds[0]->year,  2005, 'Published in 2005');
+
+	is($cds[1]->title, 'Why Am I So Ugly?', 'A Coaster');
+	is($cds[1]->year,  2006, 'Published in 2006');
+
+	is($cds[2]->title, 'I Got Surgery and am now Popular', 'Selling un-attainable dreams');
+	is($cds[2]->year,  2007, 'Published in 2007');
+
+	$artist = $rs->search({name => 'Like I Give A Damn'})->single;
+	ok($artist);
+
+	is($artist->cds->count, 0, 'No CDs');
+}
+




More information about the Bast-commits mailing list