Switch around inheritance of MSSQL drivers, remove some duplicate code
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ODBC / Microsoft_SQL_Server.pm
CommitLineData
c1cac633 1package DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server;
2use strict;
3use warnings;
4
eb0323df 5use base qw/DBIx::Class::Storage::DBI::MSSQL/;
c1cac633 6
7sub _prep_for_execute {
8 my $self = shift;
9 my ($op, $extra_bind, $ident, $args) = @_;
10
c3515436 11 my ($sql, $bind) = $self->next::method (@_);
c1cac633 12 $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert';
13
14 return ($sql, $bind);
15}
16
2eebd801 17sub _execute {
18 my $self = shift;
19 my ($op) = @_;
c1cac633 20
2eebd801 21 my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
77af494b 22 if ($op eq 'insert') {
23 $self->{_scope_identity} = $sth->fetchrow_array;
24 $sth->finish;
25 }
c1cac633 26
2eebd801 27 return wantarray ? ($rv, $sth, @bind) : $rv;
c1cac633 28}
29
30sub last_insert_id { shift->{_scope_identity} }
31
c1cac633 321;
33
34__END__
35
36=head1 NAME
37
a89c6fc0 38DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server - Support specific
39to Microsoft SQL Server over ODBC
c1cac633 40
41=head1 DESCRIPTION
42
43This class implements support specific to Microsoft SQL Server over ODBC,
44including auto-increment primary keys and SQL::Abstract::Limit dialect. It
45is loaded automatically by by DBIx::Class::Storage::DBI::ODBC when it
46detects a MSSQL back-end.
47
48=head1 IMPLEMENTATION NOTES
49
50Microsoft SQL Server supports three methods of retrieving the IDENTITY
51value for inserted row: IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY().
52SCOPE_IDENTITY is used here because it is the safest. However, it must
53be called is the same execute statement, not just the same connection.
54
55So, this implementation appends a SELECT SCOPE_IDENTITY() statement
56onto each INSERT to accommodate that requirement.
57
c1cac633 58=head1 AUTHORS
59
60Marc Mims C<< <marc@questright.com> >>
61
62=head1 LICENSE
63
64You may distribute this code under the same terms as Perl itself.
65
66=cut