updated _resolve_column_source to _resolve_column_info as per ribasushi's suggestion
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ODBC / Microsoft_SQL_Server.pm
index ad92e1a..ff79f54 100644 (file)
@@ -4,6 +4,11 @@ use warnings;
 
 use base qw/DBIx::Class::Storage::DBI::MSSQL/;
 
+sub insert_bulk {
+  my ($self, $source, $cols, $data) = @_;
+  next::method(@_);
+}
+
 sub _prep_for_execute {
   my $self = shift;
   my ($op, $extra_bind, $ident, $args) = @_;
@@ -11,31 +16,21 @@ sub _prep_for_execute {
   my ($sql, $bind) = $self->next::method (@_);
   $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert';
 
-  use Scalar::Util 'blessed';
-  use List::Util 'first';
-  if ( blessed $ident ) {
-    my %auto_inc_columns;
-    foreach my $column ($ident->columns) {
-      if ($ident->column_info($column)->{is_auto_increment}) {
-       $auto_inc_columns{$column} = 1;
-      }
-    }
+  my %identity_insert_tables;
+  my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
 
-    my $table = $ident->from;
-    my $auto_inc_col = 0;
-    BINDS:
-    foreach my $bound (@{$bind}) {
-      my $col =  $bound->[0];
-      if ($auto_inc_columns{$col}) {
-       $auto_inc_col = 1;
-       last BINDS;
-      }
-    }
-    if ($auto_inc_col) {
-      $sql = "SET IDENTITY_INSERT $table ON; $sql; SET IDENTITY_INSERT $table OFF;"
+  foreach my $bound (@{$bind}) {
+    my $col = $bound->[0];
+    if ($col_info->{$col}->{is_auto_increment}) {
+      my $table = $col_info->{$col}->{-result_source}->from;
+      $identity_insert_tables{$table} = 1;
     }
   }
 
+  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);
 }