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