Use new _resolve_column_sources method and begin insert_bulk method
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ODBC / Microsoft_SQL_Server.pm
index 108b8cf..8fa8fe4 100644 (file)
@@ -2,16 +2,36 @@ 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->next::method (@_);
+  $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert';
 
-    my ($sql, $bind) = $self->next::method (@_);
-    $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert';
+  my %identity_insert_tables;
+  my $col_sources = $self->_resolve_column_sources($ident, [map $_->[0], @{$bind}]);
 
-    return ($sql, $bind);
+  foreach my $bound (@{$bind}) {
+    my $col = $bound->[0];
+    my $rsrc = $col_sources->{$col};
+    if ($rsrc && $rsrc->column_info($col)->{is_auto_increment}) {
+      $identity_insert_tables{$rsrc->from} = 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);
 }
 
 sub _execute {
@@ -29,26 +49,6 @@ sub _execute {
 
 sub last_insert_id { shift->{_scope_identity} }
 
-sub sqlt_type { 'SQLServer' }
-
-sub _sql_maker_opts {
-    my ( $self, $opts ) = @_;
-
-    if ( $opts ) {
-        $self->{_sql_maker_opts} = { %$opts };
-    }
-
-    return { limit_dialect => 'Top', %{$self->{_sql_maker_opts}||{}} };
-}
-
-sub build_datetime_parser {
-  my $self = shift;
-  my $type = "DateTime::Format::Strptime";
-  eval "use ${type}";
-  $self->throw_exception("Couldn't load ${type}: $@") if $@;
-  return $type->new( pattern => '%F %T' );
-}
-
 1;
 
 __END__
@@ -75,17 +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 METHODS
-
-=head2 last_insert_id
-
-=head2 sqlt_type
-
-=head2 build_datetime_parser
-
-The resulting parser handles the MSSQL C<DATETIME> type, but is almost
-certainly not sufficient for the other MSSQL 2008 date/time types.
-
 =head1 AUTHORS
 
 Marc Mims C<< <marc@questright.com> >>
@@ -95,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