1 package DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server;
5 use base qw/DBIx::Class::Storage::DBI::MSSQL/;
9 my ($self, $source, $cols, $data) = @_;
11 my $identity_insert = 0;
14 foreach my $col (@{$cols}) {
15 if ($source->column_info($col)->{is_auto_increment}) {
21 if ($identity_insert) {
22 my $table = $source->from;
23 $self->dbh->do("SET IDENTITY_INSERT $table ON");
28 if ($identity_insert) {
29 my $table = $source->from;
30 $self->dbh->do("SET IDENTITY_INSERT $table OFF");
34 sub _prep_for_execute {
36 my ($op, $extra_bind, $ident, $args) = @_;
38 my ($sql, $bind) = $self->next::method (@_);
40 if ($op eq 'insert') {
41 $sql .= ';SELECT SCOPE_IDENTITY()';
43 my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
44 if (List::Util::first { $_->{is_auto_increment} } (values %$col_info) ) {
46 my $table = $ident->from;
47 my $identity_insert_on = "SET IDENTITY_INSERT $table ON";
48 my $identity_insert_off = "SET IDENTITY_INSERT $table OFF";
49 $sql = "$identity_insert_on; $sql; $identity_insert_off";
60 my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
61 if ($op eq 'insert') {
62 $self->{_scope_identity} = $sth->fetchrow_array;
66 return wantarray ? ($rv, $sth, @bind) : $rv;
69 sub last_insert_id { shift->{_scope_identity} }
77 DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server - Support specific
78 to Microsoft SQL Server over ODBC
82 This class implements support specific to Microsoft SQL Server over ODBC,
83 including auto-increment primary keys and SQL::Abstract::Limit dialect. It
84 is loaded automatically by by DBIx::Class::Storage::DBI::ODBC when it
85 detects a MSSQL back-end.
87 =head1 IMPLEMENTATION NOTES
89 Microsoft SQL Server supports three methods of retrieving the IDENTITY
90 value for inserted row: IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY().
91 SCOPE_IDENTITY is used here because it is the safest. However, it must
92 be called is the same execute statement, not just the same connection.
94 So, this implementation appends a SELECT SCOPE_IDENTITY() statement
95 onto each INSERT to accommodate that requirement.
99 Marc Mims C<< <marc@questright.com> >>
103 You may distribute this code under the same terms as Perl itself.