1 package DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server;
5 use base qw/DBIx::Class::Storage::DBI::MSSQL/;
8 my ($self, $source, $cols, $data) = @_;
10 my $identity_insert = 0;
13 foreach my $col (@{$cols}) {
14 if ($source->column_info($col)->{is_auto_increment}) {
20 my $table = $source->from;
21 $source->storage->dbh_do(sub {
22 my ($storage, $dbh, @cols) = @_;
23 $dbh->do("SET IDENTITY_INSERT $table ON;");
28 $source->storage->dbh_do(sub {
29 my ($storage, $dbh, @cols) = @_;
30 $dbh->do("SET IDENTITY_INSERT $table OFF;");
35 sub _prep_for_execute {
37 my ($op, $extra_bind, $ident, $args) = @_;
39 my ($sql, $bind) = $self->next::method (@_);
40 $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert';
42 my %identity_insert_tables;
43 my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
45 foreach my $bound (@{$bind}) {
46 my $col = $bound->[0];
47 if ($col_info->{$col}->{is_auto_increment}) {
48 my $table = $col_info->{$col}->{-result_source}->from;
49 $identity_insert_tables{$table} = 1;
53 my $identity_insert_on = join '', map { "SET IDENTITY_INSERT $_ ON; " } keys %identity_insert_tables;
54 my $identity_insert_off = join '', map { "SET IDENTITY_INSERT $_ OFF; " } keys %identity_insert_tables;
55 $sql = "$identity_insert_on $sql $identity_insert_off";
64 my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
65 if ($op eq 'insert') {
66 $self->{_scope_identity} = $sth->fetchrow_array;
70 return wantarray ? ($rv, $sth, @bind) : $rv;
73 sub last_insert_id { shift->{_scope_identity} }
81 DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server - Support specific
82 to Microsoft SQL Server over ODBC
86 This class implements support specific to Microsoft SQL Server over ODBC,
87 including auto-increment primary keys and SQL::Abstract::Limit dialect. It
88 is loaded automatically by by DBIx::Class::Storage::DBI::ODBC when it
89 detects a MSSQL back-end.
91 =head1 IMPLEMENTATION NOTES
93 Microsoft SQL Server supports three methods of retrieving the IDENTITY
94 value for inserted row: IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY().
95 SCOPE_IDENTITY is used here because it is the safest. However, it must
96 be called is the same execute statement, not just the same connection.
98 So, this implementation appends a SELECT SCOPE_IDENTITY() statement
99 onto each INSERT to accommodate that requirement.
103 Marc Mims C<< <marc@questright.com> >>
107 You may distribute this code under the same terms as Perl itself.