From: Matt Phillips Date: Fri, 8 Nov 2013 22:36:20 +0000 (-0500) Subject: first parse, _normalize_populate_args X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=53d89fc72e729d280d2d8d48d3a862b5ed20dddb;p=dbsrgits%2FDBIx-Class.git first parse, _normalize_populate_args --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 222e175..5fa0d04 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2326,28 +2326,54 @@ sub populate { # populate() arguments went over several incarnations -# What we ultimately support is AoH +# can be any mixture of : +# 1. AoA +# 2. AoH +# 3. AoS(calar) followed buy Arrayrefref (\@cols, $rs->as_query) +# 4. coderef (tuple generator) sub _normalize_populate_args { - my ($self, $arg) = @_; + my ($self, $args) = @_; - if (ref $arg eq 'ARRAY') { - if (!@$arg) { - return []; - } - elsif (ref $arg->[0] eq 'HASH') { - return $arg; - } - elsif (ref $arg->[0] eq 'ARRAY') { - my @ret; - my @colnames = @{$arg->[0]}; - foreach my $values (@{$arg}[1 .. $#$arg]) { - push @ret, { map { $colnames[$_] => $values->[$_] } (0 .. $#colnames) }; + use DDP; + my @normalized; + +ARG: for (my $idx = 0; $idx < $#$args; $idx++) { + my $arg = $args->[$idx]; + + if (ref $arg eq 'ARRAY') { + # AoH + if (ref $arg eq 'ARRAY' && @$arg > 0 && ref $arg->[0] eq 'HASH') { + push @normalized, $arg; + next ARG; + } + # AoA + elsif (ref $arg eq 'ARRAY' && @$arg > 0 && ref $arg->[0] eq 'ARRAY') { + my @ret; + my @colnames = @{$arg->[0]}; + foreach my $values (@{$arg}[1 .. $#$arg]) { + push @ret, { map { $colnames[$_] => $values->[$_] } (0 .. $#colnames) }; + } + push @normalized, \@ret; + next ARG; + } + # AoS, Arrayrefref (subq) + elsif (ref $arg eq 'ARRAY' && ref $arg->[0] eq '') { + push @normalized, $arg, $args->[$idx+1]; + $idx += 1; # we are consuming the next element, skip it next time + } + # Coderef + elsif (ref $arg eq 'CODE') { + push @normalized, $arg; } - return \@ret; } } - $self->throw_exception('Populate expects an arrayref of hashrefs or arrayref of arrayrefs'); + if (scalar @normalized == 0) { + $self->throw_exception('Populate expects some combination of arrayref of hashrefs, arrayref of arrayrefs, array of cols followed by arrayrefref or coderef'); + } + + p @normalized; + return \@normalized; } =head2 pager diff --git a/t/100populate.t b/t/100populate.t index 177231a..94a9737 100644 --- a/t/100populate.t +++ b/t/100populate.t @@ -23,7 +23,7 @@ my $schema = DBICTest->init_schema(); # [ 10000, "ntn" ], my $start_id = 'populateXaaaaaa'; -my $rows = 10_000; +my $rows = 10; my $offset = 3; $schema->populate('Artist', [ [ qw/artistid name/ ], map { [ ($_ + $offset) => $start_id++ ] } shuffle ( 1 .. $rows ) ] ); @@ -446,3 +446,18 @@ lives_ok ( sub { }, 'empty has_many relationship accepted by populate'); done_testing; + +my $artist_rs = $schema->resultset('Artist'); +my $artist_src = $artist_rs->result_source; + +my $q = $schema->resultset('CD')->search({}, { columns => [qw/artist year/], group_by => 'artist' })->as_query; +my $count = 0; +my $name = "zaaaaa"; + +#$schema->storage->insert_bulk($artist_src, [qw/name rank/], [[[qw/foo 1/], [qw/baz 2/]], sub { +# return [$name++, ++$count] unless $count > 5; +# return undef; +#}, $q, [[qw/asdf 5/], [qw/uhiuh 6/]]]); + +my @cols = $artist_src->columns; +$rs->populate(\@cols, $rs->search(undef)->as_query )