silly warns.
[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
7 sub insert_bulk {
8   my ($self, $source, $cols, $data) = @_;
9
10   my $identity_insert = 0;
11
12   COLUMNS:
13   foreach my $col (@{$cols}) {
14     if ($source->column_info($col)->{is_auto_increment}) {
15       $identity_insert = 1;
16       last COLUMNS;
17     }
18   }
19
20   my $table = $source->from;
21   if ($identity_insert) {
22     $source->storage->dbh_do(sub {
23         my ($storage, $dbh, @cols) = @_;
24         $dbh->do("SET IDENTITY_INSERT $table ON;");
25       });
26   }
27
28   next::method(@_);
29
30   if ($identity_insert) {
31     $source->storage->dbh_do(sub {
32         my ($storage, $dbh, @cols) = @_;
33         $dbh->do("SET IDENTITY_INSERT $table OFF;");
34       });
35   }
36
37 }
38
39 sub _prep_for_execute {
40   my $self = shift;
41   my ($op, $extra_bind, $ident, $args) = @_;
42
43   my ($sql, $bind) = $self->next::method (@_);
44   $sql .= ';SELECT SCOPE_IDENTITY()' if $op eq 'insert';
45
46   my %identity_insert_tables;
47   my $col_info = $self->_resolve_column_info($ident, [map $_->[0], @{$bind}]);
48
49   foreach my $bound (@{$bind}) {
50     my $col = $bound->[0];
51     if ($col_info->{$col}->{is_auto_increment}) {
52       my $table = $col_info->{$col}->{-result_source}->from;
53       $identity_insert_tables{$table} = 1;
54     }
55   }
56
57   my $identity_insert_on = join '', map { "SET IDENTITY_INSERT $_ ON; " } keys %identity_insert_tables;
58   my $identity_insert_off = join '', map { "SET IDENTITY_INSERT $_ OFF; " } keys %identity_insert_tables;
59   $sql = "$identity_insert_on $sql $identity_insert_off";
60
61   return ($sql, $bind);
62 }
63
64 sub _execute {
65     my $self = shift;
66     my ($op) = @_;
67
68     my ($rv, $sth, @bind) = $self->dbh_do($self->can('_dbh_execute'), @_);
69     if ($op eq 'insert') {
70       $self->{_scope_identity} = $sth->fetchrow_array;
71       $sth->finish;
72     }
73
74     return wantarray ? ($rv, $sth, @bind) : $rv;
75 }
76
77 sub last_insert_id { shift->{_scope_identity} }
78
79 1;
80
81 __END__
82
83 =head1 NAME
84
85 DBIx::Class::Storage::DBI::ODBC::Microsoft_SQL_Server - Support specific
86 to Microsoft SQL Server over ODBC
87
88 =head1 DESCRIPTION
89
90 This class implements support specific to Microsoft SQL Server over ODBC,
91 including auto-increment primary keys and SQL::Abstract::Limit dialect.  It
92 is loaded automatically by by DBIx::Class::Storage::DBI::ODBC when it
93 detects a MSSQL back-end.
94
95 =head1 IMPLEMENTATION NOTES
96
97 Microsoft SQL Server supports three methods of retrieving the IDENTITY
98 value for inserted row: IDENT_CURRENT, @@IDENTITY, and SCOPE_IDENTITY().
99 SCOPE_IDENTITY is used here because it is the safest.  However, it must
100 be called is the same execute statement, not just the same connection.
101
102 So, this implementation appends a SELECT SCOPE_IDENTITY() statement
103 onto each INSERT to accommodate that requirement.
104
105 =head1 AUTHORS
106
107 Marc Mims C<< <marc@questright.com> >>
108
109 =head1 LICENSE
110
111 You may distribute this code under the same terms as Perl itself.
112
113 =cut
114 # vim: sw=2 sts=2