# 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
# [ 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 ) ] );
}, '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 )