X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FStorage%2FDBI%2FODBC%2FMicrosoft_SQL_Server.pm;h=1b661f8a64c5ed4e1b589817180446433cfd58dc;hb=893403c81741764c51e7cfff69a24d427d083c33;hp=75e9497a6567d76df5c1cb0d0a75e223b931bc96;hpb=764a1b60957e129ef39a8592ec53b128d3a9d1ae;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm b/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm index 75e9497..1b661f8 100644 --- a/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm +++ b/lib/DBIx/Class/Storage/DBI/ODBC/Microsoft_SQL_Server.pm @@ -3,35 +3,53 @@ use strict; use warnings; use base qw/DBIx::Class::Storage::DBI::MSSQL/; +use List::Util(); + +sub insert_bulk { + my ($self, $source, $cols, $data) = @_; + + my $identity_insert = 0; + + COLUMNS: + foreach my $col (@{$cols}) { + if ($source->column_info($col)->{is_auto_increment}) { + $identity_insert = 1; + last COLUMNS; + } + } + + if ($identity_insert) { + my $table = $source->from; + $self->dbh->do("SET IDENTITY_INSERT $table ON"); + } + + next::method(@_); + + if ($identity_insert) { + my $table = $source->from; + $self->dbh->do("SET IDENTITY_INSERT $table OFF"); + } +} sub _prep_for_execute { my $self = shift; my ($op, $extra_bind, $ident, $args) = @_; my ($sql, $bind) = $self->next::method (@_); - $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert'; - - my $alias2src = $self->_resolve_ident_sources($ident); - my %identity_insert_tables; - foreach my $bound (@{$bind}) { - my $col = $bound->[0]; - my $name_sep = $self->_sql_maker_opts->{name_sep} || '.'; - - $col =~ s/^([^\Q${name_sep}\E]*)\Q${name_sep}\E//; - my $alias = $1 || 'me'; - my $rsrc = $alias2src->{$alias}; - - my $is_auto_increment = $rsrc && $rsrc->column_info($col)->{is_auto_increment}; - my $table; - if ($is_auto_increment) { - $identity_insert_tables{$rsrc->from} = 1; + + if ($op eq 'insert') { + $sql .= ';SELECT SCOPE_IDENTITY()'; + + my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]); + if (List::Util::first { $_->{is_auto_increment} } (values %$col_info) ) { + + my $table = $ident->from; + my $identity_insert_on = "SET IDENTITY_INSERT $table ON"; + my $identity_insert_off = "SET IDENTITY_INSERT $table OFF"; + $sql = "$identity_insert_on; $sql; $identity_insert_off"; } } - my $identity_insert_on = join '', map { "SET IDENTITY_INSERT $_ ON; " } keys %identity_insert_tables; - my $identity_insert_off = join '', map { "SET IDENTITY_INSERT $_ OFF; " } keys %identity_insert_tables; - $sql = "$identity_insert_on $sql $identity_insert_off"; - return ($sql, $bind); }