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