minor change
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ODBC / Microsoft_SQL_Server.pm
index ad92e1a..21885d4 100644 (file)
@@ -3,36 +3,67 @@ use strict;
 use warnings;
 
 use base qw/DBIx::Class::Storage::DBI::MSSQL/;
+use mro 'c3';
+
+use List::Util();
+
+sub insert_bulk {
+  my $self = shift;
+  my ($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");
+  }
+
+  $self->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';
-
-  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;
-      }
-    }
+# cast MONEY values properly
+  if ($op eq 'insert' || $op eq 'update') {
+    my $fields = $args->[0];
+    my $col_info = $self->_resolve_column_info($ident, [keys %$fields]);
+
+    for my $col (keys %$fields) {
+      if ($col_info->{$col}{data_type} eq 'money') {
+        my $val = $fields->{$col};
 
-    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;
+        $fields->{$col} = \['CAST(? AS MONEY)', [ $col => $val ]];
       }
     }
-    if ($auto_inc_col) {
-      $sql = "SET IDENTITY_INSERT $table ON; $sql; SET IDENTITY_INSERT $table OFF;"
+  }
+
+  my ($sql, $bind) = $self->next::method (@_);
+
+  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";
     }
   }