From: John Napiorkowski Date: Mon, 14 May 2007 22:45:29 +0000 (+0000) Subject: First go at supporting the populate syntax for resultset objects. Got non void conte... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=71d496fe3a12c3644ef0628243d34b49cb701556;p=dbsrgits%2FDBIx-Class-Historic.git 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. --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index f5b65d2..1c3342b 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1240,6 +1240,70 @@ sub delete_all { 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 in L is used +to insert the data, as this is a fast method. + +Otherwise, each set of data is inserted into the database using +L, 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 diff --git a/t/101populate_rs.t b/t/101populate_rs.t index 9e1d2e1..17cfe3b 100644 --- a/t/101populate_rs.t +++ b/t/101populate_rs.t @@ -10,56 +10,90 @@ plan tests => 18; 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 } - - ] - }, - { name => 'Like I Give a Damn' } - -] ); - -my $artist = $rs->find(4); - -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 First CD', 'A CD'); -is($cds[0]->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'); - -$artist = $rs->find(5); -ok($artist, 'Found artist'); -is($artist->name, 'Angsty-Whiny Girl'); -is($artist->cds->count, 3, 'Has CDs'); - -@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[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'); +RETURN_RESULTSETS: { + + 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' } + + ] ); + + 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"); +} + +RETURN_VOID: { + + $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); + + 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 First CD', 'A CD'); + is($cds[0]->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'); + + $artist = $rs->find(5); + ok($artist, 'Found artist'); + is($artist->name, 'Angsty-Whiny Girl'); + is($artist->cds->count, 3, 'Has CDs'); + + @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[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'); +}