Some fixes after review
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / ODBC / Microsoft_SQL_Server.pm
1 package DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server;
2 use strict;
3 use warnings;
4
5 use base qw/DBIx::Class::Storage::DBI::MSSQL/;
6 use List::Util();
7
8 sub insert_bulk {
9   my ($self, $source, $cols, $data) = @_;
10
11   my $identity_insert = 0;
12
13   COLUMNS:
14   foreach my $col (@{$cols}) {
15     if ($source->column_info($col)->{is_auto_increment}) {
16       $identity_insert = 1;
17       last COLUMNS;
18     }
19   }
20
21   if ($identity_insert) {
22     my $table = $source->from;
23     $self->dbh->do("SET IDENTITY_INSERT $table ON");
24   }
25
26   next::method(@_);
27
28   if ($identity_insert) {
29     my $table = $source->from;
30     $self->dbh->do("SET IDENTITY_INSERT $table OFF");
31   }
32 }
33
34 sub _prep_for_execute {
35   my $self = shift;
36   my ($op, $extra_bind, $ident, $args) = @_;
37
38   my ($sql, $bind) = $self->next::method (@_);
39
40   if ($op eq 'insert') {
41     $sql .= ';SELECT SCOPE_IDENTITY()';
42
43     my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
44     if (List::Util::first { $_->{is_auto_increment} } (values %$col_info) ) {
45
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";
50     }
51   }
52
53   return ($sql, $bind);
54 }
55
56 sub _execute {
57     my $self = shift;
58     my ($op) = @_;
59
60     my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
61     if ($op eq 'insert') {
62       $self->{_scope_identity} = $sth->fetchrow_array;
63       $sth->finish;
64     }
65
66     return wantarray ? ($rv, $sth, @bind) : $rv;
67 }
68
69 sub last_insert_id { shift->{_scope_identity} }
70
71 1;
72
73 __END__
74
75 =head1 NAME
76
77 DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server - Support specific
78 to Microsoft SQL Server over ODBC
79
80 =head1 DESCRIPTION
81
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.
86
87 =head1 IMPLEMENTATION NOTES
88
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.
93
94 So, this implementation appends a SELECT SCOPE_IDENTITY() statement
95 onto each INSERT to accommodate that requirement.
96
97 =head1 AUTHORS
98
99 Marc Mims C<< <marc@questright.com> >>
100
101 =head1 LICENSE
102
103 You may distribute this code under the same terms as Perl itself.
104
105 =cut
106 # vim: sw=2 sts=2