From: Dagfinn Ilmari Mannsåker Date: Fri, 1 Aug 2014 14:17:21 +0000 (+0100) Subject: Fix $rs->populate with column name array but no row data X-Git-Tag: v0.082800~83 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=277e30142fc9da75bd323bc6b21267aa32523287;p=dbsrgits%2FDBIx-Class.git Fix $rs->populate with column name array but no row data Two regressions for both context types snuck in during the the big populate() rewrite d0cefd99 - In scalar context the return value incorrectly went from () to [] - In void context an empty $data was handed to ::Storage::_insert_bulk --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 97dfe78..cdbbd6c 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2248,6 +2248,8 @@ sub populate { my (@results, $guard); if (ref $data->[0] eq 'ARRAY') { + # column names only, nothing to do + return if @$data == 1; $guard = $self->result_source->schema->storage->txn_scope_guard if @$data > 2; @@ -2290,6 +2292,8 @@ sub populate { # positional(!) explicit column list if ($i == 0) { + # column names only, nothing to do + return if @$data == 1; $colinfo->{$data->[0][$_]} = { pos => $_, name => $data->[0][$_] } and push @$colnames, $data->[0][$_] for 0 .. $#{$data->[0]}; diff --git a/t/100populate.t b/t/100populate.t index e7cb830..fa07ba5 100644 --- a/t/100populate.t +++ b/t/100populate.t @@ -383,6 +383,7 @@ lives_ok { } 'literal+bind with semantically identical attrs works after normalization'; # test all kinds of population with stringified objects +# or with empty sets warnings_like { local $ENV{DBIC_RT79576_NOWARN}; @@ -438,10 +439,15 @@ warnings_like { [qw( rank name )], [ $rank, $fn ], ]}, + + 'empty set' => { AoA => [ + [qw( name rank )], + ]}, }; # generate the AoH equivalent based on the AoAs above for my $bag (values %$args) { + $bag->{AoH} = []; my @hdr = @{$bag->{AoA}[0]}; for my $v ( @{$bag->{AoA}}[1..$#{$bag->{AoA}}] ) { push @{$bag->{AoH}}, my $h = {}; diff --git a/t/101populate_rs.t b/t/101populate_rs.t index a592e56..5686c3e 100644 --- a/t/101populate_rs.t +++ b/t/101populate_rs.t @@ -13,6 +13,7 @@ use warnings; use Test::More; use Test::Warn; +use Test::Exception; use lib qw(t/lib); use DBICTest; @@ -704,6 +705,26 @@ ARRAYREF_OF_ARRAYREF_STYLE: { } } -ok(eval { $art_rs->populate([]); 1 }, "Empty populate runs but does nothing"); +EMPTY_POPULATE: { + foreach( + [ empty => [] ], + [ columns_only => [ [qw(name rank charfield)] ] ], + ) { + my ($desc, $arg) = @{$_}; + + $schema->is_executed_sql_bind( sub { + + my $rs = $art_rs; + lives_ok { $rs->populate($arg); 1 } "$desc populate in void context lives"; + + my @r = $art_rs->populate($arg); + is_deeply( \@r, [], "$desc populate in list context returns empty list" ); + + my $r = $art_rs->populate($arg); + is( $r, undef, "$desc populate in scalar context returns undef" ); + + }, [], "$desc populate executed no statements" ); + } +} done_testing;