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 88eeab6..ff79f54 100644 (file)
@@ -2,50 +2,61 @@ package DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server;
 use strict;
 use warnings;
 
-use base qw/DBIx::Class::Storage::DBI/;
+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) = @_;
+  my $self = shift;
+  my ($op, $extra_bind, $ident, $args) = @_;
 
-    my ($sql, $bind) = $self->SUPER::_prep_for_execute(@_);
-    $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert';
+  my ($sql, $bind) = $self->next::method (@_);
+  $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert';
 
-    return ($sql, $bind);
-}
+  my %identity_insert_tables;
+  my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
 
-sub insert {
-    my ($self, $source, $to_insert) = @_;
+  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 $bind_attributes = $self->source_bind_attributes($source);
-    my (undef, $sth) = $self->_execute( 'insert' => [], $source, $bind_attributes, $to_insert);
-    $self->{_scope_identity} = $sth->fetchrow_array;
+  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 $to_insert;
+  return ($sql, $bind);
 }
 
-sub last_insert_id { shift->{_scope_identity} }
-
-sub sqlt_type { 'SQLServer' }
-
-sub _sql_maker_opts {
-    my ( $self, $opts ) = @_;
+sub _execute {
+    my $self = shift;
+    my ($op) = @_;
 
-    if ( $opts ) {
-        $self->{_sql_maker_opts} = { %$opts };
+    my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
+    if ($op eq 'insert') {
+      $self->{_scope_identity} = $sth->fetchrow_array;
+      $sth->finish;
     }
 
-    return { limit_dialect => 'Top', %{$self->{_sql_maker_opts}||{}} };
+    return wantarray ? ($rv, $sth, @bind) : $rv;
 }
 
+sub last_insert_id { shift->{_scope_identity} }
+
 1;
 
 __END__
 
 =head1 NAME
 
-DBIx::Class::Storage::ODBC::Microsoft_SQL_Server - Support specific to
-Microsoft SQL Server over ODBC
+DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server - Support specific
+to Microsoft SQL Server over ODBC
 
 =head1 DESCRIPTION
 
@@ -64,7 +75,6 @@ be called is the same execute statement, not just the same connection.
 So, this implementation appends a SELECT SCOPE_IDENTITY() statement
 onto each INSERT to accommodate that requirement.
 
-
 =head1 AUTHORS
 
 Marc Mims C<< <marc@questright.com> >>
@@ -74,3 +84,4 @@ Marc Mims C<< <marc@questright.com> >>
 You may distribute this code under the same terms as Perl itself.
 
 =cut
+# vim: sw=2 sts=2