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