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