[Bast-commits] r3413 - in branches/DBIx-Class-current: . lib/DBIx/Class t

matthewt at dev.catalyst.perl.org matthewt at dev.catalyst.perl.org
Tue May 29 02:07:41 GMT 2007


Author: matthewt
Date: 2007-05-29 02:07:40 +0100 (Tue, 29 May 2007)
New Revision: 3413

Modified:
   branches/DBIx-Class-current/
   branches/DBIx-Class-current/lib/DBIx/Class/ResultSet.pm
   branches/DBIx-Class-current/t/101populate_rs.t
Log:
 r51612 at cain (orig r3314):  jnapiorkowski | 2007-05-14 22:45:29 +0000
 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.



Property changes on: branches/DBIx-Class-current
___________________________________________________________________
Name: svk:merge
   - 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.bulk_create:29204
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.current:29201
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.oracle8:29250
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/bulk_create:3311
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/collapse_result_rewrite:3292
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/column_info_from_storage:2596
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/load_namespaces:2725
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/param_bind:3015
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/source-handle:2975
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/storage_exceptions:2617
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/versioning:2930
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3355
   + 168d5346-440b-0410-b799-f706be625ff1:/DBIx-Class-current:2207
462d4d0c-b505-0410-bf8e-ce8f877b3390:/local/bast/DBIx-Class:3159
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.bulk_create:29204
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.current:29201
78d5d833-3a7e-474d-aed6-cfba645156b5:/local/DBIx-Class.oracle8:29250
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-C3:318
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-joins:173
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class-resultset:570
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/bulk_create:3314
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/collapse_result_rewrite:3292
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/column_info_from_storage:2596
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/datetime:1716
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_compat:1855
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/find_unique_query_fixes:2142
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/inflate:1988
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/load_namespaces:2725
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/many_to_many:2025
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/param_bind:3015
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/re_refactor_bugfix:1944
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/reorganize_tests:1827
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset-new-refactor:1766
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_2_electric_boogaloo:2175
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/resultset_cleanup:2102
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/source-handle:2975
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/sqlt_tests_refactor:2043
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/storage_exceptions:2617
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/branches/DBIx-Class/versioning:2930
bd8105ee-0ff8-0310-8827-fb3f25b6796d:/trunk/DBIx-Class:3355

Modified: branches/DBIx-Class-current/lib/DBIx/Class/ResultSet.pm
===================================================================
--- branches/DBIx-Class-current/lib/DBIx/Class/ResultSet.pm	2007-05-29 01:07:34 UTC (rev 3412)
+++ branches/DBIx-Class-current/lib/DBIx/Class/ResultSet.pm	2007-05-29 01:07:40 UTC (rev 3413)
@@ -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-current/t/101populate_rs.t
===================================================================
--- branches/DBIx-Class-current/t/101populate_rs.t	2007-05-29 01:07:34 UTC (rev 3412)
+++ branches/DBIx-Class-current/t/101populate_rs.t	2007-05-29 01:07:40 UTC (rev 3413)
@@ -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