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