arrayrefs. The arrayrefs should contain a list of column names,
followed by one or many sets of matching data for the given columns.
-Each set of data is inserted into the database using
+In void context, C<insert_bulk> in L<DBIx::Class::Storage::DBI> is used
+to insert the data, as this is a fast method. However, insert_bulk currently
+assumes that your datasets all contain the same type of values, using scalar
+references in a column in one row, and not in another will probably not work.
+
+Otherwise, each set of data is inserted into the database using
L<DBIx::Class::ResultSet/create>, and a arrayref of the resulting row
objects is returned.
return $to_insert;
}
+## Still not quite perfect, and EXPERIMENTAL
+## Currently it is assumed that all values passed will be "normal", i.e. not
+## scalar refs, or at least, all the same type as the first set, the statement is
+## only prepped once.
sub insert_bulk {
my ($self, $table, $cols, $data) = @_;
- my ($sql, @bind) = $self->sql_maker->insert($table, @$data);
+ my %colvalues;
+ @colvalues{@$cols} = (0..$#$cols);
+ my ($sql, @bind) = $self->sql_maker->insert($table, \%colvalues);
+# print STDERR "BIND".Dumper(\@bind);
if ($self->debug) {
my @debug_bind = map { defined $_ ? qq{'$_'} : q{'NULL'} } @bind;
# @bind = map { ref $_ ? ''.$_ : $_ } @bind; # stringify args
my $rv;
+ ## This must be an arrayref, else nothing works!
+ my $tuple_status = [];
+# use Data::Dumper;
+# print STDERR Dumper($data);
if ($sth) {
my $time = time();
- $rv = eval { $sth->execute_array({ ArrayTupleFetch => sub { return shift @$data; }}) };
-
- if ($@ || !$rv) {
- $self->throw_exception("Error executing '$sql': ".($@ || $sth->errstr));
+ $rv = eval { $sth->execute_array({ ArrayTupleFetch => sub { my $values = shift @$data; return if !$values; return [ @{$values}[@bind] ]},
+ ArrayTupleStatus => $tuple_status }) };
+# print STDERR Dumper($tuple_status);
+# print STDERR "RV: $rv\n";
+ if ($@ || !defined $rv) {
+ my $errors = '';
+ foreach my $tuple (@$tuple_status)
+ {
+ $errors .= "\n" . $tuple->[1] if(ref $tuple);
+ }
+ $self->throw_exception("Error executing '$sql': ".($@ || $errors));
}
} else {
$self->throw_exception("'$sql' did not generate a statement.");