ODBC fixes: superfluous connection in rebless; undef PK on first insert in MSSQL
[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
5use base qw/DBIx::Class::Storage::DBI/;
6
7sub _prep_for_execute {
8 my $self = shift;
9 my ($op, $extra_bind, $ident, $args) = @_;
10
11 my ($sql, $bind) = $self->SUPER::_prep_for_execute(@_);
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'), @_);
22 $self->{_scope_identity} = $sth->fetchrow_array if $op eq 'insert';
c1cac633 23
2eebd801 24 return wantarray ? ($rv, $sth, @bind) : $rv;
c1cac633 25}
26
27sub last_insert_id { shift->{_scope_identity} }
28
29sub sqlt_type { 'SQLServer' }
30
31sub _sql_maker_opts {
32 my ( $self, $opts ) = @_;
33
34 if ( $opts ) {
35 $self->{_sql_maker_opts} = { %$opts };
36 }
37
38 return { limit_dialect => 'Top', %{$self->{_sql_maker_opts}||{}} };
39}
40
6a52811b 41sub build_datetime_parser {
42 my $self = shift;
43 my $type = "DateTime::Format::Strptime";
44 eval "use ${type}";
45 $self->throw_exception("Couldn't load ${type}: $@") if $@;
46 return $type->new( pattern => '%F %T' );
47}
48
c1cac633 491;
50
51__END__
52
53=head1 NAME
54
a89c6fc0 55DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server - Support specific
56to Microsoft SQL Server over ODBC
c1cac633 57
58=head1 DESCRIPTION
59
60This class implements support specific to Microsoft SQL Server over ODBC,
61including auto-increment primary keys and SQL::Abstract::Limit dialect. It
62is loaded automatically by by DBIx::Class::Storage::DBI::ODBC when it
63detects a MSSQL back-end.
64
65=head1 IMPLEMENTATION NOTES
66
67Microsoft SQL Server supports three methods of retrieving the IDENTITY
68value for inserted row: IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY().
69SCOPE_IDENTITY is used here because it is the safest. However, it must
70be called is the same execute statement, not just the same connection.
71
72So, this implementation appends a SELECT SCOPE_IDENTITY() statement
73onto each INSERT to accommodate that requirement.
74
6f96f99f 75=head1 METHODS
76
6f96f99f 77=head2 last_insert_id
78
79=head2 sqlt_type
c1cac633 80
6a52811b 81=head2 build_datetime_parser
82
83The resulting parser handles the MSSQL C<DATETIME> type, but is almost
84certainly not sufficient for the other MSSQL 2008 date/time types.
85
c1cac633 86=head1 AUTHORS
87
88Marc Mims C<< <marc@questright.com> >>
89
90=head1 LICENSE
91
92You may distribute this code under the same terms as Perl itself.
93
94=cut