From: Matt Phillips Date: Sat, 11 May 2013 14:22:35 +0000 (-0400) Subject: squash, fixes subquery with insert_bulk X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=750ff9db61b000c8169c020e1aa2b40d2d996f5a;p=dbsrgits%2FDBIx-Class.git squash, fixes subquery with insert_bulk --- diff --git a/lib/DBIx/Class/SQLMaker.pm b/lib/DBIx/Class/SQLMaker.pm index 556c54f..bee0443 100644 --- a/lib/DBIx/Class/SQLMaker.pm +++ b/lib/DBIx/Class/SQLMaker.pm @@ -227,31 +227,20 @@ sub insert { sub _insert_ARRAYREFREF { my ($self, $data) = @_; - my ($cols, $subquery) = @$data; - if (not $subuery) { - # if somehow someone is calling this with the original SQLA api, - # pass it along + my ($cols, $subquery) = @$$data; + if (not $subquery) { return next::method($self, $cols); } - my $subsql, @subbind = @$subquery; - my @fields = sort @$cols; - my ($sql, @bind) = $self->_insert_values($data); + my ($sql, @bind) = next::method($self, $subquery); - $_ = $self->_quote($_) foreach @$fields; - $sql = "( ".join(", ", sort @$fields).") ".$sql; + # as_query wraps in brackets. drop them + $_ = $self->_quote($_) for @fields; + $sql = "( ".join(", ", @fields).") " . substr($sql, 1, -1); return ($sql, @bind); - - # when passing ARRAYREFREF with [cols], SQLA drops INTO $table (cols) - # we inject the returned sql here - #$_ = $self->_quote($_) foreach @fields; - # $sql = "( ".join(", ", @fields).") ".$sql; - my $sql = sprintf( - 'INSERT INTO %s( %s ) VALUES', $_[0]->_quote($_[1]) - ); } sub _recurse_fields { diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index bc08b97..46cb4bb 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -2057,7 +2057,7 @@ sub insert_bulk { my ($sql, $proto_bind) = $self->_prep_for_execute ( 'insert', $source, - [ $proto_data || ($cols => $data) ], + [ $proto_data || \[ $cols => $data ] ], ); if (! @$proto_bind and keys %$value_type_by_col_idx) { @@ -2199,7 +2199,7 @@ sub insert_bulk { } else { # bind_param_array doesn't work if there are no binds - $self->_dbh_execute_inserts_with_no_binds( $sth, scalar(@$data)+1 ); + $self->_dbh_execute_inserts_with_no_binds( $sth, ref $data eq 'ARRAY' ? (scalar(@$data)+1) : 1 ); } }; diff --git a/t/100populate.t b/t/100populate.t index d0ac5a5..0310de5 100644 --- a/t/100populate.t +++ b/t/100populate.t @@ -450,12 +450,13 @@ done_testing; use DDP; use Data::Dumper; my $q = $schema->resultset('Artist') ->search({ - artist => { '<' => $schema->resultset('Artist')->search->get_column('id')->max_rs->as_query }, - name => 'foo' + }, + { + columns => [qw/name rank/] })->as_query; use DDP; p $q; #p $q; #diag Dumper($q); #p $schema->resultset('Artist')->result_source; #p Dumper $q; -$schema->storage->insert_bulk($schema->resultset('Artist')->result_source, [qw/artistid name/], $q); +$schema->storage->insert_bulk($schema->resultset('Artist')->result_source, [qw/name rank/], $q);