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