use strict;
use warnings;
-use base qw/DBIx::Class::Storage::DBI/;
+use base qw/DBIx::Class::Storage::DBI::MSSQL/;
+use List::Util();
-sub _prep_for_execute {
- my $self = shift;
- my ($op, $extra_bind, $ident, $args) = @_;
+sub insert_bulk {
+ my ($self, $source, $cols, $data) = @_;
- my ($sql, $bind) = $self->SUPER::_prep_for_execute(@_);
- $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert';
+ my $identity_insert = 0;
- return ($sql, $bind);
-}
+ COLUMNS:
+ foreach my $col (@{$cols}) {
+ if ($source->column_info($col)->{is_auto_increment}) {
+ $identity_insert = 1;
+ last COLUMNS;
+ }
+ }
-sub insert {
- my ($self, $source, $to_insert) = @_;
+ if ($identity_insert) {
+ my $table = $source->from;
+ $self->dbh->do("SET IDENTITY_INSERT $table ON");
+ }
- 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;
+ next::method(@_);
- return $to_insert;
+ if ($identity_insert) {
+ my $table = $source->from;
+ $self->dbh->do("SET IDENTITY_INSERT $table OFF");
+ }
}
-sub last_insert_id { shift->{_scope_identity} }
+sub _prep_for_execute {
+ my $self = shift;
+ my ($op, $extra_bind, $ident, $args) = @_;
+
+ my ($sql, $bind) = $self->next::method (@_);
-sub sqlt_type { 'SQLServer' }
+ if ($op eq 'insert') {
+ $sql .= ';SELECT SCOPE_IDENTITY()';
-sub _sql_maker_opts {
- my ( $self, $opts ) = @_;
+ my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
+ if (List::Util::first { $_->{is_auto_increment} } (values %$col_info) ) {
- if ( $opts ) {
- $self->{_sql_maker_opts} = { %$opts };
+ 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";
}
+ }
- return { limit_dialect => 'Top', %{$self->{_sql_maker_opts}||{}} };
+ return ($sql, $bind);
}
+sub _execute {
+ my $self = shift;
+ my ($op) = @_;
+
+ my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
+ if ($op eq 'insert') {
+ $self->{_scope_identity} = $sth->fetchrow_array;
+ $sth->finish;
+ }
+
+ 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
So, this implementation appends a SELECT SCOPE_IDENTITY() statement
onto each INSERT to accommodate that requirement.
-=head1 METHODS
-
-=head2 insert
-
-=head2 last_insert_id
-
-=head2 sqlt_type
-
=head1 AUTHORS
Marc Mims C<< <marc@questright.com> >>
You may distribute this code under the same terms as Perl itself.
=cut
+# vim: sw=2 sts=2