From: John Napiorkowski Date: Fri, 19 Dec 2008 20:48:35 +0000 (+0000) Subject: altered schema->populate so that it is a very thin wrapper on top of resultset->popul... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c4e67d31ed6f3fb01c07451cdf69c0782bc610a2;p=dbsrgits%2FDBIx-Class-Historic.git altered schema->populate so that it is a very thin wrapper on top of resultset->populate and changed resultset_populate so that is accepted both the arrayref of hashes and the arrayref of arrayref style of args. Documented this, updated the tests a bit to make sure it is all good. --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 17d05f5..7ef6307 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1362,8 +1362,9 @@ sub delete_all { =back -Pass an arrayref of hashrefs. Each hashref should be a structure suitable for -submitting to a $resultset->create(...) method. +Accepts either an arrayref of hashrefs or alternatively an arrayref of arrayrefs. +For the arrayref of hashrefs style each hashref should be a structure suitable +forsubmitting to a $resultset->create(...) method. In void context, C in L is used to insert the data, as this is a faster method. @@ -1403,7 +1404,18 @@ Example: Assuming an Artist Class that has many CDs Classes relating: print $ArtistOne->name; ## response is 'Artist One' print $ArtistThree->cds->count ## reponse is '2' - + +For the arrayref of arrayrefs style, the first element should be a list of the +fieldsnames to which the remaining elements are rows being inserted. For +example: + + $Arstist_rs->populate([ + [qw/artistid name/], + [100, 'A Formally Unknown Singer'], + [101, 'A singer that jumped the shark two albums ago'], + [102, 'An actually cool singer.'], + ]); + Please note an important effect on your data when choosing between void and wantarray context. Since void context goes straight to C in L this will skip any component that is overriding @@ -1415,7 +1427,10 @@ values. =cut sub populate { - my ($self, $data) = @_; + my $self = shift @_; + my $data = ref $_[0][0] eq 'HASH' + ? $_[0] : ref $_[0][0] eq 'ARRAY' ? $self->_normalize_populate_args($_[0]) : + $self->throw_exception('Populate expects an arrayref of hashes or arrayref of arrayrefs'); if(defined wantarray) { my @created; @@ -1489,6 +1504,28 @@ sub populate { } } +=head2 _normalize_populate_args ($args) + +Private method used by L to normalize it's incoming arguments. Factored +out in case you want to subclass and accept new argument structures to the +L method. + +=cut + +sub _normalize_populate_args { + my ($self, $data) = @_; + my @names = @{shift(@$data)}; + my @results_to_create; + foreach my $datum (@$data) { + my %result_to_create; + foreach my $index (0..$#names) { + $result_to_create{$names[$index]} = $$datum[$index]; + } + push @results_to_create, \%result_to_create; + } + return \@results_to_create; +} + =head2 pager =over 4 diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index a65c0a9..cf054d0 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -707,26 +707,15 @@ wantarray context if you want the PKs automatically created. sub populate { my ($self, $name, $data) = @_; - my $rs = $self->resultset($name); - my @names = @{shift(@$data)}; - if(defined wantarray) { - my @created; - foreach my $item (@$data) { - my %create; - @create{@names} = @$item; - push(@created, $rs->create(\%create)); + if(my $rs = $self->resultset($name)) { + if(defined wantarray) { + return $rs->populate($data); + } else { + $rs->populate($data); } - return @created; - } - my @results_to_create; - foreach my $datum (@$data) { - my %result_to_create; - foreach my $index (0..$#names) { - $result_to_create{$names[$index]} = $$datum[$index]; - } - push @results_to_create, \%result_to_create; + } else { + $self->throw_exception("$name is not a resultset"); } - $rs->populate(\@results_to_create); } =head2 connection diff --git a/t/101populate_rs.t b/t/101populate_rs.t index 4eca3b5..89b9f41 100644 --- a/t/101populate_rs.t +++ b/t/101populate_rs.t @@ -15,7 +15,7 @@ use Test::More; use lib qw(t/lib); use DBICTest; -plan tests => 134; +plan tests => 142; ## ---------------------------------------------------------------------------- @@ -601,4 +601,30 @@ VOID_CONTEXT: { ok( $cd2->title eq "VOID_Yet More Tweeny-Pop crap", "Got Expected CD Title"); } +} + +ARRAYREF_OF_ARRAYREF_STYLE: { + $art_rs->populate([ + [qw/artistid name/], + [1000, 'A Formally Unknown Singer'], + [1001, 'A singer that jumped the shark two albums ago'], + [1002, 'An actually cool singer.'], + ]); + + ok my $unknown = $art_rs->find(1000), "got Unknown"; + ok my $jumped = $art_rs->find(1001), "got Jumped"; + ok my $cool = $art_rs->find(1002), "got Cool"; + + is $unknown->name, 'A Formally Unknown Singer', 'Correct Name'; + is $jumped->name, 'A singer that jumped the shark two albums ago', 'Correct Name'; + is $cool->name, 'An actually cool singer.', 'Correct Name'; + + my ($cooler, $lamer) = $art_rs->populate([ + [qw/artistid name/], + [1003, 'Cooler'], + [1004, 'Lamer'], + ]); + + is $cooler->name, 'Cooler', 'Correct Name'; + is $lamer->name, 'Lamer', 'Correct Name'; } \ No newline at end of file